import {z} from 'zod' export type OrderBumpUpdate = {offer?: (string | null) | undefined, referencePrice?: (number | null) | undefined, cta?: (string | null) | undefined, title?: (string | null) | undefined, description?: (string | null) | undefined, position?: number | undefined, showImage?: boolean | undefined}; export type UpdateApiPublicApiBumpsIdArgs = {id: string, body: OrderBumpUpdate}; export type UpdateApiPublicApiBumpsIdOptions = {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 orderBump = z.object({id: z.string().max(40).optional(), product: z.string(), installments: z.number().int(), referencePrice: z.number().nullable().optional(), offer: offer, cta: z.string().max(255).nullable().optional(), title: z.string().max(255).nullable().optional(), description: z.string().nullable().optional(), position: z.number().int().optional(), image: z.string().nullable(), showImage: z.boolean().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 }; /** * Atualiza um order bump existente. * * PUT /public_api/bumps/{id}/ */ export const updateApiPublicApiBumpsId = async (args: UpdateApiPublicApiBumpsIdArgs, options?: UpdateApiPublicApiBumpsIdOptions) => { 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/bumps/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(orderBump, await res.json()) };