import {z} from 'zod' export type SubjectContext = {userId?: string | undefined, tenantId: string, permissions?: Array | undefined, attributes?: Record | undefined}; export type IsEntitledToPermissionDto = {permissionKey: string}; export type CreateApiV1DataE10sPermissionsIsEntitledToInputPermissionArgs = {body: {input?: ({subjectContext?: SubjectContext | undefined, requestContext?: IsEntitledToPermissionDto | undefined}) | undefined}}; export type CreateApiV1DataE10sPermissionsIsEntitledToInputPermissionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const entitlementsResult = z.object({justification: z.enum(["MISSING_FEATURE", "MISSING_PERMISSION", "PLAN_EXPIRED", "MISSING_ROUTE", "ROUTE_DENIED"]).optional(), monitoring: z.boolean().optional(), result: z.boolean()}); export const pDPResponse = z.object({decision_id: z.string(), metrics: z.object({}), result: entitlementsResult}); 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 }; /** * Is Entitled To Permission * * POST /v1/data/e10s/permissions/is_entitled_to_input_permission */ export const createApiV1DataE10sPermissionsIsEntitledToInputPermission = async (args: CreateApiV1DataE10sPermissionsIsEntitledToInputPermissionArgs, options?: CreateApiV1DataE10sPermissionsIsEntitledToInputPermissionOptions) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/data/e10s/permissions/is_entitled_to_input_permission${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(pDPResponse, await res.json()) };