import {z} from 'zod' export type GetApiV1IdArgs = {id: string, auditUser: string}; export type GetApiV1IdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const currency = z.string(); export const money = z.object({amount: z.number().optional(), currency: currency.optional()}); export const keyValuePair = z.object({key: z.string().max(64), value: z.string().max(64)}); export const vATValues = z.object({percentage: z.number().optional(), amount: money.optional()}); export const fuelPurchaseDetailResponse = z.object({amount: money.optional(), code: z.string().max(64).optional(), description: z.string().max(255).optional(), externalId: z.string().max(64).optional(), matchingAttributes: z.array(keyValuePair).optional(), promptedData: z.array(keyValuePair).optional(), quantity: z.number().optional(), transactionId: z.string().max(64).optional(), type: z.enum(["FUEL", "OTHER"]).optional(), unitPrice: z.number().optional(), VAT: vATValues.optional(), unit: z.enum(["KG", "KWH", "LITRE", "GALLON"]).optional(), id: z.string().optional()}); export const otherPurchaseDetailResponse = z.object({amount: money.optional(), code: z.string().max(64).optional(), description: z.string().max(255).optional(), externalId: z.string().max(64).optional(), matchingAttributes: z.array(keyValuePair).optional(), promptedData: z.array(keyValuePair).optional(), quantity: z.number().optional(), transactionId: z.string().max(64).optional(), type: z.enum(["FUEL", "OTHER"]).optional(), unitPrice: z.number().optional(), VAT: vATValues.optional(), unit: z.enum(["KG", "KWH", "LITRE", "GALLON"]).optional(), id: z.string().optional()}); export const getApiV1IdResponse = z.union([fuelPurchaseDetailResponse, otherPurchaseDetailResponse]); 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 }; /** * Retrieve one purchase detail * * GET /v1/{id} */ export const getApiV1Id = async (args: GetApiV1IdArgs, options: GetApiV1IdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${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(getApiV1IdResponse, await res.json()) };