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 RevAppModelsProduct = {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 UpdateApiV1ProductsIdArgs = {id: number, body: RevAppModelsProduct}; export type UpdateApiV1ProductsIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revWebAPIModelsSuccess = z.object({ok: z.boolean().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 }; /** * Replace product * * PUT /v1/Products/{id} */ export const updateApiV1ProductsId = async (args: UpdateApiV1ProductsIdArgs, options?: UpdateApiV1ProductsIdOptions) => { 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/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(revWebAPIModelsSuccess, await res.json()) };