@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.
@@ -1,8 +1,7 @@
1
-
2
-
3
- module.exports = { ResData: (middleware) => {
4
- return async (req,res,next) => {
5
- res.data = await middleware(req,res);
6
- next();
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
+ }
@@ -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
- module.exports = { SendRes: (data) => {
6
- return async (req,res,next) => {
7
- if(typeof data === 'function') data = await data(req,res,next);
8
- else if (typeof data === 'undefined') data = res?.data;
9
-
10
- if(typeof data === 'undefined') throw new Error('Could not resolve response data.');
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koziatek/http",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "publishConfig": {