import {z} from 'zod' export type InsightTransactionReportFilter = {filter: 'store_id' | 'payout_destination_id', value: string}; export type InsightCheckoutTransactionsReportParams = {dataType: 'checkout/transactions', name: string, emails?: Array | undefined, from: string, to: string, schedule: 'monthly' | 'weekly' | 'daily', language: 'no' | 'en', reportFilters?: Array | undefined, contentType?: ('application/pdf' | 'text/csv') | undefined}; export type InsightOrderReportFilter = {filter?: 'operation_payout_destination' | undefined, value?: string | undefined}; export type InsightOrdersOrderOperationsReportParams = {dataType: 'orders/orderoperations', name: string, emails?: Array | undefined, from: string, to: string, schedule: 'weekly' | 'twice-a-month' | 'daily', language: 'no' | 'en', reportFilters?: Array | undefined, contentType?: 'text/csv' | undefined}; export type InsightWalletsTransactionsReportParams = {dataType: 'wallets/transactions', name: string, emails?: Array | undefined, from: string, to: string, schedule: 'monthly' | 'weekly' | 'daily', language: 'no' | 'en', contentType?: ('application/pdf' | 'text/csv') | undefined}; export type InsightReportParams = InsightCheckoutTransactionsReportParams | InsightOrdersOrderOperationsReportParams | InsightWalletsTransactionsReportParams; export type CreateApiAccountsAidInsightGenerateReportArgs = {aid: unknown, body: InsightReportParams}; export type CreateApiAccountsAidInsightGenerateReportOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const insightTransactionReportFilter = z.object({filter: z.enum(["store_id", "payout_destination_id"]), value: z.string()}); export const insightCheckoutTransactionsReportParams = z.object({dataType: z.literal("checkout/transactions"), name: z.string().max(64), emails: z.array(z.string()).optional(), from: z.string(), to: z.string(), schedule: z.enum(["monthly", "weekly", "daily"]), language: z.enum(["no", "en"]), reportFilters: z.array(insightTransactionReportFilter).optional(), contentType: z.enum(["application/pdf", "text/csv"]).optional()}); export const insightOrderReportFilter = z.object({filter: z.literal("operation_payout_destination").optional(), value: z.string().optional()}); export const insightOrdersOrderOperationsReportParams = z.object({dataType: z.literal("orders/orderoperations"), name: z.string().max(64), emails: z.array(z.string()).optional(), from: z.string(), to: z.string(), schedule: z.enum(["weekly", "twice-a-month", "daily"]), language: z.enum(["no", "en"]), reportFilters: z.array(insightOrderReportFilter).optional(), contentType: z.literal("text/csv").optional()}); export const insightWalletsTransactionsReportParams = z.object({dataType: z.literal("wallets/transactions"), name: z.string().max(64), emails: z.array(z.string()).optional(), from: z.string(), to: z.string(), schedule: z.enum(["monthly", "weekly", "daily"]), language: z.enum(["no", "en"]), contentType: z.enum(["application/pdf", "text/csv"]).optional()}); export const insightReportParams = z.discriminatedUnion("dataType", [insightCheckoutTransactionsReportParams, insightOrdersOrderOperationsReportParams, insightWalletsTransactionsReportParams]); 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 }; /** * aid_insight_service_generate_report * * POST /accounts/{aid}/insight/generate-report */ export const createApiAccountsAidInsightGenerateReport = async (args: CreateApiAccountsAidInsightGenerateReportArgs, options?: CreateApiAccountsAidInsightGenerateReportOptions) => { const {baseUrl = "https://api.dintero.com/v1", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.aid))}/insight/generate-report${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(insightReportParams, await res.json()) };