Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

true-di

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

true-di - npm Package Compare versions

Comparing version 2.2.2-betta.0 to 3.0.0

esm/utils/narrow-object.d.ts

4

esm/create-instance.js

@@ -8,3 +8,3 @@ import UniqueStack from './unique-stack';

throw new Error('Cyclic dependencies couldn\'t be resolved.\n\n' +
`Requested: ${name}\nResolution stack:\n\t${stack.items.join('\n\t')}`);
`Requested: ${String(name)}\nResolution stack:\n\t${stack.items.join('\n\t')}`);
}

@@ -14,3 +14,3 @@ const itemFactory = factories[name];

if (typeof factory !== 'function') {
throw new Error(`Factory is not defined for name "${name}"`);
throw new Error(`Factory is not defined for name "${String(name)}"`);
}

@@ -17,0 +17,0 @@ const instance = factory(container);

@@ -5,4 +5,7 @@ import { IFactories, IPureFactories } from './types';

export declare const releaseAll: <C extends object>(container: C) => void;
export declare const factoriesFrom: <C extends object, N extends keyof C = keyof C>(container: C, names?: N[]) => IPureFactories<Pick<C, N>>;
declare function diContainer<C extends object>(factories: IFactories<C>): C;
export declare const factoriesFrom: <C extends object, N extends keyof C = keyof C>(container: C, names?: N[] | undefined) => IPureFactories<Pick<C, N>>;
declare const diContainer: {
<Public extends object>(factories: IFactories<Public>): Public;
<Private extends object, Public extends object>(privateFactories: Pick<IFactories<Private & Public>, keyof Private>, publicFactories?: Pick<IFactories<Private & Public>, keyof Public>): Public;
};
export default diContainer;

@@ -5,2 +5,4 @@ import createInstanceFactory from './create-instance';

import mapObject from './utils/map-object';
import narrowObject from './utils/narrow-object';
import { shallowMerge } from './utils/shallow-merge';
const $INSTANCES = Symbol('TRUE-DI::INSTANCES');

@@ -14,4 +16,7 @@ const itemsOf = (container) => assertExists(container[$INSTANCES], 'Argument is not a container');

export const factoriesFrom = (container, names) => mapObject(container, name => () => container[name], names);
function diContainer(factories) {
const diContainer = (privateFactories, publicFactories) => {
const instances = new Map();
const factories = (publicFactories != null
? shallowMerge(privateFactories, publicFactories)
: privateFactories);
const createInstance = createInstanceFactory(factories, instances);

@@ -33,4 +38,4 @@ const container = allNames(factories).reduce((containerObj, name) => Object.defineProperty(containerObj, name, {

})));
return container;
}
return publicFactories != null ? narrowObject(container, allNames(publicFactories)) : container;
};
export default diContainer;

@@ -1,5 +0,8 @@

