@jaypie/express 0.1.3 → 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
@@ -386,4 +398,76 @@ const expressHandler = (
386
398
  };
387
399
  };
388
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;
389
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
@@ -384,4 +396,73 @@ const expressHandler = (
384
396
  };
385
397
  };
386
398
 
387
- 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.3",
3
+ "version": "0.1.4",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "exports": {
@@ -28,6 +28,7 @@
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",
32
33
  "test:spec:echo.handler": "vitest run ./src/__tests__/echo.handler.spec.js",
33
34
  "test:spec:expressHandler": "vitest run ./src/__tests__/expressHandler.spec.js",
@@ -0,0 +1,11 @@
1
+ //
2
+ //
3
+ // Constants
4
+ //
5
+
6
+ export const EXPRESS = {
7
+ PATH: {
8
+ ANY: "*",
9
+ ROOT: /^\/?$/,
10
+ },
11
+ };
@@ -8,7 +8,7 @@ import summarizeRequest from "./summarizeRequest.helper.js";
8
8
  // Main
9
9
  //
10
10
 
11
- const echo = (context = {}) => {
11
+ const echoHandler = (context = {}) => {
12
12
  validate.object(context);
13
13
  // Give a default name if there isn't one
14
14
  if (!context.name) {
@@ -17,7 +17,6 @@ const echo = (context = {}) => {
17
17
 
18
18
  // Return a function that will be used as an express route
19
19
  return expressHandler(async (req) => {
20
- console.log("req.body :>> ", req.body);
21
20
  return {
22
21
  req: summarizeRequest(req),
23
22
  };
@@ -29,4 +28,4 @@ const echo = (context = {}) => {
29
28
  // Export
30
29
  //
31
30
 
32
- export default echo;
31
+ export default echoHandler;
@@ -21,7 +21,7 @@ import expressHandler from "./expressHandler.js";
21
21
  // Main
22
22
  //
23
23
 
24
- const http = (statusCode = HTTP.CODE.OK, context = {}) => {
24
+ const httpHandler = (statusCode = HTTP.CODE.OK, context = {}) => {
25
25
  // Give a default name if there isn't one
26
26
  if (!context.name) {
27
27
  context.name = "_http";
@@ -70,4 +70,4 @@ const http = (statusCode = HTTP.CODE.OK, context = {}) => {
70
70
  // Export
71
71
  //
72
72
 
73
- export default http;
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";