import {z} from 'zod' export type CreateApiFeesCollectionsArgs = {'idempotency-key': string, body: {account_id: string, type: 'SERVICE_FEE' | 'SERVICE_FEE_LIQUIDATION', collection_amount: string, currency: 'EUR' | 'GBP', period_start: string, period_end: string}}; export type CreateApiFeesCollectionsOptions = {baseUrl?: string | undefined, oauthClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiFeesCollectionsResponse = 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 }; /** * Create a fee collection * * POST /fees/collections */ export const createApiFeesCollections = async (args: CreateApiFeesCollectionsArgs, options?: CreateApiFeesCollectionsOptions) => { const {baseUrl = "https://sandbox.upvest.co", fetch: fetchFn = fetch, oauthClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (oauthClientCredentials !== undefined) headers.set('authorization', `Bearer ${oauthClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/fees/collections${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(createApiFeesCollectionsResponse, await res.json()) };