export declare type IInstanceInitializer<IContainer, N extends keyof IContainer> = (instance: IContainer[N], container: IContainer, name?: N) => void;
export declare type IInstanceInitializer<IContainer, N extends keyof IContainer> = (instance: IContainer[N], container: IContainer, name: N) => void;
export declare type VoidFn = () => void;
export declare type IFactory<IContainer, N extends keyof IContainer> = (container: IContainer) => IContainer[N];
export declare type IFactoryTuple<IContainer, N extends keyof IContainer> = [IFactory<IContainer, N>, IInstanceInitializer<IContainer, N>];
export declare type IFactoryTuple<IContainer, N extends keyof IContainer> = [
IFactory<IContainer, N>,
IInstanceInitializer<IContainer, N>
];
export declare type IFactories<IContainer extends object> = {

@@ -11,15 +14,1 @@ [name in keyof IContainer]: IFactory<IContainer, name> | IFactoryTuple<IContainer, name>;

};
export declare type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? A : B;
export declare type KeysOfType<T, F> = {
[P in keyof T]: IfEquals<{
[Q in P]: T[P];
}, {
[Q in P]: F;
}, P>;
}[keyof T];
export declare const expectStrictType: <expected>(value: expected) => void;
declare type Item<token extends string | symbol, T> = {
[p in token]: T;
};
export declare type Services<tokens extends (string | symbol)[], Deps extends any[]> = tokens extends [] ? {} : tokens extends [any] ? Item<tokens[0], Deps[0]> : tokens extends [any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> : tokens extends [any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> : tokens extends [any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> : tokens extends [any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> : tokens extends [any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> : tokens extends [any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> : tokens extends [any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> : tokens extends [any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> : tokens extends [any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> & Item<tokens[18], Deps[18]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> & Item<tokens[18], Deps[18]> & Item<tokens[19], Deps[19]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> & Item<tokens[18], Deps[18]> & Item<tokens[19], Deps[19]> & Item<tokens[20], Deps[20]> : never;
export {};

@@ -1,3 +0,1 @@

export const expectStrictType = (value) => {
const newValue = true;
};
export {};

@@ -1,5 +0,13 @@

import { KeysOfType } from '../types';
declare type KeysOfType<T, F> = {
[P in keyof T]: T[P] extends F ? P : never;
}[keyof T];
declare type ObjectFieldKeys<T> = {
[P in keyof T]: T[P] extends {
[k in any]: any;
} ? P : never;
}[keyof T];
export declare type IMapping<IContainer extends object, T> = {
[p in keyof T]?: KeysOfType<IContainer, T[p]>;
[p in keyof T]?: keyof IContainer & KeysOfType<IContainer, T[p]>;
};
export declare const assignProps: <IContainer extends object, N extends keyof IContainer>(mapping: IMapping<IContainer, IContainer[N]>) => (instance: IContainer[N], container: IContainer) => void;
export declare const assignProps: <IContainer extends object, N extends ObjectFieldKeys<IContainer>>(mapping: IMapping<IContainer, IContainer[N]>) => (instance: IContainer[N], container: IContainer) => void;
export {};

@@ -15,7 +15,4 @@ export declare type Merge<T1 extends object, T2 extends object> = Omit<T1, keyof T2> & T2;

<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>;
<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object, TD extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC, objD: TD): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>;
<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object, TD extends object, TE extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC, objD: TD, objE: TE): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>, TE>;
<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object, TD extends object, TE extends object, TF extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC, objD: TD, objE: TE, objF: TF): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>, TE>, TF>;
};
export declare const shallowMerge: IShallowMergeFn;
export {};

@@ -6,3 +6,3 @@ "use strict";

var createInstanceFactory = function (factories, instances, stack, initializers) {
if (stack === void 0) { stack = unique_stack_1.default(); }
if (stack === void 0) { stack = (0, unique_stack_1.default)(); }
if (initializers === void 0) { initializers = []; }

@@ -14,3 +14,3 @@ return function (container, name) {

throw new Error('Cyclic dependencies couldn\'t be resolved.\n\n' +
("Requested: " + name + "\nResolution stack:\n\t" + stack.items.join('\n\t')));
"Requested: ".concat(String(name), "\nResolution stack:\n\t").concat(stack.items.join('\n\t')));
}

@@ -20,3 +20,3 @@ var itemFactory = factories[name];

if (typeof factory !== 'function') {
throw new Error("Factory is not defined for name \"" + name + "\"");
throw new Error("Factory is not defined for name \"".concat(String(name), "\""));
}

@@ -23,0 +23,0 @@ var instance = factory(container);

@@ -5,4 +5,7 @@ import { IFactories, IPureFactories } from './types';

export declare const releaseAll: <C extends object>(container: C) => void;
export declare const factoriesFrom: <C extends object, N extends keyof C = keyof C>(container: C, names?: N[]) => IPureFactories<Pick<C, N>>;
declare function diContainer<C extends object>(factories: IFactories<C>): C;
export declare const factoriesFrom: <C extends object, N extends keyof C = keyof C>(container: C, names?: N[] | undefined) => IPureFactories<Pick<C, N>>;
declare const diContainer: {
<Public extends object>(factories: IFactories<Public>): Public;
<Private extends object, Public extends object>(privateFactories: Pick<IFactories<Private & Public>, keyof Private>, publicFactories?: Pick<IFactories<Private & Public>, keyof Public>): Public;
};
export default diContainer;

@@ -8,23 +8,32 @@ "use strict";

var map_object_1 = require("./utils/map-object");
var narrow_object_1 = require("./utils/narrow-object");
var shallow_merge_1 = require("./utils/shallow-merge");
var $INSTANCES = Symbol('TRUE-DI::INSTANCES');
var itemsOf = function (container) {
return assert_exists_1.default(container[$INSTANCES], 'Argument is not a container');
return (0, assert_exists_1.default)(container[$INSTANCES], 'Argument is not a container');
};
exports.isReady = function (container, name) {
var isReady = function (container, name) {
return itemsOf(container).has(name);
};
exports.prepareAll = function (container) {
return map_object_1.default(container, function (name) { return container[name]; });
exports.isReady = isReady;
var prepareAll = function (container) {
return (0, map_object_1.default)(container, function (name) { return container[name]; });
};
exports.releaseAll = function (container) {
exports.prepareAll = prepareAll;
var releaseAll = function (container) {
itemsOf(container).clear();
};
exports.factoriesFrom = function (container, names) {
return map_object_1.default(container, function (name) { return function () { return container[name]; }; }, names);
exports.releaseAll = releaseAll;
var factoriesFrom = function (container, names) {
return (0, map_object_1.default)(container, function (name) { return function () { return container[name]; }; }, names);
};
function diContainer(factories) {
exports.factoriesFrom = factoriesFrom;
var diContainer = function (privateFactories, publicFactories) {
var _a;
var instances = new Map();
var createInstance = create_instance_1.default(factories, instances);
var container = all_names_1.default(factories).reduce(function (containerObj, name) {
var factories = (publicFactories != null
? (0, shallow_merge_1.shallowMerge)(privateFactories, publicFactories)
: privateFactories);
var createInstance = (0, create_instance_1.default)(factories, instances);
var container = (0, all_names_1.default)(factories).reduce(function (containerObj, name) {
return Object.defineProperty(containerObj, name, {

@@ -46,4 +55,4 @@ configurable: false,

_a))));
return container;
}
return publicFactories != null ? (0, narrow_object_1.default)(container, (0, all_names_1.default)(publicFactories)) : container;
};
exports.default = diContainer;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {

@@ -10,3 +14,3 @@ if (k2 === undefined) k2 = k;

var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};

@@ -13,0 +17,0 @@ Object.defineProperty(exports, "__esModule", { value: true });

@@ -36,3 +36,3 @@ "use strict";

var decorateFactories = function (factories) {
return all_names_1.default(factories).reduce(function (newObject, name) { return Object.defineProperty(newObject, name, __assign(__assign({}, Object.getOwnPropertyDescriptor(factories, name)), { value: decorateItemBinding(factories[name]) })); }, Object.create(null));
return (0, all_names_1.default)(factories).reduce(function (newObject, name) { return Object.defineProperty(newObject, name, __assign(__assign({}, Object.getOwnPropertyDescriptor(factories, name)), { value: decorateItemBinding(factories[name]) })); }, Object.create(null));
};

@@ -39,0 +39,0 @@ var multiple = function (bindingOrFactories) { return (!Array.isArray(bindingOrFactories) && typeof bindingOrFactories === 'object'

@@ -1,5 +0,8 @@

export declare type IInstanceInitializer<IContainer, N extends keyof IContainer> = (instance: IContainer[N], container: IContainer, name?: N) => void;
export declare type IInstanceInitializer<IContainer, N extends keyof IContainer> = (instance: IContainer[N], container: IContainer, name: N) => void;
export declare type VoidFn = () => void;
export declare type IFactory<IContainer, N extends keyof IContainer> = (container: IContainer) => IContainer[N];
export declare type IFactoryTuple<IContainer, N extends keyof IContainer> = [IFactory<IContainer, N>, IInstanceInitializer<IContainer, N>];
export declare type IFactoryTuple<IContainer, N extends keyof IContainer> = [
IFactory<IContainer, N>,
IInstanceInitializer<IContainer, N>
];
export declare type IFactories<IContainer extends object> = {

@@ -11,15 +14,1 @@ [name in keyof IContainer]: IFactory<IContainer, name> | IFactoryTuple<IContainer, name>;

};
export declare type IfEquals<X, Y, A = X, B = never> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? A : B;
export declare type KeysOfType<T, F> = {
[P in keyof T]: IfEquals<{
[Q in P]: T[P];
}, {
[Q in P]: F;
}, P>;
}[keyof T];
export declare const expectStrictType: <expected>(value: expected) => void;
declare type Item<token extends string | symbol, T> = {
[p in token]: T;
};
export declare type Services<tokens extends (string | symbol)[], Deps extends any[]> = tokens extends [] ? {} : tokens extends [any] ? Item<tokens[0], Deps[0]> : tokens extends [any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> : tokens extends [any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> : tokens extends [any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> : tokens extends [any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> : tokens extends [any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> : tokens extends [any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> : tokens extends [any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> : tokens extends [any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> : tokens extends [any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> & Item<tokens[18], Deps[18]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> & Item<tokens[18], Deps[18]> & Item<tokens[19], Deps[19]> : tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ? Item<tokens[0], Deps[0]> & Item<tokens[1], Deps[1]> & Item<tokens[2], Deps[2]> & Item<tokens[3], Deps[3]> & Item<tokens[4], Deps[4]> & Item<tokens[5], Deps[5]> & Item<tokens[6], Deps[6]> & Item<tokens[7], Deps[7]> & Item<tokens[8], Deps[8]> & Item<tokens[9], Deps[9]> & Item<tokens[10], Deps[10]> & Item<tokens[11], Deps[11]> & Item<tokens[12], Deps[12]> & Item<tokens[13], Deps[13]> & Item<tokens[14], Deps[14]> & Item<tokens[15], Deps[15]> & Item<tokens[16], Deps[16]> & Item<tokens[17], Deps[17]> & Item<tokens[18], Deps[18]> & Item<tokens[19], Deps[19]> & Item<tokens[20], Deps[20]> : never;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectStrictType = void 0;
exports.expectStrictType = function (value) {
var newValue = true;
};

@@ -1,5 +0,13 @@

import { KeysOfType } from '../types';
declare type KeysOfType<T, F> = {
[P in keyof T]: T[P] extends F ? P : never;
}[keyof T];
declare type ObjectFieldKeys<T> = {
[P in keyof T]: T[P] extends {
[k in any]: any;
} ? P : never;
}[keyof T];
export declare type IMapping<IContainer extends object, T> = {
[p in keyof T]?: KeysOfType<IContainer, T[p]>;
[p in keyof T]?: keyof IContainer & KeysOfType<IContainer, T[p]>;
};
export declare const assignProps: <IContainer extends object, N extends keyof IContainer>(mapping: IMapping<IContainer, IContainer[N]>) => (instance: IContainer[N], container: IContainer) => void;
export declare const assignProps: <IContainer extends object, N extends ObjectFieldKeys<IContainer>>(mapping: IMapping<IContainer, IContainer[N]>) => (instance: IContainer[N], container: IContainer) => void;
export {};

@@ -5,6 +5,7 @@ "use strict";

var all_names_1 = require("./all-names");
exports.assignProps = function (mapping) { return function (instance, container) {
return all_names_1.default(mapping).forEach(function (name) {
var assignProps = function (mapping) { return function (instance, container) {
return (0, all_names_1.default)(mapping).forEach(function (name) {
instance[name] = container[mapping[name]];
});
}; };
exports.assignProps = assignProps;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {

@@ -10,3 +14,3 @@ if (k2 === undefined) k2 = k;

var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};

@@ -13,0 +17,0 @@ Object.defineProperty(exports, "__esModule", { value: true });

@@ -5,3 +5,3 @@ "use strict";

var mapObject = function (obj, mapper, names) {
if (names === void 0) { names = all_names_1.default(obj); }
if (names === void 0) { names = (0, all_names_1.default)(obj); }
return names.reduce(function (newObj, key) { return Object.defineProperty(newObj, key, {

@@ -8,0 +8,0 @@ enumerable: Object.getOwnPropertyDescriptor(obj, key).enumerable,

@@ -15,7 +15,4 @@ export declare type Merge<T1 extends object, T2 extends object> = Omit<T1, keyof T2> & T2;

<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>;
<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object, TD extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC, objD: TD): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>;
<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object, TD extends object, TE extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC, objD: TD, objE: TE): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>, TE>;
<T1 extends object, T2 extends object, T3 extends object, T4 extends object, T5 extends object, T6 extends object, T7 extends object, T8 extends object, T9 extends object, TA extends object, TB extends object, TC extends object, TD extends object, TE extends object, TF extends object>(obj1: T1, obj2: T2, obj3: T3, obj4: T4, obj5: T5, obj6: T6, obj7: T7, obj8: T8, obj9: T9, objA: TA, objB: TB, objC: TC, objD: TD, objE: TE, objF: TF): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>, TE>, TF>;
};
export declare const shallowMerge: IShallowMergeFn;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.shallowMerge = void 0;
exports.shallowMerge = function () {
var shallowMerge = function () {
var objects = [];

@@ -11,1 +11,2 @@ for (var _i = 0; _i < arguments.length; _i++) {

};
exports.shallowMerge = shallowMerge;
{
"name": "true-di",
"version": "2.2.2-betta.0",
"description": "Framework Agnostic, Zero Dependency, Isomorphic & Minimalistic Dependency Injection Container for TypeScript and JavaScript projects",
"version": "3.0.0",
"description": "Zero Dependency, Minimalistic **Type-Safe DI Container** for TypeScript and JavaScript projects",
"main": "./lib/index.js",

@@ -38,14 +38,14 @@ "module": "./esm/index.js",

"devDependencies": {
"@types/jest": "^24.9.1",
"@types/jest": "^28.1.2",
"@types/node": "^14.0.13",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"coveralls": "^3.1.0",
"eslint": "^6.7.2",
"eslint-config-airbnb": "^18.1.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jest": "^23.13.2",
"jest": "^25.1.0",
"ts-jest": "^25.0.0",
"typescript": "^3.7.5"
"@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.28.0",
"coveralls": "^3.1.1",
"eslint": "^8.18.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"jest": "^28.1.1",
"ts-jest": "^28.0.5",
"typescript": "^4.7.4"
},

@@ -94,3 +94,2 @@ "eslintConfig": {

],
"@typescript-eslint/indent": "off",
"jest/no-disabled-tests": "warn",

@@ -121,4 +120,9 @@ "jest/no-focused-tests": "error",

"/examples"
]
],
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.esm.json"
}
}
}
}
# true-di
Framework Agnostic, Zero Dependency, Isomorphic & Minimalistic **Dependency Injection Container** for TypeScript and JavaScript projects
Zero Dependency, Minimalistic **Type-Safe DI Container** for TypeScript and JavaScript projects

@@ -86,27 +86,19 @@ [![Build Status](https://travis-ci.org/DScheglov/true-di.svg?branch=master)](https://travis-ci.org/DScheglov/true-di) [![Coverage Status](https://coveralls.io/repos/github/DScheglov/true-di/badge.svg?branch=master)](https://coveralls.io/github/DScheglov/true-di?branch=master) [![npm version](https://img.shields.io/npm/v/true-di.svg?style=flat-square)](https://www.npmjs.com/package/true-di) [![npm downloads](https://img.shields.io/npm/dm/true-di.svg?style=flat-square)](https://www.npmjs.com/package/true-di) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/DScheglov/true-di/blob/master/LICENSE)

```typescript
import Express from 'express';
import { Request, Response, NextFunction as Next } from 'express';
import { IGetOrderById, IGetOrders } from './interfaces';
import { Injected } from './interfaces/IRequestInjected';
import { sendJson } from './utils/sendJson';
import { expectFound } from './utils/NotFoundError';
export const getOrders = (
{ injected: { ecommerceService } }: Injected<{ ecommerceService: IGetOrders }>,
res: Express.Response,
next: Express.NextFunction,
) =>
ecommerceService
.getOrders()
.then(sendJson(res), next);
export const getOrders = (req: Request, res: Response, next: Next) =>
({ ecommerceService }: { ecommerceService: IGetOrders }) =>
ecommerceService
.getOrders()
.then(sendJson(res), next);
export const getOrderById = (
{ params, injected: { ecommerceService } }:
{ params: { id: string } } & Injected<{ ecommerceService: IGetOrderById }>,
res: Express.Response,
next: Express.NextFunction,
) =>
ecommerceService
.getOrderById(params.id)
.then(expectFound(`Order(${params.id})`))
.then(sendJson(res), next);
export const getOrderById = ({ params }: Request<{ id: string }>, res: Response, next: Next) =>
({ ecommerceService }: { ecommerceService: IGetOrderById }) =>
ecommerceService
.getOrderById(params.id)
.then(expectFound(`Order(${params.id})`))
.then(sendJson(res), next);
```

@@ -118,2 +110,3 @@

import express from 'express';
import createContext from 'express-async-context';
import container from './container';

@@ -124,21 +117,15 @@ import { getOrderById, getOrders } from './controller';

const app = express();
const Context = createContext(() => container);
app.use((req, _, next) => {
req.injected = container;
next();
});
app.use(Context.provider);
app.get('/orders', getOrders);
app.get('/orders/:id', getOrderById);
app.get('/orders', Context.consumer(getOrders));
app.get('/orders/:id', Context.consumer(getOrderById));
app.use(handleErrors);
app.use(Context.consumer(handleErrors));
if (module.parent == null) {
app.listen(8080, () => {
console.log('Server is listening on port: 8080');
console.log('Follow: http://localhost:8080/orders');
});
}
export default app;
app.listen(8080, () => {
console.log('Server is listening on port: 8080');
console.log('Follow: http://localhost:8080/orders');
});
```

@@ -145,0 +132,0 @@

@@ -17,3 +17,3 @@ import UniqueStack from './unique-stack';

'Cyclic dependencies couldn\'t be resolved.\n\n' +
`Requested: ${name}\nResolution stack:\n\t${stack.items.join('\n\t')}`,
`Requested: ${String(name)}\nResolution stack:\n\t${stack.items.join('\n\t')}`,
);

@@ -29,3 +29,3 @@ }

if (typeof factory !== 'function') {
throw new Error(`Factory is not defined for name "${name}"`);
throw new Error(`Factory is not defined for name "${String(name)}"`);
}

@@ -32,0 +32,0 @@

@@ -6,2 +6,4 @@ import createInstanceFactory from './create-instance';

import mapObject from './utils/map-object';
import narrowObject from './utils/narrow-object';
import { shallowMerge } from './utils/shallow-merge';

@@ -32,11 +34,25 @@ const $INSTANCES = Symbol('TRUE-DI::INSTANCES');

function diContainer<C extends object>(factories: IFactories<C>): C {
const diContainer: {
<Public extends object>(factories: IFactories<Public>): Public;
<Private extends object, Public extends object>(
privateFactories: Pick<IFactories<Private & Public>, keyof Private>,
publicFactories?: Pick<IFactories<Private & Public>, keyof Public>,
): Public
} = <Private extends object, Public extends object = Private>(
privateFactories: Pick<IFactories<Private & Public>, keyof Private>,
publicFactories?: Pick<IFactories<Private & Public>, keyof Public>,
): Public => {
const instances = new Map();
const factories: IFactories<Private & Public> =
(publicFactories != null
? shallowMerge(privateFactories, publicFactories)
: privateFactories) as any;
const createInstance = createInstanceFactory(factories, instances);
const container: C = allNames(factories).reduce<C>(
const container: Private & Public = allNames(factories).reduce<Private & Public>(
(containerObj, name) =>
Object.defineProperty(containerObj, name, {
configurable: false,
enumerable: Object.getOwnPropertyDescriptor(factories, name).enumerable,
enumerable: Object.getOwnPropertyDescriptor(factories, name)!.enumerable,
get: () => createInstance(container, name),

@@ -59,5 +75,5 @@ set: (value: null | undefined) => {

return container;
}
return publicFactories != null ? narrowObject(container, allNames(publicFactories)) : container;
};
export default diContainer;

@@ -5,3 +5,3 @@ import { IFactory, IFactoryTuple, IFactories } from './types';

const cleaner = <C>(instance: any, container: C, name: keyof C) => {
container[name] = null;
container[name] = null as any;
};

@@ -8,0 +8,0 @@

@@ -1,6 +0,5 @@

/* eslint-disable max-len */
export type IInstanceInitializer<IContainer, N extends keyof IContainer> = (
instance: IContainer[N],
container: IContainer,
name?: N,
name: N,
) => void;

@@ -24,295 +23,1 @@

}
export type IfEquals<X, Y, A=X, B=never> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? A : B;
export type KeysOfType<T, F> = {
[P in keyof T]: IfEquals<{ [Q in P]: T[P] }, { [Q in P]: F }, P>
}[keyof T];
export const expectStrictType = <expected>(value: expected): void => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const newValue: IfEquals<expected, typeof value, true> = true;
};
type Item<token extends string | symbol, T> = {
[p in token]: T
}
export type Services<tokens extends (string|symbol)[], Deps extends any[]> =
tokens extends [] ? {} :
tokens extends [any] ?
& Item<tokens[0], Deps[0]>
:
tokens extends [any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
:
tokens extends [any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
:
tokens extends [any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
:
tokens extends [any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
:
tokens extends [any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
:
tokens extends [any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
:
tokens extends [any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
:
tokens extends [any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
& Item<tokens[14], Deps[14]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
& Item<tokens[14], Deps[14]>
& Item<tokens[15], Deps[15]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
& Item<tokens[14], Deps[14]>
& Item<tokens[15], Deps[15]>
& Item<tokens[16], Deps[16]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
& Item<tokens[14], Deps[14]>
& Item<tokens[15], Deps[15]>
& Item<tokens[16], Deps[16]>
& Item<tokens[17], Deps[17]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
& Item<tokens[14], Deps[14]>
& Item<tokens[15], Deps[15]>
& Item<tokens[16], Deps[16]>
& Item<tokens[17], Deps[17]>
& Item<tokens[18], Deps[18]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
& Item<tokens[14], Deps[14]>
& Item<tokens[15], Deps[15]>
& Item<tokens[16], Deps[16]>
& Item<tokens[17], Deps[17]>
& Item<tokens[18], Deps[18]>
& Item<tokens[19], Deps[19]>
:
tokens extends [any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any] ?
& Item<tokens[0], Deps[0]>
& Item<tokens[1], Deps[1]>
& Item<tokens[2], Deps[2]>
& Item<tokens[3], Deps[3]>
& Item<tokens[4], Deps[4]>
& Item<tokens[5], Deps[5]>
& Item<tokens[6], Deps[6]>
& Item<tokens[7], Deps[7]>
& Item<tokens[8], Deps[8]>
& Item<tokens[9], Deps[9]>
& Item<tokens[10], Deps[10]>
& Item<tokens[11], Deps[11]>
& Item<tokens[12], Deps[12]>
& Item<tokens[13], Deps[13]>
& Item<tokens[14], Deps[14]>
& Item<tokens[15], Deps[15]>
& Item<tokens[16], Deps[16]>
& Item<tokens[17], Deps[17]>
& Item<tokens[18], Deps[18]>
& Item<tokens[19], Deps[19]>
& Item<tokens[20], Deps[20]>
:
never

@@ -22,3 +22,3 @@ export type ETuple<E, T> = [E, null] | [null, T];

const last = stack.shift();
const last = stack.shift()!;
set.delete(last);

@@ -25,0 +25,0 @@

@@ -1,15 +0,23 @@

import { KeysOfType } from '../types';
import allNames from './all-names';
type KeysOfType<T, F> = {
[P in keyof T]: T[P] extends F ? P : never;
}[keyof T];
type ObjectFieldKeys<T> = {
[P in keyof T]: T[P] extends { [k in any]: any } ? P : never;
}[keyof T]
export type IMapping<IContainer extends object, T> = {
[p in keyof T]?: KeysOfType<IContainer, T[p]>
[p in keyof T]?: keyof IContainer & KeysOfType<IContainer, T[p]>
}
export const assignProps = <IContainer extends object, N extends keyof IContainer>(
export const assignProps = <IContainer extends object, N extends ObjectFieldKeys<IContainer>>(
mapping: IMapping<IContainer, IContainer[N]>,
) => (instance: IContainer[N], container: IContainer): void =>
allNames(mapping).forEach(
allNames<IMapping<IContainer, IContainer[N]>>(mapping).forEach(
name => {
instance[name] = container[mapping[name]] as any;
// @ts-ignore
instance[name] = container[mapping[name]!];
},
);

@@ -13,3 +13,3 @@ import allNames from './all-names';

(newObj, key) => Object.defineProperty(newObj, key, {
enumerable: Object.getOwnPropertyDescriptor(obj, key).enumerable,
enumerable: Object.getOwnPropertyDescriptor(obj, key)!.enumerable,
value: mapper(key, obj),

@@ -16,0 +16,0 @@ }),

@@ -265,140 +265,2 @@ export type Merge<T1 extends object, T2 extends object> =

>;
<
T1 extends object,
T2 extends object,
T3 extends object,
T4 extends object,
T5 extends object,
T6 extends object,
T7 extends object,
T8 extends object,
T9 extends object,
TA extends object,
TB extends object,
TC extends object,
TD extends object,
>(
obj1: T1,
obj2: T2,
obj3: T3,
obj4: T4,
obj5: T5,
obj6: T6,
obj7: T7,
obj8: T8,
obj9: T9,
objA: TA,
objB: TB,
objC: TC,
objD: TD,
): Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD
>;
<
T1 extends object,
T2 extends object,
T3 extends object,
T4 extends object,
T5 extends object,
T6 extends object,
T7 extends object,
T8 extends object,
T9 extends object,
TA extends object,
TB extends object,
TC extends object,
TD extends object,
TE extends object,
>(
obj1: T1,
obj2: T2,
obj3: T3,
obj4: T4,
obj5: T5,
obj6: T6,
obj7: T7,
obj8: T8,
obj9: T9,
objA: TA,
objB: TB,
objC: TC,
objD: TD,
objE: TE,
): Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>, TE
>;
<
T1 extends object,
T2 extends object,
T3 extends object,
T4 extends object,
T5 extends object,
T6 extends object,
T7 extends object,
T8 extends object,
T9 extends object,
TA extends object,
TB extends object,
TC extends object,
TD extends object,
TE extends object,
TF extends object,
>(
obj1: T1,
obj2: T2,
obj3: T3,
obj4: T4,
obj5: T5,
obj6: T6,
obj7: T7,
obj8: T8,
obj9: T9,
objA: TA,
objB: TB,
objC: TC,
objD: TD,
objE: TE,
objF: TF,
): Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
Merge<
T1, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>, TA>, TB>, TC>, TD>, TE>, TF
>;
}

@@ -405,0 +267,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc