-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from codyjdalton/initial
Add initial injector
- Loading branch information
Showing
14 changed files
with
216 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
[![Build Status](https://travis-ci.org/codyjdalton/ts-module-seed.svg?branch=master)](https://travis-ci.org/codyjdalton/ts-module-seed) [![Coverage Status](https://coveralls.io/repos/github/codyjdalton/ts-module-seed/badge.svg?branch=master)](https://coveralls.io/github/codyjdalton/ts-module-seed?branch=master) | ||
|
||
# Typescript Module Seed | ||
|
||
One Paragraph of project description goes here | ||
|
||
## Getting Started | ||
|
||
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. | ||
|
||
### Installing | ||
|
||
A step by step series of examples that tell you have to get a development env running | ||
|
||
Say what the step will be | ||
|
||
``` | ||
npm i project-name --save | ||
``` | ||
|
||
End with an example of getting some data out of the system or using it for a little demo | ||
|
||
## Running the tests | ||
|
||
Explain how to run the automated tests for this system | ||
|
||
``` | ||
npm test | ||
``` | ||
|
||
## Deployment | ||
|
||
Add additional notes about how to deploy this on a live system | ||
|
||
## Versioning | ||
|
||
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags). | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* type.model | ||
* Object instance type | ||
*/ | ||
export interface Type<T> { | ||
new(...args: any[]): T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Generic 'ClassDecorator' type | ||
*/ | ||
export type GenericClassDecorator<T> = (target: T) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* service.decorator | ||
*/ | ||
|
||
import { Type } from '../common/models/type.model'; | ||
import { GenericClassDecorator } from '../common/types/class-decorator.type'; | ||
|
||
/** | ||
* @returns {GenericClassDecorator<Type<any>>} | ||
* @constructor | ||
*/ | ||
export const Injectable = (): GenericClassDecorator<Type<any>> => { | ||
return (target: Type<any>) => { | ||
// do something with `target`, e.g. some kind of validation or passing it to the Injector and store them | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// export your main module: | ||
export { SomeModule } from './modules/some.module'; | ||
export { SomeModuleInst } from './modules/some.module'; | ||
export { Injectable } from './decorators/injectable.decorator'; | ||
export { Injector } from './modules/injector.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* some.module.spec | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { Injectable } from '../decorators/injectable.decorator'; | ||
import { Injector } from './injector.module'; | ||
|
||
describe('Injector', () => { | ||
|
||
it('should include class dependencies', () => { | ||
|
||
@Injectable() | ||
class AnotherClass { | ||
public message: string = 'test-message'; | ||
} | ||
|
||
@Injectable() | ||
class TestClass { | ||
|
||
constructor(private anotherClass: AnotherClass) { | ||
} | ||
|
||
get testMessage(): string { | ||
return this.anotherClass.message; | ||
} | ||
} | ||
|
||
const aTestClass: TestClass = Injector.resolve<TestClass>(TestClass); | ||
|
||
expect(aTestClass.testMessage).to.equal('test-message'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* injector.module | ||
* @description Inject dependencies | ||
*/ | ||
import 'reflect-metadata'; | ||
|
||
import { Type } from '../common/models/type.model'; | ||
/** | ||
* The Injector stores services and resolves requested instances. | ||
*/ | ||
export const Injector = new class { | ||
|
||
/** | ||
* Resolves instances by injecting required services | ||
* @param {Type<any>} target | ||
* @returns {T} | ||
*/ | ||
public resolve<T>(target: Type<any>): T { | ||
// tokens are required dependencies, while injections are resolved tokens from the Injector | ||
const tokens: Array<Type<any>> = Reflect.getMetadata('design:paramtypes', target) || []; | ||
const injections = tokens.map((token) => Injector.resolve<any>(token)); | ||
|
||
return new target(...injections); | ||
} | ||
|
||
/** | ||
* Stores a service in the Injector | ||
* @param {Type<any>} target | ||
* @param {object} config - an object containing metadata | ||
* @param {string | symbol} propKey | ||
*/ | ||
/*public set(target: Type<any>, config: object = {}, propKey: string | symbol): void { | ||
Object.keys(config).forEach( | ||
(key: string) => Reflect.defineMetadata(key, config[key], target, propKey), | ||
); | ||
} | ||
/** | ||
* @function get | ||
* @param {Type<any>} target | ||
* @param {string} key | ||
* @param {any} defaultValue | ||
*/ | ||
/*public get(target: Type<any>, key: string, defaultValue: any = null, propKey: any = null): any { | ||
return Reflect.hasMetadata(key, target, propKey ? propKey : undefined) ? | ||
Reflect.getMetadata(key, target, propKey ? propKey : undefined) : | ||
defaultValue; | ||
} | ||
/** | ||
* @function getAll | ||
* @param target | ||
* @param propertyKey | ||
* | ||
* Return all values from a given object | ||
*/ | ||
/*public getAll(target: Type<any>, propertyKey: string): object { | ||
return Reflect.getMetadataKeys(target, propertyKey).reduce( | ||
(result: object, key: string): object => { | ||
result[key] = this.get(target, key, undefined, propertyKey); | ||
return result; | ||
}, {}, | ||
); | ||
} | ||
public getParams(target: Type<any>, propertyKey: string) { | ||
return Reflect.getMetadata('design:paramtypes', target.prototype, propertyKey) || []; | ||
} | ||
*/ | ||
}(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"noImplicitAny": false, | ||
"sourceMap": true, | ||
"removeComments": true, | ||
"outDir": "dist", | ||
"lib": [ | ||
"es6", | ||
"es7", | ||
"esnext", | ||
"dom" | ||
], | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"declaration": true, | ||
"strict": true | ||
}, | ||
"exclude": [ | ||
"dist", | ||
"node_modules" | ||
], | ||
"include": [ | ||
"**/*.spec.ts" | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters