import {z} from 'zod' export type GetApiV4PayoutAccountsArgs = {limit?: number | undefined, orderBy?: (Array<'createdAt' | 'id'>) | undefined, order?: ('asc' | 'desc') | undefined, beforeCursor?: string | undefined, afterCursor?: string | undefined}; export type GetApiV4PayoutAccountsOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pageMetaDto = z.object({total: z.number(), beforeCursor: z.string().optional(), afterCursor: z.string().optional()}); export const payoutAccountEntityV4ResponseDto = z.object({id: z.string(), name: z.string(), externalId: z.string().optional(), isDefaultAccount: z.boolean(), createdAt: z.string(), updatedAt: z.string()}); export const getApiV4PayoutAccountsResponse = z.object({metadata: pageMetaDto.optional(), data: z.array(payoutAccountEntityV4ResponseDto).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 payout accounts * * GET /v4/payout-accounts */ export const getApiV4PayoutAccounts = async (args: GetApiV4PayoutAccountsArgs, options?: GetApiV4PayoutAccountsOptions) => { const {baseUrl = "https://api.slopepay.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.orderBy ?? []) query.append("orderBy", String(item)) if (args.order != null) query.append("order", String(args.order)) if (args.beforeCursor != null) query.append("beforeCursor", String(args.beforeCursor)) if (args.afterCursor != null) query.append("afterCursor", String(args.afterCursor)) if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/payout-accounts${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(getApiV4PayoutAccountsResponse, await res.json()) };