import {z} from 'zod' export type AutorisatieBase = {component: 'ac' | 'nrc' | 'zrc' | 'ztc' | 'drc' | 'brc', componentWeergave?: string | undefined, scopes: Array}; export type Applicatie = {url?: string | undefined, clientIds: Array, label: string, heeftAlleAutorisaties?: boolean | undefined, alleenIsGereedVoorPublicatie?: boolean | undefined, autorisaties?: Array | undefined}; export type PatchApiApplicatiesUuidArgs = {body: Applicatie}; export type PatchApiApplicatiesUuidOptions = {baseUrl: string, JWTClaims?: string | undefined, fetch?: typeof fetch | undefined}; export const autorisatieBase = z.object({component: z.enum(["ac", "nrc", "zrc", "ztc", "drc", "brc"]), componentWeergave: z.string().min(1).optional(), scopes: z.array(z.string().min(1).max(100))}); export const applicatie = z.object({url: z.string().min(1).max(1000).optional(), clientIds: z.array(z.string().min(1).max(50)), label: z.string().min(1).max(100), heeftAlleAutorisaties: z.boolean().optional(), alleenIsGereedVoorPublicatie: z.boolean().optional(), autorisaties: z.array(autorisatieBase).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 }; /** * Werk (een deel van) de applicatie bij. * * PATCH /applicaties/{uuid} */ export const patchApiApplicatiesUuid = async (args: PatchApiApplicatiesUuidArgs, options: PatchApiApplicatiesUuidOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTClaims} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWTClaims !== undefined) headers.set('authorization', `Bearer ${JWTClaims}`) const url = `${baseUrl.replace(/\/+$/, '')}/applicaties/${encodeURIComponent(String(args.uuid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(applicatie, await res.json()) };