import {z} from 'zod' export type RangeFilterDateTime = {eq?: string | undefined, gt?: string | undefined, gte?: string | undefined, lt?: string | undefined, lte?: string | undefined}; export type BillingNumberChargeType = 'number_activation' | 'number_fee' | 'partial_number_fee' | 'number_deposit_fee' | 'number_early_termination'; export type GetApiApiV4BillingNumberChargesArgs = {created_at?: (string | RangeFilterDateTime) | undefined, number?: string | undefined, transaction_type?: BillingNumberChargeType | undefined, country?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiV4BillingNumberChargesOptions = {baseUrl: string, StaticKey?: string | undefined, fetch?: typeof fetch | undefined}; export const billingNumberChargeType = z.enum(["number_activation", "number_fee", "partial_number_fee", "number_deposit_fee", "number_early_termination"]); export const billingNumberCharge = z.object({id: z.string().optional(), number: z.string().optional(), number_type: z.enum(["mobile", "national", "toll_free", "shared_cost", "geographic"]).nullable().optional(), country: z.string().optional(), transaction_type: billingNumberChargeType.optional(), amount: z.string().optional(), billed_month: z.string().optional(), created_at: z.string().optional()}); export const metadata = z.object({total: z.number().int().optional(), next_cursor: z.string().nullable().optional()}); export const getApiApiV4BillingNumberChargesResponse = z.object({number_charges: z.array(billingNumberCharge).optional(), metadata: metadata.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 }; /** * Get list of number charges * * GET /api/v4/billing/number_charges */ export const getApiApiV4BillingNumberCharges = async (args: GetApiApiV4BillingNumberChargesArgs, options: GetApiApiV4BillingNumberChargesOptions) => { const {baseUrl, fetch: fetchFn = fetch, StaticKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created_at != null) query.append("created_at", String(args.created_at)) if (args.number != null) query.append("number", String(args.number)) if (args.transaction_type != null) query.append("transaction_type", String(args.transaction_type)) if (args.country != null) query.append("country", String(args.country)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (StaticKey !== undefined) headers.set('authorization', `Bearer ${StaticKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/billing/number_charges${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(getApiApiV4BillingNumberChargesResponse, await res.json()) };