graphql-modules 3.1.2-alpha-20260121024117-e03d9c26de1cfd3971986f82b995963e1cc77056 → 3.1.2-alpha-20260121024633-8af430e29a180a1d00c50c4bf74d342b3518267c
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.
- package/cjs/application/apollo.js +81 -0
- package/cjs/application/application.js +149 -0
- package/cjs/application/context.js +144 -0
- package/cjs/application/di.js +48 -0
- package/cjs/application/execution.js +42 -0
- package/cjs/application/operation-controller.js +16 -0
- package/cjs/application/subscription.js +54 -0
- package/cjs/application/tokens.js +20 -0
- package/cjs/application/types.js +0 -0
- package/cjs/di/decorators.js +78 -0
- package/cjs/di/errors.js +87 -0
- package/cjs/di/forward-ref.js +26 -0
- package/cjs/di/index.js +19 -0
- package/cjs/di/injector.js +173 -0
- package/cjs/di/metadata.js +22 -0
- package/cjs/di/providers.js +60 -0
- package/cjs/di/registry.js +44 -0
- package/cjs/di/resolution.js +166 -0
- package/cjs/di/utils.js +44 -0
- package/cjs/index.js +31 -0
- package/cjs/module/factory.js +71 -0
- package/cjs/module/metadata.js +110 -0
- package/cjs/module/module.js +27 -0
- package/cjs/module/resolvers.js +341 -0
- package/cjs/module/tokens.js +21 -0
- package/cjs/module/type-defs.js +24 -0
- package/cjs/module/types.js +0 -0
- package/cjs/package.json +1 -0
- package/cjs/shared/di.js +0 -0
- package/cjs/shared/errors.js +82 -0
- package/cjs/shared/gql.js +12 -0
- package/cjs/shared/middleware.js +109 -0
- package/cjs/shared/types.js +0 -0
- package/cjs/shared/utils.js +115 -0
- package/cjs/testing/di.js +9 -0
- package/cjs/testing/graphql.js +10 -0
- package/cjs/testing/index.js +17 -0
- package/cjs/testing/test-application.js +65 -0
- package/cjs/testing/test-injector.js +22 -0
- package/cjs/testing/test-module.js +270 -0
- package/esm/application/apollo.js +77 -0
- package/esm/application/application.js +146 -0
- package/esm/application/context.js +140 -0
- package/esm/application/di.js +42 -0
- package/esm/application/execution.js +39 -0
- package/esm/application/operation-controller.js +13 -0
- package/esm/application/subscription.js +51 -0
- package/esm/application/tokens.js +17 -0
- package/esm/application/types.js +0 -0
- package/esm/di/decorators.js +72 -0
- package/esm/di/errors.js +79 -0
- package/esm/di/forward-ref.js +22 -0
- package/esm/di/index.js +4 -0
- package/esm/di/injector.js +168 -0
- package/esm/di/metadata.js +17 -0
- package/esm/di/providers.js +50 -0
- package/esm/di/registry.js +40 -0
- package/esm/di/resolution.js +159 -0
- package/esm/di/utils.js +36 -0
- package/esm/index.js +16 -0
- package/esm/module/factory.js +68 -0
- package/esm/module/metadata.js +107 -0
- package/esm/module/module.js +24 -0
- package/esm/module/resolvers.js +337 -0
- package/esm/module/tokens.js +18 -0
- package/esm/module/type-defs.js +21 -0
- package/esm/module/types.js +0 -0
- package/esm/shared/di.js +0 -0
- package/esm/shared/errors.js +69 -0
- package/esm/shared/gql.js +9 -0
- package/esm/shared/middleware.js +103 -0
- package/esm/shared/types.js +0 -0
- package/esm/shared/utils.js +101 -0
- package/esm/testing/di.js +6 -0
- package/esm/testing/graphql.js +7 -0
- package/esm/testing/index.js +14 -0
- package/esm/testing/test-application.js +62 -0
- package/esm/testing/test-injector.js +18 -0
- package/esm/testing/test-module.js +266 -0
- package/package.json +30 -10
- package/typings/application/apollo.d.ts +22 -0
- package/typings/application/application.d.ts +32 -0
- package/typings/application/context.d.ts +24 -0
- package/typings/application/di.d.ts +22 -0
- package/typings/application/execution.d.ts +8 -0
- package/typings/application/operation-controller.d.ts +5 -0
- package/typings/application/subscription.d.ts +8 -0
- package/typings/application/tokens.d.ts +17 -0
- package/typings/application/types.d.ts +130 -0
- package/typings/di/decorators.d.ts +11 -0
- package/typings/di/errors.d.ts +16 -0
- package/typings/di/forward-ref.d.ts +7 -0
- package/typings/di/index.d.ts +5 -0
- package/typings/di/injector.d.ts +50 -0
- package/typings/di/metadata.d.ts +12 -0
- package/typings/di/providers.d.ts +44 -0
- package/typings/di/registry.d.ts +11 -0
- package/typings/di/resolution.d.ts +63 -0
- package/typings/di/utils.d.ts +8 -0
- package/typings/index.d.ts +13 -0
- package/typings/module/factory.d.ts +16 -0
- package/typings/module/metadata.d.ts +12 -0
- package/typings/module/module.d.ts +22 -0
- package/typings/module/resolvers.d.ts +13 -0
- package/typings/module/tokens.d.ts +18 -0
- package/typings/module/type-defs.d.ts +7 -0
- package/typings/module/types.d.ts +51 -0
- package/typings/shared/di.d.ts +3 -0
- package/typings/shared/errors.d.ts +36 -0
- package/typings/shared/gql.d.ts +2 -0
- package/typings/shared/middleware.d.ts +21 -0
- package/typings/shared/types.d.ts +22 -0
- package/typings/shared/utils.d.ts +12 -0
- package/typings/testing/di.d.ts +2 -0
- package/typings/testing/graphql.d.ts +14 -0
- package/typings/testing/index.d.ts +14 -0
- package/typings/testing/test-application.d.ts +2 -0
- package/typings/testing/test-injector.d.ts +4 -0
- package/typings/testing/test-module.d.ts +10 -0
- package/LICENSE.md +0 -21
- package/index.js +0 -2359
- package/index.mjs +0 -2344
- /package/{application/apollo.d.ts → typings/application/apollo.d.cts} +0 -0
- /package/{application/application.d.ts → typings/application/application.d.cts} +0 -0
- /package/{application/context.d.ts → typings/application/context.d.cts} +0 -0
- /package/{application/di.d.ts → typings/application/di.d.cts} +0 -0
- /package/{application/execution.d.ts → typings/application/execution.d.cts} +0 -0
- /package/{application/operation-controller.d.ts → typings/application/operation-controller.d.cts} +0 -0
- /package/{application/subscription.d.ts → typings/application/subscription.d.cts} +0 -0
- /package/{application/tokens.d.ts → typings/application/tokens.d.cts} +0 -0
- /package/{application/types.d.ts → typings/application/types.d.cts} +0 -0
- /package/{di/decorators.d.ts → typings/di/decorators.d.cts} +0 -0
- /package/{di/errors.d.ts → typings/di/errors.d.cts} +0 -0
- /package/{di/forward-ref.d.ts → typings/di/forward-ref.d.cts} +0 -0
- /package/{di/index.d.ts → typings/di/index.d.cts} +0 -0
- /package/{di/injector.d.ts → typings/di/injector.d.cts} +0 -0
- /package/{di/metadata.d.ts → typings/di/metadata.d.cts} +0 -0
- /package/{di/providers.d.ts → typings/di/providers.d.cts} +0 -0
- /package/{di/registry.d.ts → typings/di/registry.d.cts} +0 -0
- /package/{di/resolution.d.ts → typings/di/resolution.d.cts} +0 -0
- /package/{di/utils.d.ts → typings/di/utils.d.cts} +0 -0
- /package/{index.d.ts → typings/index.d.cts} +0 -0
- /package/{module/factory.d.ts → typings/module/factory.d.cts} +0 -0
- /package/{module/metadata.d.ts → typings/module/metadata.d.cts} +0 -0
- /package/{module/module.d.ts → typings/module/module.d.cts} +0 -0
- /package/{module/resolvers.d.ts → typings/module/resolvers.d.cts} +0 -0
- /package/{module/tokens.d.ts → typings/module/tokens.d.cts} +0 -0
- /package/{module/type-defs.d.ts → typings/module/type-defs.d.cts} +0 -0
- /package/{module/types.d.ts → typings/module/types.d.cts} +0 -0
- /package/{shared/di.d.ts → typings/shared/di.d.cts} +0 -0
- /package/{shared/errors.d.ts → typings/shared/errors.d.cts} +0 -0
- /package/{shared/gql.d.ts → typings/shared/gql.d.cts} +0 -0
- /package/{shared/middleware.d.ts → typings/shared/middleware.d.cts} +0 -0
- /package/{shared/types.d.ts → typings/shared/types.d.cts} +0 -0
- /package/{shared/utils.d.ts → typings/shared/utils.d.cts} +0 -0
- /package/{testing/di.d.ts → typings/testing/di.d.cts} +0 -0
- /package/{testing/graphql.d.ts → typings/testing/graphql.d.cts} +0 -0
- /package/{testing/index.d.ts → typings/testing/index.d.cts} +0 -0
- /package/{testing/test-application.d.ts → typings/testing/test-application.d.cts} +0 -0
- /package/{testing/test-injector.d.ts → typings/testing/test-injector.d.cts} +0 -0
- /package/{testing/test-module.d.ts → typings/testing/test-module.d.cts} +0 -0
package/cjs/di/errors.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.invalidProviderError = invalidProviderError;
|
|
4
|
+
exports.noInjectableError = noInjectableError;
|
|
5
|
+
exports.noAnnotationError = noAnnotationError;
|
|
6
|
+
exports.cyclicDependencyError = cyclicDependencyError;
|
|
7
|
+
exports.noProviderError = noProviderError;
|
|
8
|
+
exports.instantiationError = instantiationError;
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
function invalidProviderError(provider) {
|
|
11
|
+
return Error(`Invalid provider - only instances of Provider and Type are allowed, got: ${provider}`);
|
|
12
|
+
}
|
|
13
|
+
function noInjectableError(type) {
|
|
14
|
+
return Error(`Missing @Injectable decorator for '${(0, utils_1.stringify)(type)}'`);
|
|
15
|
+
}
|
|
16
|
+
function noAnnotationError(typeOrFunc, params) {
|
|
17
|
+
const signature = [];
|
|
18
|
+
for (let i = 0, len = params.length; i < len; i++) {
|
|
19
|
+
const parameter = params[i];
|
|
20
|
+
if (!parameter.type) {
|
|
21
|
+
signature.push('?');
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
signature.push((0, utils_1.stringify)(parameter.type));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return Error("Cannot resolve all parameters for '" +
|
|
28
|
+
(0, utils_1.stringify)(typeOrFunc) +
|
|
29
|
+
"'(" +
|
|
30
|
+
signature.join(', ') +
|
|
31
|
+
'). ' +
|
|
32
|
+
"Make sure that all the parameters are decorated with Inject or have valid type annotations and that '" +
|
|
33
|
+
(0, utils_1.stringify)(typeOrFunc) +
|
|
34
|
+
"' is decorated with Injectable.");
|
|
35
|
+
}
|
|
36
|
+
function cyclicDependencyError(injector, key) {
|
|
37
|
+
return injectionError(injector, key, function () {
|
|
38
|
+
return `Cannot instantiate cyclic dependency!${constructResolvingPath(this.keys)}`;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function noProviderError(injector, key) {
|
|
42
|
+
return injectionError(injector, key, function () {
|
|
43
|
+
const first = (0, utils_1.stringify)(this.keys[0].token);
|
|
44
|
+
return `No provider for ${first}!${constructResolvingPath(this.keys)}`;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function instantiationError(injector, originalException, key) {
|
|
48
|
+
return injectionError(injector, key, function () {
|
|
49
|
+
const first = (0, utils_1.stringify)(this.keys[0].token);
|
|
50
|
+
return `Error during instantiation of ${first}: ${(0, utils_1.getOriginalError)(this).message}${constructResolvingPath(this.keys)}`;
|
|
51
|
+
}, originalException);
|
|
52
|
+
}
|
|
53
|
+
function injectionError(injector, key, constructResolvingMessage, originalError) {
|
|
54
|
+
const error = (originalError ? (0, utils_1.wrappedError)('', originalError) : Error());
|
|
55
|
+
error.addKey = addKey;
|
|
56
|
+
error.keys = [key];
|
|
57
|
+
error.constructResolvingMessage =
|
|
58
|
+
function wrappedConstructResolvingMessage() {
|
|
59
|
+
return (constructResolvingMessage.call(this) + ` - in ${injector.displayName}`);
|
|
60
|
+
};
|
|
61
|
+
error.message = error.constructResolvingMessage();
|
|
62
|
+
error[utils_1.ERROR_ORIGINAL_ERROR] = originalError;
|
|
63
|
+
return error;
|
|
64
|
+
}
|
|
65
|
+
function constructResolvingPath(keys) {
|
|
66
|
+
if (keys.length > 1) {
|
|
67
|
+
const reversed = findFirstClosedCycle(keys.slice().reverse());
|
|
68
|
+
const tokenStrs = reversed.map((k) => (0, utils_1.stringify)(k.token));
|
|
69
|
+
return ' (' + tokenStrs.join(' -> ') + ')';
|
|
70
|
+
}
|
|
71
|
+
return '';
|
|
72
|
+
}
|
|
73
|
+
function findFirstClosedCycle(keys) {
|
|
74
|
+
const res = [];
|
|
75
|
+
for (let i = 0; i < keys.length; ++i) {
|
|
76
|
+
if (res.indexOf(keys[i]) > -1) {
|
|
77
|
+
res.push(keys[i]);
|
|
78
|
+
return res;
|
|
79
|
+
}
|
|
80
|
+
res.push(keys[i]);
|
|
81
|
+
}
|
|
82
|
+
return res;
|
|
83
|
+
}
|
|
84
|
+
function addKey(key) {
|
|
85
|
+
this.keys.push(key);
|
|
86
|
+
this.message = this.constructResolvingMessage();
|
|
87
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.forwardRef = forwardRef;
|
|
4
|
+
exports.resolveForwardRef = resolveForwardRef;
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const forwardRefSymbol = Symbol('__forward_ref__');
|
|
7
|
+
/**
|
|
8
|
+
* Useful in "circular dependencies of modules" situation
|
|
9
|
+
*/
|
|
10
|
+
function forwardRef(forwardRefFn) {
|
|
11
|
+
forwardRefFn[forwardRefSymbol] = forwardRef;
|
|
12
|
+
forwardRefFn.toString = function () {
|
|
13
|
+
return (0, utils_1.stringify)(this());
|
|
14
|
+
};
|
|
15
|
+
return forwardRefFn;
|
|
16
|
+
}
|
|
17
|
+
function resolveForwardRef(type) {
|
|
18
|
+
if (typeof type === 'function' &&
|
|
19
|
+
type.hasOwnProperty(forwardRefSymbol) &&
|
|
20
|
+
type[forwardRefSymbol] === forwardRef) {
|
|
21
|
+
return type();
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return type;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/cjs/di/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReflectiveInjector = exports.Injector = exports.onlyOperationProviders = exports.onlySingletonProviders = exports.Scope = exports.Type = exports.InjectionToken = exports.forwardRef = exports.ExecutionContext = exports.Inject = exports.Optional = exports.Injectable = void 0;
|
|
4
|
+
var decorators_1 = require("./decorators");
|
|
5
|
+
Object.defineProperty(exports, "Injectable", { enumerable: true, get: function () { return decorators_1.Injectable; } });
|
|
6
|
+
Object.defineProperty(exports, "Optional", { enumerable: true, get: function () { return decorators_1.Optional; } });
|
|
7
|
+
Object.defineProperty(exports, "Inject", { enumerable: true, get: function () { return decorators_1.Inject; } });
|
|
8
|
+
Object.defineProperty(exports, "ExecutionContext", { enumerable: true, get: function () { return decorators_1.ExecutionContext; } });
|
|
9
|
+
var forward_ref_1 = require("./forward-ref");
|
|
10
|
+
Object.defineProperty(exports, "forwardRef", { enumerable: true, get: function () { return forward_ref_1.forwardRef; } });
|
|
11
|
+
var providers_1 = require("./providers");
|
|
12
|
+
Object.defineProperty(exports, "InjectionToken", { enumerable: true, get: function () { return providers_1.InjectionToken; } });
|
|
13
|
+
Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return providers_1.Type; } });
|
|
14
|
+
Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return providers_1.Scope; } });
|
|
15
|
+
Object.defineProperty(exports, "onlySingletonProviders", { enumerable: true, get: function () { return providers_1.onlySingletonProviders; } });
|
|
16
|
+
Object.defineProperty(exports, "onlyOperationProviders", { enumerable: true, get: function () { return providers_1.onlyOperationProviders; } });
|
|
17
|
+
var injector_1 = require("./injector");
|
|
18
|
+
Object.defineProperty(exports, "Injector", { enumerable: true, get: function () { return injector_1.Injector; } });
|
|
19
|
+
Object.defineProperty(exports, "ReflectiveInjector", { enumerable: true, get: function () { return injector_1.ReflectiveInjector; } });
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReflectiveInjector = exports.Injector = void 0;
|
|
4
|
+
const resolution_1 = require("./resolution");
|
|
5
|
+
const registry_1 = require("./registry");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const _THROW_IF_NOT_FOUND = new Object();
|
|
8
|
+
const UNDEFINED = new Object();
|
|
9
|
+
const NOT_FOUND = new Object();
|
|
10
|
+
function notInExecutionContext() {
|
|
11
|
+
throw new Error('Not in execution context');
|
|
12
|
+
}
|
|
13
|
+
// Publicly available Injector.
|
|
14
|
+
// We use ReflectiveInjector everywhere
|
|
15
|
+
// but we don't want to leak its API to everyone
|
|
16
|
+
class Injector {
|
|
17
|
+
}
|
|
18
|
+
exports.Injector = Injector;
|
|
19
|
+
class ReflectiveInjector {
|
|
20
|
+
constructor({ name, providers, parent, fallbackParent, globalProvidersMap = new Map(), }) {
|
|
21
|
+
this._constructionCounter = 0;
|
|
22
|
+
this._executionContextGetter = notInExecutionContext;
|
|
23
|
+
this.displayName = name;
|
|
24
|
+
this._parent = parent || null;
|
|
25
|
+
this._fallbackParent = fallbackParent || null;
|
|
26
|
+
this._providers = providers;
|
|
27
|
+
this._globalProvidersMap = globalProvidersMap;
|
|
28
|
+
const len = this._providers.length;
|
|
29
|
+
this._keyIds = new Array(len);
|
|
30
|
+
this._objs = new Array(len);
|
|
31
|
+
for (let i = 0; i < len; i++) {
|
|
32
|
+
this._keyIds[i] = this._providers[i].key.id;
|
|
33
|
+
this._objs[i] = UNDEFINED;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
static createFromResolved({ name, providers, parent, fallbackParent, globalProvidersMap, }) {
|
|
37
|
+
return new ReflectiveInjector({
|
|
38
|
+
name,
|
|
39
|
+
providers,
|
|
40
|
+
parent,
|
|
41
|
+
fallbackParent,
|
|
42
|
+
globalProvidersMap,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
static resolve(providers) {
|
|
46
|
+
return (0, resolution_1.resolveProviders)(providers);
|
|
47
|
+
}
|
|
48
|
+
get parent() {
|
|
49
|
+
return this._parent;
|
|
50
|
+
}
|
|
51
|
+
get fallbackParent() {
|
|
52
|
+
return this._fallbackParent;
|
|
53
|
+
}
|
|
54
|
+
get(token, notFoundValue = _THROW_IF_NOT_FOUND) {
|
|
55
|
+
return this._getByKey(registry_1.Key.get(token), notFoundValue);
|
|
56
|
+
}
|
|
57
|
+
setExecutionContextGetter(getter) {
|
|
58
|
+
this._executionContextGetter = getter;
|
|
59
|
+
}
|
|
60
|
+
_getByKey(key, notFoundValue) {
|
|
61
|
+
let inj = this;
|
|
62
|
+
function getObj() {
|
|
63
|
+
while (inj instanceof ReflectiveInjector) {
|
|
64
|
+
const inj_ = inj;
|
|
65
|
+
const obj = inj_._getObjByKeyId(key.id);
|
|
66
|
+
if (obj !== UNDEFINED) {
|
|
67
|
+
return obj;
|
|
68
|
+
}
|
|
69
|
+
inj = inj_._parent;
|
|
70
|
+
}
|
|
71
|
+
return NOT_FOUND;
|
|
72
|
+
}
|
|
73
|
+
const resolvedValue = getObj();
|
|
74
|
+
if (resolvedValue !== NOT_FOUND) {
|
|
75
|
+
return resolvedValue;
|
|
76
|
+
}
|
|
77
|
+
// search in fallback Injector
|
|
78
|
+
if (this._fallbackParent) {
|
|
79
|
+
inj = this._fallbackParent;
|
|
80
|
+
const resolvedFallbackValue = getObj();
|
|
81
|
+
if (resolvedFallbackValue !== NOT_FOUND) {
|
|
82
|
+
return resolvedFallbackValue;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (inj !== null) {
|
|
86
|
+
return inj.get(key.token, notFoundValue);
|
|
87
|
+
}
|
|
88
|
+
return this._throwOrNull(key, notFoundValue);
|
|
89
|
+
}
|
|
90
|
+
_isObjectDefinedByKeyId(keyId) {
|
|
91
|
+
for (let i = 0; i < this._keyIds.length; i++) {
|
|
92
|
+
if (this._keyIds[i] === keyId) {
|
|
93
|
+
return this._objs[i] !== UNDEFINED;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
_getObjByKeyId(keyId) {
|
|
99
|
+
var _a, _b;
|
|
100
|
+
if ((_a = this._globalProvidersMap) === null || _a === void 0 ? void 0 : _a.has(keyId)) {
|
|
101
|
+
return (_b = this._globalProvidersMap.get(keyId)) === null || _b === void 0 ? void 0 : _b._getObjByKeyId(keyId);
|
|
102
|
+
}
|
|
103
|
+
for (let i = 0; i < this._keyIds.length; i++) {
|
|
104
|
+
if (this._keyIds[i] === keyId) {
|
|
105
|
+
if (this._objs[i] === UNDEFINED) {
|
|
106
|
+
this._objs[i] = this._new(this._providers[i]);
|
|
107
|
+
}
|
|
108
|
+
return this._objs[i];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return UNDEFINED;
|
|
112
|
+
}
|
|
113
|
+
_throwOrNull(key, notFoundValue) {
|
|
114
|
+
if (notFoundValue !== _THROW_IF_NOT_FOUND) {
|
|
115
|
+
return notFoundValue;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
throw (0, errors_1.noProviderError)(this, key);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
instantiateAll() {
|
|
122
|
+
this._providers.forEach((provider) => {
|
|
123
|
+
this._getByKey(provider.key, _THROW_IF_NOT_FOUND);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
_instantiateProvider(provider) {
|
|
127
|
+
const factory = provider.factory.factory;
|
|
128
|
+
let deps;
|
|
129
|
+
try {
|
|
130
|
+
deps = provider.factory.dependencies.map((dep) => this._getByDependency(dep));
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
if (e.addKey) {
|
|
134
|
+
e.addKey(provider.key);
|
|
135
|
+
}
|
|
136
|
+
throw e;
|
|
137
|
+
}
|
|
138
|
+
let obj;
|
|
139
|
+
try {
|
|
140
|
+
obj = factory(...deps);
|
|
141
|
+
// attach execution context getter
|
|
142
|
+
if (provider.factory.executionContextIn.length > 0) {
|
|
143
|
+
for (const prop of provider.factory.executionContextIn) {
|
|
144
|
+
Object.defineProperty(obj, prop, {
|
|
145
|
+
get: () => {
|
|
146
|
+
return this._executionContextGetter();
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
throw (0, errors_1.instantiationError)(this, e, provider.key);
|
|
154
|
+
}
|
|
155
|
+
return obj;
|
|
156
|
+
}
|
|
157
|
+
_getByDependency(dep) {
|
|
158
|
+
return this._getByKey(dep.key, dep.optional ? null : _THROW_IF_NOT_FOUND);
|
|
159
|
+
}
|
|
160
|
+
_new(provider) {
|
|
161
|
+
if (this._constructionCounter++ > this._getMaxNumberOfObjects()) {
|
|
162
|
+
throw (0, errors_1.cyclicDependencyError)(this, provider.key);
|
|
163
|
+
}
|
|
164
|
+
return this._instantiateProvider(provider);
|
|
165
|
+
}
|
|
166
|
+
_getMaxNumberOfObjects() {
|
|
167
|
+
return this._objs.length;
|
|
168
|
+
}
|
|
169
|
+
toString() {
|
|
170
|
+
return this.displayName;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.ReflectiveInjector = ReflectiveInjector;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INJECTABLE = void 0;
|
|
4
|
+
exports.readInjectableMetadata = readInjectableMetadata;
|
|
5
|
+
exports.ensureInjectableMetadata = ensureInjectableMetadata;
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
exports.INJECTABLE = Symbol('di:injectable');
|
|
8
|
+
function readInjectableMetadata(type, throwOnMissing) {
|
|
9
|
+
const meta = type[exports.INJECTABLE];
|
|
10
|
+
if (!meta && throwOnMissing) {
|
|
11
|
+
throw (0, errors_1.noInjectableError)(type);
|
|
12
|
+
}
|
|
13
|
+
return meta;
|
|
14
|
+
}
|
|
15
|
+
function ensureInjectableMetadata(type) {
|
|
16
|
+
if (!readInjectableMetadata(type)) {
|
|
17
|
+
const meta = {
|
|
18
|
+
params: [],
|
|
19
|
+
};
|
|
20
|
+
type[exports.INJECTABLE] = meta;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Scope = exports.InjectionToken = exports.Type = void 0;
|
|
4
|
+
exports.isToken = isToken;
|
|
5
|
+
exports.isType = isType;
|
|
6
|
+
exports.onlySingletonProviders = onlySingletonProviders;
|
|
7
|
+
exports.onlyOperationProviders = onlyOperationProviders;
|
|
8
|
+
exports.isClassProvider = isClassProvider;
|
|
9
|
+
exports.isFactoryProvider = isFactoryProvider;
|
|
10
|
+
const metadata_1 = require("./metadata");
|
|
11
|
+
exports.Type = Function;
|
|
12
|
+
/// @ts-ignore
|
|
13
|
+
class InjectionToken {
|
|
14
|
+
constructor(_desc) {
|
|
15
|
+
this._desc = _desc;
|
|
16
|
+
}
|
|
17
|
+
toString() {
|
|
18
|
+
return `InjectionToken ${this._desc}`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.InjectionToken = InjectionToken;
|
|
22
|
+
function isToken(v) {
|
|
23
|
+
return v && v instanceof InjectionToken;
|
|
24
|
+
}
|
|
25
|
+
function isType(v) {
|
|
26
|
+
return typeof v === 'function' && v !== Object;
|
|
27
|
+
}
|
|
28
|
+
var Scope;
|
|
29
|
+
(function (Scope) {
|
|
30
|
+
Scope[Scope["Singleton"] = 0] = "Singleton";
|
|
31
|
+
Scope[Scope["Operation"] = 1] = "Operation";
|
|
32
|
+
})(Scope || (exports.Scope = Scope = {}));
|
|
33
|
+
function onlySingletonProviders(providers = []) {
|
|
34
|
+
return providers.filter((provider) => {
|
|
35
|
+
if (isType(provider)) {
|
|
36
|
+
const { options } = (0, metadata_1.readInjectableMetadata)(provider, true);
|
|
37
|
+
return (options === null || options === void 0 ? void 0 : options.scope) !== Scope.Operation;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return provider.scope !== Scope.Operation;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function onlyOperationProviders(providers = []) {
|
|
45
|
+
return providers.filter((provider) => {
|
|
46
|
+
if (isType(provider)) {
|
|
47
|
+
const { options } = (0, metadata_1.readInjectableMetadata)(provider, true);
|
|
48
|
+
return (options === null || options === void 0 ? void 0 : options.scope) === Scope.Operation;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return provider.scope === Scope.Operation;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function isClassProvider(provider) {
|
|
56
|
+
return typeof provider.useClass !== 'undefined';
|
|
57
|
+
}
|
|
58
|
+
function isFactoryProvider(provider) {
|
|
59
|
+
return typeof provider.useFactory !== 'undefined';
|
|
60
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Key = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const forward_ref_1 = require("./forward-ref");
|
|
6
|
+
class Key {
|
|
7
|
+
constructor(token, id) {
|
|
8
|
+
this.token = token;
|
|
9
|
+
this.id = id;
|
|
10
|
+
if (!token) {
|
|
11
|
+
throw new Error('Token must be defined!');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns a stringified token.
|
|
16
|
+
*/
|
|
17
|
+
get displayName() {
|
|
18
|
+
return (0, utils_1.stringify)(this.token);
|
|
19
|
+
}
|
|
20
|
+
static get(token) {
|
|
21
|
+
return _globalKeyRegistry.get((0, forward_ref_1.resolveForwardRef)(token));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.Key = Key;
|
|
25
|
+
class GlobalKeyRegistry {
|
|
26
|
+
constructor() {
|
|
27
|
+
this._allKeys = new Map();
|
|
28
|
+
}
|
|
29
|
+
get(token) {
|
|
30
|
+
if (token instanceof Key) {
|
|
31
|
+
return token;
|
|
32
|
+
}
|
|
33
|
+
if (this._allKeys.has(token)) {
|
|
34
|
+
return this._allKeys.get(token);
|
|
35
|
+
}
|
|
36
|
+
const newKey = new Key(token, _globalKeyRegistry.numberOfKeys);
|
|
37
|
+
this._allKeys.set(token, newKey);
|
|
38
|
+
return newKey;
|
|
39
|
+
}
|
|
40
|
+
get numberOfKeys() {
|
|
41
|
+
return this._allKeys.size;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const _globalKeyRegistry = new GlobalKeyRegistry();
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Dependency = exports.ResolvedFactory = exports.ResolvedProvider = void 0;
|
|
4
|
+
exports.resolveProviders = resolveProviders;
|
|
5
|
+
const providers_1 = require("./providers");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const registry_1 = require("./registry");
|
|
8
|
+
const forward_ref_1 = require("./forward-ref");
|
|
9
|
+
const metadata_1 = require("./metadata");
|
|
10
|
+
const _EMPTY_LIST = [];
|
|
11
|
+
class ResolvedProvider {
|
|
12
|
+
constructor(key, factory) {
|
|
13
|
+
this.key = key;
|
|
14
|
+
this.factory = factory;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ResolvedProvider = ResolvedProvider;
|
|
18
|
+
class ResolvedFactory {
|
|
19
|
+
constructor(
|
|
20
|
+
/**
|
|
21
|
+
* Factory function which can return an instance of an object represented by a key.
|
|
22
|
+
*/
|
|
23
|
+
factory,
|
|
24
|
+
/**
|
|
25
|
+
* Arguments (dependencies) to the `factory` function.
|
|
26
|
+
*/
|
|
27
|
+
dependencies,
|
|
28
|
+
/**
|
|
29
|
+
* Methods invoked within ExecutionContext.
|
|
30
|
+
*/
|
|
31
|
+
executionContextIn,
|
|
32
|
+
/**
|
|
33
|
+
* Has onDestroy hook
|
|
34
|
+
*/
|
|
35
|
+
hasOnDestroyHook,
|
|
36
|
+
/**
|
|
37
|
+
* Is Global
|
|
38
|
+
*/
|
|
39
|
+
isGlobal) {
|
|
40
|
+
this.factory = factory;
|
|
41
|
+
this.dependencies = dependencies;
|
|
42
|
+
this.executionContextIn = executionContextIn;
|
|
43
|
+
this.hasOnDestroyHook = hasOnDestroyHook;
|
|
44
|
+
this.isGlobal = isGlobal;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ResolvedFactory = ResolvedFactory;
|
|
48
|
+
class Dependency {
|
|
49
|
+
constructor(key, optional) {
|
|
50
|
+
this.key = key;
|
|
51
|
+
this.optional = optional;
|
|
52
|
+
}
|
|
53
|
+
static fromKey(key) {
|
|
54
|
+
return new Dependency(key, false);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.Dependency = Dependency;
|
|
58
|
+
function resolveProviders(providers) {
|
|
59
|
+
const normalized = normalizeProviders(providers, []);
|
|
60
|
+
const resolved = normalized.map(resolveProvider);
|
|
61
|
+
const resolvedProviderMap = mergeResolvedProviders(resolved, new Map());
|
|
62
|
+
return Array.from(resolvedProviderMap.values());
|
|
63
|
+
}
|
|
64
|
+
function resolveProvider(provider) {
|
|
65
|
+
return new ResolvedProvider(registry_1.Key.get(provider.provide), resolveFactory(provider));
|
|
66
|
+
}
|
|
67
|
+
function mergeResolvedProviders(providers, normalizedProvidersMap) {
|
|
68
|
+
for (let i = 0; i < providers.length; i++) {
|
|
69
|
+
const provider = providers[i];
|
|
70
|
+
normalizedProvidersMap.set(provider.key.id, provider);
|
|
71
|
+
}
|
|
72
|
+
return normalizedProvidersMap;
|
|
73
|
+
}
|
|
74
|
+
function normalizeProviders(providers, res) {
|
|
75
|
+
providers.forEach((token) => {
|
|
76
|
+
if (token instanceof providers_1.Type) {
|
|
77
|
+
res.push({ provide: token, useClass: token });
|
|
78
|
+
}
|
|
79
|
+
else if (token &&
|
|
80
|
+
typeof token === 'object' &&
|
|
81
|
+
token.provide !== undefined) {
|
|
82
|
+
res.push(token);
|
|
83
|
+
}
|
|
84
|
+
else if (token instanceof Array) {
|
|
85
|
+
normalizeProviders(token, res);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw (0, errors_1.invalidProviderError)(token);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
return res;
|
|
92
|
+
}
|
|
93
|
+
function resolveFactory(provider) {
|
|
94
|
+
let factoryFn;
|
|
95
|
+
let resolvedDeps = _EMPTY_LIST;
|
|
96
|
+
let executionContextIn = _EMPTY_LIST;
|
|
97
|
+
let hasOnDestroyHook = false;
|
|
98
|
+
let isGlobal;
|
|
99
|
+
if ((0, providers_1.isClassProvider)(provider)) {
|
|
100
|
+
const useClass = (0, forward_ref_1.resolveForwardRef)(provider.useClass);
|
|
101
|
+
factoryFn = makeFactory(useClass);
|
|
102
|
+
resolvedDeps = dependenciesFor(useClass);
|
|
103
|
+
executionContextIn = executionContextInFor(useClass);
|
|
104
|
+
isGlobal = globalFor(useClass);
|
|
105
|
+
hasOnDestroyHook = typeof useClass.prototype.onDestroy === 'function';
|
|
106
|
+
}
|
|
107
|
+
else if ((0, providers_1.isFactoryProvider)(provider)) {
|
|
108
|
+
factoryFn = provider.useFactory;
|
|
109
|
+
resolvedDeps = constructDependencies(provider.useFactory, provider.deps || []);
|
|
110
|
+
isGlobal = provider.global;
|
|
111
|
+
if (provider.executionContextIn) {
|
|
112
|
+
executionContextIn = provider.executionContextIn;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
factoryFn = () => provider.useValue;
|
|
117
|
+
resolvedDeps = _EMPTY_LIST;
|
|
118
|
+
isGlobal = provider.global;
|
|
119
|
+
}
|
|
120
|
+
return new ResolvedFactory(factoryFn, resolvedDeps, executionContextIn, hasOnDestroyHook, isGlobal !== null && isGlobal !== void 0 ? isGlobal : false);
|
|
121
|
+
}
|
|
122
|
+
function dependenciesFor(type) {
|
|
123
|
+
const { params } = (0, metadata_1.readInjectableMetadata)(type, true);
|
|
124
|
+
if (!params) {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
if (params.some((p) => p.type == null)) {
|
|
128
|
+
throw (0, errors_1.noAnnotationError)(type, params);
|
|
129
|
+
}
|
|
130
|
+
return params.map((p) => extractToken(p, params));
|
|
131
|
+
}
|
|
132
|
+
function executionContextInFor(type) {
|
|
133
|
+
const { options } = (0, metadata_1.readInjectableMetadata)(type, true);
|
|
134
|
+
if ((options === null || options === void 0 ? void 0 : options.executionContextIn) &&
|
|
135
|
+
options.executionContextIn !== _EMPTY_LIST) {
|
|
136
|
+
return options === null || options === void 0 ? void 0 : options.executionContextIn;
|
|
137
|
+
}
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
function globalFor(type) {
|
|
141
|
+
var _a;
|
|
142
|
+
const { options } = (0, metadata_1.readInjectableMetadata)(type);
|
|
143
|
+
return (_a = options === null || options === void 0 ? void 0 : options.global) !== null && _a !== void 0 ? _a : false;
|
|
144
|
+
}
|
|
145
|
+
function constructDependencies(typeOrFunc, dependencies) {
|
|
146
|
+
if (!dependencies) {
|
|
147
|
+
return dependenciesFor(typeOrFunc);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
const params = dependencies.map((d) => ({ type: d, optional: false }));
|
|
151
|
+
return params.map((t) => extractToken(t, params));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function extractToken(param, params) {
|
|
155
|
+
const token = (0, forward_ref_1.resolveForwardRef)(param.type);
|
|
156
|
+
if (token) {
|
|
157
|
+
return createDependency(token, param.optional);
|
|
158
|
+
}
|
|
159
|
+
throw (0, errors_1.noAnnotationError)(param.type, params);
|
|
160
|
+
}
|
|
161
|
+
function createDependency(token, optional) {
|
|
162
|
+
return new Dependency(registry_1.Key.get(token), optional);
|
|
163
|
+
}
|
|
164
|
+
function makeFactory(t) {
|
|
165
|
+
return (...args) => new t(...args);
|
|
166
|
+
}
|
package/cjs/di/utils.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ERROR_LOGGER = exports.ERROR_ORIGINAL_ERROR = exports.ERROR_TYPE = void 0;
|
|
4
|
+
exports.getType = getType;
|
|
5
|
+
exports.getOriginalError = getOriginalError;
|
|
6
|
+
exports.getErrorLogger = getErrorLogger;
|
|
7
|
+
exports.wrappedError = wrappedError;
|
|
8
|
+
exports.stringify = stringify;
|
|
9
|
+
exports.ERROR_TYPE = 'diType';
|
|
10
|
+
exports.ERROR_ORIGINAL_ERROR = 'diOriginalError';
|
|
11
|
+
exports.ERROR_LOGGER = 'diErrorLogger';
|
|
12
|
+
function getType(error) {
|
|
13
|
+
return error[exports.ERROR_TYPE];
|
|
14
|
+
}
|
|
15
|
+
function getOriginalError(error) {
|
|
16
|
+
return error[exports.ERROR_ORIGINAL_ERROR];
|
|
17
|
+
}
|
|
18
|
+
function defaultErrorLogger(console, ...values) {
|
|
19
|
+
// eslint-disable-next-line no-console
|
|
20
|
+
console.error(...values);
|
|
21
|
+
}
|
|
22
|
+
function getErrorLogger(error) {
|
|
23
|
+
return error[exports.ERROR_LOGGER] || defaultErrorLogger;
|
|
24
|
+
}
|
|
25
|
+
function wrappedError(message, originalError) {
|
|
26
|
+
const msg = `${message} caused by: ${originalError instanceof Error ? originalError.message : originalError}`;
|
|
27
|
+
const error = Error(msg);
|
|
28
|
+
error[exports.ERROR_ORIGINAL_ERROR] = originalError;
|
|
29
|
+
return error;
|
|
30
|
+
}
|
|
31
|
+
function stringify(token) {
|
|
32
|
+
if (typeof token === 'string') {
|
|
33
|
+
return token;
|
|
34
|
+
}
|
|
35
|
+
if (token == null) {
|
|
36
|
+
return '' + token;
|
|
37
|
+
}
|
|
38
|
+
if (token.name) {
|
|
39
|
+
return `${token.name}`;
|
|
40
|
+
}
|
|
41
|
+
const res = token.toString();
|
|
42
|
+
const newLineIndex = res.indexOf('\n');
|
|
43
|
+
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
|
44
|
+
}
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gql = exports.Scope = exports.InjectionToken = exports.forwardRef = exports.ExecutionContext = exports.Optional = exports.Injectable = exports.Inject = exports.Injector = exports.createModule = exports.createApplication = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
// application
|
|
6
|
+
var application_1 = require("./application/application");
|
|
7
|
+
Object.defineProperty(exports, "createApplication", { enumerable: true, get: function () { return application_1.createApplication; } });
|
|
8
|
+
tslib_1.__exportStar(require("./application/tokens"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./application/types"), exports);
|
|
10
|
+
// modules
|
|
11
|
+
var module_1 = require("./module/module");
|
|
12
|
+
Object.defineProperty(exports, "createModule", { enumerable: true, get: function () { return module_1.createModule; } });
|
|
13
|
+
tslib_1.__exportStar(require("./module/types"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./module/metadata"), exports);
|
|
15
|
+
tslib_1.__exportStar(require("./module/tokens"), exports);
|
|
16
|
+
// di
|
|
17
|
+
var di_1 = require("./di");
|
|
18
|
+
Object.defineProperty(exports, "Injector", { enumerable: true, get: function () { return di_1.Injector; } });
|
|
19
|
+
Object.defineProperty(exports, "Inject", { enumerable: true, get: function () { return di_1.Inject; } });
|
|
20
|
+
Object.defineProperty(exports, "Injectable", { enumerable: true, get: function () { return di_1.Injectable; } });
|
|
21
|
+
Object.defineProperty(exports, "Optional", { enumerable: true, get: function () { return di_1.Optional; } });
|
|
22
|
+
Object.defineProperty(exports, "ExecutionContext", { enumerable: true, get: function () { return di_1.ExecutionContext; } });
|
|
23
|
+
Object.defineProperty(exports, "forwardRef", { enumerable: true, get: function () { return di_1.forwardRef; } });
|
|
24
|
+
Object.defineProperty(exports, "InjectionToken", { enumerable: true, get: function () { return di_1.InjectionToken; } });
|
|
25
|
+
Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return di_1.Scope; } });
|
|
26
|
+
require("./shared/types");
|
|
27
|
+
var gql_1 = require("./shared/gql");
|
|
28
|
+
Object.defineProperty(exports, "gql", { enumerable: true, get: function () { return gql_1.gql; } });
|
|
29
|
+
tslib_1.__exportStar(require("./shared/di"), exports);
|
|
30
|
+
// testing
|
|
31
|
+
tslib_1.__exportStar(require("./testing"), exports);
|