Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default service identifier target to class when omitted #186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/decorator/fluent_provide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import ProvideDoneSyntax from "../syntax/provide_done_syntax";
import interfaces from "../interfaces/interfaces";
import { interfaces as inversifyInterfaces } from "inversify";

function fluentProvide(serviceIdentifier: inversifyInterfaces.ServiceIdentifier<any>) {

let bindingWhenOnSyntax = (bind: inversifyInterfaces.Bind, target: any) => bind<any>(serviceIdentifier).to(target);
function fluentProvide(serviceIdentifier?: inversifyInterfaces.ServiceIdentifier<any>) {
let bindingWhenOnSyntax = (bind: inversifyInterfaces.Bind, target: any) => bind<any>(serviceIdentifier || target).to(target);
let bindingConstraintFunction = (bind: inversifyInterfaces.Bind, target: any) => (<any>bindingWhenOnSyntax(bind, target))._binding;
let provideDoneSyntax = new ProvideDoneSyntax(bindingConstraintFunction);

Expand Down
6 changes: 3 additions & 3 deletions src/decorator/provide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import interfaces from "../interfaces/interfaces";
import { METADATA_KEY } from "../constants";

function provide(
serviceIdentifier: inversifyInterfaces.ServiceIdentifier<any>,
serviceIdentifier?: inversifyInterfaces.ServiceIdentifier<any>,
force?: boolean
) {

return function (target: any) {

const isAlreadyDecorated = Reflect.hasOwnMetadata(inversify_METADATA_KEY.PARAM_TYPES, target);
const redecorateWithInject = force === true;

Expand All @@ -30,7 +29,8 @@ function provide(
}

const currentMetadata: interfaces.ProvideSyntax = {
constraint: (bind: inversifyInterfaces.Bind, bindTarget: any) => bind(serviceIdentifier).to(bindTarget),
constraint: (bind: inversifyInterfaces.Bind, bindTarget: any) =>
bind(serviceIdentifier || bindTarget).to(bindTarget),
implementationType: target
};

Expand Down
25 changes: 8 additions & 17 deletions test/decorator/fluent_provide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,14 @@ describe("fluentProvide", () => {

});

it("Should work if @provide is applied more than once with force flag", () => {

const provideSingleton = (identifier: any) => {
return fluentProvide(identifier)
.inSingletonScope()
.done(true); // IMPORTANT!
};

function shouldThrow() {
@provideSingleton("Ninja")
@provideSingleton("SilentNinja")
class Ninja { }
return Ninja;
}

expect(shouldThrow).not.to.throw();

it("Should work if @fluentProvide is applied with no arguments", () => {
function shouldThrow() {
@fluentProvide().inSingletonScope().done()
class Ninja {}
return Ninja;
}

expect(shouldThrow).not.to.throw();
});

});
90 changes: 90 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,51 @@ describe("inversify-binding-decorators", () => {

});

it("Should be able to declare bindings using classes as identifiers on empty provide arguments", () => {
let container = new Container();

@provide()
class Katana {
public hit() {
return "cut!";
}
}

@provide()
class Shuriken {
public throw() {
return "hit!";
}
}

@provide()
class Ninja {
public katana: Katana;
public shuriken: Shuriken;

public constructor(katana: Katana, shuriken: Shuriken) {
this.katana = katana;
this.shuriken = shuriken;
}

public fight() {
return this.katana.hit();
}
public sneak() {
return this.shuriken.throw();
}
}

container.load(buildProviderModule());
let ninja = container.get<Ninja>(Ninja);

expect(ninja instanceof Ninja).eql(true);
expect(ninja.katana instanceof Katana).eql(true);
expect(ninja.shuriken instanceof Shuriken).eql(true);
expect(ninja.fight()).eql("cut!");
expect(ninja.sneak()).eql("hit!");
});

it("Should be able to declare bindings using symbols as identifiers", () => {

let container = new Container();
Expand Down Expand Up @@ -482,4 +527,49 @@ describe("inversify-binding-decorators", () => {

});

it("Should be able to declare bindings using classes as identifiers on empty provide arguments", () => {
let container = new Container();

@fluentProvide().inSingletonScope().done()
class Katana {
public hit() {
return "cut!";
}
}

@fluentProvide().inSingletonScope().done()
class Shuriken {
public throw() {
return "hit!";
}
}

@provide()
class Ninja {
public katana: Katana;
public shuriken: Shuriken;

public constructor(katana: Katana, shuriken: Shuriken) {
this.katana = katana;
this.shuriken = shuriken;
}

public fight() {
return this.katana.hit();
}
public sneak() {
return this.shuriken.throw();
}
}

container.load(buildProviderModule());
let ninja = container.get<Ninja>(Ninja);

expect(ninja instanceof Ninja).eql(true);
expect(ninja.katana instanceof Katana).eql(true);
expect(ninja.shuriken instanceof Shuriken).eql(true);
expect(ninja.fight()).eql("cut!");
expect(ninja.sneak()).eql("hit!");
});

});