import {z} from 'zod' export type RevAppComponentsLink = {rel?: string | undefined, href?: string | undefined}; export type RevAppModelsFieldValue = {field_id?: number | undefined, label?: string | undefined, value?: string | undefined, links?: Array | undefined}; export type RevAppPostModelsProductPost = {currency_code?: ('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') | undefined, product_id?: number | undefined, product_type_id: number, description: string, code_1?: string | undefined, code_2?: string | undefined, rate?: number | undefined, cost?: number | undefined, buy_rate?: number | undefined, created_date?: string | undefined, created_by?: number | undefined, active?: boolean | undefined, creates_order?: boolean | undefined, provider_id: number, bills_in_arrears?: boolean | undefined, bill_every?: ('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, prorates?: boolean | undefined, prorates_on_disconnect?: boolean | undefined, customer_class?: ('Business' | 'Residential') | undefined, long_description?: string | undefined, ledger_code?: string | undefined, free_months?: number | undefined, automatic_expiration_months?: number | undefined, order_completion_billing?: boolean | undefined, tax_class_id?: number | undefined, wholesale_description?: string | undefined, smart_pricing_id?: number | undefined, bill_profile_ids?: Array | undefined, service_type_ids?: Array | undefined, fields?: Array | undefined, category_id?: number | undefined, is_credit_account_type?: boolean | undefined, links?: Array | undefined}; export type CreateApiV1ProductsArgs = {body: RevAppPostModelsProductPost}; export type CreateApiV1ProductsOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revWebAPIModelsCreate = z.object({ok: z.boolean().optional(), id: z.number().int().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 }; /** * Create product * * POST /v1/Products */ export const createApiV1Products = async (args: CreateApiV1ProductsArgs, options?: CreateApiV1ProductsOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(revWebAPIModelsCreate, await res.json()) };