import {z} from 'zod' export type Authentication = {basic?: ({password?: string | undefined, username?: string | undefined}) | undefined, bearer?: ({token?: string | undefined}) | undefined, type?: ('NONE' | 'BASIC' | 'BEARER') | undefined}; export type EnabledEvents = Array<{entity?: string | undefined, types?: Array | undefined}>; export type UpdateWebhookAllFields = {authentication?: Authentication | undefined, enabled?: boolean | undefined, enabled_events?: EnabledEvents | undefined, generate_new_secret?: boolean | undefined, nickname?: string | undefined, previous_secret_active_hours?: number | undefined, url?: string | undefined}; export type UpdateWebhookAuthentication = {authentication?: Authentication | undefined}; export type UpdateWebhookEnableDisable = {enabled?: boolean | undefined}; export type UpdateWebhookEventFiltering = {enabled_events?: EnabledEvents | undefined}; export type UpdateApiWebhooksWebhookIdArgs = {body: UpdateWebhookAllFields | UpdateWebhookAuthentication | UpdateWebhookEnableDisable | UpdateWebhookEventFiltering}; export type UpdateApiWebhooksWebhookIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string().min(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const applicationId = z.string().min(1); export const enabledEvents = z.array(z.object({entity: z.string().optional(), types: z.array(z.string()).optional()})); export const applicationLink = z.object({href: z.string().min(1).optional()}); export const self = z.object({href: z.string().min(1).optional()}); export const webhook = z.object({id: id.optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), application: applicationId.optional(), authentication: z.object({type: z.enum(["NONE", "BASIC", "BEARER"]).optional()}).optional(), enabled: z.boolean().optional(), enabled_events: enabledEvents.optional(), is_accepting_events: z.boolean().optional(), nickname: z.string().nullable().optional(), previous_secret_expires_at: z.string().nullable().optional(), secret_signing_key: z.string().nullable().optional(), url: z.string().optional(), _links: z.object({application: applicationLink.optional(), self: self.optional()}).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 * * PUT /webhooks/{webhook_id} */ export const updateApiWebhooksWebhookId = async (args: UpdateApiWebhooksWebhookIdArgs, options?: UpdateApiWebhooksWebhookIdOptions) => { const {baseUrl = "https://finix.sandbox-payments-api.com", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/${encodeURIComponent(String(args.webhook_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(webhook, await res.json()) };