import {z} from 'zod' export type GetApiV2BillsTransactionsArgs = {bills?: number | undefined, items?: number | undefined, start?: number | undefined, end?: number | undefined, transactions?: number | undefined, limit?: number | undefined, locations?: number | undefined, cursor?: number | undefined, body: Record}; export type GetApiV2BillsTransactionsOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2BillsTransactionsResponse = z.array(z.object({transactionID: z.number().int().optional(), billID: z.number().int().optional(), poItemID: z.number().int().optional(), qtyReceived: z.number().int().optional(), dateReceived: z.number().int().optional(), meta: z.object({bills: z.string().optional()}).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 }; /** * Bill Transactions * * GET /v2/bills/transactions */ export const getApiV2BillsTransactions = async (args: GetApiV2BillsTransactionsArgs, options: GetApiV2BillsTransactionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.bills != null) query.append("bills", String(args.bills)) if (args.items != null) query.append("items", String(args.items)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.transactions != null) query.append("transactions", String(args.transactions)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.locations != null) query.append("locations", String(args.locations)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/bills/transactions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2BillsTransactionsResponse, await res.json()) };