Skip to content

Commit 1010a37

Browse files
committed
feat(context): add @globalInterceptor decorator
1 parent 3221d9f commit 1010a37

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

packages/context/src/__tests__/unit/interceptor.unit.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ import {
88
asGlobalInterceptor,
99
Context,
1010
ContextBindings,
11+
ContextTags,
12+
createBindingFromClass,
13+
globalInterceptor,
14+
GLOBAL_INTERCEPTOR_NAMESPACE,
1115
Interceptor,
1216
InterceptorOrKey,
1317
InvocationContext,
1418
mergeInterceptors,
19+
Provider,
1520
} from '../..';
1621

1722
describe('mergeInterceptors', () => {
@@ -140,6 +145,40 @@ describe('globalInterceptors', () => {
140145
]);
141146
});
142147

148+
it('applies asGlobalInterceptor', () => {
149+
const binding = ctx
150+
.bind('globalInterceptors.authInterceptor')
151+
.to(authInterceptor)
152+
.apply(asGlobalInterceptor('auth'));
153+
154+
expect(binding.tagMap).to.eql({
155+
[ContextTags.NAMESPACE]: GLOBAL_INTERCEPTOR_NAMESPACE,
156+
[ContextTags.GLOBAL_INTERCEPTOR]: ContextTags.GLOBAL_INTERCEPTOR,
157+
[ContextTags.GLOBAL_INTERCEPTOR_GROUP]: 'auth',
158+
});
159+
});
160+
161+
it('supports @globalInterceptor', () => {
162+
@globalInterceptor('auth', {
163+
tags: {[ContextTags.NAME]: 'my-auth-interceptor'},
164+
})
165+
class MyAuthInterceptor implements Provider<Interceptor> {
166+
value() {
167+
return authInterceptor;
168+
}
169+
}
170+
const binding = createBindingFromClass(MyAuthInterceptor);
171+
172+
expect(binding.tagMap).to.eql({
173+
[ContextTags.TYPE]: 'provider',
174+
[ContextTags.PROVIDER]: 'provider',
175+
[ContextTags.NAMESPACE]: GLOBAL_INTERCEPTOR_NAMESPACE,
176+
[ContextTags.GLOBAL_INTERCEPTOR]: ContextTags.GLOBAL_INTERCEPTOR,
177+
[ContextTags.GLOBAL_INTERCEPTOR_GROUP]: 'auth',
178+
[ContextTags.NAME]: 'my-auth-interceptor',
179+
});
180+
});
181+
143182
class MyController {
144183
greet(name: string) {
145184
return `Hello, ${name}`;

packages/context/src/interceptor.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import {
1414
import * as assert from 'assert';
1515
import * as debugFactory from 'debug';
1616
import {Binding, BindingTemplate} from './binding';
17+
import {bind} from './binding-decorator';
1718
import {filterByTag} from './binding-filter';
19+
import {BindingSpec} from './binding-inspector';
1820
import {BindingAddress} from './binding-key';
1921
import {sortBindingsByPhase} from './binding-sorter';
2022
import {Context} from './context';
21-
import {ContextBindings, ContextTags} from './keys';
23+
import {
24+
ContextBindings,
25+
ContextTags,
26+
GLOBAL_INTERCEPTOR_NAMESPACE,
27+
} from './keys';
2228
import {
2329
transformValueOrPromise,
2430
tryWithFinally,
@@ -200,15 +206,26 @@ export class InvocationContext extends Context {
200206
/**
201207
* The `BindingTemplate` function to configure a binding as a global interceptor
202208
* by tagging it with `ContextTags.INTERCEPTOR`
203-
* @param binding - Binding object
209+
* @param group - Group for ordering the interceptor
204210
*/
205211
export function asGlobalInterceptor(group?: string): BindingTemplate {
206212
return binding => {
207-
binding.tag(ContextTags.GLOBAL_INTERCEPTOR);
213+
binding
214+
.tag(ContextTags.GLOBAL_INTERCEPTOR)
215+
.tag({[ContextTags.NAMESPACE]: GLOBAL_INTERCEPTOR_NAMESPACE});
208216
if (group) binding.tag({[ContextTags.GLOBAL_INTERCEPTOR_GROUP]: group});
209217
};
210218
}
211219

220+
/**
221+
* `@globalInterceptor` decorator to mark the class as a global interceptor
222+
* @param group - Group for ordering the interceptor
223+
* @param specs - Extra binding specs
224+
*/
225+
export function globalInterceptor(group?: string, ...specs: BindingSpec[]) {
226+
return bind(asGlobalInterceptor(group), ...specs);
227+
}
228+
212229
/**
213230
* Interceptor function to intercept method invocations
214231
*/

packages/context/src/keys.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@ export namespace ContextTags {
3939
* Binding tag for global interceptors
4040
*/
4141
export const GLOBAL_INTERCEPTOR = 'globalInterceptor';
42+
4243
/**
4344
* Binding tag for group name of global interceptors
4445
*/
4546
export const GLOBAL_INTERCEPTOR_GROUP = 'globalInterceptorGroup';
4647
}
4748

49+
/**
50+
* Default namespace for global interceptors
51+
*/
52+
export const GLOBAL_INTERCEPTOR_NAMESPACE = 'globalInterceptors';
53+
4854
/**
4955
* Namespace for context bindings
5056
*/

0 commit comments

Comments
 (0)