import {z} from 'zod' export type GetApiApiV3DirectoryRelationsArgs = {object_type?: string | undefined, object_id?: string | undefined, relation?: string | undefined, subject_type?: string | undefined, subject_id?: string | undefined, subject_relation?: string | undefined, with_objects?: boolean | undefined, with_empty_subject_relation?: boolean | undefined, 'page.size'?: number | undefined, 'page.token'?: string | undefined}; export type GetApiApiV3DirectoryRelationsOptions = {baseUrl: string, DirectoryAPIKey?: string | undefined, TenantID?: string | undefined, fetch?: typeof fetch | undefined}; 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 v3PaginationResponse = z.object({next_token: z.string().optional()}); export const v3Relation = z.object({created_at: z.string().optional(), etag: z.string().optional(), object_id: z.string(), object_type: z.string(), relation: z.string(), subject_id: z.string(), subject_relation: z.string().optional(), subject_type: z.string(), updated_at: z.string().optional()}); export const v3GetRelationsResponse = z.object({objects: z.record(z.string(), v3Object).optional(), page: v3PaginationResponse.optional(), results: z.array(v3Relation).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 relations instances * * GET /api/v3/directory/relations */ export const getApiApiV3DirectoryRelations = async (args: GetApiApiV3DirectoryRelationsArgs, options: GetApiApiV3DirectoryRelationsOptions) => { 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.object_id != null) query.append("object_id", String(args.object_id)) if (args.relation != null) query.append("relation", String(args.relation)) if (args.subject_type != null) query.append("subject_type", String(args.subject_type)) if (args.subject_id != null) query.append("subject_id", String(args.subject_id)) if (args.subject_relation != null) query.append("subject_relation", String(args.subject_relation)) if (args.with_objects != null) query.append("with_objects", String(args.with_objects)) if (args.with_empty_subject_relation != null) query.append("with_empty_subject_relation", String(args.with_empty_subject_relation)) 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/relations${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(v3GetRelationsResponse, await res.json()) };