import {z} from 'zod' export type GetApiV1PackageProductsIdArgs = {id: number}; export type GetApiV1PackageProductsIdOptions = {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 revWebAPIModelsGetOrSearchOfRevAppModelsPackageProduct = z.object({ok: z.boolean().optional(), package_product_id: z.number().int().optional(), product_id: z.number().int().gte(1).lte(2147483647), package_id: z.number().int().gte(1).lte(2147483647), description: z.string().max(255), code_1: z.string().max(4000).optional(), code_2: z.string().max(4000).optional(), rate: z.number().gte(0).optional(), cost: z.number().gte(0).optional(), buy_rate: z.number().gte(0).optional(), quantity: z.number().int().gte(1).lte(2147483647).optional(), tax_included: z.boolean().optional(), group_on_bill: z.boolean().optional(), itemize: z.boolean().optional(), credit: z.boolean().optional(), state_provinces: z.array(z.string()).optional(), order_type: z.enum(["ALL", "NEW", "CONVERSION", "OTHER"]).optional(), links: z.array(revAppComponentsLink).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 }; /** * View package product * * GET /v1/PackageProducts/{id} */ export const getApiV1PackageProductsId = async (args: GetApiV1PackageProductsIdArgs, options?: GetApiV1PackageProductsIdOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/PackageProducts/${encodeURIComponent(String(args.id))}${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(revWebAPIModelsGetOrSearchOfRevAppModelsPackageProduct, await res.json()) };