Skip to content

Commit

Permalink
Merge c8b2557 into 2ff0ac3
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodluckhf committed Jul 2, 2019
2 parents 2ff0ac3 + c8b2557 commit 8af83d3
Show file tree
Hide file tree
Showing 31 changed files with 556 additions and 193 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
- npm run report-coverage
- stage: release
node_js: '8'
before_script:
- npm prune
script:
- npm run build
- npm run semantic-release
31 changes: 30 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
]
},
"dependencies": {
"eerror": "2.0.0"
"class-transformer": "^0.2.3",
"class-validator": "^0.9.1",
"eerror": "2.0.0",
"reflect-metadata": "^0.1.13"
},
"husky": {
"hooks": {
Expand Down
77 changes: 0 additions & 77 deletions src/Injector.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/container.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ManifestInterface } from './manifest.interface';
import { Token } from './internal-types';
import { ManifestInterface as publicManifestInterface } from './public-interfaces/manifest.interface';

export interface ContainerInterface {
loadManifests(manifests: ManifestInterface[]);
loadManifests(manifests: publicManifestInterface[]);
compile();
get(moduleName: string, token: Token);
}
53 changes: 12 additions & 41 deletions src/container.spec.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,21 @@
import 'reflect-metadata';
import { Container } from './container';
import { Module } from './module';
import { AlreadyCompiledError } from './errors/already-compiled.error';

class TestServiceA {}
class TestServiceB {
constructor(testServiceA) {
describe('IoC container', function() {
beforeEach(() => {
// @ts-ignore
this.testServiceA = testServiceA;
}
}

describe('IoC container', () => {
it('should parse manifest to module', () => {
const container = new Container();
const diManifest = {
moduleName: 'tesModule',
providers: [
{
token: 'testServiceA',
useClass: TestServiceA,
},
],
};

container.loadManifests([diManifest]);
// @ts-ignore
expect(container.modules.size).toEqual(1);
// @ts-ignore
expect([...container.modules.values()][0]).toBeInstanceOf(Module);
this.container = new Container({}, {});
});

it('Should throw error if has already compiled', () => {
const container = new Container();
const diManifest = {
moduleName: 'tesModule',
providers: [
{
token: 'testServiceA',
useClass: TestServiceA,
},
],
};
container.loadManifests([diManifest]);
container.compile();
expect(() => {
container.compile();
}).toThrow(AlreadyCompiledError);
expect.assertions(1);
this.container.applyPublicProviders = () => {};
this.container.compile();
try {
this.container.compile();
} catch (e) {
expect(e).toBeInstanceOf(AlreadyCompiledError);
}
});
});
29 changes: 22 additions & 7 deletions src/container.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ContainerInterface } from './container.interface';
import { ManifestInterface } from './manifest.interface';
import { buildPublicToken } from './helpers';
import { InstanceWrapperFactory } from './instance-wrapper-factory';
import { Module } from './module';
import { Injector } from './injector';
import { ModuleHasAlreadyExists } from './errors/module-has-already-exists.error';
Expand All @@ -13,24 +11,41 @@ import {
ModuleInterface,
Token,
} from './internal-types';
import { InstanceWrapperFactoryInterface } from './instance-wrapper-factory.interface';
import { ManifestTransformerInterface } from './manifest-transformer.interface';
import { ManifestInterface as publicManifestInterface } from './public-interfaces/manifest.interface';
import { ManifestInterface } from './dto/manifest.interface';

export class Container implements ContainerInterface {
private readonly instanceWrapperFactory: InstanceWrapperFactoryInterface;

private readonly manifestTransformer: ManifestTransformerInterface;

private readonly publicProviders: Map<Token, InstanceWrapperInterface>;

private readonly modules: Map<string, ModuleInterface>;

private compiled: boolean;

public constructor() {
public constructor(
instanceWrapperAbstractFactory: InstanceWrapperFactoryInterface,
manifestTransformer: ManifestTransformerInterface,
) {
this.instanceWrapperFactory = instanceWrapperAbstractFactory;
this.manifestTransformer = manifestTransformer;

this.publicProviders = new Map();
this.modules = new Map();
this.compiled = false;
}

public loadManifests(manifests: ManifestInterface[]) {
const instanceWrapperFactory = new InstanceWrapperFactory();
manifests.forEach(manifest => {
const newModule = new Module(instanceWrapperFactory, manifest);
public loadManifests(manifestsData: publicManifestInterface[]) {
const parsedManifests: ManifestInterface[] = this.manifestTransformer.transform(
manifestsData,
);

parsedManifests.forEach(manifest => {
const newModule = new Module(this.instanceWrapperFactory, manifest);
if (this.modules.has(newModule.name)) {
throw new ModuleHasAlreadyExists().combine({ module: newModule.name });
}
Expand Down
26 changes: 0 additions & 26 deletions src/dependency.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/dto/dependency.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { buildPublicToken } from '../helpers';
import { DependencyInterface, Token } from '../internal-types';
import { DependencyType } from '../public-interfaces/dependency.interface';

export class DependencyDto implements DependencyInterface {
@IsString()
@IsNotEmpty()
public readonly token: Token;

@IsOptional()
@IsString()
public readonly fromModule: string | null = null;

@IsBoolean()
public readonly autoFactory: boolean = false;

public constructor(config: DependencyType) {
if (Array.isArray(config)) {
const [token, dependencyOptions] = config;
this.token = token;
this.autoFactory =
(dependencyOptions && dependencyOptions.autoFactory) || false;
this.fromModule = dependencyOptions && dependencyOptions.fromModule;
if (this.fromModule) {
this.token = buildPublicToken(this.fromModule, this.token);
}
} else {
this.token = config;
}
}
}
23 changes: 23 additions & 0 deletions src/dto/manifest.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Type } from 'class-transformer';
import {
ArrayNotEmpty,
IsArray,
IsNotEmpty,
IsString,
ValidateNested,
} from 'class-validator';
import { ManifestInterface } from './manifest.interface';
import { ProviderInterface } from './provider.interface';
import { ProviderDto } from './provider.dto';

export class ManifestDto implements ManifestInterface {
@IsNotEmpty()
@IsString()
public moduleName: string;

@IsArray()
@ArrayNotEmpty()
@ValidateNested()
@Type(() => ProviderDto)
public providers: ProviderInterface[];
}
File renamed without changes.
Loading

0 comments on commit 8af83d3

Please sign in to comment.