Skip to content

Commit 8aa77b2

Browse files
author
liuliquan
committed
feat: remove route transformer
1 parent 3d0c130 commit 8aa77b2

File tree

3 files changed

+0
-71
lines changed

3 files changed

+0
-71
lines changed

app-routes.js

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,13 @@ const helper = require("./src/common/helper");
1010
const errors = require("./src/common/errors");
1111
const logger = require("./src/common/logger");
1212
const routes = require("./src/routes");
13-
const transformations = require("./src/common/transformations");
1413
const authenticator = require("tc-core-library-js").middleware.jwtAuthenticator;
1514

1615
/**
1716
* Configure all routes for express app
1817
* @param app the express app
1918
*/
2019
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-
2920
// Load all routes
3021
_.each(routes, (verbs, path) => {
3122
_.each(verbs, (def, verb) => {
@@ -45,42 +36,6 @@ module.exports = (app) => {
4536
next();
4637
});
4738

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-
8439
actions.push((req, res, next) => {
8540
if (_.get(req, "query.token")) {
8641
_.set(req, "headers.authorization", `Bearer ${_.trim(req.query.token)}`);

src/common/transformations.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/routes.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module.exports = {
2020
constants.UserRoles.User,
2121
],
2222
scopes: [READ, ALL],
23-
versioned: true,
2423
},
2524
post: {
2625
controller: "ChallengeController",
@@ -52,7 +51,6 @@ module.exports = {
5251
controller: "ChallengeController",
5352
method: "getChallenge",
5453
scopes: [READ, ALL],
55-
versioned: true,
5654
},
5755
// @deprecated
5856
put: {

0 commit comments

Comments
 (0)