import {z} from 'zod' export type WebhookDeliveryStatus = 'success' | 'failed'; export type GetApiV1OrganizationWebhookDestinationWebhookDestinationIdDeliveryArgs = {webhook_destination_id: string, organization: string, status?: (WebhookDeliveryStatus | null) | undefined, type?: (string | null) | undefined, created_after?: (string | null) | undefined, created_before?: (string | null) | undefined, limit?: number | undefined, continuation_token?: number | undefined, sort_by?: Array | undefined, 'x-mongo-cluster-name'?: (string | null) | undefined, 'Sec-WebSocket-Protocol'?: Array | undefined}; export type GetApiV1OrganizationWebhookDestinationWebhookDestinationIdDeliveryOptions = {baseUrl?: string | undefined, BearerAuthorization?: string | undefined, BearerAuthorizationOrganization?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const webhookDeliveryStatus = z.enum(["success", "failed"]); export const deliveryAttempt = z.object({delivery_time: z.string(), status_code: z.number().int()}); export const webhookDeliveryInstance = z.object({id: z.string(), type: z.string(), webhook_content: z.record(z.string(), z.unknown()), status: webhookDeliveryStatus, delivery_attempts: z.array(deliveryAttempt), dual_signed: z.boolean(), created_at: z.string()}); export const srcAppEndpointsWebhookDestinationGetWebhookDeliveriesResponse = z.object({webhook_deliveries: z.array(webhookDeliveryInstance), has_more: z.boolean(), continuation_token: z.number().int().nullable()}); 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 }; /** * Get webhook deliveries * * GET /v1/{organization}/webhook_destination/{webhook_destination_id}/delivery */ export const getApiV1OrganizationWebhookDestinationWebhookDestinationIdDelivery = async (args: GetApiV1OrganizationWebhookDestinationWebhookDestinationIdDeliveryArgs, options?: GetApiV1OrganizationWebhookDestinationWebhookDestinationIdDeliveryOptions) => { const {baseUrl = "https://api.amigo.ai", fetch: fetchFn = fetch, BearerAuthorization, BearerAuthorizationOrganization, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.continuation_token != null) query.append("continuation_token", String(args.continuation_token)) for (const item of args.sort_by ?? []) query.append("sort_by", String(item)) if (args['x-mongo-cluster-name'] != null) headers.set("x-mongo-cluster-name", String(args['x-mongo-cluster-name'])) if (args['Sec-WebSocket-Protocol'] != null) headers.set("Sec-WebSocket-Protocol", args['Sec-WebSocket-Protocol'].join(',')) if (BearerAuthorization !== undefined) headers.set('authorization', `Bearer ${BearerAuthorization}`) if (BearerAuthorizationOrganization !== undefined) headers.set("X-ORG-ID", BearerAuthorizationOrganization) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.organization))}/webhook_destination/${encodeURIComponent(String(args.webhook_destination_id))}/delivery${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(srcAppEndpointsWebhookDestinationGetWebhookDeliveriesResponse, await res.json()) };