-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boot): add a booter for global interceptors
- Loading branch information
1 parent
94ba1d5
commit da707f3
Showing
7 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
packages/boot/src/__tests__/fixtures/interceptor.artifact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/boot | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {globalInterceptor, Interceptor, Provider} from '@loopback/context'; | ||
|
||
/** | ||
* This class will be bound to the application as a global `Interceptor` during | ||
* `boot` | ||
*/ | ||
@globalInterceptor('auth') | ||
export class MyGlobalInterceptor implements Provider<Interceptor> { | ||
/* | ||
constructor() {} | ||
*/ | ||
|
||
value() { | ||
const interceptor: Interceptor = async (invocationCtx, next) => { | ||
// Add pre-invocation logic here | ||
const result = await next(); | ||
// Add post-invocation logic here | ||
return result; | ||
}; | ||
return interceptor; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/boot/src/__tests__/integration/interceptor.booter.integration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/boot | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
BindingScope, | ||
ContextTags, | ||
GLOBAL_INTERCEPTOR_NAMESPACE, | ||
} from '@loopback/core'; | ||
import {expect, TestSandbox} from '@loopback/testlab'; | ||
import {resolve} from 'path'; | ||
import {BooterApp} from '../fixtures/application'; | ||
|
||
describe('global interceptor script booter integration tests', () => { | ||
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox'); | ||
const sandbox = new TestSandbox(SANDBOX_PATH); | ||
|
||
let app: BooterApp; | ||
|
||
beforeEach('reset sandbox', () => sandbox.reset()); | ||
beforeEach(getApp); | ||
|
||
it('boots global interceptors when app.boot() is called', async () => { | ||
const expectedBinding = { | ||
key: `${GLOBAL_INTERCEPTOR_NAMESPACE}.MyGlobalInterceptor`, | ||
tags: [ | ||
ContextTags.PROVIDER, | ||
ContextTags.TYPE, | ||
ContextTags.GLOBAL_INTERCEPTOR, | ||
ContextTags.NAMESPACE, | ||
ContextTags.GLOBAL_INTERCEPTOR_GROUP, | ||
], | ||
scope: BindingScope.TRANSIENT, | ||
}; | ||
|
||
await app.boot(); | ||
|
||
const bindings = app | ||
.findByTag(ContextTags.GLOBAL_INTERCEPTOR) | ||
.map(b => ({key: b.key, tags: b.tagNames, scope: b.scope})); | ||
expect(bindings).to.containEql(expectedBinding); | ||
}); | ||
|
||
async function getApp() { | ||
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); | ||
await sandbox.copyFile( | ||
resolve(__dirname, '../fixtures/interceptor.artifact.js'), | ||
'interceptors/interceptor.interceptor.js', | ||
); | ||
|
||
const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp; | ||
app = new MyApp(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/boot | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
BindingScope, | ||
Constructor, | ||
createBindingFromClass, | ||
GLOBAL_INTERCEPTOR_NAMESPACE, | ||
inject, | ||
Interceptor, | ||
Provider, | ||
} from '@loopback/context'; | ||
import {Application, CoreBindings} from '@loopback/core'; | ||
import * as debugFactory from 'debug'; | ||
import {ArtifactOptions} from '../interfaces'; | ||
import {BootBindings} from '../keys'; | ||
import {BaseArtifactBooter} from './base-artifact.booter'; | ||
|
||
const debug = debugFactory('loopback:boot:interceptor-booter'); | ||
|
||
type InterceptorProviderClass = Constructor<Provider<Interceptor>>; | ||
|
||
/** | ||
* A class that extends BaseArtifactBooter to boot the 'InterceptorProvider' artifact type. | ||
* | ||
* Supported phases: configure, discover, load | ||
* | ||
* @param app Application instance | ||
* @param projectRoot Root of User Project relative to which all paths are resolved | ||
* @param [bootConfig] InterceptorProvider Artifact Options Object | ||
*/ | ||
export class InterceptorProviderBooter extends BaseArtifactBooter { | ||
interceptors: InterceptorProviderClass[]; | ||
|
||
constructor( | ||
@inject(CoreBindings.APPLICATION_INSTANCE) | ||
public app: Application, | ||
@inject(BootBindings.PROJECT_ROOT) projectRoot: string, | ||
@inject(`${BootBindings.BOOT_OPTIONS}#interceptors`) | ||
public interceptorConfig: ArtifactOptions = {}, | ||
) { | ||
super( | ||
projectRoot, | ||
// Set InterceptorProvider Booter Options if passed in via bootConfig | ||
Object.assign({}, InterceptorProviderDefaults, interceptorConfig), | ||
); | ||
} | ||
|
||
/** | ||
* Uses super method to get a list of Artifact classes. Boot each file by | ||
* creating a DataSourceConstructor and binding it to the application class. | ||
*/ | ||
async load() { | ||
await super.load(); | ||
|
||
this.interceptors = this.classes as InterceptorProviderClass[]; | ||
for (const interceptor of this.interceptors) { | ||
debug('Bind global interceptor: %s', interceptor.name); | ||
const binding = createBindingFromClass(interceptor, { | ||
namespace: GLOBAL_INTERCEPTOR_NAMESPACE, | ||
defaultScope: BindingScope.TRANSIENT, | ||
}); | ||
this.app.add(binding); | ||
debug('Binding created for global interceptor: %j', binding); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Default ArtifactOptions for DataSourceBooter. | ||
*/ | ||
export const InterceptorProviderDefaults: ArtifactOptions = { | ||
dirs: ['interceptors'], | ||
extensions: ['.interceptor.js'], | ||
nested: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters