jaypie 1.0.26 → 1.0.28

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.
Files changed (2) hide show
  1. package/README.md +78 -2
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -60,6 +60,7 @@ These packages are included in `jaypie`. They may be installed separately in the
60
60
  | ------- | ------- | ----------- |
61
61
  | `@jaypie/aws` | `getMessages`, `getSecret`, `sendBatchMessages`, `sendMessage` | AWS helpers |
62
62
  | `@jaypie/datadog | `submitMetric`, `submitMetricSet` | Datadog helpers |
63
+ | `@jaypie/express` | `expressHandler` | Express entry point |
63
64
  | `@jaypie/lambda` | `lambdaHandler` | Lambda entry point |
64
65
  | `@jaypie/mongoose` | `connect`, `connectFromSecretEnv`, `disconnect`, `mongoose` | MongoDB management |
65
66
 
@@ -179,6 +180,7 @@ import {
179
180
  CDK,
180
181
  DATADOG,
181
182
  ERROR,
183
+ EXPRESS,
182
184
  HTTP,
183
185
  VALIDATE,
184
186
  } from "jaypie";
@@ -210,6 +212,11 @@ Default messages and titles for Jaypie errors.
210
212
 
211
213
  See `HTTP` for status codes.
212
214
 
215
+ #### `EXPRESS`
216
+
217
+ * `EXPRESS.PATH.ANY` - String `*` for any path
218
+ * `EXPRESS.PATH.ROOT` - Regular expression for root path
219
+
213
220
  #### `HTTP`
214
221
 
215
222
  * `HTTP.ALLOW.ANY`
@@ -284,7 +291,6 @@ await submitMetricSet({
284
291
  });
285
292
  ```
286
293
 
287
-
288
294
  | Parameter | Type | Required | Description |
289
295
  | --------- | ---- | -------- | ----------- |
290
296
  | apiKey | `string` | No | Datadog API key; checks `process.env.DATADOG_API_KEY` |
@@ -380,6 +386,76 @@ ALWAYS internal to the app, NEVER something the client did
380
386
  * For example, a complicated chain of `if`/`else` that should always return and cover all cases, may throw this as the last `else`
381
387
  * A configuration error means what happened was possible but should not have happened, an unreachable error means it should not have been possible
382
388
 
389
+ ### Express
390
+
391
+ The Express handler wraps the Jaypie handler for Express running on AWS Lambda. It will call lifecycle methods and provide logging. Unhandled errors will be thrown as `UnhandledError`. It adds the `locals` lifecycle call. It extends `jaypieHandler`, not the lambda handler.
392
+
393
+ ```javascript
394
+ const { expressHandler } = require("jaypie");
395
+
396
+ const handler = expressHandler(async(req, res) => {
397
+ // await new Promise(r => setTimeout(r, 2000));
398
+ // log.debug("Hello World");
399
+ return { message: "Hello World" };
400
+ }, { name: "lambdaReference"});
401
+ ```
402
+
403
+ #### Return Types
404
+
405
+ Do not use `res.send` or `res.json` in the handler. The return value of the handler is the response body. The status code is determined by the error thrown or the return value. Custom status codes can be set by calling `res.status` in the handler.
406
+
407
+ | Return Type | Status Code | Content-Type |
408
+ | ----------- | ----------- | ------------ |
409
+ | Object, Array | 200 OK | `application/json` |
410
+ | String | 200 OK | `text/html` |
411
+ | `true` | 201 Created | None |
412
+ | Falsy values | 204 No Content | None |
413
+
414
+ Errors can be returned by throwing the appropriate Jaypie error.
415
+
416
+ #### Lifecycle Methods
417
+
418
+ In addition to the Jaypie lifecycle methods, `expressHandler` adds `locals`, an object of scalars or functions that will be called at the end of `setup` and available to the handler as `req.locals`.
419
+
420
+ ```javascript
421
+ const handler = expressHandler(async(req) => {
422
+ // req.locals.asyncFn = "async"
423
+ // req.locals.fn = "function"
424
+ // req.locals.key = "static"
425
+ return { message: "Hello World" };
426
+ }, {
427
+ name: "lambdaReference",
428
+ locals: {
429
+ asyncFn: async() => "async",
430
+ fn: () => "function",
431
+ key: "static"
432
+ },
433
+ });
434
+ ```
435
+
436
+ #### Convenience Routes
437
+
438
+ _A "handler" returns a function that can be used as an Express route. A "route" does not require instantiation._
439
+
440
+ ```javascript
441
+ import {
442
+ echoRoute,
443
+ EXPRESS,
444
+ forbiddenRoute,
445
+ goneRoute,
446
+ noContentRoute,
447
+ notFoundRoute,
448
+ notImplementedRoute,
449
+ } from "jaypie";
450
+
451
+ app.get(EXPRESS.PATH.ROOT, noContentRoute); // 204 No Content
452
+ app.get(EXPRESS.PATH.ANY, echoRoute); // 200 OK returning the request
453
+ app.post(EXPRESS.PATH.ANY, forbiddenRoute); // 403 Forbidden
454
+ app.any("/future", notImplementedRoute); // 400 Bad Request
455
+ ```
456
+
457
+ `notImplementedRoute` returns "400 Bad Request" as a placeholder for future functionality. In this regard, calling it is a "user error." The "501 Not Implemented" status code is reserved for the server not supporting parts of the HTTP protocol such as `POST` or `PUT`.
458
+
383
459
  ### Functions
384
460
 
385
461
  #### `cloneDeep`
@@ -561,7 +637,7 @@ Called after the handler (e.g., disconnect from a database). Runs even if setup
561
637
 
562
638
  ### Lambda Handler
563
639
 
564
- The Lambda handler wraps the Jaypie handler that is specifically for AWS Lambda. It will call lifecycle methods and provide logging. Unhandled errors will be thrown as `UnhandledError`.
640
+ The Lambda handler wraps the Jaypie handler for AWS Lambda. It will call lifecycle methods and provide logging. Unhandled errors will be thrown as `UnhandledError`.
565
641
 
566
642
  ```javascript
567
643
  const { lambdaHandler } = require("jaypie");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jaypie",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "author": "Finlayson Studio",
5
5
  "type": "module",
6
6
  "exports": {
@@ -30,7 +30,7 @@
30
30
  "@jaypie/aws": "^1.0.5",
31
31
  "@jaypie/core": "^1.0.34",
32
32
  "@jaypie/datadog": "^1.0.0",
33
- "@jaypie/express": "^0.1.3",
33
+ "@jaypie/express": "^1.0.2",
34
34
  "@jaypie/lambda": "^1.0.4",
35
35
  "@jaypie/mongoose": "^1.0.7"
36
36
  },