import {z} from 'zod' export type ResendWebhookDto = {event: 'new-sub-transaction' | 'updated-sub-transaction' | 'new-sub-checkout' | 'updated-sub-checkout' | 'new-zoop-transaction' | 'updated-zoop-transaction' | 'new-pagseguro-transaction' | 'updated-pagseguro-transaction' | 'new-establishment' | 'updated-establishment-status' | 'updated-establishment-data' | 'new-billet' | 'updated-billet-status' | 'updated-establishment-gateway' | 'new-sub-split' | 'canceled-sub-split' | 'new-transfer-ted-out' | 'updated-transfer-ted-out' | 'new-transfer-ted-in' | 'new-transfer-pix-out' | 'updated-transfer-pix-out' | 'new-transfer-pix-in' | 'new-transfer-p2p-in' | 'new-transfer-p2p-out' | 'updated-transfer-p2p-out' | 'new-payment-billet' | 'updated-payment-billet' | 'updated-export-file', id?: (string | number) | undefined}; export type CreateApiV1MarketplaceHooksResendArgs = {'integration-key': string, 'x-token': string, establishment_id?: string | undefined, body: ResendWebhookDto}; export type CreateApiV1MarketplaceHooksResendOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookResendResponse = z.object({message: z.string()}); 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 }; /** * Redisparar webhook * * POST /v1/marketplace/hooks/resend */ export const createApiV1MarketplaceHooksResend = async (args: CreateApiV1MarketplaceHooksResendArgs, options?: CreateApiV1MarketplaceHooksResendOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['integration-key'] != null) headers.set("integration-key", String(args['integration-key'])) if (args['x-token'] != null) headers.set("x-token", String(args['x-token'])) if (args.establishment_id != null) headers.set("establishment_id", String(args.establishment_id)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/hooks/resend${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(webhookResendResponse, await res.json()) };