@discordx/di
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -1,5 +0,7 @@ | ||
import type { DependencyContainer } from "tsyringe"; | ||
import type { Container } from "typedi"; | ||
export declare type InstanceOf<T> = T extends new (...args: any[]) => infer R ? R : any; | ||
export declare type DIServiceContainer = DependencyContainer | typeof Container; | ||
import { DefaultDependencyRegistryEngine, TsyringeDependencyRegistryEngine, TypeDiDependencyRegistryEngine } from "./logic/impl/index.js"; | ||
import type { IDependencyRegistryEngine } from "./logic/index.js"; | ||
export declare const typeDiDependencyRegistryEngine: TypeDiDependencyRegistryEngine; | ||
export declare const tsyringeDependencyRegistryEngine: TsyringeDependencyRegistryEngine; | ||
export declare const defaultDependencyRegistryEngine: DefaultDependencyRegistryEngine; | ||
export declare type InstanceOf<T> = T extends new (...args: unknown[]) => infer R ? R : unknown; | ||
/** | ||
@@ -11,16 +13,23 @@ * The dependency injection service creates a single instance of a class and stores it globally using the singleton design pattern | ||
export declare class DIService { | ||
private static _diEngineToUse; | ||
private static _instance; | ||
private static _container?; | ||
static get container(): DIServiceContainer | undefined; | ||
static set container(container: DIServiceContainer | undefined); | ||
static get engine(): IDependencyRegistryEngine; | ||
static set engine(engine: IDependencyRegistryEngine); | ||
static get instance(): DIService; | ||
private _services; | ||
private static _ServiceSet; | ||
/** | ||
* Get all the services from the DI container | ||
* Get all Discord service classes | ||
* @returns {Set<unknown>} | ||
*/ | ||
static get allServices(): Set<unknown>; | ||
/** | ||
* Add a service from the IOC container. | ||
* @param {T} classType - The type of service to add | ||
*/ | ||
addService<T>(classType: T): void; | ||
getService<T>(classType: T): InstanceOf<T>; | ||
private isTsyringe; | ||
/** | ||
* Get a service from the IOC container | ||
* @param {T} classType - the Class of the service to retrieve | ||
* @returns {InstanceOf<T> | null} the instance of this service or null if there is no instance | ||
*/ | ||
getService<T>(classType: T): InstanceOf<T> | null; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DIService = void 0; | ||
const typedi_1 = require("typedi"); | ||
exports.DIService = exports.defaultDependencyRegistryEngine = exports.tsyringeDependencyRegistryEngine = exports.typeDiDependencyRegistryEngine = void 0; | ||
const index_js_1 = require("./logic/impl/index.js"); | ||
// util instances of built-in engines | ||
exports.typeDiDependencyRegistryEngine = index_js_1.TypeDiDependencyRegistryEngine.instance; | ||
exports.tsyringeDependencyRegistryEngine = index_js_1.TsyringeDependencyRegistryEngine.instance; | ||
exports.defaultDependencyRegistryEngine = index_js_1.DefaultDependencyRegistryEngine.instance; | ||
/** | ||
@@ -11,9 +15,9 @@ * The dependency injection service creates a single instance of a class and stores it globally using the singleton design pattern | ||
class DIService { | ||
static _diEngineToUse = exports.defaultDependencyRegistryEngine; | ||
static _instance; | ||
static _container; | ||
static get container() { | ||
return this._container; | ||
static get engine() { | ||
return DIService._diEngineToUse; | ||
} | ||
static set container(container) { | ||
this._container = container; | ||
static set engine(engine) { | ||
DIService._diEngineToUse = engine; | ||
} | ||
@@ -26,45 +30,26 @@ static get instance() { | ||
} | ||
_services = new Map(); | ||
static _ServiceSet = new Set(); | ||
/** | ||
* Get all the services from the DI container | ||
* Get all Discord service classes | ||
* @returns {Set<unknown>} | ||
*/ | ||
static get allServices() { | ||
return DIService._ServiceSet; | ||
return DIService.engine.getAllServices(); | ||
} | ||
/** | ||
* Add a service from the IOC container. | ||
* @param {T} classType - The type of service to add | ||
*/ | ||
addService(classType) { | ||
const clazz = classType; | ||
DIService._ServiceSet.add(clazz); | ||
if (DIService.container) { | ||
if (this.isTsyringe(DIService.container)) { | ||
DIService.container.registerSingleton(clazz); | ||
} | ||
else { | ||
/* | ||
TypeDI classes MUST use @Service(), setting it on the container ONLY apply to tokenization, this is BY design. | ||
we call the decorator directly here. | ||
*/ | ||
(0, typedi_1.Service)()(clazz); | ||
} | ||
} | ||
else { | ||
const instance = new clazz(); | ||
this._services.set(clazz, instance); | ||
} | ||
DIService.engine.addService(classType); | ||
} | ||
/** | ||
* Get a service from the IOC container | ||
* @param {T} classType - the Class of the service to retrieve | ||
* @returns {InstanceOf<T> | null} the instance of this service or null if there is no instance | ||
*/ | ||
getService(classType) { | ||
const clazz = classType; | ||
if (DIService.container) { | ||
if (this.isTsyringe(DIService.container)) { | ||
return DIService.container.resolve(clazz); | ||
} | ||
return DIService.container.get(classType); | ||
} | ||
return this._services.get(classType); | ||
return DIService.engine.getService(classType); | ||
} | ||
isTsyringe(diContainer) { | ||
return "registerSingleton" in diContainer; | ||
} | ||
} | ||
exports.DIService = DIService; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,7 @@ | ||
import type { DependencyContainer } from "tsyringe"; | ||
import type { Container } from "typedi"; | ||
export declare type InstanceOf<T> = T extends new (...args: any[]) => infer R ? R : any; | ||
export declare type DIServiceContainer = DependencyContainer | typeof Container; | ||
import { DefaultDependencyRegistryEngine, TsyringeDependencyRegistryEngine, TypeDiDependencyRegistryEngine } from "./logic/impl/index.js"; | ||
import type { IDependencyRegistryEngine } from "./logic/index.js"; | ||
export declare const typeDiDependencyRegistryEngine: TypeDiDependencyRegistryEngine; | ||
export declare const tsyringeDependencyRegistryEngine: TsyringeDependencyRegistryEngine; | ||
export declare const defaultDependencyRegistryEngine: DefaultDependencyRegistryEngine; | ||
export declare type InstanceOf<T> = T extends new (...args: unknown[]) => infer R ? R : unknown; | ||
/** | ||
@@ -11,16 +13,23 @@ * The dependency injection service creates a single instance of a class and stores it globally using the singleton design pattern | ||
export declare class DIService { | ||
private static _diEngineToUse; | ||
private static _instance; | ||
private static _container?; | ||
static get container(): DIServiceContainer | undefined; | ||
static set container(container: DIServiceContainer | undefined); | ||
static get engine(): IDependencyRegistryEngine; | ||
static set engine(engine: IDependencyRegistryEngine); | ||
static get instance(): DIService; | ||
private _services; | ||
private static _ServiceSet; | ||
/** | ||
* Get all the services from the DI container | ||
* Get all Discord service classes | ||
* @returns {Set<unknown>} | ||
*/ | ||
static get allServices(): Set<unknown>; | ||
/** | ||
* Add a service from the IOC container. | ||
* @param {T} classType - The type of service to add | ||
*/ | ||
addService<T>(classType: T): void; | ||
getService<T>(classType: T): InstanceOf<T>; | ||
private isTsyringe; | ||
/** | ||
* Get a service from the IOC container | ||
* @param {T} classType - the Class of the service to retrieve | ||
* @returns {InstanceOf<T> | null} the instance of this service or null if there is no instance | ||
*/ | ||
getService<T>(classType: T): InstanceOf<T> | null; | ||
} |
@@ -1,2 +0,6 @@ | ||
import { Service } from "typedi"; | ||
import { DefaultDependencyRegistryEngine, TsyringeDependencyRegistryEngine, TypeDiDependencyRegistryEngine, } from "./logic/impl/index.js"; | ||
// util instances of built-in engines | ||
export const typeDiDependencyRegistryEngine = TypeDiDependencyRegistryEngine.instance; | ||
export const tsyringeDependencyRegistryEngine = TsyringeDependencyRegistryEngine.instance; | ||
export const defaultDependencyRegistryEngine = DefaultDependencyRegistryEngine.instance; | ||
/** | ||
@@ -8,9 +12,9 @@ * The dependency injection service creates a single instance of a class and stores it globally using the singleton design pattern | ||
export class DIService { | ||
static _diEngineToUse = defaultDependencyRegistryEngine; | ||
static _instance; | ||
static _container; | ||
static get container() { | ||
return this._container; | ||
static get engine() { | ||
return DIService._diEngineToUse; | ||
} | ||
static set container(container) { | ||
this._container = container; | ||
static set engine(engine) { | ||
DIService._diEngineToUse = engine; | ||
} | ||
@@ -23,44 +27,25 @@ static get instance() { | ||
} | ||
_services = new Map(); | ||
static _ServiceSet = new Set(); | ||
/** | ||
* Get all the services from the DI container | ||
* Get all Discord service classes | ||
* @returns {Set<unknown>} | ||
*/ | ||
static get allServices() { | ||
return DIService._ServiceSet; | ||
return DIService.engine.getAllServices(); | ||
} | ||
/** | ||
* Add a service from the IOC container. | ||
* @param {T} classType - The type of service to add | ||
*/ | ||
addService(classType) { | ||
const clazz = classType; | ||
DIService._ServiceSet.add(clazz); | ||
if (DIService.container) { | ||
if (this.isTsyringe(DIService.container)) { | ||
DIService.container.registerSingleton(clazz); | ||
} | ||
else { | ||
/* | ||
TypeDI classes MUST use @Service(), setting it on the container ONLY apply to tokenization, this is BY design. | ||
we call the decorator directly here. | ||
*/ | ||
Service()(clazz); | ||
} | ||
} | ||
else { | ||
const instance = new clazz(); | ||
this._services.set(clazz, instance); | ||
} | ||
DIService.engine.addService(classType); | ||
} | ||
/** | ||
* Get a service from the IOC container | ||
* @param {T} classType - the Class of the service to retrieve | ||
* @returns {InstanceOf<T> | null} the instance of this service or null if there is no instance | ||
*/ | ||
getService(classType) { | ||
const clazz = classType; | ||
if (DIService.container) { | ||
if (this.isTsyringe(DIService.container)) { | ||
return DIService.container.resolve(clazz); | ||
} | ||
return DIService.container.get(classType); | ||
} | ||
return this._services.get(classType); | ||
return DIService.engine.getService(classType); | ||
} | ||
isTsyringe(diContainer) { | ||
return "registerSingleton" in diContainer; | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
# Stage | ||
## Features | ||
- added support for typedi and unified the docs ([#598](https://github.com/oceanroleplay/discord.ts/issues/598)) ([ea8edb](https://github.com/oceanroleplay/discord.ts/commit/ea8edb99974fc8c1604c333283272e893460777b)) | ||
## Changed | ||
- rename myClass with clazz ([98b15b](https://github.com/oceanroleplay/discord.ts/commit/98b15bc4638591cb945060d402f8d5d1eb9606f1)) | ||
- typo ([#535](https://github.com/oceanroleplay/discord.ts/issues/535)) ([356697](https://github.com/oceanroleplay/discord.ts/commit/356697e0af3e8db832d80d38d671f7e75eae68aa)) | ||
- sort imports ([fb5b0f](https://github.com/oceanroleplay/discord.ts/commit/fb5b0f82661313a4e9e6638db71670a7fb524ac2)) | ||
- seperate internal logics ([57e032](https://github.com/oceanroleplay/discord.ts/commit/57e032a765bee0a66a7f36fabdde0499319606a8)) |
{ | ||
"name": "@discordx/di", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"private": false, | ||
@@ -27,2 +27,3 @@ "description": "dependency injection service with TSyringe support", | ||
"contributors": [ | ||
"Owen Calvin <owen.gombas@gmail.com> (https://github.com/OwenCalvin)", | ||
"Vijay Meena <indianoceanroleplay@gmail.com> (https://github.com/oceanroleplay)", | ||
@@ -55,7 +56,7 @@ "Victorique Blois <loli@victorique.moe> (https://github.com/VictoriqueMoe)" | ||
"dependencies": { | ||
"tsyringe": "^4.6.0", | ||
"tsyringe": "^4.7.0", | ||
"typedi": "^0.10.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.6.3" | ||
"typescript": "^4.7.4" | ||
}, | ||
@@ -62,0 +63,0 @@ "engines": { |
@@ -41,3 +41,3 @@ <div> | ||
Dependency injection service with TSyringe support. | ||
Dependency injection service with TSyringe and TypeDI support. | ||
@@ -44,0 +44,0 @@ # 💻 Installation |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
55122
55
667
1
Updatedtsyringe@^4.7.0