import {z} from 'zod' export type GetApiStatementsArgs = {customer_id?: string | undefined, ref_ids?: Array | undefined, statement_ids?: Array | undefined, statuses?: (Array<'STATEMENT_STATUS_UNSPECIFIED' | 'STATEMENT_STATUS_INITIATED' | 'STATEMENT_STATUS_GENERATED' | 'STATEMENT_STATUS_PAID'>) | undefined, limit?: number | undefined, order?: ('DESC' | 'ASC') | undefined, page_cursor?: string | undefined}; export type GetApiStatementsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const statementType = z.enum(["STATEMENT_TYPE_UNSPECIFIED", "STATEMENT_TYPE_USDG_REWARD"]); export const product = z.literal("USDG_REWARDS"); export const statementStatus = z.enum(["STATEMENT_STATUS_UNSPECIFIED", "STATEMENT_STATUS_INITIATED", "STATEMENT_STATUS_GENERATED", "STATEMENT_STATUS_PAID"]); export const statementBalance = z.object({asset: z.string().optional(), amount_paid: z.string().optional(), total_amount_owed: z.string().optional(), next_payment_ref_id: z.string().optional()}); export const statementFile = z.object({format: z.string().optional(), url: z.string().optional(), expires_at: z.string().optional(), generated_at: z.string().optional()}); export const bufAny = z.object({"@type": z.string().optional()}).and(z.record(z.string(), z.unknown())); export const statement = z.object({id: z.string().optional(), ref_id: z.string().optional(), customer_id: z.string().optional(), type: statementType.optional(), product: product.optional(), status: statementStatus.optional(), metadata: z.record(z.string(), z.string()).optional(), statement_balances: z.array(statementBalance).optional(), period_start: z.string().optional(), period_end: z.string().optional(), generated_at: z.string().optional(), paid_at: z.string().optional(), created_at: z.string().optional(), statement_group_id: z.string().optional(), version: z.number().int().optional(), files: z.array(statementFile).optional(), data: bufAny.optional()}); export const listStatementsResponse = z.object({statements: z.array(statement).optional(), next_page_cursor: 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 }; /** * List Statements * * GET /statements */ export const getApiStatements = async (args: GetApiStatementsArgs, options?: GetApiStatementsOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) for (const item of args.ref_ids ?? []) query.append("ref_ids", String(item)) for (const item of args.statement_ids ?? []) query.append("statement_ids", String(item)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/statements${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(listStatementsResponse, await res.json()) };