-
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): implement Service booter
- Loading branch information
Showing
13 changed files
with
323 additions
and
5 deletions.
There are no files selected for viewing
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
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,82 @@ | ||
// 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 {CoreBindings} from '@loopback/core'; | ||
import {ApplicationWithServices} from '@loopback/service-proxy'; | ||
import {inject, Provider, Constructor} from '@loopback/context'; | ||
import {ArtifactOptions} from '../interfaces'; | ||
import {BaseArtifactBooter} from './base-artifact.booter'; | ||
import {BootBindings} from '../keys'; | ||
|
||
type ServiceProviderClass = Constructor<Provider<object>>; | ||
|
||
/** | ||
* A class that extends BaseArtifactBooter to boot the 'DataSource' artifact type. | ||
* Discovered DataSources are bound using `app.controller()`. | ||
* | ||
* Supported phases: configure, discover, load | ||
* | ||
* @param app Application instance | ||
* @param projectRoot Root of User Project relative to which all paths are resolved | ||
* @param [bootConfig] DataSource Artifact Options Object | ||
*/ | ||
export class ServiceBooter extends BaseArtifactBooter { | ||
serviceProviders: ServiceProviderClass[]; | ||
|
||
constructor( | ||
@inject(CoreBindings.APPLICATION_INSTANCE) | ||
public app: ApplicationWithServices, | ||
@inject(BootBindings.PROJECT_ROOT) projectRoot: string, | ||
@inject(`${BootBindings.BOOT_OPTIONS}#services`) | ||
public serviceConfig: ArtifactOptions = {}, | ||
) { | ||
super( | ||
projectRoot, | ||
// Set DataSource Booter Options if passed in via bootConfig | ||
Object.assign({}, ServiceDefaults, serviceConfig), | ||
); | ||
} | ||
|
||
/** | ||
* 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.serviceProviders = this.classes.filter(isServiceProvider); | ||
|
||
/** | ||
* If Service providers were discovered, we need to make sure ServiceMixin | ||
* was used (so we have `app.serviceProvider()`) to perform the binding of a | ||
* Service provider class. | ||
*/ | ||
if (this.serviceProviders.length > 0) { | ||
if (!this.app.serviceProvider) { | ||
console.warn( | ||
'app.serviceProvider() function is needed for ServiceBooter. You can add ' + | ||
'it to your Application using ServiceMixin from @loopback/service-proxy.', | ||
); | ||
} else { | ||
this.serviceProviders.forEach(cls => { | ||
this.app.serviceProvider(cls as Constructor<Provider<object>>); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Default ArtifactOptions for DataSourceBooter. | ||
*/ | ||
export const ServiceDefaults: ArtifactOptions = { | ||
dirs: ['services'], | ||
extensions: ['.service.js'], | ||
nested: true, | ||
}; | ||
|
||
function isServiceProvider(cls: Constructor<{}>): cls is ServiceProviderClass { | ||
return /Provider$/.test(cls.name); | ||
} |
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,10 @@ | ||
// 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 | ||
|
||
export class GreetingService { | ||
greet(whom: string = 'world') { | ||
return Promise.resolve(`Hello ${whom}`); | ||
} | ||
} |
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,28 @@ | ||
// 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 {Provider} from '@loopback/core'; | ||
|
||
export interface GeoPoint { | ||
lat: number; | ||
lng: number; | ||
} | ||
|
||
export interface GeocoderService { | ||
geocode(address: string): Promise<GeoPoint>; | ||
} | ||
|
||
// A dummy service instance to make unit testing easier | ||
const GeocoderSingleton: GeocoderService = { | ||
geocode(address: string) { | ||
return Promise.resolve({lat: 0, lng: 0}); | ||
}, | ||
}; | ||
|
||
export class GeocoderServiceProvider implements Provider<GeocoderService> { | ||
value(): Promise<GeocoderService> { | ||
return Promise.resolve(GeocoderSingleton); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/boot/test/integration/service.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,49 @@ | ||
// 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 {expect, TestSandbox} from '@loopback/testlab'; | ||
import {resolve} from 'path'; | ||
import {BooterApp} from '../fixtures/application'; | ||
|
||
describe('service booter integration tests', () => { | ||
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox'); | ||
const sandbox = new TestSandbox(SANDBOX_PATH); | ||
|
||
const SERVICES_PREFIX = 'services'; | ||
const SERVICES_TAG = 'service'; | ||
|
||
let app: BooterApp; | ||
|
||
beforeEach('reset sandbox', () => sandbox.reset()); | ||
beforeEach(getApp); | ||
|
||
it('boots services when app.boot() is called', async () => { | ||
const expectedBindings = [ | ||
`${SERVICES_PREFIX}.GeocoderService`, | ||
// greeting service is skipped - service classes are not supported (yet) | ||
]; | ||
|
||
await app.boot(); | ||
|
||
const bindings = app.findByTag(SERVICES_TAG).map(b => b.key); | ||
expect(bindings.sort()).to.eql(expectedBindings.sort()); | ||
}); | ||
|
||
async function getApp() { | ||
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); | ||
await sandbox.copyFile( | ||
resolve(__dirname, '../fixtures/service-provider.artifact.js'), | ||
'services/geocoder.service.js', | ||
); | ||
|
||
await sandbox.copyFile( | ||
resolve(__dirname, '../fixtures/service-class.artifact.js'), | ||
'services/greeting.service.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
Oops, something went wrong.