import {z} from 'zod' export type GetApiV1PaymentsArgs = {company_id: string | number, external_ids?: (string | Array) | undefined, payee_ids?: (string | Array) | undefined, page?: (string | number) | undefined, count?: (string | number) | undefined, status?: string | undefined}; export type GetApiV1PaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1PaymentsResponse = z.object({data: z.array(z.object({amount_usd: z.string(), approved_pay_date: z.string().nullable(), pay_hash: z.string().nullable(), salt: z.string().nullable(), status: z.string(), external_id: z.string().nullable(), transaction_hash: z.string().nullable(), payee: z.object({id: z.union([z.string(), z.number()]).nullable(), avatar: z.string().nullable(), email: z.string().nullable(), name: z.string().nullable(), address: z.string().nullable()})}))}); 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 payments (V1 Legacy) * * GET /v1/payments */ export const getApiV1Payments = async (args: GetApiV1PaymentsArgs, options?: GetApiV1PaymentsOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company_id != null) query.append("company_id", String(args.company_id)) if (args.external_ids != null) query.append("external_ids", String(args.external_ids)) if (args.payee_ids != null) query.append("payee_ids", String(args.payee_ids)) if (args.page != null) query.append("page", String(args.page)) if (args.count != null) query.append("count", String(args.count)) if (args.status != null) query.append("status", String(args.status)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/payments${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(getApiV1PaymentsResponse, await res.json()) };