-
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(booter): add @loopback/booter for common boot interfaces
Signed-off-by: Raymond Feng <[email protected]>
- Loading branch information
1 parent
6edfe73
commit 78a4789
Showing
22 changed files
with
946 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package-lock=true | ||
scripts-prepend-node-path=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright (c) IBM Corp. 2020. | ||
Node module: @loopback/booter | ||
This project is licensed under the MIT License, full text below. | ||
|
||
-------- | ||
|
||
MIT license | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,54 @@ | ||
# @loopback/booter | ||
|
||
This module defines common types/interfaces for LoopBack booters that are | ||
plugged into `@loopback/boot` as extensions. | ||
|
||
A Booter is a Class that can be bound to an Application and is called to perform | ||
a task before the Application is started. A Booter may have multiple phases to | ||
complete its task. The task for a convention based Booter is to discover and | ||
bind Artifacts (Controllers, Repositories, Models, etc.). | ||
|
||
An example task of a Booter may be to discover and bind all artifacts of a given | ||
type. | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install @loopback/booter | ||
``` | ||
|
||
## Basic Use | ||
|
||
### Implement a Booter | ||
|
||
```ts | ||
@booter('my-artifacts') | ||
class MyBooter implements Booter {} | ||
``` | ||
|
||
### ArtifactOptions | ||
|
||
| Options | Type | Description | | ||
| ------------ | -------------------- | ------------------------------------------------------------------------------------------------------------ | | ||
| `dirs` | `string \| string[]` | Paths relative to projectRoot to look in for Artifact | | ||
| `extensions` | `string \| string[]` | File extensions to match for Artifact | | ||
| `nested` | `boolean` | Look in nested directories in `dirs` for Artifact | | ||
| `glob` | `string` | A `glob` pattern string. This takes precedence over above 3 options (which are used to make a glob pattern). | | ||
|
||
## Contributions | ||
|
||
- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md) | ||
- [Join the team](https://github.com/strongloop/loopback-next/issues/110) | ||
|
||
## Tests | ||
|
||
Run `npm test` from the root folder. | ||
|
||
## Contributors | ||
|
||
See | ||
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
|
||
## License | ||
|
||
MIT |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,54 @@ | ||
{ | ||
"name": "@loopback/booter", | ||
"version": "1.0.0", | ||
"description": "Types/interfaces for booters", | ||
"keywords": [ | ||
"loopback" | ||
], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"engines": { | ||
"node": "^10.16 || 12 || 14" | ||
}, | ||
"scripts": { | ||
"build": "lb-tsc", | ||
"build:watch": "lb-tsc --watch", | ||
"pretest": "npm run clean && npm run build", | ||
"test": "lb-mocha \"dist/__tests__/**/*.js\"", | ||
"clean": "lb-clean dist *.tsbuildinfo .eslintcache" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/strongloop/loopback-next.git", | ||
"directory": "packages/booter" | ||
}, | ||
"author": "IBM Corp.", | ||
"license": "MIT", | ||
"files": [ | ||
"README.md", | ||
"dist", | ||
"src", | ||
"!*/__tests__" | ||
], | ||
"peerDependencies": { | ||
"@loopback/core": "^2.11.0" | ||
}, | ||
"dependencies": { | ||
"debug": "^4.2.0", | ||
"glob": "^7.1.6", | ||
"tslib": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@loopback/build": "^6.2.5", | ||
"@loopback/core": "^2.11.0", | ||
"@loopback/testlab": "^3.2.7", | ||
"@types/debug": "^4.1.5", | ||
"@types/glob": "^7.1.3", | ||
"@types/node": "^10.17.37", | ||
"typescript": "~4.0.3" | ||
}, | ||
"copyright.owner": "IBM Corp.", | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,6 @@ | ||
// 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 | ||
|
||
// THIS FILE IS INTENTIONALLY LEFT EMPTY! |
55 changes: 55 additions & 0 deletions
55
packages/booter/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,55 @@ | ||
// Copyright IBM Corp. 2019,2020. 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, | ||
InvocationContext, | ||
InvocationResult, | ||
Provider, | ||
ValueOrPromise, | ||
} from '@loopback/core'; | ||
|
||
/** | ||
* This class will be bound to the application as a global `Interceptor` during | ||
* `boot` | ||
*/ | ||
@globalInterceptor('auth', {tags: {name: 'myGlobalInterceptor'}}) | ||
export class MyGlobalInterceptor implements Provider<Interceptor> { | ||
/* | ||
constructor() {} | ||
*/ | ||
|
||
/** | ||
* This method is used by LoopBack context to produce an interceptor function | ||
* for the binding. | ||
* | ||
* @returns An interceptor function | ||
*/ | ||
value() { | ||
return this.intercept.bind(this); | ||
} | ||
|
||
/** | ||
* The logic to intercept an invocation | ||
* @param invocationCtx - Invocation context | ||
* @param next - A function to invoke next interceptor or the target method | ||
*/ | ||
async intercept( | ||
invocationCtx: InvocationContext, | ||
next: () => ValueOrPromise<InvocationResult>, | ||
) { | ||
// eslint-disable-next-line no-useless-catch | ||
try { | ||
// Add pre-invocation logic here | ||
const result = await next(); | ||
// Add post-invocation logic here | ||
return result; | ||
} catch (err) { | ||
// Add error handling logic here | ||
throw err; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/booter/src/__tests__/fixtures/multiple.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,20 @@ | ||
// 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 | ||
|
||
export class ArtifactOne { | ||
one() { | ||
return 'ControllerOne.one()'; | ||
} | ||
} | ||
|
||
export class ArtifactTwo { | ||
two() { | ||
return 'ControllerTwo.two()'; | ||
} | ||
} | ||
|
||
export function hello() { | ||
return 'hello world'; | ||
} |
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,19 @@ | ||
{ | ||
"name": "boot-test-app", | ||
"version": "1.0.0", | ||
"description": "boot-test-app", | ||
"keywords": [ | ||
"loopback-application", | ||
"loopback" | ||
], | ||
"engines": { | ||
"node": ">=10.16" | ||
}, | ||
"scripts": { | ||
}, | ||
"repository": { | ||
"type": "git" | ||
}, | ||
"author": "IBM Corp.", | ||
"license": "MIT" | ||
} |
Oops, something went wrong.