import {z} from 'zod' export type GetApiV3IamPrincipalsArgs = {limit?: number | undefined, page_token?: string | undefined, resource_binding_type?: ('organization' | 'application' | 'workspace' | 'grant') | undefined, resource_binding_id?: string | undefined}; export type GetApiV3IamPrincipalsOptions = {baseUrl?: string | undefined, IAMAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceBinding = z.object({type: z.enum(["organization", "application", "workspace", "grant"]), id: z.string().min(1).max(256)}); export const principal = z.object({id: z.string(), organization_id: z.string(), name: z.string().min(1).max(128), description: z.string().max(1024), status: z.enum(["active", "disabled"]), roles: z.array(z.string()), direct_permissions: z.array(z.string()), resource_binding: resourceBinding.nullable(), created_at: z.number().int(), updated_at: z.number().int()}); export const getApiV3IamPrincipalsResponse = z.object({request_id: z.string().optional(), data: z.array(principal).optional(), next_cursor: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List IAM principals * * GET /v3/iam/principals */ export const getApiV3IamPrincipals = async (args: GetApiV3IamPrincipalsArgs, options?: GetApiV3IamPrincipalsOptions) => { const {baseUrl = "https://api.us.nylas.com", fetch: fetchFn = fetch, IAMAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.resource_binding_type != null) query.append("resource_binding_type", String(args.resource_binding_type)) if (args.resource_binding_id != null) query.append("resource_binding_id", String(args.resource_binding_id)) if (IAMAPIKEY !== undefined) headers.set('authorization', `Bearer ${IAMAPIKEY}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/iam/principals${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV3IamPrincipalsResponse, await res.json()) };