import {z} from 'zod' export type GetApiWebhookSubscriptionsArgs = {limit?: number | undefined, lastEvaluated?: string | undefined, orderBy?: string | undefined}; export type GetApiWebhookSubscriptionsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiWebhookSubscriptionsResponse = z.object({webhookSubscriptions: z.array(z.object({subscriberId: z.string().optional(), webhookSubscriptionId: z.string().optional(), endpointUrl: z.string().optional(), version: z.literal("2025-02-01").optional(), subscriptions: z.array(z.object({resourceType: z.enum(["review", "loyalty_adjustment_transaction", "loyalty_customer", "loyalty_earning_transaction", "loyalty_redemption_transaction"]).optional(), topic: z.enum(["create", "update", "delete"]).optional()})).optional(), isEnabled: z.boolean().optional(), dateCreated: z.string().optional(), dateModified: z.string().optional()})).optional(), nextUrl: z.string().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 }; /** * List Webhook Subscriptions * * GET /webhook_subscriptions */ export const getApiWebhookSubscriptions = async (args: GetApiWebhookSubscriptionsArgs, options?: GetApiWebhookSubscriptionsOptions) => { const {baseUrl = "https://api.okendo.io/enterprise", fetch: fetchFn = fetch, basic} = 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 (args.lastEvaluated != null) query.append("lastEvaluated", String(args.lastEvaluated)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/webhook_subscriptions${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(getApiWebhookSubscriptionsResponse, await res.json()) };