import {z} from 'zod' export type GetApiPackageGetByCustomerArgs = {customer_id: number, per_page?: number | undefined}; export type GetApiPackageGetByCustomerOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const nullableDateTime = z.string().nullable(); export const packageOptionValue = z.object({id: z.number().int(), option_id: z.number().int(), display_name: z.string(), cycle_type: z.number().int(), price: z.number(), fee: z.number(), created_at: nullableDateTime.optional(), updated_at: nullableDateTime.optional(), deleted_at: nullableDateTime.optional()}).and(z.record(z.string(), z.unknown())); export const packageOption = z.object({id: z.number().int(), user_id: z.number().int().optional(), internal_name: z.string().optional(), display_name: z.string(), api_type: z.string().nullable().optional(), required: z.number().int(), type: z.number().int(), values: z.array(packageOptionValue), created_at: nullableDateTime.optional(), updated_at: nullableDateTime.optional(), deleted_at: nullableDateTime.optional()}).and(z.record(z.string(), z.unknown())); export const packageListResponse = z.object({success: z.literal(true), packages: z.object({data: z.array(z.object({options: z.array(packageOption)}).and(z.record(z.string(), z.unknown())))}).and(z.record(z.string(), z.unknown()))}).and(z.record(z.string(), z.unknown())); 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 packages available to a customer * * GET /package/get-by-customer */ export const getApiPackageGetByCustomer = async (args: GetApiPackageGetByCustomerArgs, options?: GetApiPackageGetByCustomerOptions) => { const {baseUrl = "https://demo.onlinebillingform.com/api/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/package/get-by-customer${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(packageListResponse, await res.json()) };