import {z} from 'zod' export type GetApiV1WebhooksArgs = {page_number?: number | undefined, page_size?: number | undefined}; export type GetApiV1WebhooksOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const environmentMode = z.enum(["test", "prod", "sandbox"]); export const webhookDeliveryMode = z.enum(["http", "cli"]); export const webhookStatus = z.enum(["enabled", "disabled"]); 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 webhookEntity = z.object({id: z.string(), mode: environmentMode, object: z.string(), store_id: z.string(), name: z.string().nullable(), url: z.string(), delivery_mode: webhookDeliveryMode, status: webhookStatus, events: z.array(webhookEventType), secret: z.string().optional()}); export const paginationEntity = z.object({total_records: z.number(), total_pages: z.number(), current_page: z.number(), next_page: z.number().nullable(), prev_page: z.number().nullable()}); export const webhookListEntity = z.object({items: z.array(webhookEntity), pagination: paginationEntity}); 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 endpoints * * GET /v1/webhooks */ export const getApiV1Webhooks = async (args: GetApiV1WebhooksArgs, options?: GetApiV1WebhooksOptions) => { 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.page_number != null) query.append("page_number", String(args.page_number)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks${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(webhookListEntity, await res.json()) };