import {z} from 'zod' export type GetApiEsimOffersArgs = {_limit: number, _offset: number, brand?: string | undefined, country?: string | undefined, regions?: ('Africa' | 'Asia' | 'Caribbean' | 'Central America' | 'Eastern Europe' | 'Global' | 'Middle East and North Africa' | 'North America' | 'Oceania' | 'South America' | 'South Asia' | 'Southeast Asia' | 'Western Europe') | undefined, subType?: string | undefined}; export type GetApiEsimOffersOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoCost = z.object({currency: z.string(), currencyDivisor: z.number().int(), discount: z.number(), fee: z.number().int(), feePct: z.number(), fixed: z.number().int().optional(), fx: z.number().optional(), max: z.number().int().optional(), min: z.number().int().optional()}); export const dtoPrice = z.object({currency: z.string(), currencyDivisor: z.number().int(), fee: z.number().int(), fixed: z.number().int().optional(), fx: z.number().optional(), increment: z.number().int().optional(), margin: z.number().optional(), max: z.number().int().optional(), min: z.number().int().optional(), suggestedFixed: z.number().int().optional(), suggestedFx: z.number().optional()}); export const dtoPriceType = z.enum(["FIXED", "RANGE"]); export const dtoProductType = z.enum(["TOPUP", "ESIM", "VOUCHER", "BILL_PAY", "WALLET_RECHARGE", "REFUND", "BULK_CHECKOUT"]); export const dtoESimNetwork = z.object({brand: z.string(), brandName: z.string(), dataSpeeds: z.array(z.string())}); export const dtoESimRoaming = z.object({country: z.string(), dataSpeeds: z.array(z.string()), networks: z.array(dtoESimNetwork)}); export const dtoESimOffer = z.object({brand: z.string(), brandName: z.string(), cost: dtoCost, country: z.string(), createdAt: z.string(), dataGB: z.number(), dataSpeeds: z.array(z.string()), dataUnlimited: z.boolean(), durationDays: z.number().int(), enabled: z.boolean(), notes: z.string(), offerId: z.string(), price: dtoPrice, priceType: dtoPriceType, productType: dtoProductType, regions: z.array(z.string()), roaming: z.array(dtoESimRoaming), shortNotes: z.string(), smsNumber: z.number().int(), smsUnlimited: z.boolean(), subTypes: z.array(z.string()), updatedAt: z.string(), voiceMinutes: z.number().int(), voiceUnlimited: z.boolean()}); export const dtoESimOffersResponse = z.object({limit: z.number().int(), list: z.array(dtoESimOffer), offset: z.number().int(), total: z.number().int()}); 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 }; /** * Get list of eSIM offers * * GET /esim/offers */ export const getApiEsimOffers = async (args: GetApiEsimOffersArgs, options: GetApiEsimOffersOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = 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._offset != null) query.append("_offset", String(args._offset)) if (args.brand != null) query.append("brand", String(args.brand)) if (args.country != null) query.append("country", String(args.country)) if (args.regions != null) query.append("regions", String(args.regions)) if (args.subType != null) query.append("subType", String(args.subType)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/esim/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(dtoESimOffersResponse, await res.json()) };