import {z} from 'zod' export type V3CheckRequest = {object_id: string, object_type: string, relation: string, subject_id: string, subject_type: string, trace?: boolean | undefined}; export type V3CheckPermissionRequest = {object_id: string, object_type: string, permission: string, subject_id: string, subject_type: string, trace?: boolean | undefined}; export type V3CheckRelationRequest = {object_id: string, object_type: string, relation: string, subject_id: string, subject_type: string, trace?: boolean | undefined}; export type V3Assert = {check?: V3CheckRequest | undefined, check_permission?: V3CheckPermissionRequest | undefined, check_relation?: V3CheckRelationRequest | undefined, description?: string | undefined, expected: boolean, id?: number | undefined}; export type V3SetAssertionRequest = {assert: V3Assert}; export type CreateApiApiV3DirectoryAssertionArgs = {body: V3SetAssertionRequest}; export type CreateApiApiV3DirectoryAssertionOptions = {baseUrl: string, DirectoryAPIKey?: string | undefined, TenantID?: string | undefined, fetch?: typeof fetch | undefined}; export const v3CheckRequest = z.object({object_id: z.string(), object_type: z.string(), relation: z.string(), subject_id: z.string(), subject_type: z.string(), trace: z.boolean().optional()}); export const v3CheckPermissionRequest = z.object({object_id: z.string(), object_type: z.string(), permission: z.string(), subject_id: z.string(), subject_type: z.string(), trace: z.boolean().optional()}); export const v3CheckRelationRequest = z.object({object_id: z.string(), object_type: z.string(), relation: z.string(), subject_id: z.string(), subject_type: z.string(), trace: z.boolean().optional()}); export const v3Assert = z.object({check: v3CheckRequest.optional(), check_permission: v3CheckPermissionRequest.optional(), check_relation: v3CheckRelationRequest.optional(), description: z.string().optional(), expected: z.boolean(), id: z.number().int().optional()}); export const v3SetAssertionResponse = z.object({result: v3Assert.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 assertion * * POST /api/v3/directory/assertion */ export const createApiApiV3DirectoryAssertion = async (args: CreateApiApiV3DirectoryAssertionArgs, options: CreateApiApiV3DirectoryAssertionOptions) => { 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/assertion${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(v3SetAssertionResponse, await res.json()) };