import {z} from 'zod' export type OfferStatus = 'active' | 'disabled' | 'deleted'; export type ProductType = 'unique' | 'subscription'; export type IntervalTypeEnum = 'week' | 'month' | 'year' | 'lifetime'; export type Offer = {id: string, name: string, image?: (string | null) | undefined, price: number, units?: number | undefined, default: boolean, product: string, status?: OfferStatus | undefined, type?: ProductType | undefined, intervalType?: IntervalTypeEnum | undefined, interval?: number | undefined, recurrence_period?: number | undefined, quantity_recurrences?: number | undefined, trial_days?: number | undefined, max_retries?: number | undefined, retry_interval?: number | undefined}; export type CreateApiPublicApiOffersArgs = {body: Offer}; export type CreateApiPublicApiOffersOptions = {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 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, * * POST /public_api/offers/ */ export const createApiPublicApiOffers = async (args: CreateApiPublicApiOffersArgs, options?: CreateApiPublicApiOffersOptions) => { const {baseUrl = "https://api.cakto.com.br", fetch: fetchFn = fetch, OAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(offer, await res.json()) };