import {z} from 'zod' export type V3Object = {created_at?: string | undefined, display_name?: string | undefined, etag?: string | undefined, id: string, properties?: Record | undefined, type: string, updated_at?: string | undefined}; export type V3SetObjectRequest = {object: V3Object}; export type CreateApiApiV3DirectoryObjectArgs = {body: V3SetObjectRequest}; export type CreateApiApiV3DirectoryObjectOptions = {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 v3SetObjectResponse = z.object({result: 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 }; /** * Set object * * POST /api/v3/directory/object */ export const createApiApiV3DirectoryObject = async (args: CreateApiApiV3DirectoryObjectArgs, options: CreateApiApiV3DirectoryObjectOptions) => { const {baseUrl, fetch: fetchFn = fetch, DirectoryAPIKey, TenantID} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (DirectoryAPIKey !== undefined) headers.set("authorization", DirectoryAPIKey) if (TenantID !== undefined) headers.set("aserto-tenant-id", TenantID) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/directory/object${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(v3SetObjectResponse, await res.json()) };