import {z} from 'zod' export type GetApiClearloopActionsArgs = {fromTimestamp?: string | undefined, toTimestamp?: string | undefined, clientAccountId?: string | undefined, portfolioIds?: Array | undefined, actionType?: string | undefined, limit?: string | undefined, offset?: string | undefined, sortDirection?: string | undefined, filterExecuted?: boolean | undefined}; export type GetApiClearloopActionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const clearLoopAction = z.object({action: z.string(), amount: z.string(), clearLoopPortfolioId: z.string().optional(), clientAccountId: z.string(), completedAt: z.string().optional(), createdAt: z.string(), currency: z.string(), delegatedOrganizationId: z.string(), exchangeId: z.string().optional(), network: z.string().optional(), orderId: z.string().optional(), organizationId: z.string(), status: z.string().optional(), updatedAt: z.string().optional(), usdAmount: z.string().optional()}); export const clearLoopPagination = z.object({limit: z.string().optional(), page: z.string().optional(), pages: z.string().optional(), totalItems: z.string().optional()}); export const getActionsResponse = z.object({actions: z.array(clearLoopAction), pagination: clearLoopPagination.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 ClearLoop Actions * * GET /clearloop/actions */ export const getApiClearloopActions = async (args: GetApiClearloopActionsArgs, options?: GetApiClearloopActionsOptions) => { const {baseUrl = "https://api.copper.co/platform", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fromTimestamp != null) query.append("fromTimestamp", String(args.fromTimestamp)) if (args.toTimestamp != null) query.append("toTimestamp", String(args.toTimestamp)) if (args.clientAccountId != null) query.append("clientAccountId", String(args.clientAccountId)) for (const item of args.portfolioIds ?? []) query.append("portfolioIds", String(item)) if (args.actionType != null) query.append("actionType", String(args.actionType)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (args.filterExecuted != null) query.append("filterExecuted", String(args.filterExecuted)) const url = `${baseUrl.replace(/\/+$/, '')}/clearloop/actions${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(getActionsResponse, await res.json()) };