import {z} from 'zod' export type GetApiDashboardOrderMetricsArgs = {startDate?: string | undefined, endDate?: string | undefined, utmSource?: string | undefined}; export type GetApiDashboardOrderMetricsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const metricValueDto = z.object({value: z.object({}), unit: z.string()}); export const orderMetricsResponseDto = z.object({averageOrderValue: metricValueDto, multipleDocumentRatio: metricValueDto, fastTrackRatio: metricValueDto, paperCopyRatio: metricValueDto, totalOrders: z.number(), extraDocumentOrders: z.number(), byOS: z.object({})}); 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 comprehensive order metrics * * GET /dashboard/order-metrics */ export const getApiDashboardOrderMetrics = async (args: GetApiDashboardOrderMetricsArgs, options?: GetApiDashboardOrderMetricsOptions) => { const {baseUrl = "https://api.findmydocument.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.utmSource != null) query.append("utmSource", String(args.utmSource)) const url = `${baseUrl.replace(/\/+$/, '')}/dashboard/order-metrics${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(orderMetricsResponseDto, await res.json()) };