@@ -10,22 +10,13 @@ const helper = require("./src/common/helper");
10
10
const errors = require ( "./src/common/errors" ) ;
11
11
const logger = require ( "./src/common/logger" ) ;
12
12
const routes = require ( "./src/routes" ) ;
13
- const transformations = require ( "./src/common/transformations" ) ;
14
13
const authenticator = require ( "tc-core-library-js" ) . middleware . jwtAuthenticator ;
15
14
16
15
/**
17
16
* Configure all routes for express app
18
17
* @param app the express app
19
18
*/
20
19
module . exports = ( app ) => {
21
- app . use ( ( req , res , next ) => {
22
- req . appVersion = req . headers [ "app-version" ] || "1.0.0" ;
23
- if ( ! transformations [ req . appVersion ] ) {
24
- req . appVersion = "1.0.0" ; // default to 1.0.0 if provided version doesn't match any transformation
25
- }
26
- next ( ) ;
27
- } ) ;
28
-
29
20
// Load all routes
30
21
_ . each ( routes , ( verbs , path ) => {
31
22
_ . each ( verbs , ( def , verb ) => {
@@ -45,42 +36,6 @@ module.exports = (app) => {
45
36
next ( ) ;
46
37
} ) ;
47
38
48
- if ( def . versioned ) {
49
- actions . push ( ( req , res , next ) => {
50
- // TODO: Overriding res.send is a temporary solution to inject version-based transformations.
51
- // TODO: A more conventional approach in Express would be to use res.locals to pass data through middleware,
52
- // TODO: and then send the response in a centralized middleware after all transformations are applied.
53
- // TODO: This would require a refactor of the current controllers' response handling.
54
- // TODO: Consider revisiting this implementation in the future for a more maintainable architecture.
55
-
56
- const originalSend = res . send ;
57
- const originalStatus = res . status ;
58
- let currentStatusCode = 200 ; // Default status code for Express
59
-
60
- // Override res.status to capture the status code
61
- res . status = function ( code ) {
62
- currentStatusCode = code ;
63
- return originalStatus . apply ( this , arguments ) ;
64
- } ;
65
-
66
- res . send = ( data ) => {
67
- // If the status code indicates a successful response, apply the transformation
68
- if ( currentStatusCode >= 200 && currentStatusCode < 300 ) {
69
- const transformer = transformations [ req . appVersion ] || transformations [ "1.0.0" ] ;
70
- data = transformer ( data ) ;
71
- }
72
-
73
- // Reset the send function to its original behavior
74
- res . send = originalSend ;
75
-
76
- // Call the original send function with the transformed (or original) data
77
- originalSend . call ( res , data ) ;
78
- } ;
79
-
80
- next ( ) ;
81
- } ) ;
82
- }
83
-
84
39
actions . push ( ( req , res , next ) => {
85
40
if ( _ . get ( req , "query.token" ) ) {
86
41
_ . set ( req , "headers.authorization" , `Bearer ${ _ . trim ( req . query . token ) } ` ) ;
0 commit comments