import {z} from 'zod' export type PlanRequest = {amount: number, currency?: string | undefined, expiry_count?: number | undefined, frequency: number, id?: string | undefined, interval: 'week' | 'half_month' | 'month' | 'year', name: string, trial_period_days?: number | undefined, max_retries?: number | undefined, retry_delay_hours?: number | undefined}; export type CreateApiPlansArgs = {'Accept-Language'?: ('es' | 'en') | undefined, 'X-Child-Company-Id'?: string | undefined, body: PlanRequest}; export type CreateApiPlansOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const planResponse = z.object({amount: z.number().int().optional(), created_at: z.number().int().optional(), currency: z.string().max(3).optional(), expiry_count: z.number().int().nullable().optional(), frequency: z.number().int().optional(), id: z.string().optional(), interval: z.string().optional(), livemode: z.boolean().optional(), name: z.string().optional(), object: z.string().optional(), trial_period_days: z.number().int().nullable().optional(), max_retries: z.number().int().optional(), retry_delay_hours: 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 }; /** * Create Plan * * POST /plans */ export const createApiPlans = async (args: CreateApiPlansArgs, options?: CreateApiPlansOptions) => { const {baseUrl = "https://api.conekta.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.conekta-v2.3.0+json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args['X-Child-Company-Id'] != null) headers.set("X-Child-Company-Id", String(args['X-Child-Company-Id'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/plans${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(planResponse, await res.json()) };