Skip to content

Commit

Permalink
feat(booter): add @loopback/booter for common boot interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Raymond Feng <[email protected]>
  • Loading branch information
raymondfeng committed Oct 13, 2020
1 parent 6edfe73 commit 78a4789
Show file tree
Hide file tree
Showing 22 changed files with 946 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
# - Primary owner(s): @jannyHou
# - Standby owner(s): n/a
/packages/boot @jannyHou
/packages/booter @raymondfeng @jannyHou

#
# Migration from LB3
Expand Down
1 change: 1 addition & 0 deletions docs/site/MONOREPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ one in the monorepo: `npm run update-monorepo-file`
| [packages/authentication](https://github.com/strongloop/loopback-next/tree/master/packages/authentication) | @loopback/authentication | A LoopBack component for authentication support. |
| [packages/authorization](https://github.com/strongloop/loopback-next/tree/master/packages/authorization) | @loopback/authorization | A LoopBack component for authorization support. |
| [packages/boot](https://github.com/strongloop/loopback-next/tree/master/packages/boot) | @loopback/boot | A collection of Booters for LoopBack 4 Applications |
| [packages/booter](https://github.com/strongloop/loopback-next/tree/master/packages/booter) | @loopback/booter | @loopback/booter |
| [packages/booter-lb3app](https://github.com/strongloop/loopback-next/tree/master/packages/booter-lb3app) | @loopback/booter-lb3app | A booter component for LoopBack 3 applications to expose their REST API via LoopBack 4 |
| [packages/build](https://github.com/strongloop/loopback-next/tree/master/packages/build) | @loopback/build | A set of common scripts and default configurations to build LoopBack 4 or other TypeScript modules |
| [packages/cli](https://github.com/strongloop/loopback-next/tree/master/packages/cli) | @loopback/cli | Yeoman generator for LoopBack 4 |
Expand Down
2 changes: 2 additions & 0 deletions packages/booter/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=true
scripts-prepend-node-path=true
25 changes: 25 additions & 0 deletions packages/booter/LICENSE
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.
54 changes: 54 additions & 0 deletions packages/booter/README.md
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
137 changes: 137 additions & 0 deletions packages/booter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions packages/booter/package.json
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"
}
}
6 changes: 6 additions & 0 deletions packages/booter/src/__tests__/fixtures/empty.artifact.ts
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 packages/booter/src/__tests__/fixtures/interceptor.artifact.ts
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 packages/booter/src/__tests__/fixtures/multiple.artifact.ts
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';
}
19 changes: 19 additions & 0 deletions packages/booter/src/__tests__/fixtures/package.json
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"
}
Loading

0 comments on commit 78a4789

Please sign in to comment.