import {z} from 'zod' export type GetApiV1ProductsArgs = {'search.product_id'?: Array | undefined, 'search.provider_id'?: Array | undefined, 'search.active'?: boolean | undefined, 'search.product_type_id'?: Array | undefined, 'search.description'?: Array | undefined, 'search.code_1'?: Array | undefined, 'search.created_date_start'?: string | undefined, 'search.created_date_end'?: string | undefined, 'search.bill_profile_ids'?: Array | undefined, 'search.service_type_ids'?: Array | undefined, 'search.currency_code'?: Array | undefined, 'search.bill_every'?: (Array<'FOLLOW_BILL_PROFILE' | 'THREE_MONTHS' | 'SIX_MONTHS' | 'ONE_YEAR' | 'TWO_YEARS' | 'THREE_YEARS' | 'FOUR_YEARS' | 'FIVE_YEARS' | 'SIX_YEARS' | 'SEVEN_YEARS' | 'EIGHT_YEARS' | 'NINE_YEARS' | 'TEN_YEARS'>) | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1ProductsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revAppModelsFieldValue = z.object({field_id: z.number().int().optional(), label: z.string().optional(), value: z.string().optional(), links: z.array(revAppComponentsLink).optional()}); export const revAppViewModelsProductView = z.object({currency_code: z.enum(["USD", "AFN", "DZD", "ARS", "AMD", "AWG", "AUD", "AZN", "BSD", "BHD", "THB", "PAB", "BBD", "BYN", "BZD", "BMD", "VEF", "BOB", "BRL", "BND", "BGN", "BIF", "CVE", "CAD", "KYD", "XOF", "XAF", "XPF", "CLP", "COP", "KMF", "CDF", "BAM", "NIO", "CRC", "CUP", "CZK", "GMD", "DKK", "MKD", "DJF", "STD", "DOP", "VND", "XCD", "EGP", "SVC", "ETB", "EUR", "FKP", "FJD", "HUF", "GHS", "GIP", "HTG", "PYG", "GNF", "GYD", "HKD", "UAH", "ISK", "INR", "IRR", "IQD", "JMD", "JOD", "KES", "PGK", "LAK", "HRK", "KWD", "AOA", "MMK", "GEL", "LBP", "ALL", "HNL", "SLL", "LRD", "LYD", "SZL", "LSL", "MGA", "MWK", "MYR", "MUR", "MXN", "MXV", "MDL", "MAD", "MZN", "BOV", "NGN", "ERN", "NAD", "NPR", "ANG", "ILS", "TWD", "NZD", "BTN", "KPW", "NOK", "MRO", "TOP", "PKR", "MOP", "CUC", "UYU", "PHP", "GBP", "BWP", "QAR", "GTQ", "ZAR", "OMR", "KHR", "RON", "MVR", "IDR", "RUB", "RWF", "SHP", "SAR", "RSD", "SCR", "SGD", "PEN", "SBD", "KGS", "SOS", "TJS", "SSP", "LKR", "SDG", "SRD", "SEK", "CHF", "SYP", "BDT", "WST", "TZS", "KZT", "TTD", "MNT", "TND", "TRY", "TMT", "AED", "UGX", "CLF", "COU", "UYI", "USN", "UZS", "VUV", "CHE", "CHW", "KRW", "YER", "JPY", "CNY", "ZMW", "ZWL", "PLN"]).optional(), product_id: z.number().int().optional(), product_type_id: z.number().int(), description: z.string().max(255), code_1: z.string().max(4000).optional(), code_2: z.string().max(1024).optional(), rate: z.number().optional(), cost: z.number().optional(), buy_rate: z.number().optional(), created_date: z.string().optional(), created_by: z.number().int().optional(), active: z.boolean().optional(), creates_order: z.boolean().optional(), provider_id: z.number().int(), bills_in_arrears: z.boolean().optional(), bill_every: z.enum(["FOLLOW_BILL_PROFILE", "THREE_MONTHS", "SIX_MONTHS", "ONE_YEAR", "TWO_YEARS", "THREE_YEARS", "FOUR_YEARS", "FIVE_YEARS", "SIX_YEARS", "SEVEN_YEARS", "EIGHT_YEARS", "NINE_YEARS", "TEN_YEARS"]).optional(), prorates: z.boolean().optional(), prorates_on_disconnect: z.boolean().optional(), customer_class: z.enum(["Business", "Residential"]).optional(), long_description: z.string().optional(), ledger_code: z.string().optional(), free_months: z.number().int().optional(), automatic_expiration_months: z.number().int().optional(), order_completion_billing: z.boolean().optional(), tax_class_id: z.number().int().optional(), wholesale_description: z.string().optional(), smart_pricing_id: z.number().int().optional(), bill_profile_ids: z.array(z.number().int()).optional(), service_type_ids: z.array(z.number().int()).optional(), fields: z.array(revAppModelsFieldValue).optional(), category_id: z.number().int().gte(0).lte(2147483647).optional(), is_credit_account_type: z.boolean().optional(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppViewModelsProductView = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppViewModelsProductView).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 }; /** * Search products * * GET /v1/Products */ export const getApiV1Products = async (args: GetApiV1ProductsArgs, options?: GetApiV1ProductsOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['search.product_id'] ?? []) query.append("search.product_id", String(item)) for (const item of args['search.provider_id'] ?? []) query.append("search.provider_id", String(item)) if (args['search.active'] != null) query.append("search.active", String(args['search.active'])) for (const item of args['search.product_type_id'] ?? []) query.append("search.product_type_id", String(item)) for (const item of args['search.description'] ?? []) query.append("search.description", String(item)) for (const item of args['search.code_1'] ?? []) query.append("search.code_1", String(item)) if (args['search.created_date_start'] != null) query.append("search.created_date_start", String(args['search.created_date_start'])) if (args['search.created_date_end'] != null) query.append("search.created_date_end", String(args['search.created_date_end'])) for (const item of args['search.bill_profile_ids'] ?? []) query.append("search.bill_profile_ids", String(item)) for (const item of args['search.service_type_ids'] ?? []) query.append("search.service_type_ids", String(item)) for (const item of args['search.currency_code'] ?? []) query.append("search.currency_code", String(item)) for (const item of args['search.bill_every'] ?? []) query.append("search.bill_every", String(item)) if (args['search.page_size'] != null) query.append("search.page_size", String(args['search.page_size'])) if (args['search.page'] != null) query.append("search.page", String(args['search.page'])) if (args['search.sort'] != null) query.append("search.sort", String(args['search.sort'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Products${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppViewModelsProductView, await res.json()) };