import {z} from 'zod' export type GetApiApiV3DirectoryObjectsArgs = {object_type?: string | undefined, 'page.size'?: number | undefined, 'page.token'?: string | undefined}; export type GetApiApiV3DirectoryObjectsOptions = {baseUrl: string, DirectoryAPIKey?: string | undefined, TenantID?: string | undefined, fetch?: typeof fetch | undefined}; export const v3PaginationResponse = z.object({next_token: z.string().optional()}); export const v3Object = z.object({created_at: z.string().optional(), display_name: z.string().optional(), etag: z.string().optional(), id: z.string(), properties: z.object({}).optional(), type: z.string(), updated_at: z.string().optional()}); export const v3GetObjectsResponse = z.object({page: v3PaginationResponse.optional(), results: z.array(v3Object).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 object instances * * GET /api/v3/directory/objects */ export const getApiApiV3DirectoryObjects = async (args: GetApiApiV3DirectoryObjectsArgs, options: GetApiApiV3DirectoryObjectsOptions) => { const {baseUrl, fetch: fetchFn = fetch, DirectoryAPIKey, TenantID} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.object_type != null) query.append("object_type", String(args.object_type)) if (args['page.size'] != null) query.append("page.size", String(args['page.size'])) if (args['page.token'] != null) query.append("page.token", String(args['page.token'])) if (DirectoryAPIKey !== undefined) headers.set("authorization", DirectoryAPIKey) if (TenantID !== undefined) headers.set("aserto-tenant-id", TenantID) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/directory/objects${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(v3GetObjectsResponse, await res.json()) };