Skip to content

Commit

Permalink
v0.4.0: Add ability to get all metadata on a class method
Browse files Browse the repository at this point in the history
  • Loading branch information
codyjdalton committed Jun 15, 2018
1 parent c663629 commit 81fcff7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ console.log(Injector.get(TestClass, 'someProp'));
// logs "Hello World" to the console
```

### Set metadata on class methods
### Set and get metadata on class methods

You can set metadata at design time and retrieve it at run time:

Expand All @@ -75,7 +75,8 @@ import { Metadata, Injector } from 'super-injector';
class SomeClass {

@Metadata({
someProp: 'Hello World'
someProp: 'Hello World',
anotherProp: 'Hi World'
})
someMethod(): void {
// ..
Expand All @@ -85,8 +86,10 @@ class SomeClass {
const someClassInstance: AnotherClass = Injector.resolve(someClassInstance);

console.log(Injector.get(someClassInstance, 'someProp', null, 'someMethod'));
// logs Hello World to the console

// logs "Hello World" to the console
console.log(Injector.getAll(someClassInstance, 'someMethod'));
// logs { "someProp": "Hello World", "anotherProp": "Hi World" } to the console
```

## Versioning
Expand Down
23 changes: 23 additions & 0 deletions lib/modules/injector.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,27 @@ describe('Injector', () => {
expect(propVal).to.equal('another-test');
expect(anotherVal).to.equal(null);
});

it('should get all metadata for on a method', () => {

const expectedMetadata: any = {
path: 'some-path',
produces: 'some-test'
};

@Injectable()
class TestComponent {

@Metadata(expectedMetadata)
public someTest() {
// ..
}
}

const aTestComponent: any = Injector.resolve(TestComponent);
const metadata: any = Injector.getAll(aTestComponent, 'someTest');

expect(metadata.path).to.equal(expectedMetadata.path);
expect(metadata.produces).to.equal(expectedMetadata.produces);
});
});
7 changes: 3 additions & 4 deletions lib/modules/injector.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const Injector = new class {
*
* Return all values from a given object
*/
/*public getAll(target: Type<any>, propertyKey: string): 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);
Expand All @@ -63,8 +63,7 @@ export const Injector = new class {
);
}

public getParams(target: Type<any>, propertyKey: string) {
/*public getParams(target: Type<any>, propertyKey: string) {
return Reflect.getMetadata('design:paramtypes', target.prototype, propertyKey) || [];
}
*/
}*/
}();
2 changes: 1 addition & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "super-injector",
"description": "",
"version": "0.3.1",
"description": "Simple DI for Typescript applications",
"version": "0.4.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [],
Expand Down

0 comments on commit 81fcff7

Please sign in to comment.