import {z} from 'zod' export type GetApiV1RequestProductsArgs = {'search.request_product_id'?: Array | undefined, 'search.request_service_id'?: Array | undefined, 'search.request_id'?: Array | undefined, 'search.product_id'?: Array | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1RequestProductsOptions = {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 revAppModelsRequestProduct = z.object({request_product_id: z.number().int().optional(), request_service_id: z.number().int(), request_id: z.number().int(), product_id: z.number().int(), quantity: z.number().int(), rate: z.number().optional(), description: z.string().optional(), code_1: z.string().optional(), code_2: z.string().optional(), contract_start: z.string().optional(), contract_end: z.string().optional(), group_on_bill: z.boolean().optional(), tax_included: z.boolean().optional(), itemized: z.boolean().optional(), cost: z.number().optional(), buy_rate: z.number().optional(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsRequestProduct = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsRequestProduct).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 request products * * GET /v1/RequestProducts */ export const getApiV1RequestProducts = async (args: GetApiV1RequestProductsArgs, options?: GetApiV1RequestProductsOptions) => { 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.request_product_id'] ?? []) query.append("search.request_product_id", String(item)) for (const item of args['search.request_service_id'] ?? []) query.append("search.request_service_id", String(item)) for (const item of args['search.request_id'] ?? []) query.append("search.request_id", String(item)) for (const item of args['search.product_id'] ?? []) query.append("search.product_id", 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/RequestProducts${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsRequestProduct, await res.json()) };