import {z} from 'zod' export type GetApiPublicApiOffersArgs = {limit?: number | undefined, page?: number | undefined}; export type GetApiPublicApiOffersOptions = {baseUrl?: string | undefined, OAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const offerStatus = z.enum(["active", "disabled", "deleted"]); export const productType = z.enum(["unique", "subscription"]); export const intervalTypeEnum = z.enum(["week", "month", "year", "lifetime"]); export const offer = z.object({id: z.string(), name: z.string().max(255), image: z.string().nullable().optional(), price: z.number(), units: z.number().int().gte(1).optional(), default: z.boolean(), product: z.string(), status: offerStatus.optional(), type: productType.optional(), intervalType: intervalTypeEnum.optional(), interval: z.number().int().optional(), recurrence_period: z.number().int().optional(), quantity_recurrences: z.number().int().optional(), trial_days: z.number().int().optional(), max_retries: z.number().int().optional(), retry_interval: z.number().int().optional()}); export const paginatedOfferList = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(offer).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 }; /** * Public API for managing offers, inherits from OfferAPIView, * * GET /public_api/offers/ */ export const getApiPublicApiOffers = async (args: GetApiPublicApiOffersArgs, options?: GetApiPublicApiOffersOptions) => { 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/offers/${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(paginatedOfferList, await res.json()) };