import {z} from 'zod' export type GetApiFeesCollectionsArgs = {account_id?: string | undefined, account_group_id?: string | undefined, sort?: ('created_at' | 'updated_at') | undefined, order?: ('ASC' | 'DESC') | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiFeesCollectionsOptions = {baseUrl?: string | undefined, oauthClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiFeesCollectionsResponse = z.object({meta: z.object({offset: z.number().int(), limit: z.number().int(), count: z.number().int(), total_count: z.number().int(), sort: z.string().optional(), order: z.enum(["ASC", "DESC"]).optional()}), data: z.array(z.object({id: z.string(), created_at: z.string(), updated_at: z.string(), account_id: z.string(), account_group_id: z.string(), type: z.enum(["SERVICE_FEE", "SERVICE_FEE_LIQUIDATION"]), collection_amount: z.string(), processed_amount: z.object({cash_balance: z.string().optional(), sell_to_cover: z.string().optional(), total_residual_amount: z.string().optional()}), sell_to_cover_orders: z.array(z.object({id: z.string(), residual_amount: z.string()})).optional(), currency: z.enum(["EUR", "GBP"]), status: z.enum(["PROCESSING", "FINALISED", "CANCELLED"]), period_start: z.string(), period_end: z.string(), calculation_breakdown: z.array(z.object({fee_model_id: z.string(), subperiod_start: z.string(), subperiod_end: z.string(), subtotal_amount: z.string(), components: z.array(z.object({type: z.enum(["TRANSACTION_LUMP_SUM", "PLATFORM", "SERVICE", "VAT", "CUSTODY"]), amount: z.string()}))})).optional(), replaced_collection_id: z.string().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 fee collections * * GET /fees/collections */ export const getApiFeesCollections = async (args: GetApiFeesCollectionsArgs, options?: GetApiFeesCollectionsOptions) => { const {baseUrl = "https://sandbox.upvest.co", fetch: fetchFn = fetch, oauthClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account_id != null) query.append("account_id", String(args.account_id)) if (args.account_group_id != null) query.append("account_group_id", String(args.account_group_id)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.order != null) query.append("order", String(args.order)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (oauthClientCredentials !== undefined) headers.set('authorization', `Bearer ${oauthClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/fees/collections${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(getApiFeesCollectionsResponse, await res.json()) };