import {z} from 'zod' export type CreateApiPublicUserCheckoutQuoteArgs = {'Content-Type': string, body: {country_id?: string | undefined, cycle_interval?: ('year' | 'month' | 'week' | 'day') | undefined, cycle_interval_count?: number | undefined, product_code?: string | undefined, product_protocol?: ('ipv4' | 'ipv6' | 'dual') | undefined, product_type?: ('datacenter' | 'isp' | 'residential') | undefined, promotional_code?: string | undefined, quantity: number, service_fulfillment_filter?: Record | undefined}}; export type CreateApiPublicUserCheckoutQuoteOptions = {baseUrl?: string | undefined, APIPrivateKey?: string | undefined, APIPublicKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPublicUserCheckoutQuoteResponse = z.object({data: z.object({before_discount_total: z.number().int().optional(), credit_balance: z.number().int().optional(), credit_only_checkout: z.boolean().optional(), customer_credit_balance_applied: z.boolean().optional(), customer_credit_balance_applied_amount: z.number().int().optional(), discount: z.number().int().optional(), discounted: z.boolean().optional(), invalid_reason: z.string().optional(), is_top_up: z.boolean().optional(), is_valid: z.boolean().optional(), line_items: z.array(z.object({item_country_id: z.string().optional(), item_name: z.string().optional(), item_per_unit_amount: z.number().int().optional(), item_price_id: z.string().optional(), item_price_type: z.string().optional(), item_quantity: z.number().int().optional(), item_total: z.number().int().optional(), item_type: z.string().optional(), service_fulfillment_filter: z.object({}).optional()})).optional(), promotional_code: z.string().optional(), promotional_code_id: z.string().optional(), top_up_amount: z.number().int().optional(), total: z.number().int().optional(), total_after_applied_credit: z.number().int().optional()}).optional(), message: z.string().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 }; /** * Generate Checkout Quote * * POST /public/user/checkout/quote */ export const createApiPublicUserCheckoutQuote = async (args: CreateApiPublicUserCheckoutQuoteArgs, options?: CreateApiPublicUserCheckoutQuoteOptions) => { const {baseUrl = "https://api.byteful.com/1.0", fetch: fetchFn = fetch, APIPrivateKey, APIPublicKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (APIPrivateKey !== undefined) headers.set("X-API-Private-Key", APIPrivateKey) if (APIPublicKey !== undefined) headers.set("X-API-Public-Key", APIPublicKey) const url = `${baseUrl.replace(/\/+$/, '')}/public/user/checkout/quote${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(createApiPublicUserCheckoutQuoteResponse, await res.json()) };