import {z} from 'zod' export type OperationType = 'unknown' | 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test'; export type JsonPatchOperation = {op: OperationType, path: string, from?: (string | null) | undefined, value: unknown}; export type PatchApiV1WebhooksWebhookIdArgs = {webhookId: number, body: Array}; export type PatchApiV1WebhooksWebhookIdOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const webHookHttpMethod = z.enum(["get", "post"]); export const webhookHeaderResponseModel = z.object({key: z.string(), value: z.string(), isSecure: z.boolean()}); export const webhookConfig = z.object({name: z.string(), configId: z.string()}); export const webhookEnvironment = z.object({name: z.string(), environmentId: z.string()}); export const webhookResponseModel = z.object({webhookId: z.number().int(), url: z.string(), httpMethod: webHookHttpMethod, content: z.string().nullable(), webHookHeaders: z.array(webhookHeaderResponseModel), config: webhookConfig, environment: webhookEnvironment}); 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 }; /** * Update Webhook * * PATCH /v1/webhooks/{webhookId} */ export const patchApiV1WebhooksWebhookId = async (args: PatchApiV1WebhooksWebhookIdArgs, options?: PatchApiV1WebhooksWebhookIdOptions) => { const {baseUrl = "https://api.configcat.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/${encodeURIComponent(String(args.webhookId))}${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(webhookResponseModel, await res.json()) };