import {z} from 'zod' export type CreateApiReportsArgs = {'X-Organization-Code': string, body: {type: 'PAYOUTS' | 'PAYMENTS' | 'TRANSACTIONS' | 'SETTLEMENT_FEES' | 'RECONCILIATION_OVERVIEW' | 'COMMUNICATIONS', user_code?: string | undefined, start_date: string, end_date: string, merchant_reference_id?: string | undefined, account_id?: string | undefined, acquirer?: string | undefined, payment_status?: string | undefined, payment_sub_status?: string | undefined, payment_method?: string | undefined, currency?: string | undefined, country?: string | undefined, transaction_type?: string | undefined, transaction_status?: string | undefined, updated_at_start?: string | undefined, updated_at_end?: string | undefined, columns?: string | undefined, metadata_fields?: string | undefined}}; export type CreateApiReportsOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiReportsResponse = z.object({id: z.string().optional(), type: z.string().optional(), start_date: z.string().optional(), end_date: z.string().optional(), merchant_reference_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), expires_at: z.string().optional(), status: z.string().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 }; /** * Create a Report * * POST /reports */ export const createApiReports = async (args: CreateApiReportsArgs, options?: CreateApiReportsOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Organization-Code'] != null) headers.set("X-Organization-Code", String(args['X-Organization-Code'])) if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) 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()) };