import {z} from 'zod' export type GetApiSettlementsArgs = {start_time?: string | undefined, end_time?: string | undefined, per_page?: number | undefined, page?: number | undefined}; export type GetApiSettlementsOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(25).max(25); export const status = z.enum(["pending", "paid", "cancelled", "bounced", "suspended"]); export const cents = z.number().int(); export const currency = z.enum(["JPY", "USD", "EUR", "TWD", "KRW", "PLN", "GBP", "HKD", "SGD", "NZD", "AUD", "IDR", "MYR", "PHP", "THB", "CNY", "BRL", "CHF", "CAD", "VND"]); export const settlement = z.object({id: uUID, resource: z.literal("settlement"), reference: z.string(), status: status, merchant_name: z.string(), company_name: z.string().nullable(), cycle: z.string(), transaction_amount_cents: cents, currency: currency, fee_amount_cents: cents, fee_tax_amount_cents: cents, settlement_amount_cents: cents, fx_currency: currency, fx_conversion_rate: z.string(), fx_conversion_amount_cents: cents, bank_transfer_fee_amount_cents: cents, remittance_amount_cents: cents, cutoff_time: z.string(), created_at: z.string(), download: z.object({csv: z.string().nullable(), xls: z.string().nullable()})}); export const settlementList = z.object({resource: z.string(), data: z.array(settlement), start_time: z.string(), end_time: z.string(), total: z.number().int(), page: z.number().int(), per_page: z.number().int(), last_page: z.number().int()}); 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 }; /** * Settlement: Index * * GET /settlements */ export const getApiSettlements = async (args: GetApiSettlementsArgs, options?: GetApiSettlementsOptions) => { const {baseUrl = "https://komoju.com/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/settlements${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(settlementList, await res.json()) };