import {z} from 'zod' export type GetApiPoliciesIdArgs = {id: string}; export type GetApiPoliciesIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const screeningPolicySnapshot = z.object({name: z.string().min(2).max(120), purpose: z.string().min(3).max(1000), jurisdictions: z.array(z.string().min(2).max(100)), matching: z.object({profile: z.enum(["standard", "focused"]), partialNames: z.enum(["review", "exclude"]).optional()}).optional(), requiredSources: z.record(z.string(), z.array(z.string().min(1).max(80))), optionalSources: z.record(z.string(), z.array(z.string().min(1).max(80))), exclusions: z.array(z.string().min(3).max(500)), externalChecks: z.array(z.string().min(3).max(200)), externalCheckGuidance: z.array(z.object({label: z.string().min(3).max(200), instructions: z.string().min(3).max(2000), evidenceExample: z.string().min(3).max(1000).optional()})).optional(), review: z.object({requireSecondReview: z.boolean().optional(), secondReviewFor: z.array(z.enum(["same_identity", "allow", "restrict", "escalate"])).optional(), approverRoles: z.array(z.enum(["owner", "admin", "analyst"])).optional()}), monitoringIntervalHours: z.union([z.literal(6), z.literal(24), z.literal(168)]), retentionTrigger: z.enum(["review_completed", "relationship_ended", "business_event"]), id: z.string(), version: z.number().int().gte(0), environment: z.enum(["sandbox", "production"]), createdAt: z.string()}); export const policyList = z.object({items: z.array(screeningPolicySnapshot), requirePolicy: z.boolean(), nextCursor: z.string().nullable()}); export const getApiPoliciesIdResponse = z.object({data: policyList}); 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 }; /** * Read the current version of an approved policy * * GET /policies/{id} */ export const getApiPoliciesId = async (args: GetApiPoliciesIdArgs, options: GetApiPoliciesIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/policies/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiPoliciesIdResponse, await res.json()) };