import {z} from 'zod' export type GetApiV1StatisticsTurnovercomparisonArgs = {from?: string | undefined, to?: string | undefined, year?: number | undefined, expenses?: boolean | undefined, paid?: boolean | undefined}; export type GetApiV1StatisticsTurnovercomparisonOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dateRangeModel = z.object({from: z.string().optional(), to: z.string().optional()}); export const turnoverPeriodValue = z.object({label: z.string().nullable().optional(), range: dateRangeModel.optional(), value: z.number().optional()}); export const turnoverComparisonResult = z.object({current: turnoverPeriodValue.optional(), precedingPeriod: turnoverPeriodValue.optional(), previousYear: turnoverPeriodValue.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 }; /** * Turnover over a period, compared with the same period one year earlier and with the period * of equal length immediately before it * * GET /v1/statistics/turnovercomparison */ export const getApiV1StatisticsTurnovercomparison = async (args: GetApiV1StatisticsTurnovercomparisonArgs, options?: GetApiV1StatisticsTurnovercomparisonOptions) => { const {baseUrl = "https://api.altoviz.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.year != null) query.append("year", String(args.year)) if (args.expenses != null) query.append("expenses", String(args.expenses)) if (args.paid != null) query.append("paid", String(args.paid)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/statistics/turnovercomparison${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(turnoverComparisonResult, await res.json()) };