chore: npm run format
This commit is contained in:
@@ -76,4 +76,3 @@ jobs:
|
|||||||
uses: codecov/codecov-action@v4
|
uses: codecov/codecov-action@v4
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ import {
|
|||||||
ServiceCollection,
|
ServiceCollection,
|
||||||
createServiceIdentifier,
|
createServiceIdentifier,
|
||||||
injectable,
|
injectable,
|
||||||
inject
|
inject,
|
||||||
} from '@stevanfreeborn/netdi';
|
} from '@stevanfreeborn/netdi';
|
||||||
|
|
||||||
// Define interfaces
|
// Define interfaces
|
||||||
@@ -82,9 +82,7 @@ class GreetingService implements IGreetingService {
|
|||||||
|
|
||||||
@injectable()
|
@injectable()
|
||||||
class Greeter implements IGreeter {
|
class Greeter implements IGreeter {
|
||||||
constructor(
|
constructor(@inject(greetingServiceIdentifier) private greetingService: IGreetingService) {}
|
||||||
@inject(greetingServiceIdentifier) private greetingService: IGreetingService
|
|
||||||
) {}
|
|
||||||
|
|
||||||
greet(name: string): string {
|
greet(name: string): string {
|
||||||
return this.greetingService.createGreeting(name);
|
return this.greetingService.createGreeting(name);
|
||||||
@@ -146,7 +144,7 @@ services.addTransient(serviceIdentifier, Implementation);
|
|||||||
For complex service instantiation, you can use factory methods:
|
For complex service instantiation, you can use factory methods:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
services.addSingleton(serviceIdentifier, (provider) => {
|
services.addSingleton(serviceIdentifier, provider => {
|
||||||
// Use the provider to get dependencies
|
// Use the provider to get dependencies
|
||||||
const dependency = provider.getService(dependencyIdentifier);
|
const dependency = provider.getService(dependencyIdentifier);
|
||||||
|
|
||||||
@@ -192,7 +190,7 @@ Specifies a dependency for a parameter:
|
|||||||
class MyService {
|
class MyService {
|
||||||
constructor(
|
constructor(
|
||||||
@inject(loggerIdentifier) private logger: ILogger,
|
@inject(loggerIdentifier) private logger: ILogger,
|
||||||
@inject(configIdentifier) private config: IConfig
|
@inject(configIdentifier) private config: IConfig,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
import "reflect-metadata";
|
import 'reflect-metadata';
|
||||||
|
|
||||||
export * from './serviceProvider.js';
|
export * from './serviceProvider.js';
|
||||||
export * from './types.js';
|
export * from './types.js';
|
||||||
|
|||||||
@@ -120,7 +120,10 @@ export class ServiceCollection implements IServiceCollection {
|
|||||||
implementationOrFactory: ServiceFactory<T> | Constructor<T>,
|
implementationOrFactory: ServiceFactory<T> | Constructor<T>,
|
||||||
lifetime: ServiceLifetime,
|
lifetime: ServiceLifetime,
|
||||||
): IServiceCollection {
|
): IServiceCollection {
|
||||||
if (typeof implementationOrFactory === 'function' && this.isConstructor(implementationOrFactory) === false) {
|
if (
|
||||||
|
typeof implementationOrFactory === 'function' &&
|
||||||
|
this.isConstructor(implementationOrFactory) === false
|
||||||
|
) {
|
||||||
const descriptor: ServiceDescriptor<T> = {
|
const descriptor: ServiceDescriptor<T> = {
|
||||||
serviceType,
|
serviceType,
|
||||||
implementationType: Object as unknown as Constructor<T>,
|
implementationType: Object as unknown as Constructor<T>,
|
||||||
@@ -149,8 +152,6 @@ export class ServiceCollection implements IServiceCollection {
|
|||||||
* @returns True if the function is a constructor, false otherwise
|
* @returns True if the function is a constructor, false otherwise
|
||||||
*/
|
*/
|
||||||
private isConstructor(func: unknown): func is Constructor<unknown> {
|
private isConstructor(func: unknown): func is Constructor<unknown> {
|
||||||
return typeof func === 'function' &&
|
return typeof func === 'function' && !!func.prototype && func.prototype.constructor === func;
|
||||||
!!func.prototype &&
|
|
||||||
func.prototype.constructor === func;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-5
@@ -87,7 +87,10 @@ export interface IServiceCollection {
|
|||||||
* @param implementationType - The implementation class
|
* @param implementationType - The implementation class
|
||||||
* @returns The service collection for method chaining
|
* @returns The service collection for method chaining
|
||||||
*/
|
*/
|
||||||
addSingleton<T>(serviceType: ServiceIdentifier<T>, implementationType: Constructor<T>): IServiceCollection;
|
addSingleton<T>(
|
||||||
|
serviceType: ServiceIdentifier<T>,
|
||||||
|
implementationType: Constructor<T>,
|
||||||
|
): IServiceCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a singleton service with a factory function
|
* Registers a singleton service with a factory function
|
||||||
@@ -97,7 +100,10 @@ export interface IServiceCollection {
|
|||||||
* @param factory - A factory function that creates the service instance
|
* @param factory - A factory function that creates the service instance
|
||||||
* @returns The service collection for method chaining
|
* @returns The service collection for method chaining
|
||||||
*/
|
*/
|
||||||
addSingleton<T>(serviceType: ServiceIdentifier<T>, factory: ServiceFactory<T>): IServiceCollection;
|
addSingleton<T>(
|
||||||
|
serviceType: ServiceIdentifier<T>,
|
||||||
|
factory: ServiceFactory<T>,
|
||||||
|
): IServiceCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a scoped service with the collection
|
* Registers a scoped service with the collection
|
||||||
@@ -107,7 +113,10 @@ export interface IServiceCollection {
|
|||||||
* @param implementationType - The implementation class
|
* @param implementationType - The implementation class
|
||||||
* @returns The service collection for method chaining
|
* @returns The service collection for method chaining
|
||||||
*/
|
*/
|
||||||
addScoped<T>(serviceType: ServiceIdentifier<T>, implementationType: Constructor<T>): IServiceCollection;
|
addScoped<T>(
|
||||||
|
serviceType: ServiceIdentifier<T>,
|
||||||
|
implementationType: Constructor<T>,
|
||||||
|
): IServiceCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a scoped service with a factory function
|
* Registers a scoped service with a factory function
|
||||||
@@ -127,7 +136,10 @@ export interface IServiceCollection {
|
|||||||
* @param implementationType - The implementation class
|
* @param implementationType - The implementation class
|
||||||
* @returns The service collection for method chaining
|
* @returns The service collection for method chaining
|
||||||
*/
|
*/
|
||||||
addTransient<T>(serviceType: ServiceIdentifier<T>, implementationType: Constructor<T>): IServiceCollection;
|
addTransient<T>(
|
||||||
|
serviceType: ServiceIdentifier<T>,
|
||||||
|
implementationType: Constructor<T>,
|
||||||
|
): IServiceCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a transient service with a factory function
|
* Registers a transient service with a factory function
|
||||||
@@ -137,7 +149,10 @@ export interface IServiceCollection {
|
|||||||
* @param factory - A factory function that creates the service instance
|
* @param factory - A factory function that creates the service instance
|
||||||
* @returns The service collection for method chaining
|
* @returns The service collection for method chaining
|
||||||
*/
|
*/
|
||||||
addTransient<T>(serviceType: ServiceIdentifier<T>, factory: ServiceFactory<T>): IServiceCollection;
|
addTransient<T>(
|
||||||
|
serviceType: ServiceIdentifier<T>,
|
||||||
|
factory: ServiceFactory<T>,
|
||||||
|
): IServiceCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a service provider from the registered services
|
* Builds a service provider from the registered services
|
||||||
|
|||||||
@@ -79,7 +79,6 @@ describe('ServiceCollection', () => {
|
|||||||
expect(serviceInstance1).not.toBe(serviceInstance2);
|
expect(serviceInstance1).not.toBe(serviceInstance2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should add a transient service with a factory', () => {
|
it('should add a transient service with a factory', () => {
|
||||||
class TestService {}
|
class TestService {}
|
||||||
const serviceIdentifier = createServiceIdentifier();
|
const serviceIdentifier = createServiceIdentifier();
|
||||||
@@ -96,12 +95,12 @@ describe('ServiceCollection', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should add a service with a factory and dependencies', () => {
|
it('should add a service with a factory and dependencies', () => {
|
||||||
type IDependencyService = object
|
type IDependencyService = object;
|
||||||
class DependencyService implements IDependencyService {}
|
class DependencyService implements IDependencyService {}
|
||||||
|
|
||||||
type ITestService = {
|
type ITestService = {
|
||||||
dependency: IDependencyService;
|
dependency: IDependencyService;
|
||||||
}
|
};
|
||||||
class TestService implements ITestService {
|
class TestService implements ITestService {
|
||||||
constructor(public dependency: DependencyService) {}
|
constructor(public dependency: DependencyService) {}
|
||||||
}
|
}
|
||||||
@@ -111,7 +110,10 @@ describe('ServiceCollection', () => {
|
|||||||
|
|
||||||
const collection = new ServiceCollection();
|
const collection = new ServiceCollection();
|
||||||
collection.addSingleton(dependencyServiceIdentifier, DependencyService);
|
collection.addSingleton(dependencyServiceIdentifier, DependencyService);
|
||||||
collection.addSingleton(testServiceIdentifier, (provider) => new TestService(provider.getService(dependencyServiceIdentifier)));
|
collection.addSingleton(
|
||||||
|
testServiceIdentifier,
|
||||||
|
provider => new TestService(provider.getService(dependencyServiceIdentifier)),
|
||||||
|
);
|
||||||
|
|
||||||
const provider = collection.build();
|
const provider = collection.build();
|
||||||
const serviceInstance1 = provider.getService(testServiceIdentifier);
|
const serviceInstance1 = provider.getService(testServiceIdentifier);
|
||||||
|
|||||||
@@ -427,7 +427,7 @@ describe('ServiceProvider', () => {
|
|||||||
expect(initialService).toBe(newService);
|
expect(initialService).toBe(newService);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not clear the scoped instances on dispose from another scope when one scope is disposed", () => {
|
it('should not clear the scoped instances on dispose from another scope when one scope is disposed', () => {
|
||||||
type IService = {
|
type IService = {
|
||||||
id: string;
|
id: string;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user