@@ -4,14 +4,12 @@ import {
4
4
IErrorHandlerService ,
5
5
} from "./services/ErrorHandlerService" ;
6
6
import { IResponseService , ResponseService } from "./services/ResponseService" ;
7
- import { ILogService , LogService } from "./services/LogService" ;
8
7
import {
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" ;
15
13
/**
16
14
* For the user to get the singleton instance of the services,
17
15
* Support to use the provided container to build the services.
@@ -21,21 +19,11 @@ import pino, { Logger } from "pino";
21
19
*/
22
20
export class Containers {
23
21
private static _instance : Containers ;
24
- private _idIdentifier : string ;
25
22
private container : Container ;
26
23
27
24
private constructor ( ) {
28
25
this . container = new Container ( ) ;
29
26
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 ;
39
27
}
40
28
41
29
public static get instance ( ) {
@@ -51,68 +39,34 @@ export class Containers {
51
39
52
40
private buildContainers ( ) {
53
41
this . buildConstantsContainer ( ) ;
42
+ this . buildLibContainers ( ) ;
54
43
this . buildServiceContainer ( ) ;
55
44
}
56
45
46
+ private buildLibContainers ( ) {
47
+ useCommonServicesInversify ( this . container ) ;
48
+ useCommonErrorsInversify ( this . container ) ;
49
+ }
50
+
57
51
// Arguments that required for the services
58
52
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
-
83
53
// error callback for ErrorHandlerService
54
+ if ( this . container . isBound ( "ErrorCallback" ) ) {
55
+ this . container . unbind ( "ErrorCallback" ) ;
56
+ }
84
57
this . container
85
58
. bind < ( error : DefinedBaseError ) => void > ( "ErrorCallback" )
86
- . toConstantValue ( ( _error : DefinedBaseError ) => { } ) ;
59
+ . toConstantValue ( Config . instance . errorCallback ) ;
87
60
}
88
61
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 ) ) {
107
63
this . container
108
64
. bind < IErrorHandlerService > ( ErrorHandlerService )
109
65
. toSelf ( )
110
66
. inSingletonScope ( ) ;
111
67
}
112
68
113
- try {
114
- this . container . get < IResponseService > ( ResponseService ) ;
115
- } catch ( e ) {
69
+ if ( ! this . container . isBound ( ResponseService ) ) {
116
70
this . container
117
71
. bind < IResponseService > ( ResponseService )
118
72
. toSelf ( )
0 commit comments