import {z} from 'zod' export type GetApiV2WebhooksTeamIdWebhookIdDeliveriesDeliveryIdArgs = {teamId: string, webhookId: string, deliveryId: string}; export type GetApiV2WebhooksTeamIdWebhookIdDeliveriesDeliveryIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookAttempt = z.object({attempt: z.number().int(), status: z.enum(["success", "failed"]), status_code: z.number().int().nullable().optional(), error: z.string().nullable().optional(), request_body: z.string().nullable().optional(), response_body: z.string().nullable().optional(), created_at: z.string()}); export const webhookDeliveryDetail = z.object({delivery_id: z.string(), webhook_id: z.string(), team_id: z.string().nullable().optional(), type: z.enum(["delivery", "verification"]), event: z.string().nullable().optional(), url: z.string().optional(), status: z.enum(["success", "failed"]), status_code: z.number().int().nullable().optional(), attempts: z.number().int().nullable().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), attempt_history: z.array(webhookAttempt).optional()}); 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 a delivery * * GET /v2/webhooks/{teamId}/{webhookId}/deliveries/{deliveryId} */ export const getApiV2WebhooksTeamIdWebhookIdDeliveriesDeliveryId = async (args: GetApiV2WebhooksTeamIdWebhookIdDeliveriesDeliveryIdArgs, options?: GetApiV2WebhooksTeamIdWebhookIdDeliveriesDeliveryIdOptions) => { const {baseUrl = "https://developers.micro.so", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/webhooks/${encodeURIComponent(String(args.teamId))}/${encodeURIComponent(String(args.webhookId))}/deliveries/${encodeURIComponent(String(args.deliveryId))}${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(webhookDeliveryDetail, await res.json()) };