import {z} from 'zod' export type GetApiVoidsArgs = {offset?: number | undefined, limit?: number | undefined, type?: string | undefined, 'X-Company-Token': string}; export type GetApiVoidsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const voidedOut = z.object({fecha_de_emision: z.string(), state_type_id: z.number().int(), hash: z.string(), archivo: z.string(), ticket: z.string(), descargar_xml: z.string().nullable().optional(), descargar_cdr: z.string().nullable().optional(), id: z.number().int(), estado: z.string().nullable().optional(), descargar_pdf: z.string().nullable().optional(), eventos: z.array(z.unknown()).optional(), identificador: z.string(), company_id: z.number().int(), empresa: z.unknown()}); export const voidedResource = z.object({total: z.number().int(), limit: z.number().int(), offset: z.number().int(), total_pages: z.number().int(), current_page: z.number().int(), docs: z.array(voidedOut)}); 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 }; /** * List * * GET /voids */ export const getApiVoids = async (args: GetApiVoidsArgs, options: GetApiVoidsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.type != null) query.append("type", String(args.type)) if (args['X-Company-Token'] != null) headers.set("X-Company-Token", String(args['X-Company-Token'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/voids${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(voidedResource, await res.json()) };