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",
"version": "0.0.9",
"version": "0.1.0-alpha",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "ts-injex",
"version": "0.0.9",
"version": "0.1.0-alpha",
"license": "MIT",
"dependencies": {
"eslint-plugin-prettier": "^5.2.1",
@@ -5958,6 +5958,11 @@
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz",
"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": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.5.2.tgz",

View File

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

View File

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

View File

@@ -1,11 +1,11 @@
import { TSinjex } from '../classes/TSinjex';
import {
DependencyResolutionError,
IdentifierRequiredError,
InitializationError,
InjectorError,
NoInstantiationMethodError,
} from 'src/interfaces/Exceptions';
import { TSinjex } from '../classes/TSinjex';
} from '../interfaces/Exceptions';
import { Identifier } from '../types/Identifier';
import { InitDelegate } from '../types/InitDelegate';
@@ -48,10 +48,11 @@ export function Inject<TargetType, DependencyType, PropertyType>(
) {
return function (
constructor: undefined,
context: ClassFieldDecoratorContext<TargetType> & {
name: PropertyType;
},
): void {
context: ClassFieldDecoratorContext<TargetType>,
): (
this: TargetType,
initialValue: PropertyType | undefined,
) => PropertyType {
const _identifier = identifier ?? context.name;
if (_identifier == null && necessary === true)
@@ -69,59 +70,54 @@ export function Inject<TargetType, DependencyType, PropertyType>(
);
};
context.addInitializer(function (this: TargetType) {
Object.defineProperty(this, context.name, {
get() {
let instance: DependencyType | PropertyType | undefined;
return function (
this: TargetType,
initialValue: PropertyType | undefined,
): PropertyType {
let instance: DependencyType | PropertyType | undefined;
const dependency: DependencyType | undefined = tryAndCatch(
() => resolve(),
const dependency: DependencyType | undefined = tryAndCatch(
() => 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,
_identifier,
DependencyResolutionError,
InitializationError,
);
else if (necessary)
throw new NoInstantiationMethodError(_identifier);
} else if (necessary)
throw new DependencyResolutionError(_identifier);
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,
_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,
/**
* Replace itself with the resolved dependency
* for performance reasons.
*/
Object.defineProperty(this, context.name, {
value: instance,
writable: false,
enumerable: false,
configurable: false,
});
});
// 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 { IdentifierRequiredError } from '../interfaces/Exceptions';
import { Identifier } from '../types/Identifier';
/**

View File

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