import {z} from 'zod' export type MerchantRole = 'seller' | 'payout'; export type GetApiMerchantsArgs = {start_time?: string | undefined, end_time?: string | undefined, per_page?: number | undefined, page?: number | undefined, live?: boolean | undefined, platform_role?: MerchantRole | undefined, status?: string | undefined, payments_enabled?: boolean | undefined, payouts_enabled?: boolean | undefined}; export type GetApiMerchantsOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const amount = z.number().int().gte(0); export const merchantRole = z.enum(["seller", "payout"]); export const submerchantListItem = z.object({id: z.string(), live: z.boolean(), created_at: z.string(), updated_at: z.string(), account_id: z.string(), name: z.string(), platform_role: merchantRole, status: z.enum(["accepted", "declined"]), payments_enabled: z.boolean(), payouts_enabled: z.boolean()}); export const submerchantsList = z.object({resource: z.string(), total: amount, page: amount, per_page: amount, last_page: amount, data: z.array(submerchantListItem)}); 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 }; /** * Merchant: List * * GET /merchants */ export const getApiMerchants = async (args: GetApiMerchantsArgs, options?: GetApiMerchantsOptions) => { 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 (args.live != null) query.append("live", String(args.live)) if (args.platform_role != null) query.append("platform_role", String(args.platform_role)) if (args.status != null) query.append("status", String(args.status)) if (args.payments_enabled != null) query.append("payments_enabled", String(args.payments_enabled)) if (args.payouts_enabled != null) query.append("payouts_enabled", String(args.payouts_enabled)) if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchants${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(submerchantsList, await res.json()) };