import {z} from 'zod' export type GetApiV1WebhooksMessagesMsgIdAttemptsArgs = {msgId: string}; export type GetApiV1WebhooksMessagesMsgIdAttemptsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookMessageAttempt = z.object({id: z.string(), msgId: z.string(), endpointId: z.string(), url: z.string(), status: z.string(), responseStatusCode: z.number().int().nullable().optional(), responseDurationMs: z.number().int().nullable().optional(), response: z.string().nullable().optional(), triggerType: z.string().nullable().optional(), timestamp: z.string()}); export const webhookMessageAttemptsResponse = z.object({results: z.array(webhookMessageAttempt), iterator: z.string().nullable().optional(), prevIterator: z.string().nullable().optional(), done: z.boolean()}); 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 }; /** * List Webhook Delivery Attempts * * GET /v1/webhooks/messages/{msg_id}/attempts */ export const getApiV1WebhooksMessagesMsgIdAttempts = async (args: GetApiV1WebhooksMessagesMsgIdAttemptsArgs, options?: GetApiV1WebhooksMessagesMsgIdAttemptsOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/messages/${encodeURIComponent(String(args.msg_id))}/attempts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(webhookMessageAttemptsResponse, await res.json()) };