@jaypie/express 0.1.2 → 0.1.3
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/module.cjs.js +12 -13
- package/dist/module.esm.js +12 -13
- package/package.json +3 -1
- package/src/echo.handler.js +32 -0
- package/src/expressHandler.js +12 -13
- package/src/http.handler.js +73 -0
package/dist/module.cjs.js
CHANGED
|
@@ -232,7 +232,7 @@ const expressHandler = (
|
|
|
232
232
|
);
|
|
233
233
|
};
|
|
234
234
|
res.status = (...params) => {
|
|
235
|
-
originalRes.statusSent =
|
|
235
|
+
originalRes.statusSent = params;
|
|
236
236
|
return originalRes.status(...params);
|
|
237
237
|
};
|
|
238
238
|
|
|
@@ -273,7 +273,7 @@ const expressHandler = (
|
|
|
273
273
|
}
|
|
274
274
|
|
|
275
275
|
let response;
|
|
276
|
-
let status;
|
|
276
|
+
let status = core.HTTP.CODE.OK;
|
|
277
277
|
|
|
278
278
|
try {
|
|
279
279
|
libLogger.trace("[jaypie] Lambda execution");
|
|
@@ -319,13 +319,13 @@ const expressHandler = (
|
|
|
319
319
|
// Decorate response
|
|
320
320
|
decorateResponse(res, { handler: name });
|
|
321
321
|
|
|
322
|
+
// Allow the sent status to override the status in the response
|
|
323
|
+
if (originalRes.statusSent) {
|
|
324
|
+
status = originalRes.statusSent;
|
|
325
|
+
}
|
|
326
|
+
|
|
322
327
|
// Send response
|
|
323
328
|
try {
|
|
324
|
-
// Status
|
|
325
|
-
if (status && !originalRes.statusSent) {
|
|
326
|
-
res.status(status);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
329
|
if (!originalRes.attemptedCall) {
|
|
330
330
|
// Body
|
|
331
331
|
if (response) {
|
|
@@ -333,19 +333,18 @@ const expressHandler = (
|
|
|
333
333
|
if (typeof response.json === "function") {
|
|
334
334
|
res.json(response.json());
|
|
335
335
|
} else {
|
|
336
|
-
res.json(response);
|
|
336
|
+
res.status(status).json(response);
|
|
337
337
|
}
|
|
338
338
|
} else if (typeof response === "string") {
|
|
339
339
|
try {
|
|
340
|
-
res.json(JSON.parse(response));
|
|
340
|
+
res.status(status).json(JSON.parse(response));
|
|
341
341
|
} catch (error) {
|
|
342
|
-
res.send(response);
|
|
342
|
+
res.status(status).send(response);
|
|
343
343
|
}
|
|
344
344
|
} else if (response === true) {
|
|
345
|
-
res.status(core.HTTP.CODE.CREATED);
|
|
346
|
-
res.send();
|
|
345
|
+
res.status(core.HTTP.CODE.CREATED).send();
|
|
347
346
|
} else {
|
|
348
|
-
res.send(response);
|
|
347
|
+
res.status(status).send(response);
|
|
349
348
|
}
|
|
350
349
|
} else {
|
|
351
350
|
// No response
|
package/dist/module.esm.js
CHANGED
|
@@ -230,7 +230,7 @@ const expressHandler = (
|
|
|
230
230
|
);
|
|
231
231
|
};
|
|
232
232
|
res.status = (...params) => {
|
|
233
|
-
originalRes.statusSent =
|
|
233
|
+
originalRes.statusSent = params;
|
|
234
234
|
return originalRes.status(...params);
|
|
235
235
|
};
|
|
236
236
|
|
|
@@ -271,7 +271,7 @@ const expressHandler = (
|
|
|
271
271
|
}
|
|
272
272
|
|
|
273
273
|
let response;
|
|
274
|
-
let status;
|
|
274
|
+
let status = HTTP.CODE.OK;
|
|
275
275
|
|
|
276
276
|
try {
|
|
277
277
|
libLogger.trace("[jaypie] Lambda execution");
|
|
@@ -317,13 +317,13 @@ const expressHandler = (
|
|
|
317
317
|
// Decorate response
|
|
318
318
|
decorateResponse(res, { handler: name });
|
|
319
319
|
|
|
320
|
+
// Allow the sent status to override the status in the response
|
|
321
|
+
if (originalRes.statusSent) {
|
|
322
|
+
status = originalRes.statusSent;
|
|
323
|
+
}
|
|
324
|
+
|
|
320
325
|
// Send response
|
|
321
326
|
try {
|
|
322
|
-
// Status
|
|
323
|
-
if (status && !originalRes.statusSent) {
|
|
324
|
-
res.status(status);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
327
|
if (!originalRes.attemptedCall) {
|
|
328
328
|
// Body
|
|
329
329
|
if (response) {
|
|
@@ -331,19 +331,18 @@ const expressHandler = (
|
|
|
331
331
|
if (typeof response.json === "function") {
|
|
332
332
|
res.json(response.json());
|
|
333
333
|
} else {
|
|
334
|
-
res.json(response);
|
|
334
|
+
res.status(status).json(response);
|
|
335
335
|
}
|
|
336
336
|
} else if (typeof response === "string") {
|
|
337
337
|
try {
|
|
338
|
-
res.json(JSON.parse(response));
|
|
338
|
+
res.status(status).json(JSON.parse(response));
|
|
339
339
|
} catch (error) {
|
|
340
|
-
res.send(response);
|
|
340
|
+
res.status(status).send(response);
|
|
341
341
|
}
|
|
342
342
|
} else if (response === true) {
|
|
343
|
-
res.status(HTTP.CODE.CREATED);
|
|
344
|
-
res.send();
|
|
343
|
+
res.status(HTTP.CODE.CREATED).send();
|
|
345
344
|
} else {
|
|
346
|
-
res.send(response);
|
|
345
|
+
res.status(status).send(response);
|
|
347
346
|
}
|
|
348
347
|
} else {
|
|
349
348
|
// No response
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jaypie/express",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"author": "Finlayson Studio",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -29,8 +29,10 @@
|
|
|
29
29
|
"prepublish": "npm run build",
|
|
30
30
|
"test": "vitest",
|
|
31
31
|
"test:spec:decorateResponse.helper": "vitest run ./src/__tests__/decorateResponse.helper.spec.js",
|
|
32
|
+
"test:spec:echo.handler": "vitest run ./src/__tests__/echo.handler.spec.js",
|
|
32
33
|
"test:spec:expressHandler": "vitest run ./src/__tests__/expressHandler.spec.js",
|
|
33
34
|
"test:spec:getCurrentInvokeUuid.adapter": "vitest run ./src/__tests__/getCurrentInvokeUuid.adapter.spec.js",
|
|
35
|
+
"test:spec:http.handler": "vitest run ./src/__tests__/http.handler.spec.js",
|
|
34
36
|
"test:spec:index": "vitest run ./src/__tests__/index.spec.js",
|
|
35
37
|
"test:spec:summarizeRequest.helper": "vitest run ./src/__tests__/summarizeRequest.helper.spec.js",
|
|
36
38
|
"test:spec:summarizeResponse.helper": "vitest run ./src/__tests__/summarizeResponse.helper.spec.js",
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { validate } from "@jaypie/core";
|
|
2
|
+
|
|
3
|
+
import expressHandler from "./expressHandler.js";
|
|
4
|
+
import summarizeRequest from "./summarizeRequest.helper.js";
|
|
5
|
+
|
|
6
|
+
//
|
|
7
|
+
//
|
|
8
|
+
// Main
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
const echo = (context = {}) => {
|
|
12
|
+
validate.object(context);
|
|
13
|
+
// Give a default name if there isn't one
|
|
14
|
+
if (!context.name) {
|
|
15
|
+
context.name = "_echo";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Return a function that will be used as an express route
|
|
19
|
+
return expressHandler(async (req) => {
|
|
20
|
+
console.log("req.body :>> ", req.body);
|
|
21
|
+
return {
|
|
22
|
+
req: summarizeRequest(req),
|
|
23
|
+
};
|
|
24
|
+
}, context);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//
|
|
28
|
+
//
|
|
29
|
+
// Export
|
|
30
|
+
//
|
|
31
|
+
|
|
32
|
+
export default echo;
|
package/src/expressHandler.js
CHANGED
|
@@ -95,7 +95,7 @@ const expressHandler = (
|
|
|
95
95
|
);
|
|
96
96
|
};
|
|
97
97
|
res.status = (...params) => {
|
|
98
|
-
originalRes.statusSent =
|
|
98
|
+
originalRes.statusSent = params;
|
|
99
99
|
return originalRes.status(...params);
|
|
100
100
|
};
|
|
101
101
|
|
|
@@ -136,7 +136,7 @@ const expressHandler = (
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
let response;
|
|
139
|
-
let status;
|
|
139
|
+
let status = HTTP.CODE.OK;
|
|
140
140
|
|
|
141
141
|
try {
|
|
142
142
|
libLogger.trace("[jaypie] Lambda execution");
|
|
@@ -182,13 +182,13 @@ const expressHandler = (
|
|
|
182
182
|
// Decorate response
|
|
183
183
|
decorateResponse(res, { handler: name });
|
|
184
184
|
|
|
185
|
+
// Allow the sent status to override the status in the response
|
|
186
|
+
if (originalRes.statusSent) {
|
|
187
|
+
status = originalRes.statusSent;
|
|
188
|
+
}
|
|
189
|
+
|
|
185
190
|
// Send response
|
|
186
191
|
try {
|
|
187
|
-
// Status
|
|
188
|
-
if (status && !originalRes.statusSent) {
|
|
189
|
-
res.status(status);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
192
|
if (!originalRes.attemptedCall) {
|
|
193
193
|
// Body
|
|
194
194
|
if (response) {
|
|
@@ -196,19 +196,18 @@ const expressHandler = (
|
|
|
196
196
|
if (typeof response.json === "function") {
|
|
197
197
|
res.json(response.json());
|
|
198
198
|
} else {
|
|
199
|
-
res.json(response);
|
|
199
|
+
res.status(status).json(response);
|
|
200
200
|
}
|
|
201
201
|
} else if (typeof response === "string") {
|
|
202
202
|
try {
|
|
203
|
-
res.json(JSON.parse(response));
|
|
203
|
+
res.status(status).json(JSON.parse(response));
|
|
204
204
|
} catch (error) {
|
|
205
|
-
res.send(response);
|
|
205
|
+
res.status(status).send(response);
|
|
206
206
|
}
|
|
207
207
|
} else if (response === true) {
|
|
208
|
-
res.status(HTTP.CODE.CREATED);
|
|
209
|
-
res.send();
|
|
208
|
+
res.status(HTTP.CODE.CREATED).send();
|
|
210
209
|
} else {
|
|
211
|
-
res.send(response);
|
|
210
|
+
res.status(status).send(response);
|
|
212
211
|
}
|
|
213
212
|
} else {
|
|
214
213
|
// No response
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BadGatewayError,
|
|
3
|
+
BadRequestError,
|
|
4
|
+
ForbiddenError,
|
|
5
|
+
GatewayTimeoutError,
|
|
6
|
+
GoneError,
|
|
7
|
+
HTTP,
|
|
8
|
+
InternalError,
|
|
9
|
+
log,
|
|
10
|
+
MethodNotAllowedError,
|
|
11
|
+
NotFoundError,
|
|
12
|
+
TeapotError,
|
|
13
|
+
UnauthorizedError,
|
|
14
|
+
UnavailableError,
|
|
15
|
+
} from "@jaypie/core";
|
|
16
|
+
|
|
17
|
+
import expressHandler from "./expressHandler.js";
|
|
18
|
+
|
|
19
|
+
//
|
|
20
|
+
//
|
|
21
|
+
// Main
|
|
22
|
+
//
|
|
23
|
+
|
|
24
|
+
const http = (statusCode = HTTP.CODE.OK, context = {}) => {
|
|
25
|
+
// Give a default name if there isn't one
|
|
26
|
+
if (!context.name) {
|
|
27
|
+
context.name = "_http";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Return a function that will be used as an express route
|
|
31
|
+
return expressHandler(async (req, res) => {
|
|
32
|
+
// Map the most throwable status codes to errors and throw them!
|
|
33
|
+
const error = {
|
|
34
|
+
[HTTP.CODE.BAD_REQUEST]: BadRequestError,
|
|
35
|
+
[HTTP.CODE.UNAUTHORIZED]: UnauthorizedError,
|
|
36
|
+
[HTTP.CODE.FORBIDDEN]: ForbiddenError,
|
|
37
|
+
[HTTP.CODE.NOT_FOUND]: NotFoundError,
|
|
38
|
+
[HTTP.CODE.METHOD_NOT_ALLOWED]: MethodNotAllowedError,
|
|
39
|
+
[HTTP.CODE.GONE]: GoneError,
|
|
40
|
+
[HTTP.CODE.TEAPOT]: TeapotError,
|
|
41
|
+
[HTTP.CODE.INTERNAL_ERROR]: InternalError,
|
|
42
|
+
[HTTP.CODE.BAD_GATEWAY]: BadGatewayError,
|
|
43
|
+
[HTTP.CODE.UNAVAILABLE]: UnavailableError,
|
|
44
|
+
[HTTP.CODE.GATEWAY_TIMEOUT]: GatewayTimeoutError,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// If this maps to an error, throw it
|
|
48
|
+
if (error[statusCode]) {
|
|
49
|
+
log.trace(
|
|
50
|
+
`@knowdev/express: gracefully throwing ${statusCode} up to projectHandler`,
|
|
51
|
+
);
|
|
52
|
+
throw new error[statusCode]();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// If this is an error and we didn't get thrown, log a warning
|
|
56
|
+
if (statusCode >= 400) {
|
|
57
|
+
log.warn(
|
|
58
|
+
`@knowdev/express: status code ${statusCode} not mapped as throwable`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Send the response
|
|
63
|
+
res.status(statusCode);
|
|
64
|
+
return statusCode === HTTP.CODE.NO_CONTENT ? null : {};
|
|
65
|
+
}, context);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
//
|
|
69
|
+
//
|
|
70
|
+
// Export
|
|
71
|
+
//
|
|
72
|
+
|
|
73
|
+
export default http;
|