assemblerjs
Advanced tools
Comparing version
@@ -19,2 +19,3 @@ /** | ||
abstract require<T>(identifier: Identifier<T>): T; | ||
abstract tagged(...tags: string[]): any[]; | ||
} | ||
@@ -36,2 +37,3 @@ | ||
singleton?: false; | ||
events?: string[]; | ||
inject?: Injection<unknown>[][]; | ||
@@ -69,5 +71,6 @@ tags?: string | string[]; | ||
require<T>(identifier: Identifier<T>): T; | ||
tagged(...tags: string[]): any[]; | ||
} | ||
declare class AssemblerContext { | ||
export declare class AssemblerContext { | ||
/** | ||
@@ -80,2 +83,3 @@ * User-defined data. Can be used to add properties to context after creation. | ||
require: AbstractAssembler['require']; | ||
tagged: AbstractAssembler['tagged']; | ||
constructor(assembler: AbstractAssembler); | ||
@@ -179,2 +183,9 @@ /** | ||
get dependencies(): (Identifier<unknown> | any)[]; | ||
/** | ||
* Tags passed in assemblage's definition or in its parent definition. | ||
*/ | ||
get tags(): string[]; | ||
/** | ||
* Metadatas passed in assemblage's definition or in its parent definition. | ||
*/ | ||
get metadata(): Record<string, any>; | ||
@@ -181,0 +192,0 @@ } |
@@ -28,14 +28,14 @@ 'use strict'; | ||
const Assemblage = (n)=>{ | ||
const s = n || {}; | ||
return (n)=>{ | ||
defineCustomMetadata(ReflectIsAssemblageFlag, true, n); | ||
defineCustomMetadata(ReflectIsSingletonFlag, true, n); | ||
for(const e in s){ | ||
if (s.hasOwnProperty(e)) { | ||
switch(e){ | ||
const Assemblage = (o)=>{ | ||
const n = o || {}; | ||
return (o)=>{ | ||
defineCustomMetadata(ReflectIsAssemblageFlag, true, o); | ||
defineCustomMetadata(ReflectIsSingletonFlag, true, o); | ||
for(const r in n){ | ||
if (n.hasOwnProperty(r)) { | ||
switch(r){ | ||
case 'singleton': | ||
{ | ||
if (s.singleton === false) { | ||
defineCustomMetadata(ReflectIsSingletonFlag, false, n); | ||
if (n.singleton === false) { | ||
defineCustomMetadata(ReflectIsSingletonFlag, false, o); | ||
} | ||
@@ -46,13 +46,39 @@ break; | ||
{ | ||
if (s.controller === true) { | ||
if (typeof s.path !== 'string') { | ||
throw new Error(`Controller assemblage '${n.name}' must define a path.`); | ||
if (n.controller === true) { | ||
if (typeof n.path !== 'string') { | ||
throw new Error(`Controller assemblage '${o.name}' must define a path.`); | ||
} | ||
defineCustomMetadata(ReflectIsControllerFlag, true, n); | ||
defineCustomMetadata(ReflectIsControllerFlag, true, o); | ||
} | ||
break; | ||
} | ||
case 'tags': | ||
{ | ||
if (typeof n.tags !== 'undefined') { | ||
if (typeof n.tags === 'string') { | ||
defineCustomMetadata('tags', [ | ||
n.tags | ||
], o); | ||
} else if (Array.isArray(n.tags)) { | ||
defineCustomMetadata('tags', n.tags, o); | ||
} else { | ||
throw new Error(`Assemblage's 'tags' must be o type 'string' or 'Array'.`); | ||
} | ||
} | ||
break; | ||
} | ||
case 'inject': | ||
{ | ||
if (!Array.isArray(n.inject)) { | ||
throw new Error(`Assemblage's definition 'inject' property must be an array of 'Injection' tuples.`); | ||
} | ||
for (const r of n.inject){ | ||
if (!Array.isArray(r)) { | ||
throw new Error(`'Injection' must be an 'Array'.`); | ||
} | ||
} | ||
} | ||
default: | ||
{ | ||
defineCustomMetadata(e, s[e], n); | ||
defineCustomMetadata(r, n[r], o); | ||
} | ||
@@ -62,3 +88,3 @@ } | ||
} | ||
return n; | ||
return o; | ||
}; | ||
@@ -190,2 +216,5 @@ }; | ||
} | ||
get tags() { | ||
return getCustomMetadata('tags', this.concrete) || []; | ||
} | ||
get metadata() { | ||
@@ -211,6 +240,6 @@ return getCustomMetadata('metadata', this.concrete) || {}; | ||
function e$2(e, r, t) { | ||
if (r in e) { | ||
Object.defineProperty(e, r, { | ||
value: t, | ||
function e$2(e, t, r) { | ||
if (t in e) { | ||
Object.defineProperty(e, t, { | ||
value: r, | ||
enumerable: true, | ||
@@ -221,3 +250,3 @@ configurable: true, | ||
} else { | ||
e[r] = t; | ||
e[t] = r; | ||
} | ||
@@ -227,7 +256,7 @@ return e; | ||
class AssemblerContext { | ||
set(e, r) { | ||
set(e, t) { | ||
if (this.userData[e]) { | ||
throw new Error(`Key '${e}' is already defined in context's user data.`); | ||
} | ||
this.userData[e] = r; | ||
this.userData[e] = t; | ||
return this; | ||
@@ -238,3 +267,3 @@ } | ||
} | ||
constructor(r){ | ||
constructor(t){ | ||
e$2(this, "userData", {}); | ||
@@ -244,5 +273,7 @@ e$2(this, "register", undefined); | ||
e$2(this, "require", undefined); | ||
this.register = r.register.bind(r); | ||
this.has = r.has.bind(r); | ||
this.require = r.require.bind(r); | ||
e$2(this, "tagged", undefined); | ||
this.register = t.register.bind(t); | ||
this.has = t.has.bind(t); | ||
this.require = t.require.bind(t); | ||
this.tagged = t.tagged.bind(t); | ||
} | ||
@@ -286,2 +317,11 @@ } | ||
} | ||
tagged(...e) { | ||
const t = []; | ||
for (const i of e){ | ||
for (const [e, r] of this.injectables){ | ||
if (r.tags.includes(i)) t.push(r.build()); | ||
} | ||
} | ||
return t; | ||
} | ||
constructor(r){ | ||
@@ -314,4 +354,5 @@ e$1(this, "injectables", new Map()); | ||
exports.Assembler = Assembler; | ||
exports.AssemblerContext = AssemblerContext; | ||
exports.Configuration = e; | ||
exports.Context = s; | ||
exports.Metadata = a; |
{ | ||
"name": "assemblerjs", | ||
"description": "A simple and zero-dependency DI package written in typescript.", | ||
"version": "0.0.91", | ||
"version": "0.0.92", | ||
"author": "Benoît LAHOZ <info@benoitlahoz.io>", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/benoitlahoz/assemblerjs/issues", |
Sorry, the diff of this file is not supported yet
29350
15.42%843
12.25%