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 RevAppUpdateModelsServiceProductUpdateModel = {effective_date?: string | undefined, generate_proration: boolean, service_product_id?: number | undefined, customer_id?: number | undefined, product_id: number, package_id?: number | undefined, service_id: number, description?: string | undefined, code_1?: string | undefined, code_2?: string | undefined, rate?: number | undefined, billed_through_date?: string | undefined, canceled_date?: string | undefined, status?: ('PENDING' | 'ACTIVE' | 'DENIED' | 'DISCONNECTED') | undefined, status_date?: string | undefined, status_user_id?: number | undefined, activated_date?: string | undefined, cost?: number | undefined, wholesale_description?: string | undefined, free_start_date?: string | undefined, free_end_date?: string | undefined, quantity?: number | undefined, contract_start_date?: string | undefined, contract_end_date?: string | undefined, automatic_expiration_end_date?: string | undefined, created_date?: string | undefined, tax_included?: boolean | undefined, group_on_bill?: boolean | undefined, itemized?: boolean | undefined, credit_flag?: boolean | undefined, fields?: Array | undefined, links?: Array | undefined}; export type UpdateApiV1ServiceProductIdArgs = {id: number, body: RevAppUpdateModelsServiceProductUpdateModel}; export type UpdateApiV1ServiceProductIdOptions = {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 service product * * PUT /v1/ServiceProduct/{id} */ export const updateApiV1ServiceProductId = async (args: UpdateApiV1ServiceProductIdArgs, options?: UpdateApiV1ServiceProductIdOptions) => { 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/ServiceProduct/${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()) };