import {z} from 'zod' export type GetApiV1CreditsArgs = {'search.credit_memo_id'?: Array | undefined, 'search.customer_id'?: Array | undefined, 'search.product_id'?: Array | undefined, 'search.service_product_id'?: Array | undefined, 'search.service_id'?: Array | undefined, 'search.description'?: Array | undefined, 'search.bill_id'?: Array | undefined, 'search.created_date_start'?: string | undefined, 'search.created_date_end'?: string | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1CreditsOptions = {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 revAppModelsCredit = z.object({credit_memo_id: z.number().int().optional(), description: z.string().max(500), product_type_id: z.number().int().optional(), product_id: z.number().int().optional(), service_product_id: z.number().int().optional(), balance: z.number().optional(), closed_date: z.string().optional(), buy_rate: z.number().optional(), atype: z.string().max(100).optional(), created_date: z.string().optional(), created_by: z.number().int().optional(), tax_class_id: z.number().int().optional(), customer_id: z.number().int().gte(1).lte(2147483647), bill_id: z.number().int().optional(), service_id: z.number().int().optional(), amount: z.number().optional(), product_amount: z.number().optional(), tax_amount: z.number().optional(), geocode: z.string().max(50).optional(), proration: z.number().optional(), is_tax_included: z.boolean(), cost: z.number().optional(), is_bad_debt: z.boolean().optional(), quantity: z.number(), rate: z.number(), is_prorated: z.boolean(), start_date: z.string(), end_date: z.string(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsCredit = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsCredit).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 credits * * GET /v1/Credits */ export const getApiV1Credits = async (args: GetApiV1CreditsArgs, options?: GetApiV1CreditsOptions) => { 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.credit_memo_id'] ?? []) query.append("search.credit_memo_id", String(item)) for (const item of args['search.customer_id'] ?? []) query.append("search.customer_id", String(item)) for (const item of args['search.product_id'] ?? []) query.append("search.product_id", String(item)) for (const item of args['search.service_product_id'] ?? []) query.append("search.service_product_id", String(item)) for (const item of args['search.service_id'] ?? []) query.append("search.service_id", String(item)) for (const item of args['search.description'] ?? []) query.append("search.description", String(item)) for (const item of args['search.bill_id'] ?? []) query.append("search.bill_id", String(item)) if (args['search.created_date_start'] != null) query.append("search.created_date_start", String(args['search.created_date_start'])) if (args['search.created_date_end'] != null) query.append("search.created_date_end", String(args['search.created_date_end'])) 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/Credits${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsCredit, await res.json()) };