assemblerjs 0.0.92 → 0.0.93

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 (2) hide show
  1. package/dist/index.js +1 -347
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,347 +1 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- class AbstractAssemblage {
6
- }
7
-
8
- const ReflectCustomPrefix = '__';
9
- const ReflectCustomSuffix = '__';
10
- const ReflectParamTypes = 'design:paramtypes';
11
- const ReflectIsAssemblageFlag = `is_assemblage`;
12
- const ReflectIsSingletonFlag = `is_singleton`;
13
- const ReflectIsControllerFlag = `is_controller`;
14
- const ReflectContextParamIndex = `context_param_index`;
15
- const ReflectConfigurationParamIndex = `config_param_index`;
16
- const ReflectMetadataParamIndex = `metadata_param_index`;
17
-
18
- const defineCustomMetadata = (a, o, n)=>{
19
- Reflect.defineMetadata(`${ReflectCustomPrefix}${a}${ReflectCustomSuffix}`, o, n);
20
- };
21
- const getCustomMetadata = (a, o)=>{
22
- return Reflect.getMetadata(`${ReflectCustomPrefix}${a}${ReflectCustomSuffix}`, o);
23
- };
24
- const getOwnCustomMetadata = (a, o)=>{
25
- return Reflect.getOwnMetadata(`${ReflectCustomPrefix}${a}${ReflectCustomSuffix}`, o);
26
- };
27
-
28
- const Assemblage = (o)=>{
29
- const n = o || {};
30
- return (o)=>{
31
- defineCustomMetadata(ReflectIsAssemblageFlag, true, o);
32
- defineCustomMetadata(ReflectIsSingletonFlag, true, o);
33
- for(const r in n){
34
- if (n.hasOwnProperty(r)) {
35
- switch(r){
36
- case 'singleton':
37
- {
38
- if (n.singleton === false) {
39
- defineCustomMetadata(ReflectIsSingletonFlag, false, o);
40
- }
41
- break;
42
- }
43
- case 'controller':
44
- {
45
- if (n.controller === true) {
46
- if (typeof n.path !== 'string') {
47
- throw new Error(`Controller assemblage '${o.name}' must define a path.`);
48
- }
49
- defineCustomMetadata(ReflectIsControllerFlag, true, o);
50
- }
51
- break;
52
- }
53
- case 'tags':
54
- {
55
- if (typeof n.tags !== 'undefined') {
56
- if (typeof n.tags === 'string') {
57
- defineCustomMetadata('tags', [
58
- n.tags
59
- ], o);
60
- } else if (Array.isArray(n.tags)) {
61
- defineCustomMetadata('tags', n.tags, o);
62
- } else {
63
- throw new Error(`Assemblage's 'tags' must be o type 'string' or 'Array'.`);
64
- }
65
- }
66
- break;
67
- }
68
- case 'inject':
69
- {
70
- if (!Array.isArray(n.inject)) {
71
- throw new Error(`Assemblage's definition 'inject' property must be an array of 'Injection' tuples.`);
72
- }
73
- for (const r of n.inject){
74
- if (!Array.isArray(r)) {
75
- throw new Error(`'Injection' must be an 'Array'.`);
76
- }
77
- }
78
- }
79
- default:
80
- {
81
- defineCustomMetadata(r, n[r], o);
82
- }
83
- }
84
- }
85
- }
86
- return o;
87
- };
88
- };
89
-
90
- class AbstractAssembler {
91
- }
92
-
93
- const NoOp = (...e)=>{};
94
- const isClass = (e)=>{
95
- return e && typeof e === 'function' && typeof e.constructor !== 'undefined';
96
- };
97
- const isObject = (e)=>typeof e === 'object' && !Array.isArray(e) && !isClass(e);
98
- const switchCase = (e, t)=>(o, ...s)=>e[o] ? e[o](...s) : t ? t(o, ...s) : NoOp();
99
- const pipe = (...e)=>(t)=>e.reduce((e, t)=>t(e), t);
100
- const conditionally = (e)=>(t)=>{
101
- return e.if(t) ? e.then(t) : e.else ? e.else(t) : undefined;
102
- };
103
-
104
- const i = (n)=>{
105
- return {
106
- identifier: n[0],
107
- concrete: n[0],
108
- configuration: {}
109
- };
110
- };
111
- const c = (r)=>{
112
- const i = ()=>isClass(r[0]) && isClass(r[1]);
113
- const c = ()=>isClass(r[0]) && isObject(r[1]);
114
- const u = ()=>pipe(conditionally({
115
- if: ()=>i(),
116
- then: ()=>{
117
- return {
118
- identifier: r[0],
119
- concrete: r[1],
120
- configuration: {}
121
- };
122
- }
123
- }), conditionally({
124
- if: ()=>c(),
125
- then: ()=>{
126
- return {
127
- identifier: r[0],
128
- concrete: r[0],
129
- configuration: r[1]
130
- };
131
- },
132
- else: (n)=>n
133
- }))();
134
- return u();
135
- };
136
- const u = (n)=>{
137
- return {
138
- identifier: n[0],
139
- concrete: n[1],
140
- configuration: n[2]
141
- };
142
- };
143
- const resolveInjectionTuple = (n)=>switchCase({
144
- 1: ()=>i(n),
145
- 2: ()=>c(n),
146
- 3: ()=>u(n)
147
- }, ()=>{
148
- throw new Error(`Injection tuple must be of length 1, 2 or 3.`);
149
- })(n.length);
150
-
151
- function t(t, e, n) {
152
- if (e in t) {
153
- Object.defineProperty(t, e, {
154
- value: n,
155
- enumerable: true,
156
- configurable: true,
157
- writable: true
158
- });
159
- } else {
160
- t[e] = n;
161
- }
162
- return t;
163
- }
164
- class Injectable {
165
- static of(t, e) {
166
- return new Injectable(t, e);
167
- }
168
- build() {
169
- if (this.singleton) return this.singleton;
170
- const t = this.resolveDependencies();
171
- const e = new this.concrete(...t);
172
- if (this.isSingleton) {
173
- this.singleton = e;
174
- }
175
- return e;
176
- }
177
- resolveDependencies() {
178
- const t = [];
179
- const i = getOwnCustomMetadata(ReflectContextParamIndex, this.concrete) || [];
180
- const s = getOwnCustomMetadata(ReflectConfigurationParamIndex, this.concrete) || [];
181
- const h = getCustomMetadata(ReflectMetadataParamIndex, this.concrete) || [];
182
- let u = 0;
183
- for (const e of this.dependencies){
184
- if (i.includes(u)) {
185
- t.push(this.context);
186
- u++;
187
- continue;
188
- }
189
- if (s.includes(u)) {
190
- t.push(this.configuration);
191
- u++;
192
- continue;
193
- }
194
- if (h.includes(u)) {
195
- t.push(this.metadata);
196
- u++;
197
- continue;
198
- }
199
- t.push(this.context.require(e));
200
- u++;
201
- }
202
- return t;
203
- }
204
- get isSingleton() {
205
- return getOwnCustomMetadata(ReflectIsSingletonFlag, this.concrete) || false;
206
- }
207
- get injections() {
208
- return getOwnCustomMetadata('inject', this.concrete) || [];
209
- }
210
- get dependencies() {
211
- return Reflect.getMetadata(ReflectParamTypes, this.concrete) || [];
212
- }
213
- get tags() {
214
- return getCustomMetadata('tags', this.concrete) || [];
215
- }
216
- get metadata() {
217
- return getCustomMetadata('metadata', this.concrete) || {};
218
- }
219
- constructor(e, n){
220
- t(this, "context", undefined);
221
- t(this, "identifier", undefined);
222
- t(this, "concrete", undefined);
223
- t(this, "configuration", undefined);
224
- t(this, "singleton", undefined);
225
- this.context = n;
226
- const i = resolveInjectionTuple(e);
227
- this.identifier = i.identifier;
228
- this.concrete = i.concrete;
229
- this.configuration = i.configuration;
230
- for (const t of this.injections){
231
- this.context.register(t);
232
- }
233
- }
234
- }
235
-
236
- function e$2(e, t, r) {
237
- if (t in e) {
238
- Object.defineProperty(e, t, {
239
- value: r,
240
- enumerable: true,
241
- configurable: true,
242
- writable: true
243
- });
244
- } else {
245
- e[t] = r;
246
- }
247
- return e;
248
- }
249
- class AssemblerContext {
250
- set(e, t) {
251
- if (this.userData[e]) {
252
- throw new Error(`Key '${e}' is already defined in context's user data.`);
253
- }
254
- this.userData[e] = t;
255
- return this;
256
- }
257
- get(e) {
258
- return this.userData[e];
259
- }
260
- constructor(t){
261
- e$2(this, "userData", {});
262
- e$2(this, "register", undefined);
263
- e$2(this, "has", undefined);
264
- e$2(this, "require", undefined);
265
- e$2(this, "tagged", undefined);
266
- this.register = t.register.bind(t);
267
- this.has = t.has.bind(t);
268
- this.require = t.require.bind(t);
269
- this.tagged = t.tagged.bind(t);
270
- }
271
- }
272
-
273
- function e$1(e, t, i) {
274
- if (t in e) {
275
- Object.defineProperty(e, t, {
276
- value: i,
277
- enumerable: true,
278
- configurable: true,
279
- writable: true
280
- });
281
- } else {
282
- e[t] = i;
283
- }
284
- return e;
285
- }
286
- class Assembler {
287
- static build(e) {
288
- return new Assembler(e);
289
- }
290
- register(e) {
291
- const t = Injectable.of(e, this.context);
292
- if (this.has(t.identifier)) {
293
- throw new Error(`An assemblage is already registered with identifier '${t.identifier.name}'.`);
294
- }
295
- this.injectables.set(t.identifier, t);
296
- return t;
297
- }
298
- has(e) {
299
- return this.injectables.has(e);
300
- }
301
- require(e) {
302
- if (!this.injectables.has(e)) {
303
- throw new Error(`Assemblage with identifier '${e.name}' has not been registered.`);
304
- }
305
- const t = this.injectables.get(e);
306
- return t.build();
307
- }
308
- tagged(...e) {
309
- const t = [];
310
- for (const i of e){
311
- for (const [e, r] of this.injectables){
312
- if (r.tags.includes(i)) t.push(r.build());
313
- }
314
- }
315
- return t;
316
- }
317
- constructor(r){
318
- e$1(this, "injectables", new Map());
319
- e$1(this, "context", undefined);
320
- this.context = new AssemblerContext(this);
321
- defineCustomMetadata(ReflectIsSingletonFlag, true, r);
322
- const s = this.register([
323
- r
324
- ]);
325
- return s.build();
326
- }
327
- }
328
-
329
- const m = (o)=>()=>{
330
- return (t, n, m)=>{
331
- const s = getOwnCustomMetadata(o, t) || [];
332
- s.push(m);
333
- defineCustomMetadata(o, s, t);
334
- };
335
- };
336
- const s = m(ReflectContextParamIndex);
337
- const e = m(ReflectConfigurationParamIndex);
338
- const a = m(ReflectMetadataParamIndex);
339
-
340
- exports.AbstractAssemblage = AbstractAssemblage;
341
- exports.AbstractAssembler = AbstractAssembler;
342
- exports.Assemblage = Assemblage;
343
- exports.Assembler = Assembler;
344
- exports.AssemblerContext = AssemblerContext;
345
- exports.Configuration = e;
346
- exports.Context = s;
347
- exports.Metadata = a;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e="__",t="__",i="is_singleton",r="context_param_index",n="config_param_index",s="metadata_param_index",o=(i,r,n)=>{Reflect.defineMetadata(`${e}${i}${t}`,r,n)},a=(i,r)=>Reflect.getMetadata(`${e}${i}${t}`,r),c=(i,r)=>Reflect.getOwnMetadata(`${e}${i}${t}`,r);const h=e=>e&&"function"==typeof e&&void 0!==e.constructor,u=e=>t=>e.if(t)?e.then(t):e.else?e.else(t):void 0,l=e=>{const t=()=>h(e[0])&&(e=>"object"==typeof e&&!Array.isArray(e)&&!h(e))(e[1]);return((...e)=>t=>e.reduce(((e,t)=>t(e)),t))(u({if:()=>h(e[0])&&h(e[1]),then:()=>({identifier:e[0],concrete:e[1],configuration:{}})}),u({if:()=>t(),then:()=>({identifier:e[0],concrete:e[0],configuration:e[1]}),else:e=>e}))()},d=e=>((e,t)=>(i,...r)=>e[i]?e[i](...r):t?t(i,...r):void 0)({1:()=>(e=>({identifier:e[0],concrete:e[0],configuration:{}}))(e),2:()=>l(e),3:()=>(e=>({identifier:e[0],concrete:e[1],configuration:e[2]}))(e)},(()=>{throw new Error("Injection tuple must be of length 1, 2 or 3.")}))(e.length);function f(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class g{static of(e,t){return new g(e,t)}build(){if(this.singleton)return this.singleton;const e=this.resolveDependencies(),t=new this.concrete(...e);return this.isSingleton&&(this.singleton=t),t}resolveDependencies(){const e=[],t=c(r,this.concrete)||[],i=c(n,this.concrete)||[],o=a(s,this.concrete)||[];let h=0;for(const r of this.dependencies)t.includes(h)?(e.push(this.context),h++):i.includes(h)?(e.push(this.configuration),h++):o.includes(h)?(e.push(this.metadata),h++):(e.push(this.context.require(r)),h++);return e}get isSingleton(){return c(i,this.concrete)||!1}get injections(){return c("inject",this.concrete)||[]}get dependencies(){return Reflect.getMetadata("design:paramtypes",this.concrete)||[]}get tags(){return a("tags",this.concrete)||[]}get metadata(){return a("metadata",this.concrete)||{}}constructor(e,t){f(this,"context",void 0),f(this,"identifier",void 0),f(this,"concrete",void 0),f(this,"configuration",void 0),f(this,"singleton",void 0),this.context=t;const i=d(e);this.identifier=i.identifier,this.concrete=i.concrete,this.configuration=i.configuration;for(const r of this.injections)this.context.register(r)}}function b(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class p{set(e,t){if(this.userData[e])throw new Error(`Key '${e}' is already defined in context's user data.`);return this.userData[e]=t,this}get(e){return this.userData[e]}constructor(e){b(this,"userData",{}),b(this,"register",void 0),b(this,"has",void 0),b(this,"require",void 0),b(this,"tagged",void 0),this.register=e.register.bind(e),this.has=e.has.bind(e),this.require=e.require.bind(e),this.tagged=e.tagged.bind(e)}}function m(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}class w{static build(e){return new w(e)}register(e){const t=g.of(e,this.context);if(this.has(t.identifier))throw new Error(`An assemblage is already registered with identifier '${t.identifier.name}'.`);return this.injectables.set(t.identifier,t),t}has(e){return this.injectables.has(e)}require(e){if(!this.injectables.has(e))throw new Error(`Assemblage with identifier '${e.name}' has not been registered.`);return this.injectables.get(e).build()}tagged(...e){const t=[];for(const i of e)for(const[e,r]of this.injectables)r.tags.includes(i)&&t.push(r.build());return t}constructor(e){m(this,"injectables",new Map),m(this,"context",void 0),this.context=new p(this),o(i,!0,e);return this.register([e]).build()}}const y=e=>()=>(t,i,r)=>{const n=c(e,t)||[];n.push(r),o(e,n,t)},x=y(r),j=y(n),A=y(s);exports.AbstractAssemblage=class{},exports.AbstractAssembler=class{},exports.Assemblage=e=>{const t=e||{};return e=>{o("is_assemblage",!0,e),o(i,!0,e);for(const r in t)if(t.hasOwnProperty(r))switch(r){case"singleton":!1===t.singleton&&o(i,!1,e);break;case"controller":if(!0===t.controller){if("string"!=typeof t.path)throw new Error(`Controller assemblage '${e.name}' must define a path.`);o("is_controller",!0,e)}break;case"tags":if(void 0!==t.tags)if("string"==typeof t.tags)o("tags",[t.tags],e);else{if(!Array.isArray(t.tags))throw new Error("Assemblage's 'tags' must be o type 'string' or 'Array'.");o("tags",t.tags,e)}break;case"inject":if(!Array.isArray(t.inject))throw new Error("Assemblage's definition 'inject' property must be an array of 'Injection' tuples.");for(const e of t.inject)if(!Array.isArray(e))throw new Error("'Injection' must be an 'Array'.");default:o(r,t[r],e)}return e}},exports.Assembler=w,exports.AssemblerContext=p,exports.Configuration=j,exports.Context=x,exports.Metadata=A;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "assemblerjs",
3
3
  "description": "A simple and zero-dependency DI package written in typescript.",
4
- "version": "0.0.92",
4
+ "version": "0.0.93",
5
5
  "author": "Benoît LAHOZ <info@benoitlahoz.io>",
6
6
  "bugs": "https://github.com/benoitlahoz/assemblerjs/issues",
7
7
  "devDependencies": {