Compare commits

...

5 Commits

Author SHA1 Message Date
16bc8e4187 fix: Test 2024-08-28 15:48:45 +02:00
b3de04e3f9 fix: Try another api 2024-08-28 15:46:39 +02:00
e0ab54d38b fix: Fix import paths to relative 2024-08-28 15:12:10 +02:00
45bec44a2d fix: Fix typing in injector 2024-08-28 15:06:38 +02:00
ab07e03c0f version: Fix version number 2024-08-28 13:59:34 +02:00
6 changed files with 61 additions and 60 deletions

9
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "ts-injex", "name": "ts-injex",
"version": "0.0.9", "version": "0.1.0-alpha",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "ts-injex", "name": "ts-injex",
"version": "0.0.9", "version": "0.1.0-alpha",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"eslint-plugin-prettier": "^5.2.1", "eslint-plugin-prettier": "^5.2.1",
@@ -5958,6 +5958,11 @@
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz",
"integrity": "sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==" "integrity": "sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg=="
}, },
"node_modules/reflect-metadata": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.2.tgz",
"integrity": "sha512-urBwgfrvVP/eAyXx4hluJivBKzuEbSQs9rKWCrCkbSxNv8mxPcUZKeuoF3Uy4mJl3Lwprp6yy5/39VWigZ4K6Q=="
},
"node_modules/regexp.prototype.flags": { "node_modules/regexp.prototype.flags": {
"version": "1.5.2", "version": "1.5.2",
"resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.5.2.tgz", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.5.2.tgz",

View File

@@ -1,6 +1,6 @@
{ {
"name": "ts-injex", "name": "ts-injex",
"version": "0.1.0-.1", "version": "0.1.0-alpha",
"description": "Simple boilerplate code free dependency injection system for TypeScript.", "description": "Simple boilerplate code free dependency injection system for TypeScript.",
"type": "module", "type": "module",
"main": "./dist/index.js", "main": "./dist/index.js",

View File

@@ -1,9 +1,9 @@
/* istanbul ignore file */ /* istanbul ignore file */
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { Inject } from 'src/decorators/Inject'; import { Inject } from 'src/decorators/Inject';
import { DependencyResolutionError } from 'src/interfaces/Exceptions'; import { DependencyResolutionError } from '../interfaces/Exceptions';
import { ForceConstructor } from 'src/types/GenericContructor';
import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex'; import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex';
import { ForceConstructor } from '../types/GenericContructor';
/** /**
* Test the Inject decorator. * Test the Inject decorator.

View File

@@ -1,11 +1,11 @@
import { TSinjex } from '../classes/TSinjex';
import { import {
DependencyResolutionError, DependencyResolutionError,
IdentifierRequiredError, IdentifierRequiredError,
InitializationError, InitializationError,
InjectorError, InjectorError,
NoInstantiationMethodError, NoInstantiationMethodError,
} from 'src/interfaces/Exceptions'; } from '../interfaces/Exceptions';
import { TSinjex } from '../classes/TSinjex';
import { Identifier } from '../types/Identifier'; import { Identifier } from '../types/Identifier';
import { InitDelegate } from '../types/InitDelegate'; import { InitDelegate } from '../types/InitDelegate';
@@ -48,10 +48,11 @@ export function Inject<TargetType, DependencyType, PropertyType>(
) { ) {
return function ( return function (
constructor: undefined, constructor: undefined,
context: ClassFieldDecoratorContext<TargetType> & { context: ClassFieldDecoratorContext<TargetType>,
name: PropertyType; ): (
}, this: TargetType,
): void { initialValue: PropertyType | undefined,
) => PropertyType {
const _identifier = identifier ?? context.name; const _identifier = identifier ?? context.name;
if (_identifier == null && necessary === true) if (_identifier == null && necessary === true)
@@ -69,59 +70,54 @@ export function Inject<TargetType, DependencyType, PropertyType>(
); );
}; };
context.addInitializer(function (this: TargetType) { return function (
Object.defineProperty(this, context.name, { this: TargetType,
get() { initialValue: PropertyType | undefined,
let instance: DependencyType | PropertyType | undefined; ): PropertyType {
let instance: DependencyType | PropertyType | undefined;
const dependency: DependencyType | undefined = tryAndCatch( const dependency: DependencyType | undefined = tryAndCatch(
() => resolve(), () => resolve(),
necessary,
_identifier,
DependencyResolutionError,
);
if (dependency != null) {
const initFunction: (() => PropertyType) | undefined =
typeof init === 'function' && dependency != null
? (): PropertyType => init(dependency)
: init === true && hasConstructor(dependency)
? (): PropertyType => new dependency() as PropertyType
: undefined;
if (init == null) instance = dependency;
else if (initFunction != null)
instance = tryAndCatch(
initFunction,
necessary, necessary,
_identifier, _identifier,
DependencyResolutionError, InitializationError,
); );
else if (necessary)
throw new NoInstantiationMethodError(_identifier);
} else if (necessary)
throw new DependencyResolutionError(_identifier);
if (dependency != null) { /**
const initFunction: (() => PropertyType) | undefined = * Replace itself with the resolved dependency
typeof init === 'function' && dependency != null * for performance reasons.
? (): PropertyType => init(dependency) */
: init === true && hasConstructor(dependency) Object.defineProperty(this, context.name, {
? (): PropertyType => value: instance,
new dependency() as PropertyType writable: false,
: undefined; enumerable: false,
configurable: false,
if (init == null) instance = dependency;
else if (initFunction != null)
instance = tryAndCatch(
initFunction,
necessary,
_identifier,
InitializationError,
);
else if (necessary)
throw new NoInstantiationMethodError(_identifier);
} else if (necessary)
throw new DependencyResolutionError(_identifier);
/**
* Replace itself with the resolved dependency
* for performance reasons.
*/
Object.defineProperty(this, context.name, {
value: instance,
writable: false,
enumerable: false,
configurable: false,
});
return instance;
},
/**
* Make the property configurable to allow replacing it
*/
configurable: true,
}); });
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return instance as any;
};
}; };
} }

View File

@@ -1,5 +1,5 @@
import { IdentifierRequiredError } from 'src/interfaces/Exceptions';
import { TSinjex } from '../classes/TSinjex'; import { TSinjex } from '../classes/TSinjex';
import { IdentifierRequiredError } from '../interfaces/Exceptions';
import { Identifier } from '../types/Identifier'; import { Identifier } from '../types/Identifier';
/** /**

View File

@@ -1,5 +1,5 @@
import { IdentifierRequiredError } from 'src/interfaces/Exceptions';
import { TSinjex } from '../classes/TSinjex'; import { TSinjex } from '../classes/TSinjex';
import { IdentifierRequiredError } from '../interfaces/Exceptions';
import { Identifier } from '../types/Identifier'; import { Identifier } from '../types/Identifier';
import { InitDelegate } from '../types/InitDelegate'; import { InitDelegate } from '../types/InitDelegate';