-
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.
Implement Injectable decorator to enable services
- Loading branch information
1 parent
ac26e97
commit 57d1bae
Showing
24 changed files
with
329 additions
and
71 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getIntance, injectableInstance } from '../src/decorators' | ||
|
||
describe('Injectable Test', () => { | ||
test('Injectable from instance', () => { | ||
class A { | ||
getName() { | ||
return 'john' | ||
} | ||
} | ||
const a = new A() | ||
injectableInstance(a) | ||
|
||
class B { | ||
constructor(public a?: A) {} | ||
} | ||
|
||
const b = new B() | ||
|
||
if (b.a) { | ||
expect(b.a.getName()).toBe(a.getName()) | ||
} | ||
|
||
const aInstance = getIntance(A) | ||
expect(aInstance.getName()).toBe(a.getName()) | ||
}) | ||
}) |
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,9 @@ | ||
import { start } from '../src' | ||
|
||
test('Start app', async () => { | ||
try { | ||
await start(3000) | ||
} catch (e) { | ||
expect(e.code).toBe('FST_ERR_REOPENED_SERVER') | ||
} | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Injectable } from '../../src' | ||
|
||
@Injectable() | ||
export class CatsService { | ||
findOne(id: string): string { | ||
return `This action returns a #${id} cat` | ||
} | ||
} |
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
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,4 @@ | ||
export * from './controller' | ||
export * from './methods' | ||
export * from './params' | ||
export * from './injectable' |
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,32 @@ | ||
import 'reflect-metadata' | ||
import { MOONSHARD_INJECTABLE } from '../utils/constants' | ||
|
||
export function injectableInstance(instance): void { | ||
Reflect.defineMetadata(MOONSHARD_INJECTABLE, instance, instance.constructor) | ||
} | ||
|
||
export function getIntance<T>(target: new () => T): T { | ||
return Reflect.getMetadata(MOONSHARD_INJECTABLE, target) | ||
} | ||
|
||
export function Injectable() { | ||
return (target: any) => { | ||
const newConstructor = spellbook(target) || target | ||
Reflect.defineMetadata(MOONSHARD_INJECTABLE, new newConstructor(), newConstructor) | ||
return newConstructor | ||
} | ||
} | ||
|
||
export function spellbook(target) { | ||
const paramTypes: any[] = Reflect.getOwnMetadata('design:paramtypes', target) | ||
if (paramTypes) { | ||
const newArgs = paramTypes.map((param) => { | ||
const currentInstance = Reflect.getMetadata(MOONSHARD_INJECTABLE, param) | ||
if (currentInstance) { | ||
return currentInstance | ||
} | ||
}) | ||
return target.bind(null, ...newArgs) | ||
} | ||
return Reflect.getMetadata(MOONSHARD_INJECTABLE, target) | ||
} |
Oops, something went wrong.