import {z} from 'zod' export type GetApiSheetArgs = {tab: 'books' | 'vat', range: string, customer_id?: string | undefined, limit?: number | undefined, offset?: number | undefined, sort?: ('' | 'newest' | 'oldest' | 'amount' | 'category' | 'status') | undefined}; export type GetApiSheetOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const portalSheet = z.object({customer: z.record(z.string(), z.unknown()).optional(), active_tab: z.enum(["books", "vat"]).optional(), sheet_status: z.record(z.string(), z.unknown()).optional(), selected_range: z.string().optional(), available_ranges: z.array(z.record(z.string(), z.unknown())).optional(), summary_cards: z.array(z.record(z.string(), z.unknown())).optional(), columns: z.array(z.record(z.string(), z.unknown())).optional(), rows: z.array(z.record(z.string(), z.unknown())).optional(), row_window: z.record(z.string(), z.unknown()).optional(), sort: z.string().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 }; /** * Get the Books or VAT sheet * * GET /sheet */ export const getApiSheet = async (args: GetApiSheetArgs, options?: GetApiSheetOptions) => { const {baseUrl = "https://api.app.lastaccountingcompany.com/portal", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.tab != null) query.append("tab", String(args.tab)) if (args.range != null) query.append("range", String(args.range)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort != null) query.append("sort", String(args.sort)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/sheet${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(portalSheet, await res.json()) };