Skip to content

Commit 47e78ec

Browse files
committed
feat(example-context): add standalone examples for @loopback/context
1 parent 122b1fb commit 47e78ec

30 files changed

+1627
-0
lines changed

examples/context/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=true

examples/context/.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
*.json

examples/context/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"bracketSpacing": false,
3+
"singleQuote": true,
4+
"printWidth": 80,
5+
"trailingComma": "all"
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"editor.rulers": [80],
3+
"editor.tabCompletion": "on",
4+
"editor.tabSize": 2,
5+
"editor.trimAutoWhitespace": true,
6+
"editor.formatOnSave": true,
7+
8+
"files.exclude": {
9+
"**/.DS_Store": true,
10+
"**/.git": true,
11+
"**/.hg": true,
12+
"**/.svn": true,
13+
"**/CVS": true,
14+
"dist": true,
15+
},
16+
"files.insertFinalNewline": true,
17+
"files.trimTrailingWhitespace": true,
18+
19+
"tslint.ignoreDefinitionFiles": true,
20+
"typescript.tsdk": "./node_modules/typescript/lib"
21+
}

examples/context/.vscode/tasks.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "Watch and Compile Project",
8+
"type": "shell",
9+
"command": "npm",
10+
"args": ["--silent", "run", "build:watch"],
11+
"group": {
12+
"kind": "build",
13+
"isDefault": true
14+
},
15+
"problemMatcher": "$tsc-watch"
16+
},
17+
{
18+
"label": "Build, Test and Lint",
19+
"type": "shell",
20+
"command": "npm",
21+
"args": ["--silent", "run", "test:dev"],
22+
"group": {
23+
"kind": "test",
24+
"isDefault": true
25+
},
26+
"problemMatcher": ["$tsc", "$tslint5"]
27+
}
28+
]
29+
}

examples/context/LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) IBM Corp. 2019. All Rights Reserved.
2+
Node module: @loopback/example-context
3+
This project is licensed under the MIT License, full text below.
4+
5+
--------
6+
7+
MIT license
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.

examples/context/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# @loopback/example-context
2+
3+
This project contains a list of standalone examples to illustrate Inversion of
4+
Control (IoC) and Dependency Injection (DI) capabilities provided by
5+
[`@loopback/context`](https://github.com/strongloop/loopback-next/blob/master/packages/context).
6+
7+
## Examples
8+
9+
| Example | Description |
10+
| :------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------------------- |
11+
| [binding-types.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/binding-types.ts) | Various ways to provide values for a binding |
12+
| [configuration-injection.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/configuration-injection.ts) | Configuration for bindings and injection of configurations |
13+
| [context-chain.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/context-chain.ts) | Contexts are chained to create a hierarchy of registries |
14+
| [context-observation.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/context-observation.ts) | Observe context (bind/unbind) and context view (refresh) events |
15+
| [custom-configuration-resolver.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/custom-configuration-resolver.ts) | Override how configuration is resolved from a given binding |
16+
| [custom-inject-decorator.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/custom-inject-decorator.ts) | How to create a new decorator for custom injections |
17+
| [custom-inject-resolve.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/custom-inject-resolve.ts) | How to specify a custom resolve function for bindings |
18+
| [dependency-injection.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/dependency-injection.ts) | Different styles of dependency injection |
19+
| [find-bindings.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/find-bindings.ts) | Different flavors of finding bindings in a context |
20+
| [injection-without-binding.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/injection-without-binding.ts) | Perform dependency injection without binding a class |
21+
| [interceptor-proxy.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/interceptor-proxy.ts) | Get proxies to intercept method invocations |
22+
| [parameterized-decoration.ts](https://github.com/strongloop/loopback-next/blob/master/examples/context/src/parameterized-decoration.ts) | Apply decorators that require parameters as arguments |
23+
24+
## Use
25+
26+
Start all examples:
27+
28+
```sh
29+
npm start
30+
```
31+
32+
To run individual examples:
33+
34+
```sh
35+
npm run build
36+
node dist/<an-example>
37+
```
38+
39+
## Contributions
40+
41+
- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
42+
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)
43+
44+
## Tests
45+
46+
Run `npm test` from the root folder.
47+
48+
## Contributors
49+
50+
See
51+
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).
52+
53+
## License
54+
55+
MIT
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
> binding-types.js
2+
[2019-05-31T17:52:20.865Z] (/greet#1) Hello, John
3+
[2019-05-31T17:52:20.865Z] (/greet#2) Hello, John
4+
5+
> configuration-injection.js
6+
[2019-05-31T17:52:20.867Z] >>>: Hello, Ray
7+
8+
> context-chain.js
9+
[app] Hello, John!
10+
[request] Hello, John!
11+
[app] Hello, John!
12+
13+
> context-observation.js
14+
Adding EnglishGreeter
15+
[observer] bind greeters.EnglishGreeter
16+
[view.refresh] ["greeters.EnglishGreeter"]
17+
Adding ChineseGreeter
18+
[observer] bind greeters.ChineseGreeter
19+
[view.refresh] ["greeters.EnglishGreeter","greeters.ChineseGreeter"]
20+
Removing ChineseGreeter
21+
[observer] unbind greeters.ChineseGreeter
22+
[view.refresh] ["greeters.EnglishGreeter"]
23+
Adding ChineseGreeter to request context
24+
[view.refresh] ["greeters.ChineseGreeter","greeters.EnglishGreeter"]
25+
26+
> custom-configuration-resolver.js
27+
{ bar: 'abc' }
28+
xyz
29+
30+
> custom-inject-decorator.js
31+
Hello, John
32+
33+
> custom-inject-resolve.js
34+
Context: invocation-context Binding: greeter
35+
Injection: Greeter.constructor[0]
36+
Context: invocation-context Binding: greeter
37+
Injection: Greeter.prototype.prefix
38+
[2019-05-31T17:52:20.877Z] Hello, John
39+
40+
> dependency-injection.js
41+
[2019-05-31T17:52:20.879Z] (en) Hello, Jane!
42+
[2019-05-31T17:52:20.880Z] Hello, John!
43+
[2019-05-31T17:52:20.880Z] (zh) 你好,John!
44+
[2019-05-31T17:52:20.881Z] (en) Hello, Jane!
45+
46+
> find-bindings.js
47+
greeters.EnglishGreeter
48+
[ 'greeters.EnglishGreeter' ]
49+
[ 'greeters.EnglishGreeter' ]
50+
[ 'greeters.EnglishGreeter', 'greeters.ChineseGreeter' ]
51+
[ 'greeters.EnglishGreeter', 'greeters.ChineseGreeter' ]
52+
[ 'greeters.EnglishGreeter', 'greeters.ChineseGreeter' ]
53+
[ 'greeters.ChineseGreeter', 'greeters.EnglishGreeter' ]
54+
55+
> injection-without-binding.js
56+
Hello, John
57+
58+
> interceptor-proxy.js
59+
Adding request id at Greeter.prototype.greet: [request] 1
60+
Request id found at Converter.prototype.toUpperCase: [request] 1
61+
Hello, JOHN
62+
63+
> parameterized-decoration.js
64+
1: { tags: { prefix: '1' } }
65+
2: { tags: { prefix: '2' } }
66+
1: Hello, John
67+
2: Hello, Jane
68+

examples/context/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright IBM Corp. 2018. All Rights Reserved.
2+
// Node module: @loopback/example-context
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
export * from './dist';

examples/context/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright IBM Corp. 2018. All Rights Reserved.
2+
// Node module: @loopback/example-context
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
const examples = require('./dist');
7+
8+
if (require.main === module) {
9+
examples.main().catch(err => {
10+
console.error('Fails to run examples.', err);
11+
process.exit(1);
12+
});
13+
}

0 commit comments

Comments
 (0)