import {z} from 'zod' export type ReportInterchangeFeeParameters = {report_type: 'interchange_fee', start_date?: string | undefined, end_date?: string | undefined, nickname?: string | undefined}; export type ReportProceedsParameters = {report_type: 'proceeds', start_date?: string | undefined, end_date?: string | undefined, nickname?: string | undefined}; export type ReportPayoutParameters = {report_type: 'payout', start_date?: string | undefined, end_date?: string | undefined, nickname?: string | undefined}; export type ReportSubAccountSummaryParameters = {report_type: 'sub_account_summary', start_date?: string | undefined, end_date?: string | undefined, nickname?: string | undefined}; export type ReportPaymentListParameters = {report_type: 'payment_list', payment_status?: ('authorized' | 'failed' | 'succeeded' | 'canceled') | undefined, payment_method_id?: string | undefined, terminal_id?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, nickname?: string | undefined}; export type CreateApiReportsArgs = {Authorization: string, 'Sub-Account'?: string | undefined, body: ReportInterchangeFeeParameters | ReportProceedsParameters | ReportPayoutParameters | ReportSubAccountSummaryParameters | ReportPaymentListParameters}; export type CreateApiReportsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reportType = z.enum(["proceeds", "payout", "interchange_fee", "sub_account_summary", "payment_list"]); export const reportParameters = z.union([z.object({report_type: z.literal("proceeds"), start_date: z.string().optional(), end_date: z.string().optional(), nickname: z.string().optional(), account_id: z.string().optional(), platform_account_id: z.string().optional()}), z.object({report_type: z.literal("payout"), start_date: z.string().optional(), end_date: z.string().optional(), nickname: z.string().optional(), account_id: z.string().optional(), platform_account_id: z.string().optional()}), z.object({report_type: z.literal("interchange_fee"), start_date: z.string().optional(), end_date: z.string().optional(), nickname: z.string().optional(), account_id: z.string().optional(), platform_account_id: z.string().optional()}), z.object({report_type: z.literal("sub_account_summary"), start_date: z.string().optional(), end_date: z.string().optional(), nickname: z.string().optional(), account_id: z.string().optional(), platform_account_id: z.string().optional()}), z.object({report_type: z.literal("payment_list"), payment_status: z.enum(["authorized", "failed", "succeeded", "canceled"]).optional(), payment_method_id: z.string().optional(), terminal_id: z.string().optional(), start_date: z.string().optional(), end_date: z.string().optional(), nickname: z.string().optional(), account_id: z.string().optional(), platform_account_id: z.string().optional()})]); export const createApiReportsResponse = z.object({id: z.string().optional(), type: z.string().optional(), data: z.object({id: z.string().optional(), report_type: reportType.optional(), nickname: z.string().nullable().optional(), status: z.enum(["scheduled", "processing", "completed", "failed", "canceled", "expired"]).optional(), scheduled_at: z.string().optional(), run_at: z.string().optional(), created_at: z.string().optional(), error_description: z.string().optional(), account_id: z.string().optional(), presigned_url: z.string().optional(), platform_account_id: z.string().optional(), parameters: reportParameters.optional()}).optional(), page_info: z.unknown()}); 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 }; /** * Create a report * * POST /reports */ export const createApiReports = async (args: CreateApiReportsArgs, options?: CreateApiReportsOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Sub-Account'] != null) headers.set("Sub-Account", String(args['Sub-Account'])) const url = `${baseUrl.replace(/\/+$/, '')}/reports${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(createApiReportsResponse, await res.json()) };