@koziatek/http 1.0.1 → 1.0.2
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/middleware/ResData.js +7 -8
- package/middleware/SendRes.js +22 -11
- package/package.json +1 -1
package/middleware/ResData.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}};
|
|
1
|
+
module.exports = {
|
|
2
|
+
ResData: (middleware) => (req, res, next) => {
|
|
3
|
+
Promise.resolve(middleware(req, res))
|
|
4
|
+
.then((data) => { res.locals.data = data; next(); })
|
|
5
|
+
.catch(next);
|
|
6
|
+
}
|
|
7
|
+
}
|
package/middleware/SendRes.js
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
SendRes: (resolver) => {
|
|
3
|
+
return async (req, res, next) => {
|
|
4
|
+
try {
|
|
5
|
+
let payload;
|
|
1
6
|
|
|
7
|
+
if (typeof resolver === 'function') {
|
|
8
|
+
payload = await resolver(req, res, next);
|
|
9
|
+
} else if (typeof resolver === 'undefined') {
|
|
10
|
+
payload = res.locals?.data ?? res.data; // prefer locals if you switch
|
|
11
|
+
} else {
|
|
12
|
+
payload = resolver;
|
|
13
|
+
}
|
|
2
14
|
|
|
15
|
+
if (typeof payload === 'undefined') {
|
|
16
|
+
throw new Error('Could not resolve response data.');
|
|
17
|
+
}
|
|
3
18
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return res.send(data);
|
|
13
|
-
}
|
|
14
|
-
}};
|
|
19
|
+
return res.json(payload); // or res.send(payload) if you really want
|
|
20
|
+
} catch (err) {
|
|
21
|
+
return next(err);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
};
|