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

Support For Typescript #66

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage/
dist
typings
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src
spec
coverage
typings
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ function Module(app, something, another) {

look at the `example`-folder for a simple howto

# usage with typescript

import the definition file
```javascript
/// <reference path="../node_modules/tiny-di/tiny-di.d.ts" />
```

import tiny-di and do some magic stuff
```javascript
import tinyDi = require('tiny-di');
let injector = new tinyDi();
```

modules which use the $inject configuration have to inherit from TinyDiInjectableBase
```javascript
import TinyDiInjectableBase from 'tiny-di/dist/injectable';

export default class MyModule extends TinyDiInjectableBase {}
MyModule.$inject = {
deps: [],
callAs: 'class'
};
```


# develop

```javascript
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"scripts": {
"prepublish": "npm run clean && npm run build && npm test",
"clean": "rimraf dist",
"test": "istanbul cover jasmine",
"test": "typings install && npm run prepare-spec && istanbul cover jasmine && npm run clean-spec",
"test-travis": "istanbul cover jasmine && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"build": "gulp",
"build": "gulp && tsc",
"watch": "gulp watch",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"prepare-spec": "cd spec && copyfiles **/*.spec.js *.spec.js ../buildspec && cd .. && tsc -p tsconfig.spec.json",
"clean-spec": "rimraf buildspec"
},
"repository": {
"type": "git",
Expand All @@ -28,6 +30,7 @@
"author": "Dennis Saenger <[email protected]>",
"license": "MIT",
"devDependencies": {
"copyfiles": "^1.0.0",
"coveralls": "2.11.4",
"git-changelog": "0.1.8",
"grunt": "0.4.5",
Expand All @@ -41,8 +44,10 @@
"jasmine": "2.3.2",
"proxyquire": "1.7.3",
"rimraf": "2.4.3",
"semantic-release": "^4.3.5",
"source-map-support": "0.3.2",
"semantic-release": "^4.3.5"
"typescript": "^1.8.10",
"typings": "^1.3.1"
},
"dependencies": {
"babel-runtime": "5.8.25"
Expand Down
11 changes: 11 additions & 0 deletions spec/exampleclass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import TinyDiInjectableBase from '../dist/injectable';

export default class MyClass extends TinyDiInjectableBase {
constructor(private dep: any) {
super();
}
}
MyClass.$inject = {
deps: ['dependency'],
callAs: 'class'
};
2 changes: 1 addition & 1 deletion spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
"../buildspec/**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
Expand Down
28 changes: 28 additions & 0 deletions spec/typescript.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
///<reference path="../tiny-di.d.ts" />
import TinyDiInjectableBase from '../dist/injectable';

describe('typescript class', () => {
it('can define $inject property', () => {
class MyClass extends TinyDiInjectableBase {}
MyClass.$inject = ['dependency'];
expect(MyClass.$inject).toBeDefined();
});

it('can be loaded by tiny-di', () => {
let tiny = require('../dist');
let injector: TinyDiInjector = new tiny();
injector.setResolver(resolver);
injector.bind('dependency').to('exampleDep');
injector.bind('myClassInstance').load(__dirname + '/exampleclass.js');

let instance = injector.get('myClassInstance');
expect(instance.dep).toEqual('exampleDep');
});
});

/**
* define resolver to load default exported classes
*/
function resolver(module: string) {
return require(module).default;
}
6 changes: 6 additions & 0 deletions src/injectable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default class TinyDiInjectableBase {
public static $inject: string[] | {
deps: string[],
callAs: 'class' | 'function'
};
}
37 changes: 37 additions & 0 deletions tiny-di.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
declare module "tiny-di" {
export = TinyDiInjector;
}

declare class TinyDiInjector {
bind(name: string): {
to(module: any): TinyDiInjector,
load(module: any): TinyDiInjector,
lazy(module: any): TinyDiInjector
};

ns(namespace: string): {
to(newNamespace: string): TinyDiInjector
}

get(module: string): any

setResolver(resolver: Function): void;

provide(provider: string): {
by(somethingProvider: any): TinyDiInjector
}

setLogger(logger: any): void;

set(key: string, object: any): any;

//Helper
/*getDefaultResolver(req: any): any;
resolveKey(key: string): string;
hasBindings(key): boolean;
setNsBinding(ns: string, dir:string): void;
load(key: string, what: any): any;
lazy(key: string): any;
...
*/
}
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"noImplicitAny": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"sourceMap": true,
"declaration": true,
"target": "es5",
"outDir": "dist"
},

"exclude": [
"node_modules",
"spec",
"dist"
]
}
18 changes: 18 additions & 0 deletions tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"noImplicitAny": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"sourceMap": true,
"declaration": true,
"target": "es5",
"outDir": "buildspec"
},

"exclude": [
"node_modules",
"src",
"buildspec"
]
}
33 changes: 33 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"rules": {
"class-name": true,
"indent": [true, "spaces"],
"no-duplicate-variable": true,
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-var-keyword": true,
"one-line": true,
"quotemark": [true, "single"],
"semicolon": true,
"triple-equals": true,
"typedef-whitespace": [true, {
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}],
"variable-name": [true, "ban-keywords"],
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
],
"eofline": true,
"max-line-length": [true, 100],
"no-consecutive-blank-lines": true
}
}
6 changes: 6 additions & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"globalDependencies": {
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
"node": "registry:dt/node#6.0.0+20160621231320"
}
}