import {z} from 'zod' export type PaginationFirst = number; export type PaginationAfter = string; export type AccountId = string; export type AccountLegalName = string; export type Country = unknown; export type QueryAccountsRequest = {type: 'accounts', first?: PaginationFirst | undefined, after?: PaginationAfter | undefined, filters?: ({id?: AccountId | undefined, legal_name?: AccountLegalName | undefined, country?: Country | undefined}) | undefined, sorts?: (Array<{field: 'id' | 'created_at' | 'legal_name' | 'country', direction: 'ascending' | 'descending'}>) | undefined}; export type CreateApiV2AccountsQueryArgs = {'Getpaid-Idempotency-Key': string, body: QueryAccountsRequest}; export type CreateApiV2AccountsQueryOptions = {baseUrl?: string | undefined, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const accountId = z.string().min(30).max(50); export const accountLegalName = z.string().min(1).max(250); export const country = z.string().min(2).max(2); export const createdAt = z.string().min(24).max(24); export const queryAccountsResponse = z.object({type: z.literal("accounts"), cursor: z.string().max(250).optional(), data: z.array(z.object({id: accountId, legal_name: accountLegalName, country: country, created_at: createdAt}))}); 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 }; /** * Query accounts * * POST /v2/accounts/query */ export const createApiV2AccountsQuery = async (args: CreateApiV2AccountsQueryArgs, options?: CreateApiV2AccountsQueryOptions) => { const {baseUrl = "https://api.getpaid.io", fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Getpaid-Idempotency-Key'] != null) headers.set("Getpaid-Idempotency-Key", String(args['Getpaid-Idempotency-Key'])) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/query${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(queryAccountsResponse, await res.json()) };