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 CreateWebhookRequest = {url: string, secret?: (string | null) | undefined, event_types?: Array | undefined, enabled?: boolean | undefined, http_config?: WebhookHttpConfig | undefined}; export type CreateApiV1DefaultBanksBankIdWebhooksArgs = {bank_id: string, authorization?: (string | null) | undefined, body: CreateWebhookRequest}; export type CreateApiV1DefaultBanksBankIdWebhooksOptions = {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 }; /** * Register webhook * * POST /v1/default/banks/{bank_id}/webhooks */ export const createApiV1DefaultBanksBankIdWebhooks = async (args: CreateApiV1DefaultBanksBankIdWebhooksArgs, options: CreateApiV1DefaultBanksBankIdWebhooksOptions) => { 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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()) };