Skip to content

Commit 90ff4c0

Browse files
committed
Rename autoInject => autoInjectable to match injectable
1 parent c2b5df8 commit 90ff4c0

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

Diff for: src/decorators.ts

+8-15
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import "reflect-metadata";
22

33
import {instance as globalContainer, typeInfo} from "./dependency-container";
44
import { InjectionToken, Provider } from "./providers";
5-
import { constructor, Dictionary, RegistrationOptions } from "./types";
6-
7-
const injectionTokenMetadataKey = "injectionTokens";
5+
import { constructor, RegistrationOptions } from "./types";
6+
import {INJECTION_TOKEN_METADATA_KEY, getParamInfo} from "./reflection-helpers";
87

98
/**
109
* Class decorator factory that allows the class' dependencies to be injected
@@ -14,13 +13,7 @@ const injectionTokenMetadataKey = "injectionTokens";
1413
*/
1514
export function injectable<T>(): (target: constructor<T>) => void {
1615
return function(target: constructor<T>): void {
17-
const params: any[] = Reflect.getMetadata("design:paramtypes", target) || [];
18-
const injectionTokens: Dictionary<InjectionToken<any>> = Reflect.getOwnMetadata(injectionTokenMetadataKey, target) || {};
19-
Object.keys(injectionTokens).forEach(key => {
20-
params[+key] = injectionTokens[key];
21-
});
22-
23-
typeInfo.set(target, params);
16+
typeInfo.set(target, getParamInfo(target));
2417
};
2518
}
2619

