import {z} from 'zod' export type TransfermateWebhookInput = {event_id: string, customer_id: string, event_code: string, event_details?: ({Unique_Record_id?: string | undefined, description?: string | undefined, status?: string | undefined, updated_at?: string | undefined, value_date?: string | undefined, Global_Account?: string | undefined, currency?: string | undefined, amount?: string | undefined, payer?: string | undefined, ga_account_id?: number | undefined, reference?: string | undefined, rail_type?: string | undefined} | {[key: string]: unknown}) | undefined} | {[key: string]: unknown}; export type CreateApiApiV1WebhooksTransfermateArgs = {body: TransfermateWebhookInput}; export type CreateApiApiV1WebhooksTransfermateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transfermateWebhookSuccessResponse = z.object({event_id: z.string(), customer_id: z.string(), event_code: z.string(), event_details: z.object({Unique_Record_id: z.string().optional(), description: z.string().optional(), status: z.string().optional(), updated_at: z.string().optional(), value_date: z.string().optional(), Global_Account: z.string().optional(), currency: z.string().optional(), amount: z.string().optional(), payer: z.string().optional(), ga_account_id: z.number().optional(), reference: z.string().optional(), rail_type: z.string().optional()}).and(z.record(z.string(), z.unknown())).optional()}).and(z.record(z.string(), z.unknown())); 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 }; /** * Handle TransferMate webhook events * * POST /api/v1/webhooks/transfermate */ export const createApiApiV1WebhooksTransfermate = async (args: CreateApiApiV1WebhooksTransfermateArgs, options?: CreateApiApiV1WebhooksTransfermateOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webhooks/transfermate${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(transfermateWebhookSuccessResponse, await res.json()) };