49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
|
|
import { listAppPermissions, listAppPermissionsResolved } from "../../graph/app.ts";
|
|
|
|
import { filterByPermissionName } from "./shared.ts";
|
|
import { getGraphClient } from "../../graph/index.ts";
|
|
|
|
type ListAppPermissionsOptions = {
|
|
appId?: string;
|
|
resolve?: boolean;
|
|
short?: boolean;
|
|
filter?: string;
|
|
};
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
}
|
|
|
|
function omitColumns(input: unknown, names: string[]): unknown {
|
|
if (!Array.isArray(input) || !input.every(isRecord)) {
|
|
return input;
|
|
}
|
|
|
|
const namesSet = new Set(names);
|
|
return input.map((record) =>
|
|
Object.fromEntries(
|
|
Object.entries(record).filter(([key]) => !namesSet.has(key)),
|
|
)
|
|
);
|
|
}
|
|
|
|
export async function runListAppPermissionsCommand(options: ListAppPermissionsOptions): Promise<unknown> {
|
|
if (!options.appId) {
|
|
throw new Error("--app-id is required for list-app-permissions");
|
|
}
|
|
|
|
const client = await getGraphClient();
|
|
let result: unknown = options.resolve || options.filter
|
|
? await listAppPermissionsResolved(client, options.appId)
|
|
: await listAppPermissions(client, options.appId);
|
|
if (options.short) {
|
|
result = omitColumns(result, ["resourceAppId", "permissionId"]);
|
|
}
|
|
if (options.filter) {
|
|
result = filterByPermissionName(result as Array<Record<string, unknown>>, options.filter);
|
|
}
|
|
return result;
|
|
}
|