@@ -4,14 +4,12 @@ import {
44 IErrorHandlerService ,
55} from "./services/ErrorHandlerService" ;
66import { IResponseService , ResponseService } from "./services/ResponseService" ;
7- import { ILogService , LogService } from "./services/LogService" ;
87import {
9- IMessageCodeService ,
10- MessageCodeService ,
11- } from "./services/MessageCodeService" ;
12- import { DefinedBaseError } from "./models/Errors" ;
13- import pino , { Logger } from "pino" ;
14-
8+ DefinedBaseError ,
9+ useInversify as useCommonErrorsInversify ,
10+ } from "@lst97/common-errors" ;
11+ import { useInversify as useCommonServicesInversify } from "@lst97/common-services" ;
12+ import { Config } from "./CommonResponse.config" ;
1513/**
1614 * For the user to get the singleton instance of the services,
1715 * Support to use the provided container to build the services.
@@ -21,21 +19,11 @@ import pino, { Logger } from "pino";
2119 */
2220export class Containers {
2321 private static _instance : Containers ;
24- private _idIdentifier : string ;
2522 private container : Container ;
2623
2724 private constructor ( ) {
2825 this . container = new Container ( ) ;
2926 this . buildContainers ( ) ;
30- this . _idIdentifier = "unknown" ;
31- }
32-
33- public set idIdentifier ( value : string ) {
34- this . _idIdentifier = value ;
35- }
36-
37- public get idIdentifier ( ) {
38- return this . _idIdentifier ;
3927 }
4028
4129 public static get instance ( ) {
@@ -51,68 +39,34 @@ export class Containers {
5139
5240 private buildContainers ( ) {
5341 this . buildConstantsContainer ( ) ;
42+ this . buildLibContainers ( ) ;
5443 this . buildServiceContainer ( ) ;
5544 }
5645
46+ private buildLibContainers ( ) {
47+ useCommonServicesInversify ( this . container ) ;
48+ useCommonErrorsInversify ( this . container ) ;
49+ }
50+
5751 // Arguments that required for the services
5852 private buildConstantsContainer ( ) {
59- // using pino for logging with pretty print by default
60- const logger = pino ( {
61- level : process . env . LOG_LEVEL || "info" ,
62- formatters : {
63- level : ( label ) => {
64- return { level : label } ;
65- } ,
66- } ,
67- transport : {
68- target : "pino-pretty" ,
69- options : {
70- colorize : true ,
71- translateTime : "SYS:dd-mm-yyyy HH:MM:ss o" ,
72- } ,
73- } ,
74- } ) ;
75- this . container . bind < Logger < never > > ( "Logger" ) . toConstantValue ( logger ) ;
76-
77- // TODO: user can provide the values
78- // path to the message codes json file
79- this . container
80- . bind < string > ( "MessageCodesJsonPath" )
81- . toConstantValue ( "src/models/MessageCodes.json" ) ;
82-
8353 // error callback for ErrorHandlerService
54+ if ( this . container . isBound ( "ErrorCallback" ) ) {
55+ this . container . unbind ( "ErrorCallback" ) ;
56+ }
8457 this . container
8558 . bind < ( error : DefinedBaseError ) => void > ( "ErrorCallback" )
86- . toConstantValue ( ( _error : DefinedBaseError ) => { } ) ;
59+ . toConstantValue ( Config . instance . errorCallback ) ;
8760 }
8861 private buildServiceContainer ( ) {
89- try {
90- this . container . get < ILogService > ( LogService ) ;
91- } catch ( e ) {
92- this . container . bind < ILogService > ( LogService ) . toSelf ( ) . inSingletonScope ( ) ;
93- }
94-
95- try {
96- this . container . get < IMessageCodeService > ( MessageCodeService ) ;
97- } catch ( e ) {
98- this . container
99- . bind < IMessageCodeService > ( MessageCodeService )
100- . toSelf ( )
101- . inSingletonScope ( ) ;
102- }
103-
104- try {
105- this . container . get < IErrorHandlerService > ( ErrorHandlerService ) ;
106- } catch ( e ) {
62+ if ( ! this . container . isBound ( ErrorHandlerService ) ) {
10763 this . container
10864 . bind < IErrorHandlerService > ( ErrorHandlerService )
10965 . toSelf ( )
11066 . inSingletonScope ( ) ;
11167 }
11268
113- try {
114- this . container . get < IResponseService > ( ResponseService ) ;
115- } catch ( e ) {
69+ if ( ! this . container . isBound ( ResponseService ) ) {
11670 this . container
11771 . bind < IResponseService > ( ResponseService )
11872 . toSelf ( )
0 commit comments