@jaypie/express 0.1.2 → 0.1.4

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.
@@ -3,6 +3,18 @@
3
3
  var core = require('@jaypie/core');
4
4
  var serverlessExpress = require('@codegenie/serverless-express');
5
5
 
6
+ //
7
+ //
8
+ // Constants
9
+ //
10
+
11
+ const EXPRESS = {
12
+ PATH: {
13
+ ANY: "*",
14
+ ROOT: /^\/?$/,
15
+ },
16
+ };
17
+
6
18
  //
7
19
  //
8
20
  // Helper Functions
@@ -232,7 +244,7 @@ const expressHandler = (
232
244
  );
233
245
  };
234
246
  res.status = (...params) => {
235
- originalRes.statusSent = true;
247
+ originalRes.statusSent = params;
236
248
  return originalRes.status(...params);
237
249
  };
238
250
 
@@ -273,7 +285,7 @@ const expressHandler = (
273
285
  }
274
286
 
275
287
  let response;
276
- let status;
288
+ let status = core.HTTP.CODE.OK;
277
289
 
278
290
  try {
279
291
  libLogger.trace("[jaypie] Lambda execution");
@@ -319,13 +331,13 @@ const expressHandler = (
319
331
  // Decorate response
320
332
  decorateResponse(res, { handler: name });
321
333
 
334
+ // Allow the sent status to override the status in the response
335
+ if (originalRes.statusSent) {
336
+ status = originalRes.statusSent;
337
+ }
338
+
322
339
  // Send response
323
340
  try {
324
- // Status
325
- if (status && !originalRes.statusSent) {
326
- res.status(status);
327
- }
328
-
329
341
  if (!originalRes.attemptedCall) {
330
342
  // Body
331
343
  if (response) {
@@ -333,19 +345,18 @@ const expressHandler = (
333
345
  if (typeof response.json === "function") {
334
346
  res.json(response.json());
335
347
  } else {
336
- res.json(response);
348
+ res.status(status).json(response);
337
349
  }
338
350
  } else if (typeof response === "string") {
339
351
  try {
340
- res.json(JSON.parse(response));
352
+ res.status(status).json(JSON.parse(response));
341
353
  } catch (error) {
342
- res.send(response);
354
+ res.status(status).send(response);
343
355
  }
344
356
  } else if (response === true) {
345
- res.status(core.HTTP.CODE.CREATED);
346
- res.send();
357
+ res.status(core.HTTP.CODE.CREATED).send();
347
358
  } else {
348
- res.send(response);
359
+ res.status(status).send(response);
349
360
  }
350
361
  } else {
351
362
  // No response
@@ -387,4 +398,76 @@ const expressHandler = (
387
398
  };
388
399
  };
389
400
 
401
+ //
402
+ //
403
+ // Main
404
+ //
405
+
406
+ const echoHandler = (context = {}) => {
407
+ core.validate.object(context);
408
+ // Give a default name if there isn't one
409
+ if (!context.name) {
410
+ context.name = "_echo";
411
+ }
412
+
413
+ // Return a function that will be used as an express route
414
+ return expressHandler(async (req) => {
415
+ return {
416
+ req: summarizeRequest(req),
417
+ };
418
+ }, context);
419
+ };
420
+
421
+ //
422
+ //
423
+ // Main
424
+ //
425
+
426
+ const httpHandler = (statusCode = core.HTTP.CODE.OK, context = {}) => {
427
+ // Give a default name if there isn't one
428
+ if (!context.name) {
429
+ context.name = "_http";
430
+ }
431
+
432
+ // Return a function that will be used as an express route
433
+ return expressHandler(async (req, res) => {
434
+ // Map the most throwable status codes to errors and throw them!
435
+ const error = {
436
+ [core.HTTP.CODE.BAD_REQUEST]: core.BadRequestError,
437
+ [core.HTTP.CODE.UNAUTHORIZED]: core.UnauthorizedError,
438
+ [core.HTTP.CODE.FORBIDDEN]: core.ForbiddenError,
439
+ [core.HTTP.CODE.NOT_FOUND]: core.NotFoundError,
440
+ [core.HTTP.CODE.METHOD_NOT_ALLOWED]: core.MethodNotAllowedError,
441
+ [core.HTTP.CODE.GONE]: core.GoneError,
442
+ [core.HTTP.CODE.TEAPOT]: core.TeapotError,
443
+ [core.HTTP.CODE.INTERNAL_ERROR]: core.InternalError,
444
+ [core.HTTP.CODE.BAD_GATEWAY]: core.BadGatewayError,
445
+ [core.HTTP.CODE.UNAVAILABLE]: core.UnavailableError,
446
+ [core.HTTP.CODE.GATEWAY_TIMEOUT]: core.GatewayTimeoutError,
447
+ };
448
+
449
+ // If this maps to an error, throw it
450
+ if (error[statusCode]) {
451
+ core.log.trace(
452
+ `@knowdev/express: gracefully throwing ${statusCode} up to projectHandler`,
453
+ );
454
+ throw new error[statusCode]();
455
+ }
456
+
457
+ // If this is an error and we didn't get thrown, log a warning
458
+ if (statusCode >= 400) {
459
+ core.log.warn(
460
+ `@knowdev/express: status code ${statusCode} not mapped as throwable`,
461
+ );
462
+ }
463
+
464
+ // Send the response
465
+ res.status(statusCode);
466
+ return statusCode === core.HTTP.CODE.NO_CONTENT ? null : {};
467
+ }, context);
468
+ };
469
+
470
+ exports.EXPRESS = EXPRESS;
471
+ exports.expressEchoHandler = echoHandler;
390
472
  exports.expressHandler = expressHandler;
473
+ exports.expressHttpHandler = httpHandler;
@@ -1,6 +1,18 @@
1
- import { log, JAYPIE, HTTP, validate, force, jaypieHandler, UnhandledError } from '@jaypie/core';
1
+ import { log, JAYPIE, HTTP, validate, force, jaypieHandler, UnhandledError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, MethodNotAllowedError, GoneError, TeapotError, InternalError, BadGatewayError, UnavailableError, GatewayTimeoutError } from '@jaypie/core';
2
2
  import { getCurrentInvoke } from '@codegenie/serverless-express';
3
3
 
4
+ //
5
+ //
6
+ // Constants
7
+ //
8
+
9
+ const EXPRESS = {
10
+ PATH: {
11
+ ANY: "*",
12
+ ROOT: /^\/?$/,
13
+ },
14
+ };
15
+
4
16
  //
5
17
  //
6
18
  // Helper Functions
@@ -230,7 +242,7 @@ const expressHandler = (
230
242
  );
231
243
  };
232
244
  res.status = (...params) => {
233
- originalRes.statusSent = true;
245
+ originalRes.statusSent = params;
234
246
  return originalRes.status(...params);
235
247
  };
236
248
 
@@ -271,7 +283,7 @@ const expressHandler = (
271
283
  }
272
284
 
273
285
  let response;
274
- let status;
286
+ let status = HTTP.CODE.OK;
275
287
 
276
288
  try {
277
289
  libLogger.trace("[jaypie] Lambda execution");
@@ -317,13 +329,13 @@ const expressHandler = (
317
329
  // Decorate response
318
330
  decorateResponse(res, { handler: name });
319
331
 
332
+ // Allow the sent status to override the status in the response
333
+ if (originalRes.statusSent) {
334
+ status = originalRes.statusSent;
335
+ }
336
+
320
337
  // Send response
321
338
  try {
322
- // Status
323
- if (status && !originalRes.statusSent) {
324
- res.status(status);
325
- }
326
-
327
339
  if (!originalRes.attemptedCall) {
328
340
  // Body
329
341
  if (response) {
@@ -331,19 +343,18 @@ const expressHandler = (
331
343
  if (typeof response.json === "function") {
332
344
  res.json(response.json());
333
345
  } else {
334
- res.json(response);
346
+ res.status(status).json(response);
335
347
  }
336
348
  } else if (typeof response === "string") {
337
349
  try {
338
- res.json(JSON.parse(response));
350
+ res.status(status).json(JSON.parse(response));
339
351
  } catch (error) {
340
- res.send(response);
352
+ res.status(status).send(response);
341
353
  }
342
354
  } else if (response === true) {
343
- res.status(HTTP.CODE.CREATED);
344
- res.send();
355
+ res.status(HTTP.CODE.CREATED).send();
345
356
  } else {
346
- res.send(response);
357
+ res.status(status).send(response);
347
358
  }
348
359
  } else {
349
360
  // No response
@@ -385,4 +396,73 @@ const expressHandler = (
385
396
  };
386
397
  };
387
398
 
388
- export { expressHandler };
399
+ //
400
+ //
401
+ // Main
402
+ //
403
+
404
+ const echoHandler = (context = {}) => {
405
+ validate.object(context);
406
+ // Give a default name if there isn't one
407
+ if (!context.name) {
408
+ context.name = "_echo";
409
+ }
410
+
411
+ // Return a function that will be used as an express route
412
+ return expressHandler(async (req) => {
413
+ return {
414
+ req: summarizeRequest(req),
415
+ };
416
+ }, context);
417
+ };
418
+
419
+ //
420
+ //
421
+ // Main
422
+ //
423
+
424
+ const httpHandler = (statusCode = HTTP.CODE.OK, context = {}) => {
425
+ // Give a default name if there isn't one
426
+ if (!context.name) {
427
+ context.name = "_http";
428
+ }
429
+
430
+ // Return a function that will be used as an express route
431
+ return expressHandler(async (req, res) => {
432
+ // Map the most throwable status codes to errors and throw them!
433
+ const error = {
434
+ [HTTP.CODE.BAD_REQUEST]: BadRequestError,
435
+ [HTTP.CODE.UNAUTHORIZED]: UnauthorizedError,
436
+ [HTTP.CODE.FORBIDDEN]: ForbiddenError,
437
+ [HTTP.CODE.NOT_FOUND]: NotFoundError,
438
+ [HTTP.CODE.METHOD_NOT_ALLOWED]: MethodNotAllowedError,
439
+ [HTTP.CODE.GONE]: GoneError,
440
+ [HTTP.CODE.TEAPOT]: TeapotError,
441
+ [HTTP.CODE.INTERNAL_ERROR]: InternalError,
442
+ [HTTP.CODE.BAD_GATEWAY]: BadGatewayError,
443
+ [HTTP.CODE.UNAVAILABLE]: UnavailableError,
444
+ [HTTP.CODE.GATEWAY_TIMEOUT]: GatewayTimeoutError,
445
+ };
446
+
447
+ // If this maps to an error, throw it
448
+ if (error[statusCode]) {
449
+ log.trace(
450
+ `@knowdev/express: gracefully throwing ${statusCode} up to projectHandler`,
451
+ );
452
+ throw new error[statusCode]();
453
+ }
454
+
455
+ // If this is an error and we didn't get thrown, log a warning
456
+ if (statusCode >= 400) {
457
+ log.warn(
458
+ `@knowdev/express: status code ${statusCode} not mapped as throwable`,
459
+ );
460
+ }
461
+
462
+ // Send the response
463
+ res.status(statusCode);
464
+ return statusCode === HTTP.CODE.NO_CONTENT ? null : {};
465
+ }, context);
466
+ };
467
+
468
+ export { EXPRESS, echoHandler as expressEchoHandler, expressHandler, httpHandler as expressHttpHandler };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/express",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "exports": {
@@ -28,9 +28,12 @@
28
28
  "new:test": "hygen jaypie vitest",
29
29
  "prepublish": "npm run build",
30
30
  "test": "vitest",
31
+ "test:spec:constants": "vitest run ./src/__tests__/constants.spec.js",
31
32
  "test:spec:decorateResponse.helper": "vitest run ./src/__tests__/decorateResponse.helper.spec.js",
33
+ "test:spec:echo.handler": "vitest run ./src/__tests__/echo.handler.spec.js",
32
34
  "test:spec:expressHandler": "vitest run ./src/__tests__/expressHandler.spec.js",
33
35
  "test:spec:getCurrentInvokeUuid.adapter": "vitest run ./src/__tests__/getCurrentInvokeUuid.adapter.spec.js",
36
+ "test:spec:http.handler": "vitest run ./src/__tests__/http.handler.spec.js",
34
37
  "test:spec:index": "vitest run ./src/__tests__/index.spec.js",
35
38
  "test:spec:summarizeRequest.helper": "vitest run ./src/__tests__/summarizeRequest.helper.spec.js",
36
39
  "test:spec:summarizeResponse.helper": "vitest run ./src/__tests__/summarizeResponse.helper.spec.js",
@@ -0,0 +1,11 @@
1
+ //
2
+ //
3
+ // Constants
4
+ //
5
+
6
+ export const EXPRESS = {
7
+ PATH: {
8
+ ANY: "*",
9
+ ROOT: /^\/?$/,
10
+ },
11
+ };
@@ -0,0 +1,31 @@
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 echoHandler = (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
+ return {
21
+ req: summarizeRequest(req),
22
+ };
23
+ }, context);
24
+ };
25
+
26
+ //
27
+ //
28
+ // Export
29
+ //
30
+
31
+ export default echoHandler;
@@ -95,7 +95,7 @@ const expressHandler = (
95
95
  );
96
96
  };
97
97
  res.status = (...params) => {
98
- originalRes.statusSent = true;
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 httpHandler = (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 httpHandler;
package/src/index.js CHANGED
@@ -1 +1,4 @@
1
+ export { EXPRESS } from "./constants.js";
2
+ export { default as expressEchoHandler } from "./echo.handler.js";
1
3
  export { default as expressHandler } from "./expressHandler.js";
4
+ export { default as expressHttpHandler } from "./http.handler.js";