Skip to content

Instantly share code, notes, and snippets.

@mreis1
Created February 19, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreis1/a4586d8cf8a8fd87d8a1076234d8558d to your computer and use it in GitHub Desktop.
Save mreis1/a4586d8cf8a8fd87d8a1076234d8558d to your computer and use it in GitHub Desktop.
Node Decorators
import {
Injectable,
Inject,
Container,
InjectionToken
} from '@decorators/di';
import {FBGenericParser} from 'htz-firebird';
const API_URL = new InjectionToken('API_URL');
const MY_FACTORY = new InjectionToken('MY_FACTORY');
@Injectable()
export class MyFactory {
constructor(
@Inject(API_URL) private apiUrl: string,
) {
}
test() {
console.log({
value: this,
url: this.apiUrl
})
}
}
@Injectable()
export class HttpService {
constructor(
@Inject(API_URL) private apiUrl: string,
@Inject(MY_FACTORY) private factory: MyFactory
) {
}
public send(options: object): Promise<any> {
console.log(this.factory);
return Promise.resolve('test')//fetch(this.apiUrl, options);
}
}
Container.provide([
{provide: API_URL, useValue: 'http://server.localhost'},
{provide: MY_FACTORY, useValue: new MyFactory('TEST')}
]);
const httpService = Container.get<HttpService>(HttpService);
httpService.send({});
console.log(httpService);
export * from './sale';
@mreis1
Copy link
Author

mreis1 commented Feb 19, 2019

$ ts-node ./src/index.ts
MyFactory { apiUrl: 'TEST' }
HttpService {
apiUrl: 'http://server.localhost',
factory: MyFactory { apiUrl: 'TEST' } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment