@loopback/context 4.0.0-alpha.6 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +116 -0
  3. package/dist/binding-config.d.ts +40 -0
  4. package/dist/binding-config.js +33 -0
  5. package/dist/binding-config.js.map +1 -0
  6. package/dist/binding-decorator.d.ts +45 -0
  7. package/dist/binding-decorator.js +118 -0
  8. package/dist/binding-decorator.js.map +1 -0
  9. package/dist/binding-filter.d.ts +108 -0
  10. package/dist/binding-filter.js +162 -0
  11. package/dist/binding-filter.js.map +1 -0
  12. package/dist/binding-inspector.d.ts +150 -0
  13. package/dist/binding-inspector.js +249 -0
  14. package/dist/binding-inspector.js.map +1 -0
  15. package/dist/binding-key.d.ts +66 -0
  16. package/dist/binding-key.js +121 -0
  17. package/dist/binding-key.js.map +1 -0
  18. package/dist/binding-sorter.d.ts +71 -0
  19. package/dist/binding-sorter.js +89 -0
  20. package/dist/binding-sorter.js.map +1 -0
  21. package/dist/binding.d.ts +577 -0
  22. package/dist/binding.js +788 -0
  23. package/dist/binding.js.map +1 -0
  24. package/dist/context-event.d.ts +23 -0
  25. package/dist/context-event.js +7 -0
  26. package/dist/context-event.js.map +1 -0
  27. package/dist/context-observer.d.ts +36 -0
  28. package/dist/context-observer.js +7 -0
  29. package/dist/context-observer.js.map +1 -0
  30. package/dist/context-subscription.d.ts +147 -0
  31. package/dist/context-subscription.js +317 -0
  32. package/dist/context-subscription.js.map +1 -0
  33. package/dist/context-tag-indexer.d.ts +42 -0
  34. package/dist/context-tag-indexer.js +135 -0
  35. package/dist/context-tag-indexer.js.map +1 -0
  36. package/dist/context-view.d.ts +209 -0
  37. package/dist/context-view.js +240 -0
  38. package/dist/context-view.js.map +1 -0
  39. package/dist/context.d.ts +513 -0
  40. package/dist/context.js +717 -0
  41. package/dist/context.js.map +1 -0
  42. package/dist/index.d.ts +52 -0
  43. package/dist/index.js +60 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/inject-config.d.ts +67 -0
  46. package/dist/inject-config.js +181 -0
  47. package/dist/inject-config.js.map +1 -0
  48. package/dist/inject.d.ts +250 -0
  49. package/dist/inject.js +535 -0
  50. package/dist/inject.js.map +1 -0
  51. package/dist/interception-proxy.d.ts +76 -0
  52. package/dist/interception-proxy.js +67 -0
  53. package/dist/interception-proxy.js.map +1 -0
  54. package/dist/interceptor-chain.d.ts +121 -0
  55. package/dist/interceptor-chain.js +148 -0
  56. package/dist/interceptor-chain.js.map +1 -0
  57. package/dist/interceptor.d.ts +138 -0
  58. package/dist/interceptor.js +299 -0
  59. package/dist/interceptor.js.map +1 -0
  60. package/dist/invocation.d.ts +101 -0
  61. package/dist/invocation.js +163 -0
  62. package/dist/invocation.js.map +1 -0
  63. package/dist/json-types.d.ts +28 -0
  64. package/dist/json-types.js +7 -0
  65. package/dist/json-types.js.map +1 -0
  66. package/dist/keys.d.ts +65 -0
  67. package/dist/keys.js +74 -0
  68. package/dist/keys.js.map +1 -0
  69. package/dist/provider.d.ts +31 -0
  70. package/dist/provider.js +7 -0
  71. package/dist/provider.js.map +1 -0
  72. package/dist/resolution-session.d.ts +180 -0
  73. package/dist/resolution-session.js +274 -0
  74. package/dist/resolution-session.js.map +1 -0
  75. package/dist/resolver.d.ts +46 -0
  76. package/dist/resolver.js +203 -0
  77. package/dist/resolver.js.map +1 -0
  78. package/dist/unique-id.d.ts +14 -0
  79. package/dist/unique-id.js +26 -0
  80. package/dist/unique-id.js.map +1 -0
  81. package/dist/value-promise.d.ts +134 -0
  82. package/dist/value-promise.js +277 -0
  83. package/dist/value-promise.js.map +1 -0
  84. package/package.json +49 -36
  85. package/src/binding-config.ts +73 -0
  86. package/src/binding-decorator.ts +136 -0
  87. package/src/binding-filter.ts +250 -0
  88. package/src/binding-inspector.ts +371 -0
  89. package/src/binding-key.ts +136 -0
  90. package/src/binding-sorter.ts +124 -0
  91. package/src/binding.ts +1107 -0
  92. package/src/context-event.ts +30 -0
  93. package/src/context-observer.ts +50 -0
  94. package/src/context-subscription.ts +402 -0
  95. package/src/context-tag-indexer.ts +147 -0
  96. package/src/context-view.ts +440 -0
  97. package/src/context.ts +1079 -0
  98. package/src/index.ts +58 -0
  99. package/src/inject-config.ts +239 -0
  100. package/src/inject.ts +796 -0
  101. package/src/interception-proxy.ts +127 -0
  102. package/src/interceptor-chain.ts +268 -0
  103. package/src/interceptor.ts +430 -0
  104. package/src/invocation.ts +269 -0
  105. package/src/json-types.ts +35 -0
  106. package/src/keys.ts +85 -0
  107. package/src/provider.ts +37 -0
  108. package/src/resolution-session.ts +414 -0
  109. package/src/resolver.ts +282 -0
  110. package/src/unique-id.ts +24 -0
  111. package/src/value-promise.ts +318 -0
  112. package/index.d.ts +0 -6
  113. package/index.js +0 -9
  114. package/lib/binding.d.ts +0 -75
  115. package/lib/binding.js +0 -102
  116. package/lib/context.d.ts +0 -14
  117. package/lib/context.js +0 -96
  118. package/lib/index.d.ts +0 -5
  119. package/lib/index.js +0 -13
  120. package/lib/inject.d.ts +0 -24
  121. package/lib/inject.js +0 -43
  122. package/lib/isPromise.d.ts +0 -1
  123. package/lib/isPromise.js +0 -14
  124. package/lib/resolver.d.ts +0 -26
  125. package/lib/resolver.js +0 -72
  126. package/lib6/binding.d.ts +0 -75
  127. package/lib6/binding.js +0 -102
  128. package/lib6/context.d.ts +0 -14
  129. package/lib6/context.js +0 -96
  130. package/lib6/index.d.ts +0 -5
  131. package/lib6/index.js +0 -13
  132. package/lib6/inject.d.ts +0 -24
  133. package/lib6/inject.js +0 -43
  134. package/lib6/isPromise.d.ts +0 -1
  135. package/lib6/isPromise.js +0 -14
  136. package/lib6/resolver.d.ts +0 -26
  137. package/lib6/resolver.js +0 -72
package/lib6/context.js DELETED
@@ -1,96 +0,0 @@
1
- "use strict";
2
- // Copyright IBM Corp. 2013,2017. All Rights Reserved.
3
- // Node module: loopback
4
- // This file is licensed under the MIT License.
5
- // License text available at https://opensource.org/licenses/MIT
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const binding_1 = require("./binding");
8
- const isPromise_1 = require("./isPromise");
9
- class Context {
10
- constructor(_parent) {
11
- this._parent = _parent;
12
- this.registry = new Map();
13
- }
14
- bind(key) {
15
- const keyExists = this.registry.has(key);
16
- if (keyExists) {
17
- const existingBinding = this.registry.get(key);
18
- const bindingIsLocked = existingBinding && existingBinding.isLocked;
19
- if (bindingIsLocked)
20
- throw new Error(`Cannot rebind key "${key}", associated binding is locked`);
21
- }
22
- const binding = new binding_1.Binding(key);
23
- this.registry.set(key, binding);
24
- return binding;
25
- }
26
- contains(key) {
27
- return this.registry.has(key);
28
- }
29
- find(pattern) {
30
- let bindings = [];
31
- if (pattern) {
32
- // TODO(@superkhau): swap with production grade glob to regex lib
33
- const glob = new RegExp('^' + pattern.split('*').join('.*') + '$');
34
- this.registry.forEach(binding => {
35
- const isMatch = glob.test(binding.key);
36
- if (isMatch)
37
- bindings.push(binding);
38
- });
39
- }
40
- else {
41
- bindings = Array.from(this.registry.values());
42
- }
43
- const parentBindings = this._parent && this._parent.find(pattern);
44
- return this._mergeWithParent(bindings, parentBindings);
45
- }
46
- findByTag(pattern) {
47
- const bindings = [];
48
- // TODO(@superkhau): swap with production grade glob to regex lib
49
- const glob = new RegExp('^' + pattern.split('*').join('.*') + '$');
50
- this.registry.forEach(binding => {
51
- const isMatch = glob.test(binding.tagName);
52
- if (isMatch)
53
- bindings.push(binding);
54
- });
55
- const parentBindings = this._parent && this._parent.findByTag(pattern);
56
- return this._mergeWithParent(bindings, parentBindings);
57
- }
58
- _mergeWithParent(childList, parentList) {
59
- if (!parentList)
60
- return childList;
61
- const additions = parentList.filter(parentBinding => {
62
- // children bindings take precedence
63
- return !childList.some(childBinding => childBinding.key === parentBinding.key);
64
- });
65
- return childList.concat(additions);
66
- }
67
- get(key) {
68
- try {
69
- const binding = this.getBinding(key);
70
- return Promise.resolve(binding.getValue(this));
71
- }
72
- catch (err) {
73
- return Promise.reject(err);
74
- }
75
- }
76
- getSync(key) {
77
- const binding = this.getBinding(key);
78
- const valueOrPromise = binding.getValue(this);
79
- if (isPromise_1.isPromise(valueOrPromise)) {
80
- throw new Error(`Cannot get ${key} synchronously: ` +
81
- `the value requires async computation`);
82
- }
83
- return valueOrPromise;
84
- }
85
- getBinding(key) {
86
- const binding = this.registry.get(key);
87
- if (binding) {
88
- return binding;
89
- }
90
- if (this._parent) {
91
- return this._parent.getBinding(key);
92
- }
93
- throw new Error(`The key ${key} was not bound to any value.`);
94
- }
95
- }
96
- exports.Context = Context;
package/lib6/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export { Binding, BoundValue } from './binding';
2
- export { Context } from './context';
3
- export { Constructor } from './resolver';
4
- export { inject } from './inject';
5
- export declare const isPromise: any;
package/lib6/index.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- // Copyright IBM Corp. 2017. All Rights Reserved.
3
- // Node module: loopback
4
- // This file is licensed under the MIT License.
5
- // License text available at https://opensource.org/licenses/MIT
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- var binding_1 = require("./binding");
8
- exports.Binding = binding_1.Binding;
9
- var context_1 = require("./context");
10
- exports.Context = context_1.Context;
11
- var inject_1 = require("./inject");
12
- exports.inject = inject_1.inject;
13
- exports.isPromise = require('is-promise');
package/lib6/inject.d.ts DELETED
@@ -1,24 +0,0 @@
1
- import 'reflect-metadata';
2
- /**
3
- * A decorator to annotate method arguments for automatic injection
4
- * by LoopBack IoC container.
5
- *
6
- * Usage - Typescript:
7
- *
8
- * ```ts
9
- * class InfoController {
10
- * constructor(@inject('application.name') public appName: string) {
11
- * }
12
- * // ...
13
- * }
14
- * ```
15
- *
16
- * Usage - JavaScript:
17
- *
18
- * - TODO(bajtos)
19
- *
20
- * @param bindingKey What binding to use in order to resolve the value
21
- * of the annotated argument.
22
- */
23
- export declare function inject(bindingKey: string): (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
24
- export declare function describeInjectedArguments(target: Function): string[];
package/lib6/inject.js DELETED
@@ -1,43 +0,0 @@
1
- "use strict";
2
- // Copyright IBM Corp. 2013,2017. All Rights Reserved.
3
- // Node module: loopback
4
- // This file is licensed under the MIT License.
5
- // License text available at https://opensource.org/licenses/MIT
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const assert = require("assert");
8
- require("reflect-metadata");
9
- const REFLECTION_KEY = 'loopback.inject';
10
- /**
11
- * A decorator to annotate method arguments for automatic injection
12
- * by LoopBack IoC container.
13
- *
14
- * Usage - Typescript:
15
- *
16
- * ```ts
17
- * class InfoController {
18
- * constructor(@inject('application.name') public appName: string) {
19
- * }
20
- * // ...
21
- * }
22
- * ```
23
- *
24
- * Usage - JavaScript:
25
- *
26
- * - TODO(bajtos)
27
- *
28
- * @param bindingKey What binding to use in order to resolve the value
29
- * of the annotated argument.
30
- */
31
- function inject(bindingKey) {
32
- return function markArgumentAsInjected(target, propertyKey, parameterIndex) {
33
- assert(parameterIndex != undefined, '@inject decorator can be used on function arguments only!');
34
- const injectedArgs = Reflect.getOwnMetadata(REFLECTION_KEY, target, propertyKey) || [];
35
- injectedArgs[parameterIndex] = bindingKey;
36
- Reflect.defineMetadata(REFLECTION_KEY, injectedArgs, target, propertyKey);
37
- };
38
- }
39
- exports.inject = inject;
40
- function describeInjectedArguments(target) {
41
- return Reflect.getOwnMetadata(REFLECTION_KEY, target) || [];
42
- }
43
- exports.describeInjectedArguments = describeInjectedArguments;
@@ -1 +0,0 @@
1
- export declare function isPromise<T>(value: T | Promise<T>): value is Promise<T>;
package/lib6/isPromise.js DELETED
@@ -1,14 +0,0 @@
1
- "use strict";
2
- // Copyright IBM Corp. 2013,2017. All Rights Reserved.
3
- // Node module: loopback
4
- // This file is licensed under the MIT License.
5
- // License text available at https://opensource.org/licenses/MIT
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- function isPromise(value) {
8
- if (!value)
9
- return false;
10
- if (typeof value !== 'object' && typeof value !== 'function')
11
- return false;
12
- return typeof value.then === 'function';
13
- }
14
- exports.isPromise = isPromise;
@@ -1,26 +0,0 @@
1
- import { Context } from './context';
2
- import { BoundValue } from './binding';
3
- export declare type Constructor<T> = new (...args: any[]) => T;
4
- /**
5
- * Create an instance of a class which constructor has arguments
6
- * decorated with `@inject`.
7
- *
8
- * The function returns a class when all dependencies were
9
- * resolved synchronously, or a Promise otherwise.
10
- *
11
- * @param ctor The class constructor to call.
12
- * @param ctx The context containing values for `@inject` resolution
13
- */
14
- export declare function instantiateClass<T>(ctor: Constructor<T>, ctx: Context): T | Promise<T>;
15
- /**
16
- * Given a function with arguments decorated with `@inject`,
17
- * return the list of arguments resolved using the values
18
- * bound in `ctx`.
19
-
20
- * The function returns an argument array when all dependencies were
21
- * resolved synchronously, or a Promise otherwise.
22
- *
23
- * @param fn The function for which the arguments should be resolved.
24
- * @param ctx The context containing values for `@inject` resolution
25
- */
26
- export declare function resolveInjectedArguments(fn: Function, ctx: Context): BoundValue[] | Promise<BoundValue[]>;
package/lib6/resolver.js DELETED
@@ -1,72 +0,0 @@
1
- "use strict";
2
- // Copyright IBM Corp. 2013,2017. All Rights Reserved.
3
- // Node module: loopback
4
- // This file is licensed under the MIT License.
5
- // License text available at https://opensource.org/licenses/MIT
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const isPromise_1 = require("./isPromise");
8
- const inject_1 = require("./inject");
9
- /**
10
- * Create an instance of a class which constructor has arguments
11
- * decorated with `@inject`.
12
- *
13
- * The function returns a class when all dependencies were
14
- * resolved synchronously, or a Promise otherwise.
15
- *
16
- * @param ctor The class constructor to call.
17
- * @param ctx The context containing values for `@inject` resolution
18
- */
19
- function instantiateClass(ctor, ctx) {
20
- const argsOrPromise = resolveInjectedArguments(ctor, ctx);
21
- if (isPromise_1.isPromise(argsOrPromise)) {
22
- return argsOrPromise.then(args => new ctor(...args));
23
- }
24
- else {
25
- return new ctor(...argsOrPromise);
26
- }
27
- }
28
- exports.instantiateClass = instantiateClass;
29
- /**
30
- * Given a function with arguments decorated with `@inject`,
31
- * return the list of arguments resolved using the values
32
- * bound in `ctx`.
33
-
34
- * The function returns an argument array when all dependencies were
35
- * resolved synchronously, or a Promise otherwise.
36
- *
37
- * @param fn The function for which the arguments should be resolved.
38
- * @param ctx The context containing values for `@inject` resolution
39
- */
40
- function resolveInjectedArguments(fn, ctx) {
41
- // NOTE: the array may be sparse, i.e.
42
- // Object.keys(injectedArgs).length !== injectedArgs.length
43
- // Example value:
44
- // [ , 'key1', , 'key2']
45
- const injectedArgs = inject_1.describeInjectedArguments(fn);
46
- const args = new Array(fn.length);
47
- let asyncResolvers = undefined;
48
- for (let ix = 0; ix < fn.length; ix++) {
49
- const bindingKey = injectedArgs[ix];
50
- if (!bindingKey) {
51
- throw new Error(`Cannot resolve injected arguments for function ${fn.name}: ` +
52
- `The argument ${ix + 1} was not decorated for dependency injection.`);
53
- }
54
- const binding = ctx.getBinding(bindingKey);
55
- const valueOrPromise = binding.getValue(ctx);
56
- if (isPromise_1.isPromise(valueOrPromise)) {
57
- if (!asyncResolvers)
58
- asyncResolvers = [];
59
- asyncResolvers.push(valueOrPromise.then((v) => args[ix] = v));
60
- }
61
- else {
62
- args[ix] = valueOrPromise;
63
- }
64
- }
65
- if (asyncResolvers) {
66
- return Promise.all(asyncResolvers).then(() => args);
67
- }
68
- else {
69
- return args;
70
- }
71
- }
72
- exports.resolveInjectedArguments = resolveInjectedArguments;