import {z} from 'zod' export type WebhookStatus = 'ACTIVE' | 'INACTIVE'; export type WebhookUpdateRequest = {url?: string | undefined, token?: string | undefined, webhook_status?: WebhookStatus | undefined, auth_client_id?: string | undefined, auth_client_secret?: string | undefined, auth_url?: string | undefined, auth_scope?: string | undefined, auth_audience?: string | undefined}; export type PatchApiV1ClientsClientIdWebhooksIdArgs = {clientId: string, id: string, body: WebhookUpdateRequest}; export type PatchApiV1ClientsClientIdWebhooksIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookType = z.enum(["MONEY_IN", "STATUS_UPDATE", "CEP", "REPORT", "REFUND"]); export const webhookStatus = z.enum(["ACTIVE", "INACTIVE"]); export const webhookResponse = z.object({id: z.string(), clientId: z.string(), url: z.string(), token: z.string().optional(), webhookType: webhookType, webhookStatus: webhookStatus, createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().nullable().optional(), blockedAt: z.string().nullable().optional(), deletedBy: z.string().nullable().optional(), blockedBy: z.string().nullable().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 }; /** * Update a client webhook * * PATCH /v1/clients/{clientId}/webhooks/{id} */ export const patchApiV1ClientsClientIdWebhooksId = async (args: PatchApiV1ClientsClientIdWebhooksIdArgs, options?: PatchApiV1ClientsClientIdWebhooksIdOptions) => { const {baseUrl = "https://apicore.stg.finch.lat", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/clients/${encodeURIComponent(String(args.clientId))}/webhooks/${encodeURIComponent(String(args.id))}${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(webhookResponse, await res.json()) };