Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API interceptors #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ export interface StatisticsMemoization {
export interface ContributeAPIOptions<TAPI> {
includesNamespaces?: boolean
disableMonitoring?: boolean | (keyof TAPI)[]
functionInterceptors?: {
[K in keyof TAPI]: (func: TAPI[K]) => TAPI[K]
}
}

export type AnyFunction = (...args: any[]) => any
Expand Down
15 changes: 14 additions & 1 deletion src/appHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TAPI>(key)
Expand Down Expand Up @@ -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<TAPI>(api: TAPI, interceptors: ContributeAPIOptions<TAPI>['functionInterceptors']) {
let intercepted = {...api}
for (const key in intercepted) {
intercepted[key] = interceptors && interceptors[key]
? interceptors[key](intercepted[key])
: intercepted[key]
}
return intercepted
}
}