Skip to content

Commit

Permalink
Merge pull request #1 from codyjdalton/initial
Browse files Browse the repository at this point in the history
Add initial injector
  • Loading branch information
codyjdalton committed May 23, 2018
2 parents ab278e6 + c2b4d3a commit 93a8ebe
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 64 deletions.
41 changes: 41 additions & 0 deletions README.md
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
7 changes: 7 additions & 0 deletions lib/common/models/type.model.ts
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;
}
4 changes: 4 additions & 0 deletions lib/common/types/class-decorator.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Generic 'ClassDecorator' type
*/
export type GenericClassDecorator<T> = (target: T) => void;
16 changes: 16 additions & 0 deletions lib/decorators/injectable.decorator.ts
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
};
};
4 changes: 2 additions & 2 deletions lib/index.ts
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';
34 changes: 34 additions & 0 deletions lib/modules/injector.module.spec.ts
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');
});
});
70 changes: 70 additions & 0 deletions lib/modules/injector.module.ts
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) || [];
}
*/
}();
23 changes: 0 additions & 23 deletions lib/modules/some.module.spec.ts

This file was deleted.

33 changes: 0 additions & 33 deletions lib/modules/some.module.ts

This file was deleted.

27 changes: 27 additions & 0 deletions lib/tsconfig.json
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"
]
}
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "ts-module-seed",
"name": "super-injector",
"description": "",
"version": "0.1.0",
"version": "0.1.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {},
"dependencies": {
"reflect-metadata": "^0.1.12"
},
"devDependencies": {
"@types/chai": "*",
"@types/mocha": "*",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"exclude": [
"dist",
"node_modules",
"**/*.spec.ts",
"lib/**/*.spec.ts"
],
"include": [
"lib/**/*.ts"
Expand Down
2 changes: 2 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"tslint:recommended"
],
"rules": {
"interface-name": false,
"max-classes-per-file": false,
"quotemark": [true, "single"]
}
}

0 comments on commit 93a8ebe

Please sign in to comment.