@homer0/jimple 4.0.0 → 5.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.
- package/dist/index.js +1 -506
- package/dist/index.js.map +1 -1
- package/package.json +25 -13
- package/src/factories/factories.types.ts +34 -0
- package/src/factories/index.ts +4 -0
- package/src/factories/resourceCreatorFactory.ts +102 -0
- package/src/factories/resourceFactory.ts +49 -0
- package/src/factories/resourcesCollectionFactory.ts +108 -0
- package/src/helpers/index.ts +4 -0
- package/src/helpers/injectHelper.ts +124 -0
- package/src/helpers/provider.ts +42 -0
- package/src/helpers/providerCreator.ts +33 -0
- package/src/helpers/providers.ts +32 -0
- package/src/index.ts +3 -0
- package/src/jimple/index.ts +28 -0
- package/src/jimplemod/index.ts +8 -0
- package/src/jimplemod/jimple.type.ts +93 -0
- package/dist/esm/index.js +0 -481
- package/dist/esm/index.js.map +0 -1
- package/dist/index.d.mts +0 -371
- package/dist/index.d.ts +0 -371
package/dist/index.js
CHANGED
|
@@ -1,507 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
-
};
|
|
11
|
-
var __export = (target, all) => {
|
|
12
|
-
for (var name in all)
|
|
13
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
-
};
|
|
15
|
-
var __copyProps = (to, from, except, desc) => {
|
|
16
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
-
for (let key of __getOwnPropNames(from))
|
|
18
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
-
mod
|
|
30
|
-
));
|
|
31
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
-
|
|
33
|
-
// ../../../node_modules/.pnpm/jimple@1.5.0/node_modules/jimple/src/Jimple.js
|
|
34
|
-
var require_Jimple = __commonJS({
|
|
35
|
-
"../../../node_modules/.pnpm/jimple@1.5.0/node_modules/jimple/src/Jimple.js"(exports2, module2) {
|
|
36
|
-
"use strict";
|
|
37
|
-
function assert(ok, message) {
|
|
38
|
-
if (!ok) {
|
|
39
|
-
throw new Error(message);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function isFunction(fn) {
|
|
43
|
-
return Object.prototype.toString.call(fn) === "[object Function]" && fn.constructor.name === "Function";
|
|
44
|
-
}
|
|
45
|
-
function isPlainObject(value) {
|
|
46
|
-
if (Object.prototype.toString.call(value) !== "[object Object]") {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
let prototype = Object.getPrototypeOf(value);
|
|
50
|
-
return prototype === null || prototype === Object.prototype;
|
|
51
|
-
}
|
|
52
|
-
function checkDefined(container, key) {
|
|
53
|
-
assert(container.has(key), `Identifier "${key}" is not defined.`);
|
|
54
|
-
}
|
|
55
|
-
function addFunctionTo(set, fn) {
|
|
56
|
-
assert(isFunction(fn), "Service definition is not a Closure or invokable object");
|
|
57
|
-
set.add(fn);
|
|
58
|
-
return fn;
|
|
59
|
-
}
|
|
60
|
-
var Jimple3 = class {
|
|
61
|
-
static provider(register) {
|
|
62
|
-
return { register };
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Create a Jimple Container.
|
|
66
|
-
* @param {Object?} [values] - An optional object whose keys and values will be associated in the container at initialization
|
|
67
|
-
*/
|
|
68
|
-
constructor(values) {
|
|
69
|
-
this._items = {};
|
|
70
|
-
this._instances = /* @__PURE__ */ new Map();
|
|
71
|
-
this._factories = /* @__PURE__ */ new Set();
|
|
72
|
-
this._protected = /* @__PURE__ */ new Set();
|
|
73
|
-
values = isPlainObject(values) ? values : {};
|
|
74
|
-
Object.keys(values).forEach(function(key) {
|
|
75
|
-
this.set(key, values[key]);
|
|
76
|
-
}, this);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Return the specified parameter or service. If the service is not built yet, this function will construct the service
|
|
80
|
-
* injecting all the dependencies needed in it's definition so the returned object is totally usable on the fly.
|
|
81
|
-
* @param {string} key - The key of the parameter or service to return
|
|
82
|
-
* @return {*} The object related to the service or the value of the parameter associated with the key informed
|
|
83
|
-
* @throws If the key does not exist
|
|
84
|
-
*/
|
|
85
|
-
get(key) {
|
|
86
|
-
checkDefined(this, key);
|
|
87
|
-
let item = this._items[key];
|
|
88
|
-
let obj;
|
|
89
|
-
if (isFunction(item)) {
|
|
90
|
-
if (this._protected.has(item)) {
|
|
91
|
-
obj = item;
|
|
92
|
-
} else if (this._instances.has(item)) {
|
|
93
|
-
obj = this._instances.get(item);
|
|
94
|
-
} else {
|
|
95
|
-
obj = item(this);
|
|
96
|
-
if (!this._factories.has(item)) {
|
|
97
|
-
this._instances.set(item, obj);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
} else {
|
|
101
|
-
obj = item;
|
|
102
|
-
}
|
|
103
|
-
return obj;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Defines a new parameter or service.
|
|
107
|
-
* @param {string} key - The key of the parameter or service to be defined
|
|
108
|
-
* @param {*} value - The value of the parameter or a function that receives the container as parameter and constructs the service
|
|
109
|
-
*/
|
|
110
|
-
set(key, value) {
|
|
111
|
-
this._items[key] = value;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Returns if a service or parameter with the informed key is already defined in the container.
|
|
115
|
-
* @param {string} key - The key of the parameter or service to be checked.
|
|
116
|
-
* @return {boolean} If the key exists in the container or not
|
|
117
|
-
*/
|
|
118
|
-
has(key) {
|
|
119
|
-
return this._items.hasOwnProperty(key);
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Defines a service as a factory, so the instances are not cached in the service and that function is always called
|
|
123
|
-
* @param {function(Jimple):*} fn - The function that constructs the service that is a factory
|
|
124
|
-
* @return {function(Jimple):*} The same function passed as parameter
|
|
125
|
-
*/
|
|
126
|
-
factory(fn) {
|
|
127
|
-
return addFunctionTo(this._factories, fn);
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Defines a function as a parameter, so that function is not considered a service
|
|
131
|
-
* @param {*} fn - The function to not be considered a service
|
|
132
|
-
* @return {*} The same function passed as parameter
|
|
133
|
-
*/
|
|
134
|
-
protect(fn) {
|
|
135
|
-
return addFunctionTo(this._protected, fn);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Return all the keys registered in the container
|
|
139
|
-
* @returns {Array<string>}
|
|
140
|
-
*/
|
|
141
|
-
keys() {
|
|
142
|
-
return Object.keys(this._items);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Extends a service already registered in the container
|
|
146
|
-
* @param {string} key - The key of the service to be extended
|
|
147
|
-
* @param {function(*, Jimple):*} fn - The function that will be used to extend the service
|
|
148
|
-
* @throws If the key is not already defined
|
|
149
|
-
* @throws If the key saved in the container does not correspond to a service
|
|
150
|
-
* @throws If the function passed it not...well, a function
|
|
151
|
-
*/
|
|
152
|
-
extend(key, fn) {
|
|
153
|
-
checkDefined(this, key);
|
|
154
|
-
let originalItem = this._items[key];
|
|
155
|
-
assert(isFunction(originalItem) && this._protected.has(originalItem) === false, `Identifier '${key}' does not contain a service definition`);
|
|
156
|
-
assert(isFunction(fn), `The 'new' service definition for '${key}' is not a invokable object.`);
|
|
157
|
-
this._items[key] = function(app) {
|
|
158
|
-
return fn(originalItem(app), app);
|
|
159
|
-
};
|
|
160
|
-
if (this._factories.has(originalItem)) {
|
|
161
|
-
this._factories.delete(originalItem);
|
|
162
|
-
this._factories.add(this._items[key]);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Uses an provider to extend the service, so it's easy to split the service and parameter definitions across the system
|
|
167
|
-
* @param {{register: function(Jimple)}} provider - The provider to be used to register services and parameters in this container
|
|
168
|
-
*/
|
|
169
|
-
register(provider2) {
|
|
170
|
-
provider2.register(this);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Returns the raw value of a service or parameter. So, in the case of a service, for example, the value returned is the
|
|
174
|
-
* function used to construct the service.
|
|
175
|
-
* @param {string} key - The key of the service or parameter to return.
|
|
176
|
-
* @throws If the key does not exist in the container
|
|
177
|
-
* @return {*} The raw value of the service or parameter
|
|
178
|
-
*/
|
|
179
|
-
raw(key) {
|
|
180
|
-
checkDefined(this, key);
|
|
181
|
-
return this._items[key];
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
module2.exports = Jimple3;
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
// src/index.ts
|
|
189
|
-
var index_exports = {};
|
|
190
|
-
__export(index_exports, {
|
|
191
|
-
InjectHelper: () => InjectHelper,
|
|
192
|
-
Jimple: () => Jimple2,
|
|
193
|
-
createProvider: () => createProvider,
|
|
194
|
-
createProviderCreator: () => createProviderCreator,
|
|
195
|
-
createProviders: () => createProviders,
|
|
196
|
-
injectHelper: () => injectHelper,
|
|
197
|
-
jimple: () => jimple,
|
|
198
|
-
provider: () => provider,
|
|
199
|
-
providerCreator: () => providerCreator,
|
|
200
|
-
providers: () => providers,
|
|
201
|
-
resourceCreatorFactory: () => resourceCreatorFactory,
|
|
202
|
-
resourceFactory: () => resourceFactory,
|
|
203
|
-
resourcesCollectionFactory: () => resourcesCollectionFactory
|
|
204
|
-
});
|
|
205
|
-
module.exports = __toCommonJS(index_exports);
|
|
206
|
-
|
|
207
|
-
// src/jimplemod/index.ts
|
|
208
|
-
var import_jimple = __toESM(require_Jimple());
|
|
209
|
-
var Jimple = import_jimple.default;
|
|
210
|
-
|
|
211
|
-
// src/jimple/index.ts
|
|
212
|
-
var Jimple2 = class extends Jimple {
|
|
213
|
-
/**
|
|
214
|
-
* This is a version of the `get` method that doesn't throw if the key doesn't exist. It
|
|
215
|
-
* will return `undefined` instead.
|
|
216
|
-
*
|
|
217
|
-
* @param key The key of the parameter or service to return.
|
|
218
|
-
* @returns The object related to the service or the value of the parameter
|
|
219
|
-
* associated with the given key.
|
|
220
|
-
* @template T The type of the returned object.
|
|
221
|
-
*/
|
|
222
|
-
try(key) {
|
|
223
|
-
if (!this.has(key)) {
|
|
224
|
-
return void 0;
|
|
225
|
-
}
|
|
226
|
-
return this.get(key);
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
var jimple = (...args) => new Jimple2(...args);
|
|
230
|
-
|
|
231
|
-
// src/factories/resourceFactory.ts
|
|
232
|
-
var resourceFactory = () => (
|
|
233
|
-
/**
|
|
234
|
-
* Generates a new resource using the contraint defined in the factory.
|
|
235
|
-
*
|
|
236
|
-
* @param name The name of the resource.
|
|
237
|
-
* @param key The property key for the function.
|
|
238
|
-
* @param fn The resource function.
|
|
239
|
-
* @returns An object with the `name` set to `true`, and the `fn` as the `key`.
|
|
240
|
-
* @template Name The literal type of `name`, to be used in the return object.
|
|
241
|
-
* @template Key The literal type of `key`, to be used in the return object.
|
|
242
|
-
* @template Fn The type of `fn`, restricted by the factory constraint.
|
|
243
|
-
* @example
|
|
244
|
-
*
|
|
245
|
-
* const myAction = factory('action', 'fn', (c) => {
|
|
246
|
-
* c.set('foo', 'bar');
|
|
247
|
-
* });
|
|
248
|
-
*
|
|
249
|
-
*/
|
|
250
|
-
(name, key, fn) => ({
|
|
251
|
-
[name]: true,
|
|
252
|
-
[key]: fn
|
|
253
|
-
})
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
// src/factories/resourceCreatorFactory.ts
|
|
257
|
-
var resourceCreatorFactory = () => (
|
|
258
|
-
/**
|
|
259
|
-
* Generates a new resource creator using the contraint defined in the factory.
|
|
260
|
-
*
|
|
261
|
-
* @param name The name of the resource.
|
|
262
|
-
* @param key The property key for the function.
|
|
263
|
-
* @param creatorFn The curried function that returns the actual resource function.
|
|
264
|
-
* @returns A resource creator: an object that can be used as a resource, and as a
|
|
265
|
-
* function that returns a resource.
|
|
266
|
-
* @template Name The literal type of `name`, to be used in the return object.
|
|
267
|
-
* @template Key The literal type of `key`, to be used in the return object.
|
|
268
|
-
* @template ResFn The type of the resource function, the creator function needs
|
|
269
|
-
* to return. This is restricted by the factory constraint.
|
|
270
|
-
* @template CreatorFn The literal type of `creatorFn`, to be used in the return
|
|
271
|
-
* object.
|
|
272
|
-
* @example
|
|
273
|
-
*
|
|
274
|
-
* const myAction = factory('action', 'fn', (name = 'foo') => (c) => {
|
|
275
|
-
* c.set(name, 'bar');
|
|
276
|
-
* });
|
|
277
|
-
*
|
|
278
|
-
*/
|
|
279
|
-
(name, key, creatorFn) => {
|
|
280
|
-
const fnToProxy = (...args) => {
|
|
281
|
-
const actualResource = creatorFn(...args);
|
|
282
|
-
return resourceFactory()(name, key, actualResource);
|
|
283
|
-
};
|
|
284
|
-
const handler = {
|
|
285
|
-
name,
|
|
286
|
-
resource: null,
|
|
287
|
-
get(target, property) {
|
|
288
|
-
let result;
|
|
289
|
-
if (property === this.name) {
|
|
290
|
-
result = true;
|
|
291
|
-
} else if (property === key) {
|
|
292
|
-
if (this.resource === null) {
|
|
293
|
-
const newResource = creatorFn();
|
|
294
|
-
this.resource = newResource;
|
|
295
|
-
}
|
|
296
|
-
result = this.resource;
|
|
297
|
-
} else {
|
|
298
|
-
const targetKey = property;
|
|
299
|
-
result = target[targetKey];
|
|
300
|
-
}
|
|
301
|
-
return result;
|
|
302
|
-
},
|
|
303
|
-
has(target, property) {
|
|
304
|
-
if (property === this.name || property === key) {
|
|
305
|
-
return true;
|
|
306
|
-
}
|
|
307
|
-
const targetKey = property;
|
|
308
|
-
return targetKey in target;
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
return new Proxy(fnToProxy, handler);
|
|
312
|
-
}
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
// src/factories/resourcesCollectionFactory.ts
|
|
316
|
-
var resourcesCollectionFactory = () => (
|
|
317
|
-
/**
|
|
318
|
-
* Generates a function to create a collection of resources of an specified type. A
|
|
319
|
-
* collection is a dictionary of resources, and a resource itself, and when its
|
|
320
|
-
* "resource function" gets called, it calls the function of every resource (with the
|
|
321
|
-
* same args it recevied).
|
|
322
|
-
*
|
|
323
|
-
* @param name The resource name the items in the collection must have.
|
|
324
|
-
* @param key The key property the items in the collection must have.
|
|
325
|
-
* @param fn A custom function to process the items in the collection, when the
|
|
326
|
-
* collection function gets called.
|
|
327
|
-
* @returns A function that can be used to create a resources collection.
|
|
328
|
-
* @template Name The literal type of `name`, to be used in the return object.
|
|
329
|
-
* @template Key The literal type of `key`, to be used in the return object.
|
|
330
|
-
* @template ItemKey To capture the name of the resources in the collection.
|
|
331
|
-
* @template Items The kind of dictionary of resources the return function will
|
|
332
|
-
* expect.
|
|
333
|
-
* @template CustomFn The type of `fn`, restricted by the factory constraint.
|
|
334
|
-
* @example
|
|
335
|
-
*
|
|
336
|
-
* const myActions = factory('action', 'fn')({ myActionA, myActionB });
|
|
337
|
-
*
|
|
338
|
-
*/
|
|
339
|
-
(name, key, fn) => {
|
|
340
|
-
const collectionResourceFactory = resourceFactory();
|
|
341
|
-
return (items) => {
|
|
342
|
-
const invalidKeys = [name, key];
|
|
343
|
-
const itemsKeys = Object.keys(items);
|
|
344
|
-
const invalidKey = itemsKeys.some(
|
|
345
|
-
(itemKey) => invalidKeys.includes(String(itemKey))
|
|
346
|
-
);
|
|
347
|
-
if (invalidKey) {
|
|
348
|
-
throw new Error(
|
|
349
|
-
`No item on the collection can have the keys \`${name}\` nor \`${key}\``
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
const invalidItem = itemsKeys.find(
|
|
353
|
-
(itemKey) => typeof items[itemKey]?.[key] !== "function"
|
|
354
|
-
);
|
|
355
|
-
if (invalidItem) {
|
|
356
|
-
throw new Error(
|
|
357
|
-
`The item \`${String(
|
|
358
|
-
invalidItem
|
|
359
|
-
)}\` is invalid: it doesn't have a \`${key}\` function`
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
const useFn = fn ? (...args) => fn(items, ...args) : (...args) => {
|
|
363
|
-
itemsKeys.forEach((itemK) => {
|
|
364
|
-
const item = items[itemK];
|
|
365
|
-
const itemFn = item[key];
|
|
366
|
-
itemFn(...args);
|
|
367
|
-
});
|
|
368
|
-
};
|
|
369
|
-
return {
|
|
370
|
-
...collectionResourceFactory(name, key, useFn),
|
|
371
|
-
...items
|
|
372
|
-
};
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
);
|
|
376
|
-
|
|
377
|
-
// src/helpers/injectHelper.ts
|
|
378
|
-
var InjectHelper = class {
|
|
379
|
-
/**
|
|
380
|
-
* This method is meant to be used to validate the dependencies a service receives, and
|
|
381
|
-
* needs.
|
|
382
|
-
* It will check on the received dependencies, if a specific dependency exists, it will
|
|
383
|
-
* return it, otherwise, it will create a new instance.
|
|
384
|
-
*
|
|
385
|
-
* @param dependencies The dependencies received by the implementation.
|
|
386
|
-
* @param key The key of the dependency to validate.
|
|
387
|
-
* @param init A function to create a dependency in case it doesn't exist in
|
|
388
|
-
* the dictionary.
|
|
389
|
-
* @returns An instance of the dependency.
|
|
390
|
-
* @template DepKey The literal key of the dependency to validate.
|
|
391
|
-
* @template DepType The type of the dependency, obtained from the dictionary sent
|
|
392
|
-
* to the constructor.
|
|
393
|
-
* @example
|
|
394
|
-
*
|
|
395
|
-
* type Dependencies = {
|
|
396
|
-
* dep: string;
|
|
397
|
-
* };
|
|
398
|
-
* const helper = new InjectHelper<Dependencies>();
|
|
399
|
-
*
|
|
400
|
-
* type MyServiceOptions = {
|
|
401
|
-
* inject?: Partial<Dependencies>;
|
|
402
|
-
* };
|
|
403
|
-
* const myService = ({ inject = {} }: MyServiceOptions) => {
|
|
404
|
-
* const dep = helper.get(inject, 'dep', () => 'default');
|
|
405
|
-
* console.log('dep:', dep);
|
|
406
|
-
* };
|
|
407
|
-
*
|
|
408
|
-
*/
|
|
409
|
-
get(dependencies, key, init) {
|
|
410
|
-
const existing = dependencies[key];
|
|
411
|
-
if (typeof existing !== "undefined") {
|
|
412
|
-
return existing;
|
|
413
|
-
}
|
|
414
|
-
return init();
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* This is meant to be used in a provider creator to resolve dependencies' names sent as
|
|
418
|
-
* options. For each dependency, the method will first check if a new name was
|
|
419
|
-
* specified, and try to get it with that name, otherwise, it will try to get it with
|
|
420
|
-
* the default name, and if it doesn't exist, it will just keep it as `undefined` and
|
|
421
|
-
* expect the service implements {@link InjectHelper.get} to ensure the dependency.
|
|
422
|
-
*
|
|
423
|
-
* @param dependencies The dependencies needed by the service.
|
|
424
|
-
* @param container A reference to the Jimple container.
|
|
425
|
-
* @param inject A dictionary of dependencies names.
|
|
426
|
-
* @returns A dictionary of dependencies to send to the service.
|
|
427
|
-
* @template DepKey The literal key of the dependencies to validate.
|
|
428
|
-
* @template Container The type of the Jimple container.
|
|
429
|
-
* @example
|
|
430
|
-
*
|
|
431
|
-
* type Dependencies = {
|
|
432
|
-
* dep: string;
|
|
433
|
-
* };
|
|
434
|
-
* const helper = new InjectHelper<Dependencies>();
|
|
435
|
-
*
|
|
436
|
-
* type MyProviderOptions = {
|
|
437
|
-
* services?: {
|
|
438
|
-
* [key in keyof Dependencies]?: string;
|
|
439
|
-
* };
|
|
440
|
-
* };
|
|
441
|
-
*
|
|
442
|
-
* const myProvider = providerCreator(
|
|
443
|
-
* ({ services = {} }: MyProviderOptions) =>
|
|
444
|
-
* (container) => {
|
|
445
|
-
* container.set('myService', () => {
|
|
446
|
-
* const inject = helper.resolve(['dep'], container, services);
|
|
447
|
-
* return myService({ inject });
|
|
448
|
-
* });
|
|
449
|
-
* },
|
|
450
|
-
* );
|
|
451
|
-
*
|
|
452
|
-
*/
|
|
453
|
-
resolve(dependencies, container, inject) {
|
|
454
|
-
const result = dependencies.reduce((acc, key) => {
|
|
455
|
-
if (inject[key]) {
|
|
456
|
-
acc[key] = container.get(inject[key]);
|
|
457
|
-
return acc;
|
|
458
|
-
}
|
|
459
|
-
const keyStr = key;
|
|
460
|
-
if (container.has(keyStr)) {
|
|
461
|
-
acc[key] = container.get(keyStr);
|
|
462
|
-
return acc;
|
|
463
|
-
}
|
|
464
|
-
return acc;
|
|
465
|
-
}, {});
|
|
466
|
-
return result;
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
var injectHelper = () => new InjectHelper();
|
|
470
|
-
|
|
471
|
-
// src/helpers/provider.ts
|
|
472
|
-
var createProvider = () => {
|
|
473
|
-
const factory = resourceFactory();
|
|
474
|
-
return (register) => factory("provider", "register", register);
|
|
475
|
-
};
|
|
476
|
-
var provider = createProvider();
|
|
477
|
-
|
|
478
|
-
// src/helpers/providerCreator.ts
|
|
479
|
-
var createProviderCreator = () => {
|
|
480
|
-
const factory = resourceCreatorFactory();
|
|
481
|
-
return (creator) => factory("provider", "register", creator);
|
|
482
|
-
};
|
|
483
|
-
var providerCreator = createProviderCreator();
|
|
484
|
-
|
|
485
|
-
// src/helpers/providers.ts
|
|
486
|
-
var createProviders = () => {
|
|
487
|
-
const factory = resourcesCollectionFactory();
|
|
488
|
-
return factory("provider", "register");
|
|
489
|
-
};
|
|
490
|
-
var providers = createProviders();
|
|
491
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
492
|
-
0 && (module.exports = {
|
|
493
|
-
InjectHelper,
|
|
494
|
-
Jimple,
|
|
495
|
-
createProvider,
|
|
496
|
-
createProviderCreator,
|
|
497
|
-
createProviders,
|
|
498
|
-
injectHelper,
|
|
499
|
-
jimple,
|
|
500
|
-
provider,
|
|
501
|
-
providerCreator,
|
|
502
|
-
providers,
|
|
503
|
-
resourceCreatorFactory,
|
|
504
|
-
resourceFactory,
|
|
505
|
-
resourcesCollectionFactory
|
|
506
|
-
});
|
|
1
|
+
var _=Object.create;var x=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var N=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var b=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of T(e))!w.call(t,n)&&n!==r&&x(t,n,{get:()=>e[n],enumerable:!(o=D(e,n))||o.enumerable});return t};var G=(t,e,r)=>(r=t!=null?_(J(t)):{},b(e||!t||!t.__esModule?x(r,"default",{value:t,enumerable:!0}):r,t));var g=N((q,R)=>{"use strict";function d(t,e){if(!t)throw new Error(e)}function u(t){return Object.prototype.toString.call(t)==="[object Function]"&&t.constructor.name==="Function"}function O(t){if(Object.prototype.toString.call(t)!=="[object Object]")return!1;let e=Object.getPrototypeOf(t);return e===null||e===Object.prototype}function y(t,e){d(t.has(e),`Identifier "${e}" is not defined.`)}function h(t,e){return d(u(e),"Service definition is not a Closure or invokable object"),t.add(e),e}var f=class{static provider(e){return{register:e}}constructor(e){this._items={},this._instances=new Map,this._factories=new Set,this._protected=new Set,e=O(e)?e:{},Object.keys(e).forEach(function(r){this.set(r,e[r])},this)}get(e){y(this,e);let r=this._items[e],o;return u(r)?this._protected.has(r)?o=r:this._instances.has(r)?o=this._instances.get(r):(o=r(this),this._factories.has(r)||this._instances.set(r,o)):o=r,o}set(e,r){this._items[e]=r}has(e){return this._items.hasOwnProperty(e)}factory(e){return h(this._factories,e)}protect(e){return h(this._protected,e)}keys(){return Object.keys(this._items)}extend(e,r){y(this,e);let o=this._items[e];d(u(o)&&this._protected.has(o)===!1,`Identifier '${e}' does not contain a service definition`),d(u(r),`The 'new' service definition for '${e}' is not a invokable object.`),this._items[e]=function(n){return r(o(n),n)},this._factories.has(o)&&(this._factories.delete(o),this._factories.add(this._items[e]))}register(e){e.register(this)}raw(e){return y(this,e),this._items[e]}};R.exports=f});var C=G(g(),1),j=C.default;var l=class extends j{try(e){if(this.has(e))return this.get(e)}},L=(...t)=>new l(...t);var p=()=>(t,e,r)=>({[t]:!0,[e]:r});var v=()=>(t,e,r)=>{let o=(...s)=>{let i=r(...s);return p()(t,e,i)},n={name:t,resource:null,get(s,i){let c;if(i===this.name)c=!0;else if(i===e){if(this.resource===null){let m=r();this.resource=m}c=this.resource}else c=s[i];return c},has(s,i){return i===this.name||i===e?!0:i in s}};return new Proxy(o,n)};var K=()=>(t,e,r)=>{let o=p();return n=>{let s=[t,e],i=Object.keys(n);if(i.some(a=>s.includes(String(a))))throw new Error(`No item on the collection can have the keys \`${t}\` nor \`${e}\``);let m=i.find(a=>typeof n[a]?.[e]!="function");if(m)throw new Error(`The item \`${String(m)}\` is invalid: it doesn't have a \`${e}\` function`);return{...o(t,e,r?(...a)=>r(n,...a):(...a)=>{i.forEach(P=>{let I=n[P][e];I(...a)})}),...n}}};var F=class{get(e,r,o){let n=e[r];return typeof n<"u"?n:o()}resolve(e,r,o){return e.reduce((s,i)=>{if(o[i])return s[i]=r.get(o[i]),s;let c=i;return r.has(c)&&(s[i]=r.get(c)),s},{})}},re=()=>new F;var S=()=>{let t=p();return e=>t("provider","register",e)},ie=S();var $=()=>{let t=v();return e=>t("provider","register",e)},ae=$();var E=()=>K()("provider","register"),ue=E();export{F as InjectHelper,l as Jimple,S as createProvider,$ as createProviderCreator,E as createProviders,re as injectHelper,L as jimple,ie as provider,ae as providerCreator,ue as providers,v as resourceCreatorFactory,p as resourceFactory,K as resourcesCollectionFactory};
|
|
507
2
|
//# sourceMappingURL=index.js.map
|