31 lines
889 B
TypeScript
31 lines
889 B
TypeScript
// SPDX-License-Identifier: MIT
|
|
|
|
type GraphResult<T = Record<string, unknown>> = {
|
|
value?: T[];
|
|
};
|
|
|
|
export async function getServicePrincipal(client: any, appId: string): Promise<Record<string, unknown> | null> {
|
|
const result = await client
|
|
.api("/servicePrincipals")
|
|
.filter(`appId eq '${appId}'`)
|
|
.get() as GraphResult;
|
|
|
|
return Array.isArray(result.value) && result.value.length > 0 ? result.value[0] : null;
|
|
}
|
|
|
|
export async function createSp(client: any, appId: string): Promise<Record<string, unknown>> {
|
|
const sp = await client.api("/servicePrincipals").post({
|
|
appId,
|
|
}) as Record<string, unknown>;
|
|
|
|
if (!sp || typeof sp.id !== "string") {
|
|
throw new Error("Failed to create service principal");
|
|
}
|
|
|
|
return sp;
|
|
}
|
|
|
|
export async function deleteSp(client: any, spId: string): Promise<void> {
|
|
await client.api(`/servicePrincipals/${spId}`).delete();
|
|
}
|