import {z} from 'zod' export type GetApiApiV1DashboardSummaryArgs = {period?: ('30d' | '60d' | 'all') | undefined}; export type GetApiApiV1DashboardSummaryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardSummaryResponse = z.object({success: z.boolean(), data: z.object({scope: z.union([z.literal("global"), z.literal("partner"), z.literal("merchant")]), period: z.union([z.literal("30d"), z.literal("60d"), z.literal("all")]), period_days: z.number().int().nullable(), comparison_period: z.object({start: z.string(), end: z.string()}).optional(), partners: z.object({total: z.number().int(), new_in_period: z.number().int().optional()}).optional(), merchants: z.object({total: z.number().int(), new_in_period: z.number().int().optional()}).optional(), transactions: z.object({count: z.number().int(), previous_count: z.number().int().optional()}), volume_by_currency: z.array(z.object({currency: z.string().min(3).max(3), amount: z.string(), previous_amount: z.string().optional()})), fees_by_currency: z.array(z.object({currency: z.string().min(3).max(3), amount: z.string(), previous_amount: z.string().optional()})), next_payout_by_currency: z.array(z.object({currency: z.string().min(3).max(3), amount: z.string()}))})}); 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 dashboard summary with scope-aware KPI metrics * * GET /api/v1/dashboard/summary */ export const getApiApiV1DashboardSummary = async (args: GetApiApiV1DashboardSummaryArgs, options?: GetApiApiV1DashboardSummaryOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/dashboard/summary${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(dashboardSummaryResponse, await res.json()) };