@@ -32,13 +25,13 @@ export function injectable<T>(): (target: constructor<T>) => void {
3225
*
3326
* @return {Function} The class decorator
3427
*/
35-
export function autoInject(): (target: constructor<any>) => any {
28+
export function autoInjectable(): (target: constructor<any>) => any {
3629
return function(target: constructor<any>): constructor<any> {
37-
injectable()(target);
30+
const paramInfo = getParamInfo(target);
3831

3932
return class extends target {
4033
constructor(...args: any[]) {
41-
super(...args.concat(typeInfo.get(target)!.slice(args.length).map(type => globalContainer.resolve(type))));
34+
super(...args.concat(paramInfo.slice(args.length).map(type => globalContainer.resolve(type))));
4235
}
4336
}
4437
}
@@ -51,9 +44,9 @@ export function autoInject(): (target: constructor<any>) => any {
5144
*/
5245
export function inject(token: InjectionToken<any>): (target: any, propertyKey: string | symbol, parameterIndex: number) => any {
5346
return function(target: any, _propertyKey: string | symbol, parameterIndex: number): any {
54-
const injectionTokens = Reflect.getOwnMetadata(injectionTokenMetadataKey, target) || {};
47+
const injectionTokens = Reflect.getOwnMetadata(INJECTION_TOKEN_METADATA_KEY, target) || {};
5548
injectionTokens[parameterIndex] = token;
56-
Reflect.defineMetadata(injectionTokenMetadataKey, injectionTokens, target);
49+
Reflect.defineMetadata(INJECTION_TOKEN_METADATA_KEY, injectionTokens, target);
5750
};
5851
}
5952

Diff for: src/dependency-container.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class DependencyContainer implements Types.DependencyContainer {
162162

163163
const paramInfo = typeInfo.get(ctor);
164164

165-
if (!paramInfo) {
165+
if (!paramInfo || paramInfo.length === 0) {
166166
throw `TypeInfo not known for ${ctor}`
167167
}
168168

Diff for: src/reflection-helpers.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {InjectionToken} from "./providers";
2+
import {Dictionary, constructor} from "./types";
3+
4+
export const INJECTION_TOKEN_METADATA_KEY = "injectionTokens";
5+
6+
export function getParamInfo(target: constructor<any>): any[] {
7+
const params: any[] = Reflect.getMetadata("design:paramtypes", target) || [];
8+
const injectionTokens: Dictionary<InjectionToken<any>> = Reflect.getOwnMetadata(INJECTION_TOKEN_METADATA_KEY, target) || {};
9+
Object.keys(injectionTokens).forEach(key => {
10+
params[+key] = injectionTokens[key];
11+
});
12+
13+
return params;
14+
}

Diff for: tests/auto-inject.test.ts renamed to tests/auto-injectable.test.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { autoInject, injectable } from "../src/decorators";
1+
import {autoInjectable, injectable} from "../src/decorators";
22
import {instance as globalContainer} from "../src/dependency-container";
33

44
afterEach(() => {
55
globalContainer.reset();
66
});
77

8-
test("@autoInject allows for injection to be performed without using .resolve()", () => {
8+
test("@autoInjectable allows for injection to be performed without using .resolve()", () => {
99
class Bar {}
10-
@autoInject()
10+
@autoInjectable()
1111
class Foo {
1212
constructor(public myBar?: Bar) {}
1313
}
@@ -17,9 +17,9 @@ test("@autoInject allows for injection to be performed without using .resolve()"
1717
expect(myFoo.myBar instanceof Bar).toBeTruthy();
1818
});
1919

20-
test("@autoInject allows for parameters to be specified manually", () => {
20+
test("@autoInjectable allows for parameters to be specified manually", () => {
2121
class Bar {}
22-
@autoInject()
22+
@autoInjectable()
2323
class Foo {
2424
constructor(public myBar?: Bar) {}
2525
}
@@ -30,10 +30,10 @@ test("@autoInject allows for parameters to be specified manually", () => {
3030
expect(myFoo.myBar).toBe(myBar);
3131
});
3232

33-
test("@autoInject injects parameters beyond those specified manually", () => {
33+
test("@autoInjectable injects parameters beyond those specified manually", () => {
3434
class Bar {}
3535
class FooBar {}
36-
@autoInject()
36+
@autoInjectable()
3737
class Foo {
3838
constructor(public myFooBar: FooBar, public myBar?: Bar) {}
3939
}
@@ -45,12 +45,12 @@ test("@autoInject injects parameters beyond those specified manually", () => {
4545
expect(myFoo.myBar instanceof Bar).toBeTruthy();
4646
});
4747

48-
test("@autoInject works when the @autoInject is a polymorphic ancestor", () => {
48+
test("@autoInjectable works when the @autoInjectable is a polymorphic ancestor", () => {
4949
class Foo {
5050
constructor() { }
5151
}
5252

53-
@autoInject()
53+
@autoInjectable()
5454
class Ancestor {
5555
constructor(public myFoo?: Foo) {}
5656
}
@@ -66,14 +66,14 @@ test("@autoInject works when the @autoInject is a polymorphic ancestor", () => {
6666
expect(instance.myFoo instanceof Foo).toBeTruthy();
6767
});
6868

69-
test("@autoInject classes keep behavior from their ancestor's constructors", () => {
69+
test("@autoInjectable classes keep behavior from their ancestor's constructors", () => {
7070
const a = 5;
7171
const b = 4;
7272
class Foo {
7373
constructor() { }
7474
}
7575

76-
@autoInject()
76+
@autoInjectable()
7777
class Ancestor {
7878
public a: number;
7979
constructor(public myFoo?: Foo) {
@@ -96,13 +96,13 @@ test("@autoInject classes keep behavior from their ancestor's constructors", ()
9696
expect(instance.b).toBe(b);
9797
});
9898

99-
test("@autoInject classes resolve their @injectable dependencies", () => {
99+
test("@autoInjectable classes resolve their @injectable dependencies", () => {
100100
class Foo {}
101101
@injectable()
102102
class Bar {
103103
constructor(public myFoo: Foo) {}
104104
}
105-
@autoInject()
105+
@autoInjectable()
106106
class FooBar {
107107
constructor(public myBar?: Bar) {}
108108
}

0 commit comments

Comments
 (0)