import {z} from 'zod' export type GetApiV1ChargesIdArgs = {id: number}; export type GetApiV1ChargesIdOptions = {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 revWebAPIModelsGetOrSearchOfRevAppModelsCharge = z.object({ok: z.boolean().optional(), charge_id: z.number().int().optional(), product_id: z.number().int().optional(), product_type_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().gte(1).lte(2147483647).optional(), service_product_id: z.number().int().gte(1).lte(2147483647).optional(), is_tax_included: z.boolean(), tax_class_id: z.number().int().optional(), description: z.string().max(1500), amount: z.number().optional(), cost: z.number().gte(0).lte(2147483647), product_amount: z.number().optional(), is_commission_exempted: z.boolean(), tax_amount: z.number().optional(), geocode: z.string().max(50).optional(), proration: z.number().optional(), created_date: z.string().optional(), created_by: z.number().int().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 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 charge * * GET /v1/Charges/{id} */ export const getApiV1ChargesId = async (args: GetApiV1ChargesIdArgs, options?: GetApiV1ChargesIdOptions) => { 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/Charges/${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(revWebAPIModelsGetOrSearchOfRevAppModelsCharge, await res.json()) };