1
1
import { API_RES , StatusCodeEnum } from "api/enum/api.enum" ;
2
2
import { ApiResponse } from "api/types/response.interface" ;
3
- import { HttpError } from "routing-controllers" ;
4
3
import { Response } from "express" ;
5
4
import BaseEntity from "api/models/entities/types/Base.entity" ;
6
- import { SnakeCaseObj } from "@lib/validation/types " ;
5
+ import { env } from "@env " ;
7
6
8
7
export class BaseController {
9
8
protected code : StatusCodeEnum = StatusCodeEnum . OK ;
10
9
protected data : unknown ;
11
- protected message : string = "Success. " ;
10
+ protected message : string = "Success" ;
12
11
protected typeRes : API_RES = API_RES . JSON ;
13
- protected exception : HttpError ;
12
+ protected enableSnakeCase : boolean = env . config . enableSnakeCase ;
14
13
15
14
public setCode ( code : StatusCodeEnum ) : this {
16
15
this . code = code ;
17
16
return this ;
18
17
}
19
18
20
- public setData < T > ( data : T ) : this {
21
- if ( data instanceof Array ) {
22
- this . data = data . map ( ( item ) => {
23
- return this . isBaseEntity ( item ) ? this . getSnakeEntity ( item ) : item ;
24
- } ) ;
25
- return this ;
26
- }
27
- if ( this . isBaseEntity < T > ( data ) ) {
28
- this . data = this . getSnakeEntity ( data ) ;
29
- return this ;
30
- }
31
- this . data = data ;
19
+ public setData < T > ( data : T | BaseEntity < T > [ ] ) : this {
20
+ this . data = this . transformData ( data ) ;
32
21
return this ;
33
22
}
34
23
@@ -37,49 +26,60 @@ export class BaseController {
37
26
return this ;
38
27
}
39
28
29
+ public setTypeRes ( type : API_RES ) : this {
30
+ this . typeRes = type ;
31
+ return this ;
32
+ }
33
+
40
34
protected getResponse < T > ( res : Response , status : boolean ) : Response {
41
35
const result : ApiResponse < T > = {
42
- status : status ? true : false ,
36
+ status,
43
37
code : this . code ,
44
38
data : this . data as T ,
45
39
message : this . message ,
46
40
} ;
47
41
48
- if ( this . typeRes === API_RES . JSON ) {
49
- return res . status ( this . code ) . json ( result ) ;
42
+ switch ( this . typeRes ) {
43
+ case API_RES . JSON :
44
+ return res . status ( this . code ) . json ( result ) ;
45
+ case API_RES . SEND :
46
+ return res . status ( this . code ) . send ( result ) ;
47
+ default :
48
+ throw new Error ( `Unsupported response type: ${ this . typeRes } ` ) ;
50
49
}
51
-
52
- if ( this . typeRes === API_RES . SEND ) {
53
- return res . status ( this . code ) . send ( result ) ;
54
- }
55
-
56
- // Default to JSON response if type is not recognized
57
- return res . status ( this . code ) . json ( result ) ;
58
50
}
59
51
60
52
public responseSuccess < T > ( data : T , message : string , res : Response ) : Response {
61
- this . setCode ( StatusCodeEnum . OK ) ;
62
- this . setData ( data ) ;
63
- this . setMessage ( message ) ;
64
- return this . getResponse ( res , true ) ;
53
+ return this . setCode ( StatusCodeEnum . OK )
54
+ . setData ( data )
55
+ . setMessage ( message )
56
+ . getResponse < T > ( res , true ) ;
65
57
}
66
58
67
- public responseErrors (
59
+ public responseError (
68
60
res : Response ,
69
61
message : string = "An error occurred" ,
70
62
statusCode : StatusCodeEnum = StatusCodeEnum . INTERNAL_SERVER_ERROR
71
63
) : Response {
72
- this . setCode ( statusCode ) ;
73
- this . setData ( null ) ;
74
- this . setMessage ( message ) ;
75
- return this . getResponse < null > ( res , false ) ;
64
+ return this . setCode ( statusCode )
65
+ . setData ( null )
66
+ . setMessage ( message )
67
+ . getResponse < null > ( res , false ) ;
76
68
}
77
69
78
- private getSnakeEntity < T > ( data : BaseEntity < T > ) : SnakeCaseObj < T > {
79
- return data . toSnake ( ) ;
70
+ private transformData < T > ( data : T | BaseEntity < T > [ ] ) : unknown {
71
+ if ( ! this . enableSnakeCase ) return data ;
72
+
73
+ if ( Array . isArray ( data ) ) {
74
+ return data . map ( ( item ) =>
75
+ this . isBaseEntity ( item ) ? item . toSnake ( ) : item
76
+ ) ;
77
+ }
78
+
79
+ return this . isBaseEntity ( data ) ? data . toSnake ( ) : data ;
80
80
}
81
81
82
82
private isBaseEntity < T > ( object : unknown ) : object is BaseEntity < T > {
83
- return ( object as BaseEntity < T > ) . toSnake !== undefined ;
83
+ return ( object as BaseEntity < T > ) ? .toSnake !== undefined ;
84
84
}
85
85
}
0 commit comments