import {z} from 'zod' export type ApiIdentityType = 'IDENTITY_TYPE_UNKNOWN' | 'IDENTITY_TYPE_NONE' | 'IDENTITY_TYPE_SUB' | 'IDENTITY_TYPE_JWT' | 'IDENTITY_TYPE_MANUAL'; export type ApiIdentityContext = {identity?: string | undefined, type?: ApiIdentityType | undefined}; export type V2PathSeparator = 'PATH_SEPARATOR_UNKNOWN' | 'PATH_SEPARATOR_DOT' | 'PATH_SEPARATOR_SLASH'; export type V2DecisionTreeOptions = {path_separator?: V2PathSeparator | undefined}; export type ApiPolicyContext = {decisions?: Array | undefined, path?: string | undefined}; export type ApiPolicyInstance = {instance_label?: string | undefined, name?: string | undefined}; export type V2DecisionTreeRequest = {identity_context?: ApiIdentityContext | undefined, options?: V2DecisionTreeOptions | undefined, policy_context?: ApiPolicyContext | undefined, policy_instance?: ApiPolicyInstance | undefined, resource_context?: Record | undefined}; export type CreateApiApiV2AuthzDecisiontreeArgs = {body: V2DecisionTreeRequest}; export type CreateApiApiV2AuthzDecisiontreeOptions = {baseUrl: string, AuthorizerAPIKey?: string | undefined, TenantID?: string | undefined, fetch?: typeof fetch | undefined}; export const v2DecisionTreeResponse = z.object({path: z.object({}).optional(), path_root: z.string().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 }; /** * Decision tree * * POST /api/v2/authz/decisiontree */ export const createApiApiV2AuthzDecisiontree = async (args: CreateApiApiV2AuthzDecisiontreeArgs, options: CreateApiApiV2AuthzDecisiontreeOptions) => { const {baseUrl, fetch: fetchFn = fetch, AuthorizerAPIKey, TenantID} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AuthorizerAPIKey !== undefined) headers.set("authorization", AuthorizerAPIKey) if (TenantID !== undefined) headers.set("aserto-tenant-id", TenantID) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/authz/decisiontree${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(v2DecisionTreeResponse, await res.json()) };