typed-bridge 2.0.13 → 2.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bridge/index.js +15 -8
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.js +3 -1
- package/package.json +1 -1
package/dist/bridge/index.js
CHANGED
|
@@ -27,7 +27,7 @@ const createBridge = (bridge, port, path = '/bridge') => {
|
|
|
27
27
|
app.use(express_1.default.json());
|
|
28
28
|
app.use(express_1.default.urlencoded({ extended: true }));
|
|
29
29
|
// Typed bridge middleware
|
|
30
|
-
let requestId =
|
|
30
|
+
let requestId = 0;
|
|
31
31
|
app.use((req, res, next) => {
|
|
32
32
|
const _req = req;
|
|
33
33
|
const xForwardedFor = req.headers['x-forwarded-for'];
|
|
@@ -36,16 +36,16 @@ const createBridge = (bridge, port, path = '/bridge') => {
|
|
|
36
36
|
: (xForwardedFor || '').split(', ')[0] || req.socket.remoteAddress || '';
|
|
37
37
|
if (ip === '::1')
|
|
38
38
|
ip = '127.0.0.1';
|
|
39
|
+
// Set typed bridge header
|
|
40
|
+
res.setHeader('X-Powered-By', 'typed-bridge');
|
|
41
|
+
requestId++;
|
|
39
42
|
// Bind data
|
|
40
43
|
_req.bind = {
|
|
41
|
-
id:
|
|
44
|
+
id: requestId,
|
|
42
45
|
ts: Date.now(),
|
|
43
46
|
args: {},
|
|
44
47
|
ip
|
|
45
48
|
};
|
|
46
|
-
// Set typed bridge header
|
|
47
|
-
res.setHeader('X-Powered-By', 'typed-bridge');
|
|
48
|
-
requestId++;
|
|
49
49
|
// Log request
|
|
50
50
|
if (__1.tbConfig.logs.request) {
|
|
51
51
|
console.log(chalk_1.default.blueBright(`REQ | ${new Date().toISOString()} | ${requestId} :: ${req.method} | ${req.path} | ${ip}`));
|
|
@@ -93,9 +93,11 @@ const createBridge = (bridge, port, path = '/bridge') => {
|
|
|
93
93
|
};
|
|
94
94
|
exports.createBridge = createBridge;
|
|
95
95
|
const bridgeHandler = (bridge) => async (req, res) => {
|
|
96
|
+
let args = {};
|
|
97
|
+
let context = {};
|
|
96
98
|
try {
|
|
97
99
|
const path = req.path.split('/').pop() || '';
|
|
98
|
-
|
|
100
|
+
args = req.body;
|
|
99
101
|
if (!path)
|
|
100
102
|
throw new Error('Bridge not found!');
|
|
101
103
|
const serverFunction = bridge[path];
|
|
@@ -105,7 +107,7 @@ const bridgeHandler = (bridge) => async (req, res) => {
|
|
|
105
107
|
console.error(error);
|
|
106
108
|
return res.status(404).json({ error });
|
|
107
109
|
}
|
|
108
|
-
|
|
110
|
+
context = {};
|
|
109
111
|
for (const middleware of middlewares) {
|
|
110
112
|
if ((0, helpers_1.matchesPattern)(path, middleware.pattern)) {
|
|
111
113
|
const result = await middleware.handler(req, res);
|
|
@@ -118,13 +120,18 @@ const bridgeHandler = (bridge) => async (req, res) => {
|
|
|
118
120
|
res.json((await serverFunction(args, context)) || {});
|
|
119
121
|
}
|
|
120
122
|
catch (error) {
|
|
123
|
+
const id = req.bind?.id;
|
|
124
|
+
if (__1.tbConfig.logs.argsOnError)
|
|
125
|
+
console.error(`ARGS | ${id} ::`, JSON.stringify(args, null, 2));
|
|
126
|
+
if (__1.tbConfig.logs.contextOnError)
|
|
127
|
+
console.error(`CONTEXT | ${id} ::`, JSON.stringify(context, null, 2));
|
|
121
128
|
if (Array.isArray(error.errors)) {
|
|
122
129
|
const keyPath = error.errors[0].path.join('/');
|
|
123
130
|
const errorMessage = (keyPath ? keyPath + ': ' : '') + error.errors[0].message;
|
|
124
131
|
return res.status(400).send(errorMessage);
|
|
125
132
|
}
|
|
126
133
|
if (__1.tbConfig.logs.error)
|
|
127
|
-
console.error(error);
|
|
134
|
+
console.error(`ERROR | ${id} ::`, error);
|
|
128
135
|
return res.status(500).json({ error: error.message });
|
|
129
136
|
}
|
|
130
137
|
};
|
package/dist/config/index.d.ts
CHANGED
package/dist/config/index.js
CHANGED