import {z} from 'zod' export type GetApiEsimPurchasesTransactionIdArgs = {transactionId: string}; export type GetApiEsimPurchasesTransactionIdOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoESimConfirmation = z.object({activationCode: z.string(), externalReferenceId: z.string(), iccid: z.string(), redemptionInstructions: z.string(), smdpAddress: z.string()}); export const dtoError = z.object({code: z.string(), description: z.string(), message: z.string()}); export const dtoTransactionStatus = z.enum(["ACCEPTED", "PENDING", "AUTHORIZED", "IN_PROGRESS", "DONE", "FAILED"]); export const dtoTransactionLogItem = z.object({dateTime: z.string(), status: dtoTransactionStatus, statusMessage: z.string()}); export const dtoPriceType = z.enum(["FIXED", "RANGE"]); export const dtoProductType = z.enum(["TOPUP", "ESIM", "VOUCHER", "BILL_PAY", "WALLET_RECHARGE", "REFUND", "BULK_CHECKOUT"]); export const dtoESimRefund = z.object({status: dtoTransactionStatus}); export const dtoESimPurchaseRoaming = z.object({country: z.string(), dataSpeeds: z.array(z.string())}); export const dtoValueType = z.enum(["ZEND", "PRICE", "COST"]); export const dtoPurchaseValue = z.object({type: dtoValueType, value: z.number().int()}); export const dtoESimPurchase = z.object({brand: z.string(), brandName: z.string(), confirmation: dtoESimConfirmation.optional(), cost: z.number().int(), costBase: z.number().int(), costCurrency: z.string(), costCurrencyDivisor: z.number().int(), costFee: z.number().int(), country: z.string(), createdAt: z.string(), dataGB: z.number(), dataSpeeds: z.array(z.string()), dataUnlimited: z.boolean(), durationDays: z.number().int(), error: dtoError.optional(), log: z.array(dtoTransactionLogItem), notes: z.string(), offerId: z.string(), price: z.number().int(), priceBase: z.number().int(), priceCurrency: z.string(), priceCurrencyDivisor: z.number().int(), priceFee: z.number().int(), priceType: dtoPriceType, productType: dtoProductType, refund: dtoESimRefund.optional(), regions: z.array(z.string()), roaming: z.array(dtoESimPurchaseRoaming), shortNotes: z.string(), smsNumber: z.number().int(), smsUnlimited: z.boolean(), status: dtoTransactionStatus, subTypes: z.array(z.string()), transactionId: z.string(), updatedAt: z.string(), value: dtoPurchaseValue.optional(), 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 eSim transaction by id * * GET /esim/purchases/{transactionId} */ export const getApiEsimPurchasesTransactionId = async (args: GetApiEsimPurchasesTransactionIdArgs, options: GetApiEsimPurchasesTransactionIdOptions) => { 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/purchases/${encodeURIComponent(String(args.transactionId))}${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(dtoESimPurchase, await res.json()) };