diff --git a/src/API.ts b/src/API.ts index 3bd49374..ac62e6b5 100644 --- a/src/API.ts +++ b/src/API.ts @@ -143,6 +143,9 @@ export interface StatisticsMemoization { export interface ContributeAPIOptions { includesNamespaces?: boolean disableMonitoring?: boolean | (keyof TAPI)[] + functionInterceptors?: { + [K in keyof TAPI]: (func: TAPI[K]) => TAPI[K] + } } export type AnyFunction = (...args: any[]) => any diff --git a/src/appHost.ts b/src/appHost.ts index 2465680e..6c722107 100644 --- a/src/appHost.ts +++ b/src/appHost.ts @@ -777,11 +777,14 @@ miss: ${memoizedWithMissHit.miss} } const api = factory() + const interceptedAPI = apiOptions?.functionInterceptors + ? interceptApiFunctions(api, apiOptions.functionInterceptors) + : api const monitoredAPI = monitorAPI( shell, options, normalizeApiName(slotKeyToName(key)), - api /*, trace, memoizedArr*/, + interceptedAPI /*, trace, memoizedArr*/, apiOptions ) const apiSlot = declareSlot(key) @@ -847,4 +850,14 @@ miss: ${memoizedWithMissHit.miss} function normalizeApiName(name: string) { return name.charAt(0).toLowerCase() + name.substring(1).replace(new RegExp(' ', 'g'), '') } + + function interceptApiFunctions(api: TAPI, interceptors: ContributeAPIOptions['functionInterceptors']) { + let intercepted = {...api} + for (const key in intercepted) { + intercepted[key] = interceptors && interceptors[key] + ? interceptors[key](intercepted[key]) + : intercepted[key] + } + return intercepted + } }