import {z} from 'zod' export type GetApiPublicApiWebhookArgs = {limit?: number | undefined, page?: number | undefined}; export type GetApiPublicApiWebhookOptions = {baseUrl?: string | undefined, OAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const appStatus = z.enum(["active", "disabled", "waiting_config"]); export const productType = z.enum(["unique", "subscription"]); export const productStatus = z.enum(["active", "blocked", "deleted"]); export const category = z.object({id: z.string().max(255).optional(), name: z.string().max(255)}); export const product = z.object({id: z.string().max(255).optional(), name: z.string().max(255), image: z.string().nullable().optional(), description: z.string(), price: z.number(), type: productType.optional(), contentDeliveries: z.array(z.string()), emailAccessLink: z.string().max(2048).nullable().optional(), salesPage: z.string().max(2048).nullable().optional(), status: productStatus.optional(), paymentMethods: z.array(z.string()), category: category}); export const eventType = z.enum(["initiate_checkout", "checkout_abandonment", "purchase_approved", "purchase_refused", "pix_gerado", "boleto_gerado", "picpay_gerado", "openfinance_nubank_gerado", "chargeback", "refund", "subscription_created", "subscription_canceled", "subscription_renewed", "subscription_renewal_refused"]); export const event = z.object({id: z.number().int(), name: z.string().max(255), custom_id: eventType}); export const appSerializerRead = z.object({id: z.number().int(), status: appStatus.optional(), name: z.string().max(255), url: z.string().max(2048).nullable().optional(), products: z.array(product), events: z.array(event), fields: z.unknown(), createdAt: z.string(), updatedAt: z.string()}); export const paginatedAppSerializerReadList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(appSerializerRead).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 }; /** * API for managing webhook Apps, it inherits from AppPlatformAPI, * * GET /public_api/webhook/ */ export const getApiPublicApiWebhook = async (args: GetApiPublicApiWebhookArgs, options?: GetApiPublicApiWebhookOptions) => { const {baseUrl = "https://api.cakto.com.br", fetch: fetchFn = fetch, OAuthToken} = 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.page != null) query.append("page", String(args.page)) if (OAuthToken !== undefined) headers.set('authorization', `Bearer ${OAuthToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/public_api/webhook/${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(paginatedAppSerializerReadList, await res.json()) };