Skip to content

Commit

Permalink
remove tyoescript-ioc dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagobustamante committed Feb 27, 2020
1 parent e3b45df commit 3bddb02
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 87 deletions.
20 changes: 14 additions & 6 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-rest",
"version": "2.2.6",
"version": "3.0.0",
"description": "A Library to create RESTFul APIs with Typescript",
"author": "Thiago da Rosa de Bustamante <[email protected]>",
"keywords": [
Expand Down Expand Up @@ -100,7 +100,8 @@
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.17.0",
"typescript": "^3.7.5",
"typescript-ioc": "^1.2.5"
"typescript-ioc": "^3.0.1",
"typescript-rest-ioc": "^1.0.0"
},
"repository": {
"type": "git",
Expand Down
6 changes: 1 addition & 5 deletions src/server/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import * as debug from 'debug';
import * as fs from 'fs-extra';
import * as path from 'path';
Expand All @@ -14,9 +12,7 @@ export class ServerConfig {
if (CONFIG_FILE && fs.existsSync(CONFIG_FILE)) {
const config = fs.readJSONSync(CONFIG_FILE);
serverDebugger('rest.config file found: %j', config);
if (config.useIoC) {
Server.useIoC(config.es6);
} else if (config.serviceFactory) {
if (config.serviceFactory) {
if (config.serviceFactory.indexOf('.') === 0) {
config.serviceFactory = path.join(process.cwd(), config.serviceFactory);
}
Expand Down
36 changes: 0 additions & 36 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,42 +134,6 @@ export class Server {
}
}

/**
* Configure the Server to use [typescript-ioc](https://github.com/thiagobustamante/typescript-ioc)
* to instantiate the service objects.
* If You plan to use IoC, You must ensure to call this method before any typescript-rest service declaration.
* @param es6 if true, import typescript-ioc/es6
*/
public static useIoC(es6: boolean = false) {
if (!Server.locked) {
const ioc = require(es6 ? 'typescript-ioc/es6' : 'typescript-ioc');
serverDebugger('Configuring a serviceFactory to use typescript-ioc to instantiate services. ES6: ', es6);
Server.registerServiceFactory({
create: (serviceClass) => {
return ioc.Container.get(serviceClass);
},
getTargetClass: (serviceClass: Function) => {
if (_.isArray(serviceClass)) {
return null;
}
let typeConstructor: any = serviceClass;
if (typeConstructor['name'] && typeConstructor['name'] !== 'ioc_wrapper') {
return typeConstructor as FunctionConstructor;
}
typeConstructor = typeConstructor['__parent'];
while (typeConstructor) {
if (typeConstructor['name'] && typeConstructor['name'] !== 'ioc_wrapper') {
return typeConstructor as FunctionConstructor;
}
typeConstructor = typeConstructor['__parent'];
}
serverDebugger('Can not identify the base Type for requested target: %o', serviceClass);
throw TypeError('Can not identify the base Type for requested target');
}
});
}
}

/**
* Return the set oh HTTP verbs configured for the given path
* @param servicePath The path to search HTTP verbs
Expand Down
14 changes: 7 additions & 7 deletions test/integration/ioc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import * as express from 'express';
import * as _ from 'lodash';
import 'mocha';
import * as request from 'request';
import { AutoWired, Inject } from 'typescript-ioc';
import { Inject, OnlyInstantiableByContainer } from 'typescript-ioc';
import ServiceFactoryIoC from 'typescript-rest-ioc';
import { DefaultServiceFactory, GET, Path, Server } from '../../src/typescript-rest';
const expect = chai.expect;

Server.useIoC();
Server.registerServiceFactory(ServiceFactoryIoC);

@AutoWired
@OnlyInstantiableByContainer
export class InjectableObject { }

@AutoWired
@OnlyInstantiableByContainer
@Path('ioctest')
export class IoCService {
@Inject
Expand All @@ -27,7 +28,6 @@ export class IoCService {
}

@Path('ioctest2')
@AutoWired
export class IoCService2 {
@Inject
private injectedObject: InjectableObject;
Expand All @@ -39,7 +39,7 @@ export class IoCService2 {
}

@Path('ioctest3')
@AutoWired
@OnlyInstantiableByContainer
export class IoCService3 {
private injectedObject: InjectableObject;

Expand All @@ -54,7 +54,7 @@ export class IoCService3 {
}

@Path('ioctest4')
@AutoWired
@OnlyInstantiableByContainer
export class IoCService4 extends IoCService2 {
}

Expand Down
30 changes: 0 additions & 30 deletions test/unit/server-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,6 @@ describe('ServerConfig', () => {
serverStub.useIoC.restore();
});

it('should be able to search for config files', async () => {
const config = {
es6: true,
useIoC: true
};

fsStub.existsSync.onCall(0).returns(false);
fsStub.existsSync.onCall(1).returns(true);
fsStub.existsSync.onCall(2).returns(true);
fsStub.readJSONSync.returns(config);
ServerConfig.configure();

expect(serverStub.useIoC).to.have.been.calledOnceWithExactly(config.es6);
});

it('should not use ioc if useIoC is false', async () => {
const config = {
useIoC: false
};

fsStub.existsSync.onCall(0).returns(false);
fsStub.existsSync.onCall(1).returns(true);
fsStub.existsSync.onCall(2).returns(true);
fsStub.readJSONSync.returns(config);
ServerConfig.configure();

expect(serverStub.useIoC).to.not.have.been.called;
expect(serverStub.registerServiceFactory).to.not.have.been.called;
});

it('should use a custom service factory if configured', async () => {
const config = {
serviceFactory: 'myCustomFactory'
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"outDir": "dist",
"sourceMap": true,
"strictNullChecks": false,
"target": "es5"
"target": "es6"
},
"include": [
"src/**/*.ts"
Expand Down

0 comments on commit 3bddb02

Please sign in to comment.