import {z} from 'zod' export type GetApiV2BillsArgs = {bills?: number | undefined, numbers?: number | undefined, status?: string | undefined, end?: number | undefined, limit?: number | undefined, pos?: number | undefined, start?: number | undefined, locations?: number | undefined, cursor?: number | undefined, page?: number | undefined}; export type GetApiV2BillsOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bill = z.object({billID: z.number().int().optional(), poID: z.number().int().optional(), status: z.number().int().optional(), userID: z.number().int().optional(), userIDStarted: z.number().int().optional(), userIDMarkPaid: z.number().int().optional(), billNumber: z.number().int().optional(), locationID: z.number().int().optional(), date: z.number().int().optional(), meta: z.object({items: z.string().optional(), po: z.string().optional(), transactions: z.string().optional()}).optional()}); export const getApiV2BillsResponse = z.array(bill); 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 }; /** * Get Bills * * GET /v2/bills */ export const getApiV2Bills = async (args: GetApiV2BillsArgs, options: GetApiV2BillsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bills != null) query.append("bills", String(args.bills)) if (args.numbers != null) query.append("numbers", String(args.numbers)) if (args.status != null) query.append("status", String(args.status)) if (args.end != null) query.append("end", String(args.end)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.pos != null) query.append("pos", String(args.pos)) if (args.start != null) query.append("start", String(args.start)) if (args.locations != null) query.append("locations", String(args.locations)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.page != null) query.append("page", String(args.page)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/bills${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(getApiV2BillsResponse, await res.json()) };