import {z} from 'zod' export type GetApiV1WebhooksMessagesArgs = {tag?: (string | null) | undefined, eventTypes?: (string | null) | undefined, channel?: (string | null) | undefined, before?: (string | null) | undefined, after?: (string | null) | undefined, limit?: number | undefined, iterator?: (string | null) | undefined, withContent?: boolean | undefined}; export type GetApiV1WebhooksMessagesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookMessage = z.object({id: z.string(), eventId: z.string().nullable().optional(), eventType: z.string(), timestamp: z.string(), channels: z.array(z.string()).nullable().optional(), tags: z.array(z.string()).nullable().optional(), payload: z.record(z.string(), z.unknown()).nullable().optional()}); export const webhookMessagesListResponse = z.object({results: z.array(webhookMessage), 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 Messages * * GET /v1/webhooks/messages */ export const getApiV1WebhooksMessages = async (args: GetApiV1WebhooksMessagesArgs, options?: GetApiV1WebhooksMessagesOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tag != null) query.append("tag", String(args.tag)) if (args.eventTypes != null) query.append("eventTypes", String(args.eventTypes)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.iterator != null) query.append("iterator", String(args.iterator)) if (args.withContent != null) query.append("withContent", String(args.withContent)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/messages${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(webhookMessagesListResponse, await res.json()) };