import {z} from 'zod' export type GetApiEsimOffersOfferIdArgs = {offerId: string}; export type GetApiEsimOffersOfferIdOptions = {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 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 an eSIM offer by the offer ID * * GET /esim/offers/{offerId} */ export const getApiEsimOffersOfferId = async (args: GetApiEsimOffersOfferIdArgs, options: GetApiEsimOffersOfferIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/esim/offers/${encodeURIComponent(String(args.offerId))}${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(dtoESimOffer, await res.json()) };