import {z} from 'zod' export type GetApiV1ReceivablesArgs = {page: string, limit: string, sort: 'ASC' | 'DESC', status?: ('PAID' | 'CREATED' | 'REFUNDED' | 'CANCELED') | undefined, expected_on_range_gte?: string | undefined, expected_on_range_lte?: string | undefined}; export type GetApiV1ReceivablesOptions = {baseUrl: string, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const edubankSplit = z.object({recipient: z.string(), amount: z.number().optional(), percentage: z.string(), processing_fee: z.string()}); export const edubankReceivables = z.object({create_at: z.string(), expected_on: z.string(), status: z.enum(["PAID", "CREATED", "REFUNDED", "CANCELED"]), receivable_amount: z.number(), main_seller_id: z.string(), transaction_id: z.string(), reference_id: z.string(), payment_id: z.string(), payment_type: z.string(), paid_with: z.string().optional(), payment_amount: z.number(), payment_received_at: z.string(), liable: z.boolean(), split_rule: z.array(edubankSplit).nullable(), installment: z.number().nullable()}); export const edubankReceivablesResponseDto = z.object({page: z.string(), limit: z.string(), sort: z.enum(["ASC", "DESC"]), last_page: z.number(), max_items: z.number(), items: z.array(edubankReceivables)}); 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 all receivables * * GET /v1/receivables * * Also accepts auth: x-api-key, authorization */ export const getApiV1Receivables = async (args: GetApiV1ReceivablesArgs, options: GetApiV1ReceivablesOptions) => { const {baseUrl, fetch: fetchFn = fetch, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.status != null) query.append("status", String(args.status)) if (args.expected_on_range_gte != null) query.append("expected_on_range_gte", String(args.expected_on_range_gte)) if (args.expected_on_range_lte != null) query.append("expected_on_range_lte", String(args.expected_on_range_lte)) if (authorization !== undefined) headers.set("authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/v1/receivables${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(edubankReceivablesResponseDto, await res.json()) };