import {z} from 'zod' export type WebhookHttpConfig = {method?: string | undefined, timeout_seconds?: number | undefined, headers?: {[key: string]: string} | undefined, params?: {[key: string]: string} | undefined}; export type UpdateWebhookRequest = {url?: (string | null) | undefined, secret?: (string | null) | undefined, event_types?: (Array | null) | undefined, enabled?: (boolean | null) | undefined, http_config?: (WebhookHttpConfig | null) | undefined}; export type PatchApiV1DefaultBanksBankIdWebhooksWebhookIdArgs = {bank_id: string, webhook_id: string, authorization?: (string | null) | undefined, body: UpdateWebhookRequest}; export type PatchApiV1DefaultBanksBankIdWebhooksWebhookIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const webhookHttpConfig = z.object({method: z.string().optional(), timeout_seconds: z.number().int().optional(), headers: z.record(z.string(), z.string()).optional(), params: z.record(z.string(), z.string()).optional()}); export const webhookResponse = z.object({id: z.string(), bank_id: z.string().nullable(), url: z.string(), secret: z.string().nullable().optional(), event_types: z.array(z.string()), enabled: z.boolean(), http_config: webhookHttpConfig.optional(), created_at: z.string().nullable().optional(), updated_at: 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 webhook * * PATCH /v1/default/banks/{bank_id}/webhooks/{webhook_id} */ export const patchApiV1DefaultBanksBankIdWebhooksWebhookId = async (args: PatchApiV1DefaultBanksBankIdWebhooksWebhookIdArgs, options: PatchApiV1DefaultBanksBankIdWebhooksWebhookIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/default/banks/${encodeURIComponent(String(args.bank_id))}/webhooks/${encodeURIComponent(String(args.webhook_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()) };