Skip to content

Commit 01c834d

Browse files
committed
Make @optional, @optional(), fix tests
1 parent 589f473 commit 01c834d

File tree

5 files changed

+21
-28
lines changed

5 files changed

+21
-28
lines changed

Gruntfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ module.exports = function(grunt) {
1212
},
1313

1414
ts: {
15+
options: {
16+
compiler: "./node_modules/typescript/bin/tsc"
17+
},
1518
default: {
1619
src: ['src/**/*.ts'],
1720
tsconfig: './tsconfig.json'

Readme.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ This DI container supports:
1111
* Property and constructor dependencies autowiring
1212
* Simple API, just 3 methods and few decorators to go
1313
* Optional injection (from version 1.2)
14-
* Injecting container as factory (**new** 1.3 version)
15-
* Auto-Creating object factories! (**new** 1.3 version)
14+
* Injecting container as factory (from 1.3 version)
15+
* Auto-Creating object factories! (from 1.3 version)
1616

1717
## Todo
1818
* Lazy injection
@@ -234,7 +234,7 @@ You can specify dependencies by using decorators:
234234
@ConstructorInject(literal: string, method?: FactoryMethod)
235235
// Use before class property/constructor argument with either @Inject or @ConstructorInject. Specified optional (non-strict) resolution
236236
// If dependency wasn't found then leave original value (for property injection) or pass null (for constructor injection)
237-
@Optional
237+
@Optional()
238238
// Used for creating auto factories (See below)
239239
@Factory
240240
```
@@ -286,8 +286,8 @@ import {Optional} from 'huject';
286286
class Test {
287287
public constructor(
288288
service: MyService,
289-
@Optional @ConstructorInject('token') param1: string,
290-
@Optional @ConstructorInject('seed') param2: number)
289+
@Optional() @ConstructorInject('token') param1: string,
290+
@Optional() @ConstructorInject('seed') param2: number)
291291
{
292292
if (param1 !== null) {
293293
....
@@ -304,11 +304,11 @@ import {Optional, Inject} from 'huject';
304304
container.register('classToken', 'mytoken');
305305

306306
class Test {
307-
@Optional
307+
@Optional()
308308
@Inject('classToken')
309309
public classParam1: string = "default string";
310310

311-
@Optional
311+
@Optional()
312312
@Inject('servicePort')
313313
public port: number = 80;
314314
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "huject",
3-
"version": "1.3.4",
3+
"version": "1.4.0",
44
"description": "Typescript dependency injection container for humans",
55
"main": "./src/index.js",
66
"scripts": {

src/decorators.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,15 @@ export function Inject(targetOrFactoryMethodOrLiteral: any, propertyKeyOrFactory
103103
* @constructor
104104
*/
105105
export function Optional(...args: any[]): any {
106-
let target;
107-
switch (args.length) {
108-
// Property @Optional
109-
case 2:
110-
target = args[0];
111-
let propertyKey = args[1];
106+
return function (target: Object, propertyKey: string, parameterIndex: number) {
107+
if (typeof parameterIndex === "undefined") {
108+
// Property @Optional()
112109
Reflect.defineMetadata("inject:property:optional", true, target, propertyKey);
113-
break;
114-
// Constructor @Optional
115-
case 3:
116-
target = args[0];
117-
let parameterIndex = args[2];
110+
} else {
111+
// Constructor @Optional()
118112
let metadataName = 'inject:constructor:param' + parameterIndex + ':optional';
119113
Reflect.defineMetadata(metadataName, true, target);
120-
break;
121-
default:
122-
throw new Error("@Optional decorator is not allowed here");
114+
}
123115
}
124116
}
125117

tests/decorators.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ class ConstructorInjectionOptional {
142142
public num: number;
143143

144144
public constructor(
145-
@ConstructorInject(FactoryMethod.SINGLETON) @Optional service: TestInterface,
146-
@Optional @ConstructorInject('nonexist') num: number
145+
@ConstructorInject(FactoryMethod.SINGLETON) @Optional() service: TestInterface,
146+
@Optional() @ConstructorInject('nonexist') num: number
147147
) {
148148
this.test = service;
149149
this.num = num;
@@ -152,11 +152,11 @@ class ConstructorInjectionOptional {
152152

153153
class PropertyInjectionOptional {
154154
@Inject('nonexiststring')
155-
@Optional
155+
@Optional()
156156
public str: string = "default";
157157

158158
@Inject('nonexistnumber')
159-
@Optional
159+
@Optional()
160160
public num: number = 25;
161161
}
162162

@@ -264,10 +264,8 @@ describe("Testing container's autowiring by using decorators", function () {
264264
});
265265

266266
describe("When property injection has @Optional decorator", () => {
267-
let containerSpy;
268267
beforeEach(() => {
269268
container.register(PropertyInjectionOptional);
270-
containerSpy = sinon.spy(container, 'resolve');
271269
});
272270
it("Should leave property original specified value", () => {
273271
let impl: PropertyInjectionOptional = container.resolve(PropertyInjectionOptional);

0 commit comments

Comments
 (0)