import {z} from 'zod' export type GetApiV1WebhooksIdEventsPendingArgs = {id: string, limit?: number | undefined}; export type GetApiV1WebhooksIdEventsPendingOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookEventType = z.enum(["checkout.completed", "refund.created", "dispute.created", "subscription.active", "subscription.trialing", "subscription.canceled", "subscription.scheduled_cancel", "subscription.paid", "subscription.expired", "subscription.unpaid", "subscription.update", "subscription.past_due", "subscription.paused", "customer_credits.exhausted", "credits.granted", "credits.consumed", "credits.auto_recharged"]); export const webhookPendingEventEntity = z.object({id: z.string(), object: z.string(), event_type: webhookEventType, created_at: z.string(), body: z.string(), headers: z.record(z.string(), z.string())}); export const webhookPendingEventListEntity = z.object({items: z.array(webhookPendingEventEntity)}); 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 pending events for a CLI webhook endpoint * * GET /v1/webhooks/{id}/events/pending */ export const getApiV1WebhooksIdEventsPending = async (args: GetApiV1WebhooksIdEventsPendingArgs, options?: GetApiV1WebhooksIdEventsPendingOptions) => { const {baseUrl = "https://api.creem.io", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/${encodeURIComponent(String(args.id))}/events/pending${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(webhookPendingEventListEntity, await res.json()) };