import {z} from 'zod' export type GetApiReportsArgs = {Authorization: string, 'Sub-Account'?: string | undefined, nickname?: string | undefined}; export type GetApiReportsOptions = {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 report = 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()}); export const pageInfo = z.object({end_cursor: z.string().optional(), has_next: z.boolean().optional(), has_previous: z.boolean().optional(), start_cursor: z.string().optional()}); export const getApiReportsResponse = z.object({id: z.number().optional(), type: z.string().optional(), data: z.array(report).optional(), page_info: pageInfo.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 }; /** * List Reports * * GET /reports */ export const getApiReports = async (args: GetApiReportsArgs, options?: GetApiReportsOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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'])) if (args.nickname != null) query.append("nickname", String(args.nickname)) const url = `${baseUrl.replace(/\/+$/, '')}/reports${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(getApiReportsResponse, await res.json()) };