mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-22 11:57:33 +08:00
inversify: provides its own types (#13648)
This commit is contained in:
47
inversify-binding-decorators/index.d.ts
vendored
47
inversify-binding-decorators/index.d.ts
vendored
@@ -1,47 +0,0 @@
|
||||
// Type definitions for inversify 1.0
|
||||
// Project: https://github.com/inversify/inversify-binding-decorators
|
||||
// Definitions by: inversify <https://github.com/inversify/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
import inversify = require("inversify")
|
||||
|
||||
interface IProvideInSyntax<T> extends IProvideDoneSyntax<T> {
|
||||
inSingletonScope(): IProvideWhenOnSyntax<T>;
|
||||
}
|
||||
|
||||
interface IProvideDoneSyntax<T> {
|
||||
done(): (target: any) => any;
|
||||
}
|
||||
|
||||
interface IProvideOnSyntax<T> extends IProvideDoneSyntax<T> {
|
||||
onActivation(fn: (context: inversify.interfaces.Context, injectable: T) => T): IProvideWhenSyntax<T>;
|
||||
}
|
||||
|
||||
interface IProvideInWhenOnSyntax<T> extends IProvideInSyntax<T>, IProvideWhenSyntax<T>, IProvideOnSyntax<T> {}
|
||||
|
||||
interface IProvideWhenOnSyntax<T> extends IProvideWhenSyntax<T>, IProvideOnSyntax<T> {}
|
||||
|
||||
interface IProvideWhenSyntax<T> extends IProvideDoneSyntax<T> {
|
||||
when(constraint: (request: inversify.interfaces.Request) => boolean): IProvideOnSyntax<T>;
|
||||
whenTargetNamed(name: string): IProvideOnSyntax<T>;
|
||||
whenTargetTagged(tag: string, value: any): IProvideOnSyntax<T>;
|
||||
whenInjectedInto(parent: (Function|string)): IProvideOnSyntax<T>;
|
||||
whenParentNamed(name: string): IProvideOnSyntax<T>;
|
||||
whenParentTagged(tag: string, value: any): IProvideOnSyntax<T>;
|
||||
whenAnyAncestorIs(ancestor: (Function|string)): IProvideOnSyntax<T>;
|
||||
whenNoAncestorIs(ancestor: (Function|string)): IProvideOnSyntax<T>;
|
||||
whenAnyAncestorNamed(name: string): IProvideOnSyntax<T>;
|
||||
whenAnyAncestorTagged(tag: string, value: any): IProvideOnSyntax<T>;
|
||||
whenNoAncestorNamed(name: string): IProvideOnSyntax<T>;
|
||||
whenNoAncestorTagged(tag: string, value: any): IProvideOnSyntax<T>;
|
||||
whenAnyAncestorMatches(constraint: (request: inversify.interfaces.Request) => boolean): IProvideOnSyntax<T>;
|
||||
whenNoAncestorMatches(constraint: (request: inversify.interfaces.Request) => boolean): IProvideOnSyntax<T>;
|
||||
}
|
||||
|
||||
export function autoProvide(kernel: inversify.interfaces.Kernel, ...modules: any[]): void;
|
||||
|
||||
export function makeProvideDecorator(kernel: inversify.interfaces.Kernel):
|
||||
(serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>)) => (target: any) => any;
|
||||
|
||||
export function makeFluentProvideDecorator(kernel: inversify.interfaces.Kernel):
|
||||
(serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>)) => IProvideInWhenOnSyntax<any>;
|
||||
@@ -1,159 +0,0 @@
|
||||
import { inject, Kernel } from "inversify";
|
||||
import { autoProvide, makeProvideDecorator, makeFluentProvideDecorator } from "inversify-binding-decorators";
|
||||
|
||||
module decorator {
|
||||
|
||||
let kernel = new Kernel();
|
||||
let provide = makeProvideDecorator(kernel);
|
||||
|
||||
interface Warrior {
|
||||
fight(): string;
|
||||
sneak(): string;
|
||||
}
|
||||
|
||||
interface Weapon {
|
||||
hit(): string;
|
||||
}
|
||||
|
||||
interface ThrowableWeapon {
|
||||
throw(): string;
|
||||
}
|
||||
|
||||
let TYPE = {
|
||||
ThrowableWeapon: "ThrowableWeapon",
|
||||
Warrior: "Warrior",
|
||||
Weapon: "Weapon"
|
||||
};
|
||||
|
||||
@provide(TYPE.Weapon)
|
||||
class Katana implements Weapon {
|
||||
public hit() {
|
||||
return "cut!";
|
||||
}
|
||||
}
|
||||
|
||||
@provide(TYPE.ThrowableWeapon)
|
||||
class Shuriken implements ThrowableWeapon {
|
||||
public throw() {
|
||||
return "hit!";
|
||||
}
|
||||
}
|
||||
|
||||
@provide(TYPE.Warrior)
|
||||
class Ninja implements Warrior {
|
||||
|
||||
private _katana: Weapon;
|
||||
private _shuriken: ThrowableWeapon;
|
||||
|
||||
public constructor(
|
||||
@inject(TYPE.Weapon) katana: Weapon,
|
||||
@inject(TYPE.ThrowableWeapon) shuriken: ThrowableWeapon
|
||||
) {
|
||||
this._katana = katana;
|
||||
this._shuriken = shuriken;
|
||||
}
|
||||
|
||||
public fight() { return this._katana.hit(); };
|
||||
public sneak() { return this._shuriken.throw(); };
|
||||
|
||||
}
|
||||
|
||||
let ninja = kernel.get<Warrior>(TYPE.Warrior);
|
||||
console.log(ninja);
|
||||
|
||||
}
|
||||
|
||||
module fluent_decorator {
|
||||
|
||||
let kernel = new Kernel();
|
||||
let provide = makeFluentProvideDecorator(kernel);
|
||||
|
||||
let provideSingleton = function(identifier: string) {
|
||||
return provide(identifier).inSingletonScope().done();
|
||||
};
|
||||
|
||||
let provideTransient = function(identifier: string) {
|
||||
return provide(identifier).done();
|
||||
};
|
||||
|
||||
interface Warrior {
|
||||
fight(): string;
|
||||
sneak(): string;
|
||||
}
|
||||
|
||||
interface Weapon {
|
||||
hit(): string;
|
||||
}
|
||||
|
||||
interface ThrowableWeapon {
|
||||
throw(): string;
|
||||
}
|
||||
|
||||
let TYPE = {
|
||||
ThrowableWeapon: "ThrowableWeapon",
|
||||
Warrior: "Warrior",
|
||||
Weapon: "Weapon"
|
||||
};
|
||||
|
||||
@provideSingleton(TYPE.Weapon)
|
||||
class Katana implements Weapon {
|
||||
private _mark: any;
|
||||
public constructor() {
|
||||
this._mark = Math.random();
|
||||
}
|
||||
public hit() {
|
||||
return "cut! " + this._mark;
|
||||
}
|
||||
}
|
||||
|
||||
@provideTransient(TYPE.ThrowableWeapon)
|
||||
class Shuriken implements ThrowableWeapon {
|
||||
private _mark: any;
|
||||
public constructor() {
|
||||
this._mark = Math.random();
|
||||
}
|
||||
public throw() {
|
||||
return "hit! " + this._mark;
|
||||
}
|
||||
}
|
||||
|
||||
@provideTransient(TYPE.Warrior)
|
||||
class Ninja implements Warrior {
|
||||
|
||||
private _katana: Weapon;
|
||||
private _shuriken: ThrowableWeapon;
|
||||
|
||||
public constructor(
|
||||
@inject(TYPE.Weapon) katana: Weapon,
|
||||
@inject(TYPE.ThrowableWeapon) shuriken: ThrowableWeapon
|
||||
) {
|
||||
this._katana = katana;
|
||||
this._shuriken = shuriken;
|
||||
}
|
||||
|
||||
public fight() { return this._katana.hit(); };
|
||||
public sneak() { return this._shuriken.throw(); };
|
||||
|
||||
}
|
||||
|
||||
let ninja = kernel.get<Warrior>(TYPE.Warrior);
|
||||
console.log(ninja);
|
||||
|
||||
}
|
||||
|
||||
module auto_provide {
|
||||
|
||||
let warriors = {
|
||||
Ninja: class Ninja {},
|
||||
Samurai: class Samurai {}
|
||||
};
|
||||
|
||||
let weapons = {
|
||||
Katana: class Katana {},
|
||||
Shuriken: class Shuriken {},
|
||||
};
|
||||
|
||||
let kernel = new Kernel();
|
||||
autoProvide(kernel, warriors, weapons);
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"experimentalDecorators": true,
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"inversify-binding-decorators-tests.ts"
|
||||
]
|
||||
}
|
||||
60
inversify-express-utils/index.d.ts
vendored
60
inversify-express-utils/index.d.ts
vendored
@@ -1,60 +0,0 @@
|
||||
// Type definitions for inversify-express-utils 1.0.0
|
||||
// Project: https://github.com/inversify/inversify-express-utils
|
||||
// Definitions by: inversify <https://github.com/inversify/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference types="inversify" />
|
||||
/// <reference types="express" />
|
||||
|
||||
declare module "inversify-express-utils" {
|
||||
|
||||
import * as express from "express";
|
||||
import * as inversify from "inversify";
|
||||
|
||||
export namespace interfaces {
|
||||
|
||||
export interface InversifyExpressServerConstructor {
|
||||
new(kernel: inversify.interfaces.Kernel): InversifyExpressServer;
|
||||
}
|
||||
|
||||
export interface InversifyExpressServer {
|
||||
setConfig(fn: ConfigFunction): InversifyExpressServer;
|
||||
setErrorConfig(fn: ConfigFunction): InversifyExpressServer;
|
||||
build(): express.Application;
|
||||
}
|
||||
|
||||
export interface ConfigFunction {
|
||||
(app: express.Application): void;
|
||||
}
|
||||
|
||||
export interface HandlerDecoratorFactory {
|
||||
(path: string, ...middleware: express.RequestHandler[]): HandlerDecorator;
|
||||
}
|
||||
|
||||
export interface HandlerDecorator {
|
||||
(target: any, key: string, value: any): void;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export interface Controller {}
|
||||
|
||||
interface ServiceIdentifiers {
|
||||
Controller: Symbol;
|
||||
}
|
||||
|
||||
export var InversifyExpressServer: interfaces.InversifyExpressServerConstructor;
|
||||
|
||||
export var Controller: (path: string, ...middleware: express.RequestHandler[]) => (target: any) => void;
|
||||
|
||||
export var All: interfaces.HandlerDecoratorFactory;
|
||||
export var Get: interfaces.HandlerDecoratorFactory;
|
||||
export var Post: interfaces.HandlerDecoratorFactory;
|
||||
export var Put: interfaces.HandlerDecoratorFactory;
|
||||
export var Patch: interfaces.HandlerDecoratorFactory;
|
||||
export var Head: interfaces.HandlerDecoratorFactory;
|
||||
export var Delete: interfaces.HandlerDecoratorFactory;
|
||||
export var Method: (method: string, path: string, ...middleware: express.RequestHandler[]) => interfaces.HandlerDecorator;
|
||||
export var TYPE: ServiceIdentifiers;
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
import { InversifyExpressServer, Controller, Get, All, Delete, Head, Put, Patch, Post, Method, TYPE } from "inversify-express-utils";
|
||||
import * as express from "express";
|
||||
import { Kernel } from "inversify";
|
||||
|
||||
let kernel = new Kernel();
|
||||
|
||||
module server {
|
||||
|
||||
let server = new InversifyExpressServer(kernel);
|
||||
|
||||
server
|
||||
.setConfig((app: express.Application) => {
|
||||
app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
console.log("hello world");
|
||||
next();
|
||||
});
|
||||
})
|
||||
.setErrorConfig((app: express.Application) => {
|
||||
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
console.error(err.stack);
|
||||
res.status(500).send("Something broke!");
|
||||
});
|
||||
})
|
||||
.build()
|
||||
.listen(3000, "localhost");
|
||||
}
|
||||
|
||||
module decorators {
|
||||
|
||||
@Controller("/")
|
||||
class TestController {
|
||||
|
||||
@Get("/")
|
||||
public testGet() { return "GET"; }
|
||||
|
||||
@All("/")
|
||||
public testAll() { return "ALL"; }
|
||||
|
||||
@Delete("/")
|
||||
public testDelete() { return "DELETE"; }
|
||||
|
||||
@Head("/")
|
||||
public testHead() { return "HEAD"; }
|
||||
|
||||
@Put("/")
|
||||
public testPut() { return "PUT"; }
|
||||
|
||||
@Patch("/")
|
||||
public testPatch() { return "PATCH"; }
|
||||
|
||||
@Post("/")
|
||||
public testPost() { return "POST"; }
|
||||
|
||||
@Method("foo", "/")
|
||||
public testMethod() { return "METHOD:FOO"; }
|
||||
}
|
||||
|
||||
kernel.bind<Controller>(TYPE.Controller).to(TestController);
|
||||
|
||||
function m1(req: express.Request, res: express.Response, next: express.NextFunction) { next(); }
|
||||
function m2(req: express.Request, res: express.Response, next: express.NextFunction) { next(); }
|
||||
function m3(req: express.Request, res: express.Response, next: express.NextFunction) { next(); }
|
||||
|
||||
@Controller("/", m1, m2, m3)
|
||||
class TestMiddlewareController {
|
||||
|
||||
@Get("/", m1, m2, m3)
|
||||
public testGet() { return "GET"; }
|
||||
|
||||
@All("/", m1, m2, m3)
|
||||
public testAll() { return "ALL"; }
|
||||
|
||||
@Delete("/", m1, m2, m3)
|
||||
public testDelete() { return "DELETE"; }
|
||||
|
||||
@Head("/", m1, m2, m3)
|
||||
public testHead() { return "HEAD"; }
|
||||
|
||||
@Put("/", m1, m2, m3)
|
||||
public testPut() { return "PUT"; }
|
||||
|
||||
@Patch("/", m1, m2, m3)
|
||||
public testPatch() { return "PATCH"; }
|
||||
|
||||
@Post("/", m1, m2, m3)
|
||||
public testPost() { return "POST"; }
|
||||
|
||||
@Method("foo", "/", m1, m2, m3)
|
||||
public testMethod() { return "METHOD:FOO"; }
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"experimentalDecorators": true,
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"inversify-express-utils-tests.ts"
|
||||
]
|
||||
}
|
||||
23
inversify-inject-decorators/index.d.ts
vendored
23
inversify-inject-decorators/index.d.ts
vendored
@@ -1,23 +0,0 @@
|
||||
// Type definitions for inversify-inject-decorators 1.0
|
||||
// Project: https://github.com/inversify/inversify-inject-decorators
|
||||
// Definitions by: inversify <https://github.com/inversify/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
import inversify = require("inversify")
|
||||
|
||||
interface InjectDecorators {
|
||||
|
||||
lazyInject: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>)) =>
|
||||
(proto: any, key: string) => void;
|
||||
|
||||
lazyInjectNamed: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>), named: string) =>
|
||||
(proto: any, key: string) => void;
|
||||
|
||||
lazyInjectTagged: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>), key: string, value: any) =>
|
||||
(proto: any, propertyName: string) => void;
|
||||
|
||||
lazyMultiInject: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable<any>)) =>
|
||||
(proto: any, key: string) => void;
|
||||
}
|
||||
|
||||
export default function getDecorators(kernel: inversify.interfaces.Kernel): InjectDecorators;
|
||||
@@ -1,211 +0,0 @@
|
||||
import getDecorators from "inversify-inject-decorators";
|
||||
import { Kernel, injectable, tagged, named } from "inversify";
|
||||
|
||||
module lazyInject {
|
||||
|
||||
let kernel = new Kernel();
|
||||
let { lazyInject } = getDecorators(kernel);
|
||||
let TYPES = { Weapon: "Weapon" };
|
||||
|
||||
interface Weapon {
|
||||
name: string;
|
||||
durability: number;
|
||||
use(): void;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Sword implements Weapon {
|
||||
public name: string;
|
||||
public durability: number;
|
||||
public constructor() {
|
||||
this.durability = 100;
|
||||
this.name = "Sword";
|
||||
}
|
||||
public use() {
|
||||
this.durability = this.durability - 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Warrior {
|
||||
@lazyInject(TYPES.Weapon)
|
||||
public weapon: Weapon;
|
||||
}
|
||||
|
||||
kernel.bind<Weapon>(TYPES.Weapon).to(Sword);
|
||||
|
||||
let warrior = new Warrior();
|
||||
console.log(warrior.weapon instanceof Sword); // true
|
||||
|
||||
}
|
||||
|
||||
module lazyInjectNamed {
|
||||
|
||||
let kernel = new Kernel();
|
||||
let { lazyInjectNamed } = getDecorators(kernel);
|
||||
let TYPES = { Weapon: "Weapon" };
|
||||
|
||||
interface Weapon {
|
||||
name: string;
|
||||
durability: number;
|
||||
use(): void;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Sword implements Weapon {
|
||||
public name: string;
|
||||
public durability: number;
|
||||
public constructor() {
|
||||
this.durability = 100;
|
||||
this.name = "Sword";
|
||||
}
|
||||
public use() {
|
||||
this.durability = this.durability - 10;
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Shuriken implements Weapon {
|
||||
public name: string;
|
||||
public durability: number;
|
||||
public constructor() {
|
||||
this.durability = 100;
|
||||
this.name = "Shuriken";
|
||||
}
|
||||
public use() {
|
||||
this.durability = this.durability - 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Warrior {
|
||||
|
||||
@lazyInjectNamed(TYPES.Weapon, "not-throwwable")
|
||||
@named("not-throwwable")
|
||||
public primaryWeapon: Weapon;
|
||||
|
||||
@lazyInjectNamed(TYPES.Weapon, "throwwable")
|
||||
@named("throwwable")
|
||||
public secondaryWeapon: Weapon;
|
||||
|
||||
}
|
||||
|
||||
kernel.bind<Weapon>(TYPES.Weapon).to(Sword).whenTargetNamed("not-throwwable");
|
||||
kernel.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetNamed("throwwable");
|
||||
|
||||
let warrior = new Warrior();
|
||||
console.log(warrior.primaryWeapon instanceof Sword); // true
|
||||
console.log(warrior.primaryWeapon instanceof Shuriken); // true
|
||||
|
||||
}
|
||||
|
||||
module lazyInjectTagged {
|
||||
|
||||
let kernel = new Kernel();
|
||||
let { lazyInjectTagged } = getDecorators(kernel);
|
||||
let TYPES = { Weapon: "Weapon" };
|
||||
|
||||
interface Weapon {
|
||||
name: string;
|
||||
durability: number;
|
||||
use(): void;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Sword implements Weapon {
|
||||
public name: string;
|
||||
public durability: number;
|
||||
public constructor() {
|
||||
this.durability = 100;
|
||||
this.name = "Sword";
|
||||
}
|
||||
public use() {
|
||||
this.durability = this.durability - 10;
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Shuriken implements Weapon {
|
||||
public name: string;
|
||||
public durability: number;
|
||||
public constructor() {
|
||||
this.durability = 100;
|
||||
this.name = "Shuriken";
|
||||
}
|
||||
public use() {
|
||||
this.durability = this.durability - 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Warrior {
|
||||
|
||||
@lazyInjectTagged(TYPES.Weapon, "throwwable", false)
|
||||
@tagged("throwwable", false)
|
||||
public primaryWeapon: Weapon;
|
||||
|
||||
@lazyInjectTagged(TYPES.Weapon, "throwwable", true)
|
||||
@tagged("throwwable", true)
|
||||
public secondaryWeapon: Weapon;
|
||||
|
||||
}
|
||||
|
||||
kernel.bind<Weapon>(TYPES.Weapon).to(Sword).whenTargetTagged("throwwable", false);
|
||||
kernel.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetTagged("throwwable", true);
|
||||
|
||||
let warrior = new Warrior();
|
||||
console.log(warrior.primaryWeapon instanceof Sword); // true
|
||||
console.log(warrior.primaryWeapon instanceof Shuriken); // true
|
||||
|
||||
}
|
||||
|
||||
module lazyMultiInject {
|
||||
|
||||
let kernel = new Kernel();
|
||||
let { lazyMultiInject } = getDecorators(kernel);
|
||||
let TYPES = { Weapon: "Weapon" };
|
||||
|
||||
interface Weapon {
|
||||
name: string;
|
||||
durability: number;
|
||||
use(): void;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Sword implements Weapon {
|
||||
public name: string;
|
||||
public durability: number;
|
||||
public constructor() {
|
||||
this.durability = 100;
|
||||
this.name = "Sword";
|
||||
}
|
||||
public use() {
|
||||
this.durability = this.durability - 10;
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Shuriken implements Weapon {
|
||||
public name: string;
|
||||
public durability: number;
|
||||
public constructor() {
|
||||
this.durability = 100;
|
||||
this.name = "Shuriken";
|
||||
}
|
||||
public use() {
|
||||
this.durability = this.durability - 10;
|
||||
}
|
||||
}
|
||||
|
||||
class Warrior {
|
||||
|
||||
@lazyMultiInject(TYPES.Weapon)
|
||||
public weapons: Weapon[];
|
||||
|
||||
}
|
||||
|
||||
kernel.bind<Weapon>(TYPES.Weapon).to(Sword);
|
||||
kernel.bind<Weapon>(TYPES.Weapon).to(Shuriken);
|
||||
|
||||
let warrior = new Warrior();
|
||||
console.log(warrior.weapons[0] instanceof Sword); // true
|
||||
console.log(warrior.weapons[1] instanceof Shuriken); // true
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"experimentalDecorators": true,
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"inversify-inject-decorators-tests.ts"
|
||||
]
|
||||
}
|
||||
65
inversify-logger-middleware/index.d.ts
vendored
65
inversify-logger-middleware/index.d.ts
vendored
@@ -1,65 +0,0 @@
|
||||
// Type definitions for inversify-logger-middleware 1.0.0
|
||||
// Project: https://github.com/inversify/inversify-logger-middleware
|
||||
// Definitions by: inversify <https://github.com/inversify/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
import * as inversify from "inversify";
|
||||
|
||||
export namespace interfaces {
|
||||
|
||||
export interface LoggerSettings {
|
||||
request?: RequestLoggerSettings;
|
||||
time?: boolean;
|
||||
}
|
||||
|
||||
export interface RequestLoggerSettings {
|
||||
serviceIdentifier?: boolean;
|
||||
bindings?: BindingLoggerSettings;
|
||||
target?: TargetLoggerSettings;
|
||||
}
|
||||
|
||||
export interface BindingLoggerSettings {
|
||||
activated?: boolean;
|
||||
serviceIdentifier?: boolean;
|
||||
implementationType?: boolean;
|
||||
factory?: boolean;
|
||||
provider?: boolean;
|
||||
constraint?: boolean;
|
||||
onActivation?: boolean;
|
||||
cache?: boolean;
|
||||
dynamicValue?: boolean;
|
||||
scope?: boolean;
|
||||
type?: boolean;
|
||||
}
|
||||
|
||||
export interface TargetLoggerSettings {
|
||||
serviceIdentifier?: boolean;
|
||||
name?: boolean;
|
||||
metadata?: boolean;
|
||||
}
|
||||
|
||||
export interface LogEntry {
|
||||
error: boolean;
|
||||
exception: any;
|
||||
guid: string;
|
||||
multiInject: boolean;
|
||||
results: any[];
|
||||
rootRequest: inversify.interfaces.Request;
|
||||
serviceIdentifier: any;
|
||||
target: any;
|
||||
time: string;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export function makeLoggerMiddleware(
|
||||
settings?: interfaces.LoggerSettings,
|
||||
renderer?: (out: interfaces.LogEntry) => void
|
||||
): inversify.interfaces.Middleware;
|
||||
|
||||
export function textSerializer(entry: interfaces.LogEntry): string;
|
||||
export function bindingTypeFormatter(type: number): string;
|
||||
export function scopeFormatter(scope: number): string;
|
||||
|
||||
|
||||
export as namespace inversifyLoggerMiddleware;
|
||||
@@ -1,60 +0,0 @@
|
||||
import * as inversify from "inversify";
|
||||
|
||||
declare var kernel: inversify.interfaces.Kernel;
|
||||
|
||||
import * as inversifyLoggerMiddleware from "inversify-logger-middleware";
|
||||
import { makeLoggerMiddleware, textSerializer } from "inversify-logger-middleware";
|
||||
|
||||
interface LoggerOutput<T> {
|
||||
entry: T;
|
||||
}
|
||||
|
||||
let makeStringRenderer = function (loggerOutput: LoggerOutput<string>) {
|
||||
return function (entry: inversifyLoggerMiddleware.interfaces.LogEntry) {
|
||||
loggerOutput.entry = textSerializer(entry);
|
||||
};
|
||||
};
|
||||
|
||||
let makeObjRenderer = function (loggerOutput: LoggerOutput<any>) {
|
||||
return function (entry: inversifyLoggerMiddleware.interfaces.LogEntry) {
|
||||
loggerOutput.entry = entry;
|
||||
};
|
||||
};
|
||||
|
||||
let options: inversifyLoggerMiddleware.interfaces.LoggerSettings = {
|
||||
request: {
|
||||
bindings: {
|
||||
activated: true,
|
||||
cache: true,
|
||||
constraint: true,
|
||||
dynamicValue: true,
|
||||
factory: true,
|
||||
implementationType: true,
|
||||
onActivation: true,
|
||||
provider: true,
|
||||
scope: true,
|
||||
serviceIdentifier: true,
|
||||
type: true
|
||||
},
|
||||
serviceIdentifier: true,
|
||||
target: {
|
||||
metadata: true,
|
||||
name: true,
|
||||
serviceIdentifier: true
|
||||
}
|
||||
},
|
||||
time: true
|
||||
};
|
||||
|
||||
let logger = makeLoggerMiddleware();
|
||||
kernel.applyMiddleware(logger);
|
||||
|
||||
let loggerOutput1: LoggerOutput<string> = { entry: null };
|
||||
let stringRenderer1 = makeStringRenderer(loggerOutput1);
|
||||
let logger1 = makeLoggerMiddleware(options, stringRenderer1);
|
||||
kernel.applyMiddleware(logger1);
|
||||
|
||||
let loggerOutput2: LoggerOutput<inversifyLoggerMiddleware.interfaces.LogEntry> = { entry: null };
|
||||
let objRenderer2 = makeObjRenderer(loggerOutput2);
|
||||
let logger2 = makeLoggerMiddleware(options, objRenderer2);
|
||||
kernel.applyMiddleware(logger2);
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"experimentalDecorators": true,
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"inversify-logger-middleware-tests.ts"
|
||||
]
|
||||
}
|
||||
52
inversify-restify-utils/index.d.ts
vendored
52
inversify-restify-utils/index.d.ts
vendored
@@ -1,52 +0,0 @@
|
||||
// Type definitions for inversify-restify-utils 1.0.0
|
||||
// Project: https://github.com/inversify/inversify-restify-utils
|
||||
// Definitions by: inversify <https://github.com/inversify/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
import * as restify from "restify";
|
||||
import * as inversify from "inversify";
|
||||
|
||||
export namespace interfaces {
|
||||
|
||||
export interface InversifyRestifyServerConstructor {
|
||||
new(kernel: inversify.interfaces.Kernel): InversifyRestifyServer;
|
||||
}
|
||||
|
||||
export interface InversifyRestifyServer {
|
||||
setConfig(fn: ConfigFunction): InversifyRestifyServer;
|
||||
build(): restify.Server;
|
||||
}
|
||||
|
||||
export interface ConfigFunction {
|
||||
(app: restify.Server): void;
|
||||
}
|
||||
|
||||
export interface HandlerDecoratorFactory {
|
||||
(path: string, ...middleware: restify.RequestHandler[]): HandlerDecorator;
|
||||
}
|
||||
|
||||
export interface HandlerDecorator {
|
||||
(target: any, key: string, value: any): void;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export interface Controller {}
|
||||
|
||||
interface ServiceIdentifiers {
|
||||
Controller: Symbol;
|
||||
}
|
||||
|
||||
export var InversifyRestifyServer: interfaces.InversifyRestifyServerConstructor;
|
||||
|
||||
export var Controller: (path: string, ...middleware: restify.RequestHandler[]) => (target: any) => void;
|
||||
|
||||
export var Options: interfaces.HandlerDecoratorFactory;
|
||||
export var Get: interfaces.HandlerDecoratorFactory;
|
||||
export var Post: interfaces.HandlerDecoratorFactory;
|
||||
export var Put: interfaces.HandlerDecoratorFactory;
|
||||
export var Patch: interfaces.HandlerDecoratorFactory;
|
||||
export var Head: interfaces.HandlerDecoratorFactory;
|
||||
export var Delete: interfaces.HandlerDecoratorFactory;
|
||||
export var Method: (method: string, path: string, ...middleware: restify.RequestHandler[]) => interfaces.HandlerDecorator;
|
||||
export var TYPE: ServiceIdentifiers;
|
||||
@@ -1,89 +0,0 @@
|
||||
import {
|
||||
InversifyRestifyServer, Controller, Get, Options,
|
||||
Delete, Head, Put, Patch, Post, Method, TYPE
|
||||
} from "inversify-restify-utils";
|
||||
|
||||
import * as restify from "restify";
|
||||
import { Kernel } from "inversify";
|
||||
|
||||
let kernel = new Kernel();
|
||||
|
||||
module server {
|
||||
|
||||
let server = new InversifyRestifyServer(kernel);
|
||||
|
||||
server
|
||||
.setConfig((app: restify.Server) => {
|
||||
app.use((req: restify.Request, res: restify.Response, next: restify.Next) => {
|
||||
console.log("hello world");
|
||||
next();
|
||||
});
|
||||
})
|
||||
.build()
|
||||
.listen(3000, "localhost");
|
||||
}
|
||||
|
||||
module decorators {
|
||||
|
||||
@Controller("/")
|
||||
class TestController {
|
||||
|
||||
@Get("/")
|
||||
public testGet() { return "GET"; }
|
||||
|
||||
@Options("/")
|
||||
public testAll() { return "OPTIONS"; }
|
||||
|
||||
@Delete("/")
|
||||
public testDelete() { return "DELETE"; }
|
||||
|
||||
@Head("/")
|
||||
public testHead() { return "HEAD"; }
|
||||
|
||||
@Put("/")
|
||||
public testPut() { return "PUT"; }
|
||||
|
||||
@Patch("/")
|
||||
public testPatch() { return "PATCH"; }
|
||||
|
||||
@Post("/")
|
||||
public testPost() { return "POST"; }
|
||||
|
||||
@Method("opts", "/")
|
||||
public testMethod() { return "METHOD:OPTS"; }
|
||||
}
|
||||
|
||||
kernel.bind<Controller>(TYPE.Controller).to(TestController);
|
||||
|
||||
function m1(req: restify.Request, res: restify.Response, next: restify.Next) { next(); }
|
||||
function m2(req: restify.Request, res: restify.Response, next: restify.Next) { next(); }
|
||||
function m3(req: restify.Request, res: restify.Response, next: restify.Next) { next(); }
|
||||
|
||||
@Controller("/", m1, m2, m3)
|
||||
class TestMiddlewareController {
|
||||
|
||||
@Get("/", m1, m2, m3)
|
||||
public testGet() { return "GET"; }
|
||||
|
||||
@Options("/", m1, m2, m3)
|
||||
public testAll() { return "OPTIONS"; }
|
||||
|
||||
@Delete("/", m1, m2, m3)
|
||||
public testDelete() { return "DELETE"; }
|
||||
|
||||
@Head("/", m1, m2, m3)
|
||||
public testHead() { return "HEAD"; }
|
||||
|
||||
@Put("/", m1, m2, m3)
|
||||
public testPut() { return "PUT"; }
|
||||
|
||||
@Patch("/", m1, m2, m3)
|
||||
public testPatch() { return "PATCH"; }
|
||||
|
||||
@Post("/", m1, m2, m3)
|
||||
public testPost() { return "POST"; }
|
||||
|
||||
@Method("opts", "/", m1, m2, m3)
|
||||
public testMethod() { return "METHOD:OPTS"; }
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"experimentalDecorators": true,
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"inversify-restify-utils-tests.ts"
|
||||
]
|
||||
}
|
||||
273
inversify/index.d.ts
vendored
273
inversify/index.d.ts
vendored
@@ -1,273 +0,0 @@
|
||||
// Type definitions for inversify 2.0
|
||||
// Project: https://github.com/inversify/InversifyJS
|
||||
// Definitions by: inversify <https://github.com/inversify>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference types="bluebird" />
|
||||
|
||||
interface Symbol {
|
||||
toString(): string;
|
||||
valueOf(): Object;
|
||||
}
|
||||
|
||||
interface SymbolConstructor {
|
||||
(description?: string | number): Symbol;
|
||||
}
|
||||
|
||||
declare var Symbol: SymbolConstructor;
|
||||
|
||||
declare namespace inversify {
|
||||
|
||||
namespace interfaces {
|
||||
|
||||
export interface KernelConstructor {
|
||||
new (): Kernel;
|
||||
}
|
||||
|
||||
export interface KernelModuleConstructor {
|
||||
new (registry: (bind: Bind) => void): KernelModule;
|
||||
}
|
||||
|
||||
export interface Newable<T> {
|
||||
new (...args: any[]): T;
|
||||
}
|
||||
|
||||
export type ServiceIdentifier<T> = (string | Symbol | Newable<T>);
|
||||
|
||||
export interface Binding<T> extends Clonable<Binding<T>> {
|
||||
guid: string;
|
||||
moduleId: string;
|
||||
activated: boolean;
|
||||
serviceIdentifier: ServiceIdentifier<T>;
|
||||
implementationType: Newable<T>;
|
||||
factory: FactoryCreator<any>;
|
||||
provider: ProviderCreator<any>;
|
||||
constraint: (request: Request) => boolean;
|
||||
onActivation: (context: Context, injectable: T) => T;
|
||||
cache: T;
|
||||
dynamicValue: (context: Context) => T;
|
||||
scope: number; // BindingScope
|
||||
type: number; // BindingType
|
||||
}
|
||||
|
||||
export interface Factory<T> extends Function {
|
||||
(...args: any[]): (((...args: any[]) => T) | T);
|
||||
}
|
||||
|
||||
export interface FactoryCreator<T> extends Function {
|
||||
(context: Context): Factory<T>;
|
||||
}
|
||||
|
||||
export interface Provider<T> extends Function {
|
||||
(): Promise<T>;
|
||||
}
|
||||
|
||||
export interface ProviderCreator<T> extends Function {
|
||||
(context: Context): Provider<T>;
|
||||
}
|
||||
|
||||
export interface PlanAndResolve<T> {
|
||||
(args: PlanAndResolveArgs): T[];
|
||||
}
|
||||
|
||||
export interface PlanAndResolveArgs {
|
||||
multiInject: boolean;
|
||||
serviceIdentifier: ServiceIdentifier<any>;
|
||||
target: Target;
|
||||
contextInterceptor: (contexts: Context) => Context;
|
||||
}
|
||||
|
||||
export interface Middleware extends Function {
|
||||
(next: PlanAndResolve<any>): PlanAndResolve<any>;
|
||||
}
|
||||
|
||||
export interface Context {
|
||||
guid: string;
|
||||
kernel: Kernel;
|
||||
plan: Plan;
|
||||
addPlan(plan: Plan): void;
|
||||
}
|
||||
|
||||
export interface ReflectResult {
|
||||
[key: string]: Metadata[];
|
||||
}
|
||||
|
||||
export interface Metadata {
|
||||
key: string;
|
||||
value: any;
|
||||
}
|
||||
|
||||
export interface Plan {
|
||||
parentContext: Context;
|
||||
rootRequest: Request;
|
||||
}
|
||||
|
||||
export interface Planner {
|
||||
createContext(kernel: Kernel): Context;
|
||||
createPlan(parentContext: Context, binding: Binding<any>, target: Target): Plan;
|
||||
getBindings<T>(kernel: Kernel, serviceIdentifier: ServiceIdentifier<T>): Binding<T>[];
|
||||
getActiveBindings(parentRequest: Request, target: Target): Binding<any>[];
|
||||
}
|
||||
|
||||
export interface QueryableString {
|
||||
startsWith(searchString: string): boolean;
|
||||
endsWith(searchString: string): boolean;
|
||||
contains(searchString: string): boolean;
|
||||
equals(compareString: string): boolean;
|
||||
value(): string;
|
||||
}
|
||||
|
||||
export interface Request {
|
||||
guid: string;
|
||||
serviceIdentifier: ServiceIdentifier<any>;
|
||||
parentContext: Context;
|
||||
parentRequest: Request;
|
||||
childRequests: Request[];
|
||||
target: Target;
|
||||
bindings: Binding<any>[];
|
||||
addChildRequest(
|
||||
serviceIdentifier: ServiceIdentifier<any>,
|
||||
bindings: (Binding<any> | Binding<any>[]),
|
||||
target: Target
|
||||
): Request;
|
||||
}
|
||||
|
||||
export interface Target {
|
||||
guid: string;
|
||||
serviceIdentifier: ServiceIdentifier<any>;
|
||||
type: number; // TargetType
|
||||
name: QueryableString;
|
||||
metadata: Array<Metadata>;
|
||||
hasTag(key: string): boolean;
|
||||
isArray(): boolean;
|
||||
matchesArray(name: string | Symbol | Newable<any>): boolean;
|
||||
isNamed(): boolean;
|
||||
isTagged(): boolean;
|
||||
matchesNamedTag(name: string): boolean;
|
||||
matchesTag(key: string): (value: any) => boolean;
|
||||
}
|
||||
|
||||
export interface Resolver {
|
||||
resolve<T>(context: Context): T;
|
||||
}
|
||||
|
||||
export interface Kernel {
|
||||
guid: string;
|
||||
parent: Kernel;
|
||||
bind<T>(serviceIdentifier: ServiceIdentifier<T>): BindingToSyntax<T>;
|
||||
unbind(serviceIdentifier: ServiceIdentifier<any>): void;
|
||||
unbindAll(): void;
|
||||
isBound(serviceIdentifier: ServiceIdentifier<any>): boolean;
|
||||
get<T>(serviceIdentifier: ServiceIdentifier<T>): T;
|
||||
getNamed<T>(serviceIdentifier: ServiceIdentifier<T>, named: string): T;
|
||||
getTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: any): T;
|
||||
getAll<T>(serviceIdentifier: ServiceIdentifier<T>): T[];
|
||||
getAllNamed<T>(serviceIdentifier: ServiceIdentifier<T>, named: string): T[];
|
||||
getAllTagged<T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: any): T[];
|
||||
load(...modules: KernelModule[]): void;
|
||||
unload(...modules: KernelModule[]): void;
|
||||
applyMiddleware(...middleware: Middleware[]): void;
|
||||
getServiceIdentifierAsString(serviceIdentifier: ServiceIdentifier<any>): string;
|
||||
snapshot(): void;
|
||||
restore(): void;
|
||||
}
|
||||
|
||||
export interface Bind extends Function {
|
||||
<T>(serviceIdentifier: ServiceIdentifier<T>): BindingToSyntax<T>;
|
||||
}
|
||||
|
||||
export interface KernelModule {
|
||||
guid: string;
|
||||
registry: (bind: Bind) => void;
|
||||
}
|
||||
|
||||
export interface KernelSnapshot {
|
||||
bindings: Lookup<Binding<any>>;
|
||||
middleware: PlanAndResolve<any>;
|
||||
}
|
||||
|
||||
export interface Clonable<T> {
|
||||
clone(): T;
|
||||
}
|
||||
|
||||
export interface Lookup<T> extends Clonable<Lookup<T>> {
|
||||
add(serviceIdentifier: ServiceIdentifier<any>, value: T): void;
|
||||
get(serviceIdentifier: ServiceIdentifier<any>): Array<T>;
|
||||
remove(serviceIdentifier: ServiceIdentifier<any>): void;
|
||||
removeByModuleId(moduleId: string): void;
|
||||
hasKey(serviceIdentifier: ServiceIdentifier<any>): boolean;
|
||||
}
|
||||
|
||||
export interface KeyValuePair<T> {
|
||||
serviceIdentifier: ServiceIdentifier<any>;
|
||||
value: Array<T>;
|
||||
guid: string;
|
||||
}
|
||||
|
||||
export interface BindingInSyntax<T> {
|
||||
inSingletonScope(): BindingWhenOnSyntax<T>;
|
||||
inTransientScope(): BindingWhenOnSyntax<T>;
|
||||
}
|
||||
|
||||
export interface BindingInWhenOnSyntax<T> extends BindingInSyntax<T>, BindingWhenOnSyntax<T> { }
|
||||
|
||||
export interface BindingOnSyntax<T> {
|
||||
onActivation(fn: (context: Context, injectable: T) => T): BindingWhenSyntax<T>;
|
||||
}
|
||||
|
||||
export interface BindingToSyntax<T> {
|
||||
to(constructor: { new (...args: any[]): T; }): BindingInWhenOnSyntax<T>;
|
||||
toSelf(): BindingInWhenOnSyntax<T>;
|
||||
toConstantValue(value: T): BindingWhenOnSyntax<T>;
|
||||
toDynamicValue(func: (context: Context) => T): BindingWhenOnSyntax<T>;
|
||||
toConstructor<T2>(constructor: Newable<T2>): BindingWhenOnSyntax<T>;
|
||||
toFactory<T2>(factory: FactoryCreator<T2>): BindingWhenOnSyntax<T>;
|
||||
toFunction(func: T): BindingWhenOnSyntax<T>;
|
||||
toAutoFactory<T2>(serviceIdentifier: ServiceIdentifier<T2>): BindingWhenOnSyntax<T>;
|
||||
toProvider<T2>(provider: ProviderCreator<T2>): BindingWhenOnSyntax<T>;
|
||||
}
|
||||
|
||||
export interface BindingWhenOnSyntax<T> extends BindingWhenSyntax<T>, BindingOnSyntax<T> { }
|
||||
|
||||
export interface BindingWhenSyntax<T> {
|
||||
when(constraint: (request: Request) => boolean): BindingOnSyntax<T>;
|
||||
whenTargetNamed(name: string): BindingOnSyntax<T>;
|
||||
whenTargetTagged(tag: string, value: any): BindingOnSyntax<T>;
|
||||
whenInjectedInto(parent: (Function | string)): BindingOnSyntax<T>;
|
||||
whenParentNamed(name: string): BindingOnSyntax<T>;
|
||||
whenParentTagged(tag: string, value: any): BindingOnSyntax<T>;
|
||||
whenAnyAncestorIs(ancestor: (Function | string)): BindingOnSyntax<T>;
|
||||
whenNoAncestorIs(ancestor: (Function | string)): BindingOnSyntax<T>;
|
||||
whenAnyAncestorNamed(name: string): BindingOnSyntax<T>;
|
||||
whenAnyAncestorTagged(tag: string, value: any): BindingOnSyntax<T>;
|
||||
whenNoAncestorNamed(name: string): BindingOnSyntax<T>;
|
||||
whenNoAncestorTagged(tag: string, value: any): BindingOnSyntax<T>;
|
||||
whenAnyAncestorMatches(constraint: (request: Request) => boolean): BindingOnSyntax<T>;
|
||||
whenNoAncestorMatches(constraint: (request: Request) => boolean): BindingOnSyntax<T>;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export var Kernel: interfaces.KernelConstructor;
|
||||
export var KernelModule: interfaces.KernelModuleConstructor;
|
||||
export var decorate: (decorator: (ClassDecorator | ParameterDecorator), target: any, parameterIndex?: number) => void;
|
||||
export function injectable(): (typeConstructor: any) => void;
|
||||
export function tagged(metadataKey: string, metadataValue: any): (target: any, targetKey: string, index?: number) => any;
|
||||
export function named(name: string): (target: any, targetKey: string, index?: number) => any;
|
||||
export function targetName(name: string): (target: any, targetKey: string, index: number) => any;
|
||||
export function unmanaged(): (target: any, targetKey: string, index: number) => any;
|
||||
export function inject(serviceIdentifier: interfaces.ServiceIdentifier<any>): (target: any, targetKey: string, index?: number) => any;
|
||||
export function guid(): string;
|
||||
|
||||
export function multiInject(
|
||||
serviceIdentifier: interfaces.ServiceIdentifier<any>
|
||||
): (target: any, targetKey: string, index?: number) => any;
|
||||
|
||||
// constraint helpers
|
||||
export var traverseAncerstors: (request: interfaces.Request, constraint: (request: interfaces.Request) => boolean) => boolean;
|
||||
export var taggedConstraint: (tag: string) => (value: any) => (request: interfaces.Request) => boolean;
|
||||
export var namedConstraint: (value: any) => (request: interfaces.Request) => boolean;
|
||||
export var typeConstraint: (type: (Function | string)) => (request: interfaces.Request) => boolean;
|
||||
}
|
||||
|
||||
export = inversify;
|
||||
@@ -1,384 +0,0 @@
|
||||
/// <reference types="inversify" />
|
||||
/// <reference types="harmony-proxy" />
|
||||
|
||||
import * as Proxy from "harmony-proxy";
|
||||
|
||||
let injectable = inversify.injectable;
|
||||
let inject = inversify.inject;
|
||||
let tagged = inversify.tagged;
|
||||
let named = inversify.named;
|
||||
let Kernel = inversify.Kernel;
|
||||
let KernelModule = inversify.KernelModule;
|
||||
let targetName = inversify.targetName;
|
||||
let multiInject = inversify.multiInject;
|
||||
let traverseAncerstors = inversify.traverseAncerstors;
|
||||
let taggedConstraint = inversify.taggedConstraint;
|
||||
let namedConstraint = inversify.namedConstraint;
|
||||
let typeConstraint = inversify.typeConstraint;
|
||||
let unmanaged = inversify.unmanaged;
|
||||
|
||||
module external_module_test {
|
||||
|
||||
interface Warrior {
|
||||
fight(): string;
|
||||
sneak(): string;
|
||||
}
|
||||
|
||||
interface Weapon {
|
||||
hit(): string;
|
||||
}
|
||||
|
||||
interface ThrowableWeapon extends Weapon {
|
||||
throw(): string;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Katana implements Weapon {
|
||||
public hit() {
|
||||
return "cut!";
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Shuriken implements ThrowableWeapon {
|
||||
public throw() {
|
||||
return "hit!";
|
||||
}
|
||||
public hit() {
|
||||
return "hit!";
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Ninja implements Warrior {
|
||||
|
||||
private _katana: Weapon;
|
||||
private _shuriken: ThrowableWeapon;
|
||||
|
||||
public constructor(
|
||||
@inject("Weapon") katana: Weapon,
|
||||
@inject("ThrowableWeapon") shuriken: ThrowableWeapon
|
||||
) {
|
||||
this._katana = katana;
|
||||
this._shuriken = shuriken;
|
||||
}
|
||||
|
||||
public fight() { return this._katana.hit(); };
|
||||
public sneak() { return this._shuriken.throw(); };
|
||||
|
||||
}
|
||||
|
||||
let kernel: inversify.interfaces.Kernel = new Kernel();
|
||||
kernel.bind<Warrior>("Warrior").to(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Katana);
|
||||
kernel.bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken).inSingletonScope();
|
||||
|
||||
let ninja = kernel.get<Warrior>("Warrior");
|
||||
console.log(ninja);
|
||||
|
||||
// Unbind
|
||||
kernel.unbind("Warrior");
|
||||
kernel.unbindAll();
|
||||
|
||||
// Kernel modules
|
||||
let warriors: inversify.interfaces.KernelModule = new KernelModule((bind: inversify.interfaces.Bind) => {
|
||||
bind<Warrior>("Warrior").to(Ninja);
|
||||
});
|
||||
|
||||
let weapons: inversify.interfaces.KernelModule = new KernelModule((bind: inversify.interfaces.Bind) => {
|
||||
bind<Weapon>("Weapon").to(Katana);
|
||||
bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken);
|
||||
});
|
||||
|
||||
kernel = new Kernel();
|
||||
kernel.load(warriors, weapons);
|
||||
let ninja2 = kernel.get<Warrior>("Warrior");
|
||||
console.log(ninja2);
|
||||
|
||||
// middleware
|
||||
function logger(planAndResolve: inversify.interfaces.PlanAndResolve<any>): inversify.interfaces.PlanAndResolve<any> {
|
||||
return (args: inversify.interfaces.PlanAndResolveArgs) => {
|
||||
let start = new Date().getTime();
|
||||
let result = planAndResolve(args);
|
||||
let end = new Date().getTime();
|
||||
console.log(end - start);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
kernel.applyMiddleware(logger, logger);
|
||||
|
||||
// binding types
|
||||
kernel.bind<Weapon>("Weapon").to(Katana);
|
||||
kernel.bind<Weapon>("Weapon").toConstantValue(new Katana());
|
||||
kernel.bind<Weapon>("Weapon").toDynamicValue((context: inversify.interfaces.Context) => { return new Katana(); });
|
||||
|
||||
kernel.bind<inversify.interfaces.Newable<Weapon>>("Weapon").toConstructor<Weapon>(Katana);
|
||||
|
||||
kernel.bind<inversify.interfaces.Factory<Weapon>>("Weapon").toFactory<Weapon>((context) => {
|
||||
return () => {
|
||||
return kernel.get<Weapon>("Weapon");
|
||||
};
|
||||
});
|
||||
|
||||
kernel.bind<inversify.interfaces.Factory<Weapon>>("Weapon").toAutoFactory<Weapon>("Weapon");
|
||||
|
||||
kernel.bind<inversify.interfaces.Provider<Weapon>>("Weapon").toProvider<Weapon>((context) => {
|
||||
return () => {
|
||||
return new Promise<Weapon>((resolve) => {
|
||||
let katana = kernel.get<Weapon>("Weapon");
|
||||
resolve(katana);
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).onActivation((context: inversify.interfaces.Context, katanaToBeInjected: Weapon) => {
|
||||
let handler = {
|
||||
apply: function(target: any, thisArgument: any, argumentsList: any[]) {
|
||||
console.log(`Starting: ${performance.now()}`);
|
||||
let result = target.apply(thisArgument, argumentsList);
|
||||
console.log(`Finished: ${performance.now()}`);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
return new Proxy(katanaToBeInjected, handler);
|
||||
});
|
||||
|
||||
|
||||
@injectable()
|
||||
class Samurai implements Warrior {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @tagged("canThrow", false) katana: Weapon,
|
||||
@inject("ThrowableWeapon") @tagged("canThrow", true) shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
kernel.bind<Samurai>("Samurai").to(Samurai);
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).whenTargetTagged("canThrow", false);
|
||||
kernel.bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken).whenTargetTagged("canThrow", true);
|
||||
kernel.getAllTagged<Weapon>("Weapon", "canThrow", false);
|
||||
|
||||
let throwable = tagged("canThrow", true);
|
||||
let notThrowable = tagged("canThrow", false);
|
||||
|
||||
@injectable()
|
||||
class Samurai2 implements Samurai {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @throwable katana: Weapon,
|
||||
@inject("ThrowableWeapon") @notThrowable shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Samurai3 implements Samurai {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @named("strong") katana: Weapon,
|
||||
@inject("ThrowableWeapon") @named("weak") shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
kernel.bind<Warrior>("Warrior").to(Samurai3);
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).whenTargetNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("weak");
|
||||
kernel.getAllNamed<Weapon>("Weapon", "weak");
|
||||
|
||||
@injectable()
|
||||
class Samurai4 implements Samurai {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @targetName("katana") katana: Weapon,
|
||||
@inject("ThrowableWeapon") @targetName("shuriken") shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
kernel.bind<Warrior>("Warrior").to(Samurai4);
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).when((request: inversify.interfaces.Request) => {
|
||||
return request.target.name.equals("katana");
|
||||
});
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when((request: inversify.interfaces.Request) => {
|
||||
return request.target.name.equals("shuriken");
|
||||
});
|
||||
|
||||
// custom constraints
|
||||
let whenParentNamedCanThrowConstraint = (request: inversify.interfaces.Request) => {
|
||||
return namedConstraint("canThrow")(request.parentRequest);
|
||||
};
|
||||
|
||||
let whenAnyAncestorIsConstraint = (request: inversify.interfaces.Request) => {
|
||||
return traverseAncerstors(request, typeConstraint(Ninja));
|
||||
};
|
||||
|
||||
let whenAnyAncestorTaggedConstraint = (request: inversify.interfaces.Request) => {
|
||||
return traverseAncerstors(request, taggedConstraint("canThrow")(true));
|
||||
};
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenParentNamedCanThrowConstraint);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenAnyAncestorIsConstraint);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenAnyAncestorTaggedConstraint);
|
||||
|
||||
// Constraint helpers
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto("Ninja");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenParentNamed("chinese");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenParentTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorIs(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorIs("Ninja");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorMatches(whenParentNamedCanThrowConstraint);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorIs(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorIs("Ninja");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorMatches(whenParentNamedCanThrowConstraint);
|
||||
|
||||
// multi-injection
|
||||
@injectable()
|
||||
class Samurai5 implements Warrior {
|
||||
public katana: Weapon;
|
||||
public shuriken: Weapon;
|
||||
public constructor(
|
||||
@multiInject("Weapon") wpns: Weapon[]
|
||||
) {
|
||||
this.katana = wpns[0];
|
||||
this.shuriken = wpns[1];
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.hit(); };
|
||||
}
|
||||
|
||||
// symbols
|
||||
let SYMBOLS = {
|
||||
ThrowableWeapon: Symbol("ThrowableWeapon"),
|
||||
Warrior: Symbol("Warrior"),
|
||||
Weapon: Symbol("Weapon"),
|
||||
};
|
||||
|
||||
@injectable()
|
||||
class Ninja1 implements Warrior {
|
||||
|
||||
private _katana: Weapon;
|
||||
private _shuriken: ThrowableWeapon;
|
||||
|
||||
public constructor(
|
||||
@inject(SYMBOLS.Weapon) katana: Weapon,
|
||||
@inject(SYMBOLS.ThrowableWeapon) shuriken: ThrowableWeapon
|
||||
) {
|
||||
this._katana = katana;
|
||||
this._shuriken = shuriken;
|
||||
}
|
||||
|
||||
public fight() { return this._katana.hit(); };
|
||||
public sneak() { return this._shuriken.throw(); };
|
||||
|
||||
}
|
||||
|
||||
let kernel3 = new Kernel();
|
||||
kernel3.bind<Warrior>(SYMBOLS.Warrior).to(Ninja);
|
||||
kernel3.bind<Weapon>(SYMBOLS.Weapon).to(Katana);
|
||||
kernel3.bind<ThrowableWeapon>(SYMBOLS.ThrowableWeapon).to(Shuriken).inSingletonScope();
|
||||
|
||||
let ninja4 = kernel3.get<Warrior>("Warrior");
|
||||
console.log(ninja4);
|
||||
|
||||
// classes
|
||||
|
||||
@injectable()
|
||||
class Ninja2 implements Warrior {
|
||||
|
||||
private _katana: Katana;
|
||||
private _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(); };
|
||||
|
||||
}
|
||||
|
||||
let kernel4 = new Kernel();
|
||||
kernel4.bind<Ninja>(Ninja).to(Ninja);
|
||||
kernel4.bind<Katana>(Katana).to(Katana);
|
||||
kernel4.bind<Shuriken>(Shuriken).to(Shuriken).inSingletonScope();
|
||||
|
||||
let ninja5 = kernel4.get<Ninja>(Ninja);
|
||||
console.log(ninja5);
|
||||
|
||||
}
|
||||
|
||||
module snapshot {
|
||||
|
||||
let kernel = new Kernel();
|
||||
|
||||
kernel.snapshot();
|
||||
kernel.restore();
|
||||
|
||||
@injectable()
|
||||
class Test { }
|
||||
|
||||
kernel.bind<Test>(Test).toSelf();
|
||||
kernel.bind<Test>(Test).toSelf().inSingletonScope();
|
||||
}
|
||||
|
||||
module unmanaged_injection {
|
||||
|
||||
let kernel = new Kernel();
|
||||
|
||||
const BaseId = "Base";
|
||||
|
||||
@injectable()
|
||||
class Base {
|
||||
public prop: string;
|
||||
public constructor(@unmanaged() arg: string) { // injected by user
|
||||
this.prop = arg;
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Derived extends Base {
|
||||
public constructor() {
|
||||
super("unmanaged-injected-value"); // user injection
|
||||
}
|
||||
}
|
||||
|
||||
kernel.bind<Base>(BaseId).to(Derived);
|
||||
console.log(kernel.get(BaseId) instanceof Base); // true
|
||||
|
||||
}
|
||||
@@ -1,375 +0,0 @@
|
||||
import {
|
||||
Kernel,
|
||||
injectable, tagged, named, targetName,
|
||||
inject, multiInject, traverseAncerstors,
|
||||
taggedConstraint, namedConstraint, typeConstraint,
|
||||
KernelModule, interfaces, unmanaged
|
||||
} from "inversify";
|
||||
|
||||
import * as Proxy from "harmony-proxy";
|
||||
|
||||
module external_module_test {
|
||||
|
||||
interface Warrior {
|
||||
fight(): string;
|
||||
sneak(): string;
|
||||
}
|
||||
|
||||
interface Weapon {
|
||||
hit(): string;
|
||||
}
|
||||
|
||||
interface ThrowableWeapon extends Weapon {
|
||||
throw(): string;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Katana implements Weapon {
|
||||
public hit() {
|
||||
return "cut!";
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Shuriken implements ThrowableWeapon {
|
||||
public throw() {
|
||||
return "hit!";
|
||||
}
|
||||
public hit() {
|
||||
return "hit!";
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Ninja implements Warrior {
|
||||
|
||||
private _katana: Weapon;
|
||||
private _shuriken: ThrowableWeapon;
|
||||
|
||||
public constructor(
|
||||
@inject("Weapon") katana: Weapon,
|
||||
@inject("ThrowableWeapon") shuriken: ThrowableWeapon
|
||||
) {
|
||||
this._katana = katana;
|
||||
this._shuriken = shuriken;
|
||||
}
|
||||
|
||||
public fight() { return this._katana.hit(); };
|
||||
public sneak() { return this._shuriken.throw(); };
|
||||
|
||||
}
|
||||
|
||||
let kernel: interfaces.Kernel = new Kernel();
|
||||
kernel.bind<Warrior>("Warrior").to(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Katana);
|
||||
kernel.bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken).inSingletonScope();
|
||||
|
||||
let ninja = kernel.get<Warrior>("Warrior");
|
||||
console.log(ninja);
|
||||
|
||||
// Unbind
|
||||
kernel.unbind("Warrior");
|
||||
kernel.unbindAll();
|
||||
|
||||
// Kernel modules
|
||||
let warriors: interfaces.KernelModule = new KernelModule((bind: interfaces.Bind) => {
|
||||
bind<Warrior>("Warrior").to(Ninja);
|
||||
});
|
||||
|
||||
let weapons: interfaces.KernelModule = new KernelModule((bind: interfaces.Bind) => {
|
||||
bind<Weapon>("Weapon").to(Katana);
|
||||
bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken);
|
||||
});
|
||||
|
||||
kernel = new Kernel();
|
||||
kernel.load(warriors, weapons);
|
||||
let ninja2 = kernel.get<Warrior>("Warrior");
|
||||
console.log(ninja2);
|
||||
|
||||
// middleware
|
||||
function logger(planAndResolve: interfaces.PlanAndResolve<any>): interfaces.PlanAndResolve<any> {
|
||||
return (args: interfaces.PlanAndResolveArgs) => {
|
||||
let start = new Date().getTime();
|
||||
let result = planAndResolve(args);
|
||||
let end = new Date().getTime();
|
||||
console.log(end - start);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
kernel.applyMiddleware(logger, logger);
|
||||
|
||||
// binding types
|
||||
kernel.bind<Weapon>("Weapon").to(Katana);
|
||||
kernel.bind<Weapon>("Weapon").toConstantValue(new Katana());
|
||||
kernel.bind<Weapon>("Weapon").toDynamicValue((context: interfaces.Context) => { return new Katana(); });
|
||||
|
||||
kernel.bind<interfaces.Newable<Weapon>>("Weapon").toConstructor<Weapon>(Katana);
|
||||
|
||||
kernel.bind<interfaces.Factory<Weapon>>("Weapon").toFactory<Weapon>((context) => {
|
||||
return () => {
|
||||
return kernel.get<Weapon>("Weapon");
|
||||
};
|
||||
});
|
||||
|
||||
kernel.bind<interfaces.Factory<Weapon>>("Weapon").toAutoFactory<Weapon>("Weapon");
|
||||
|
||||
kernel.bind<interfaces.Provider<Weapon>>("Weapon").toProvider<Weapon>((context) => {
|
||||
return () => {
|
||||
return new Promise<Weapon>((resolve) => {
|
||||
let katana = kernel.get<Weapon>("Weapon");
|
||||
resolve(katana);
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).onActivation((context: interfaces.Context, katanaToBeInjected: Weapon) => {
|
||||
let handler = {
|
||||
apply: function(target: any, thisArgument: any, argumentsList: any[]) {
|
||||
console.log(`Starting: ${performance.now()}`);
|
||||
let result = target.apply(thisArgument, argumentsList);
|
||||
console.log(`Finished: ${performance.now()}`);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
return new Proxy(katanaToBeInjected, handler);
|
||||
});
|
||||
|
||||
|
||||
@injectable()
|
||||
class Samurai implements Warrior {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @tagged("canThrow", false) katana: Weapon,
|
||||
@inject("ThrowableWeapon") @tagged("canThrow", true) shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
kernel.bind<Samurai>("Samurai").to(Samurai);
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).whenTargetTagged("canThrow", false);
|
||||
kernel.bind<ThrowableWeapon>("ThrowableWeapon").to(Shuriken).whenTargetTagged("canThrow", true);
|
||||
kernel.getAllTagged<Weapon>("Weapon", "canThrow", false);
|
||||
|
||||
let throwable = tagged("canThrow", true);
|
||||
let notThrowable = tagged("canThrow", false);
|
||||
|
||||
@injectable()
|
||||
class Samurai2 implements Samurai {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @throwable katana: Weapon,
|
||||
@inject("ThrowableWeapon") @notThrowable shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Samurai3 implements Samurai {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @named("strong") katana: Weapon,
|
||||
@inject("ThrowableWeapon") @named("weak") shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
kernel.bind<Warrior>("Warrior").to(Samurai3);
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).whenTargetNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("weak");
|
||||
kernel.getAllNamed<Weapon>("Weapon", "weak");
|
||||
|
||||
@injectable()
|
||||
class Samurai4 implements Samurai {
|
||||
public katana: Weapon;
|
||||
public shuriken: ThrowableWeapon;
|
||||
public constructor(
|
||||
@inject("Weapon") @targetName("katana") katana: Weapon,
|
||||
@inject("ThrowableWeapon") @targetName("shuriken") shuriken: ThrowableWeapon
|
||||
) {
|
||||
this.katana = katana;
|
||||
this.shuriken = shuriken;
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.throw(); };
|
||||
}
|
||||
|
||||
kernel.bind<Warrior>("Warrior").to(Samurai4);
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Katana).when((request: interfaces.Request) => {
|
||||
return request.target.name.equals("katana");
|
||||
});
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when((request: interfaces.Request) => {
|
||||
return request.target.name.equals("shuriken");
|
||||
});
|
||||
|
||||
// custom constraints
|
||||
let whenParentNamedCanThrowConstraint = (request: interfaces.Request) => {
|
||||
return namedConstraint("canThrow")(request.parentRequest);
|
||||
};
|
||||
|
||||
let whenAnyAncestorIsConstraint = (request: interfaces.Request) => {
|
||||
return traverseAncerstors(request, typeConstraint(Ninja));
|
||||
};
|
||||
|
||||
let whenAnyAncestorTaggedConstraint = (request: interfaces.Request) => {
|
||||
return traverseAncerstors(request, taggedConstraint("canThrow")(true));
|
||||
};
|
||||
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenParentNamedCanThrowConstraint);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenAnyAncestorIsConstraint);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).when(whenAnyAncestorTaggedConstraint);
|
||||
|
||||
// Constraint helpers
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto("Ninja");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenParentNamed("chinese");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenParentTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenTargetTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorIs(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorIs("Ninja");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenAnyAncestorMatches(whenParentNamedCanThrowConstraint);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorIs(Ninja);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorIs("Ninja");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorNamed("strong");
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorTagged("canThrow", true);
|
||||
kernel.bind<Weapon>("Weapon").to(Shuriken).whenNoAncestorMatches(whenParentNamedCanThrowConstraint);
|
||||
|
||||
// multi-injection
|
||||
@injectable()
|
||||
class Samurai5 implements Warrior {
|
||||
public katana: Weapon;
|
||||
public shuriken: Weapon;
|
||||
public constructor(
|
||||
@multiInject("Weapon") wpns: Weapon[]
|
||||
) {
|
||||
this.katana = wpns[0];
|
||||
this.shuriken = wpns[1];
|
||||
}
|
||||
public fight() { return this.katana.hit(); };
|
||||
public sneak() { return this.shuriken.hit(); };
|
||||
}
|
||||
|
||||
// symbols
|
||||
let SYMBOLS = {
|
||||
ThrowableWeapon: Symbol("ThrowableWeapon"),
|
||||
Warrior: Symbol("Warrior"),
|
||||
Weapon: Symbol("Weapon"),
|
||||
};
|
||||
|
||||
@injectable()
|
||||
class Ninja1 implements Warrior {
|
||||
|
||||
private _katana: Weapon;
|
||||
private _shuriken: ThrowableWeapon;
|
||||
|
||||
public constructor(
|
||||
@inject(SYMBOLS.Weapon) katana: Weapon,
|
||||
@inject(SYMBOLS.ThrowableWeapon) shuriken: ThrowableWeapon
|
||||
) {
|
||||
this._katana = katana;
|
||||
this._shuriken = shuriken;
|
||||
}
|
||||
|
||||
public fight() { return this._katana.hit(); };
|
||||
public sneak() { return this._shuriken.throw(); };
|
||||
|
||||
}
|
||||
|
||||
let kernel3 = new Kernel();
|
||||
kernel3.bind<Warrior>(SYMBOLS.Warrior).to(Ninja);
|
||||
kernel3.bind<Weapon>(SYMBOLS.Weapon).to(Katana);
|
||||
kernel3.bind<ThrowableWeapon>(SYMBOLS.ThrowableWeapon).to(Shuriken).inSingletonScope();
|
||||
|
||||
let ninja4 = kernel3.get<Warrior>("Warrior");
|
||||
console.log(ninja4);
|
||||
|
||||
// classes
|
||||
|
||||
@injectable()
|
||||
class Ninja2 implements Warrior {
|
||||
|
||||
private _katana: Katana;
|
||||
private _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(); };
|
||||
|
||||
}
|
||||
|
||||
let kernel4 = new Kernel();
|
||||
kernel4.bind<Ninja>(Ninja).to(Ninja);
|
||||
kernel4.bind<Katana>(Katana).to(Katana);
|
||||
kernel4.bind<Shuriken>(Shuriken).to(Shuriken).inSingletonScope();
|
||||
|
||||
let ninja5 = kernel4.get<Ninja>(Ninja);
|
||||
console.log(ninja5);
|
||||
|
||||
}
|
||||
|
||||
module snapshot {
|
||||
|
||||
let kernel = new Kernel();
|
||||
|
||||
kernel.snapshot();
|
||||
kernel.restore();
|
||||
|
||||
@injectable()
|
||||
class Test { }
|
||||
|
||||
kernel.bind<Test>(Test).toSelf();
|
||||
kernel.bind<Test>(Test).toSelf().inSingletonScope();
|
||||
}
|
||||
|
||||
module unmanaged_injection {
|
||||
|
||||
let kernel = new Kernel();
|
||||
|
||||
const BaseId = "Base";
|
||||
|
||||
@injectable()
|
||||
class Base {
|
||||
public prop: string;
|
||||
public constructor(@unmanaged() arg: string) { // injected by user
|
||||
this.prop = arg;
|
||||
}
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class Derived extends Base {
|
||||
public constructor() {
|
||||
super("unmanaged-injected-value"); // user injection
|
||||
}
|
||||
}
|
||||
|
||||
kernel.bind<Base>(BaseId).to(Derived);
|
||||
console.log(kernel.get(BaseId) instanceof Base); // true
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"experimentalDecorators": true,
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"inversify-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -220,18 +220,54 @@
|
||||
"typingsPackageName": "vue",
|
||||
"sourceRepoURL": "https://github.com/vuejs/vue",
|
||||
"asOfVersion": "2.0.0"
|
||||
},
|
||||
{
|
||||
},
|
||||
{
|
||||
"libraryName": "Facebook's Immutable",
|
||||
"typingsPackageName": "immutable",
|
||||
"sourceRepoURL": "https://github.com/facebook/immutable-js",
|
||||
"asOfVersion": "3.8.7"
|
||||
},
|
||||
{
|
||||
},
|
||||
{
|
||||
"libraryName": "dva",
|
||||
"typingsPackageName": "dva",
|
||||
"sourceRepoURL": "https://github.com/dvajs/dva",
|
||||
"asOfVersion": "1.1.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "inversify",
|
||||
"typingsPackageName": "inversify",
|
||||
"sourceRepoURL": "http://inversify.io",
|
||||
"asOfVersion": "2.0.33"
|
||||
},
|
||||
{
|
||||
"libraryName": "inversify-express-utils",
|
||||
"typingsPackageName": "inversify-express-utils",
|
||||
"sourceRepoURL": "https://github.com/inversify/inversify-express-utils",
|
||||
"asOfVersion": "2.0.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "inversify-binding-decorators",
|
||||
"typingsPackageName": "inversify-binding-decorators",
|
||||
"sourceRepoURL": "https://github.com/inversify/inversify-binding-decorators",
|
||||
"asOfVersion": "2.0.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "inversify-logger-middleware",
|
||||
"typingsPackageName": "inversify-logger-middleware",
|
||||
"sourceRepoURL": "https://github.com/inversify/inversify-logger-middleware",
|
||||
"asOfVersion": "2.0.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "inversify-inject-decorators",
|
||||
"typingsPackageName": "inversify-inject-decorators",
|
||||
"sourceRepoURL": "https://github.com/inversify/inversify-inject-decorators",
|
||||
"asOfVersion": "2.0.0"
|
||||
},
|
||||
{
|
||||
"libraryName": "inversify-restify-utils",
|
||||
"typingsPackageName": "inversify-restify-utils",
|
||||
"sourceRepoURL": "https://github.com/inversify/inversify-restify-utils",
|
||||
"asOfVersion": "2.0.0"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user