import {z} from 'zod' export type V1Action = {name?: string | undefined, properties?: Record | undefined}; export type V1Page = {next_token?: string | undefined}; export type V1Resource = {id?: string | undefined, properties?: Record | undefined, type?: string | undefined}; export type V1Subject = {id?: string | undefined, properties?: Record | undefined, type?: string | undefined}; export type V1SubjectSearchRequest = {action?: V1Action | undefined, context?: Record | undefined, page?: V1Page | undefined, resource?: V1Resource | undefined, subject?: V1Subject | undefined}; export type CreateApiAccessV1SearchSubjectArgs = {body: V1SubjectSearchRequest}; export type CreateApiAccessV1SearchSubjectOptions = {baseUrl: string, DirectoryAPIKey?: string | undefined, TenantID?: string | undefined, fetch?: typeof fetch | undefined}; export const v1Page = z.object({next_token: z.string().optional()}); export const v1Subject = z.object({id: z.string().optional(), properties: z.object({}).optional(), type: z.string().optional()}); export const v1SubjectSearchResponse = z.object({page: v1Page.optional(), results: z.array(v1Subject).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 }; /** * Subject Search API * * POST /access/v1/search/subject */ export const createApiAccessV1SearchSubject = async (args: CreateApiAccessV1SearchSubjectArgs, options: CreateApiAccessV1SearchSubjectOptions) => { 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(/\/+$/, '')}/access/v1/search/subject${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(v1SubjectSearchResponse, await res.json()) };