import {z} from 'zod' export type GetApiAccountEntriesArgs = {organizationId: string, cardAccountId?: string | undefined, type?: ('TRANSACTION' | 'BILL_PAYMENT' | 'REIMBURSEMENT' | 'CARD_ACCOUNT_LOAD' | 'EXTERNAL_TRANSFER' | 'OTHER') | undefined, fromDate?: string | undefined, toDate?: string | undefined, byDateField?: ('bookingDate' | 'createdAt' | 'updatedAt') | undefined, limit?: number | undefined, page?: number | undefined, sortBy?: ('index' | 'bookingDate') | undefined, sortDirection?: ('ASC' | 'DESC') | undefined}; export type GetApiAccountEntriesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAccountEntriesResponse = z.object({data: z.array(z.object({accountEntryId: z.string().optional(), organizationId: z.string().optional(), cardAccountId: z.string().optional(), type: z.enum(["TRANSACTION", "BILL_PAYMENT", "REIMBURSEMENT", "CARD_ACCOUNT_LOAD", "EXTERNAL_TRANSFER", "OTHER"]).optional(), index: z.number().int().optional(), bookingDate: z.string().optional(), amount: z.object({value: z.number().optional(), currency: z.string().optional()}).optional(), balanceAfter: z.object({value: z.number().optional(), currency: z.string().optional()}).optional(), transactionType: z.string().nullable().optional(), transactionId: z.string().nullable().optional(), billPaymentType: z.string().nullable().optional(), billPaymentId: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), fxFee: z.object({fxFeePercentage: z.number().gte(0).lte(100).nullable().optional(), fxFeeAmount: z.object({value: z.number().optional(), currency: z.string().optional()}).nullable().optional()}).optional()})).optional(), hasNextPage: z.boolean().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 }; /** * Get Account Entries * * GET /account-entries */ export const getApiAccountEntries = async (args: GetApiAccountEntriesArgs, options?: GetApiAccountEntriesOptions) => { const {baseUrl = "https://partner-api.getpliant.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.organizationId != null) query.append("organizationId", String(args.organizationId)) if (args.cardAccountId != null) query.append("cardAccountId", String(args.cardAccountId)) if (args.type != null) query.append("type", String(args.type)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.byDateField != null) query.append("byDateField", String(args.byDateField)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/account-entries${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(getApiAccountEntriesResponse, await res.json()) };