@contextualize/lambda-router 0.0.18 → 0.0.19

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/router.js CHANGED
@@ -66,8 +66,9 @@ class Router {
66
66
  response.json({ message: exception.message });
67
67
  }
68
68
  else {
69
- console.error(`Request experienced a critical error`);
70
- throw exception;
69
+ console.error(`Request experienced a critical error: `, exception);
70
+ response.statusCode = 500;
71
+ response.json({ message: "Internal Server Error" });
71
72
  }
72
73
  }
73
74
  return response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contextualize/lambda-router",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "Allows for the creation of Express-like APIs within AWS Lambda",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
package/readme.md CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  Allows for the creation of Express-like APIs within AWS Lambda.
4
4
 
5
- ## Create Router
6
-
5
+ ## Routers
6
+ Routers allow you to construct API paths for your application
7
+ ### Basic Usage
7
8
  ```js
8
9
  import { Router, Handler } from '@contextualize/lambda-router';
9
10
 
@@ -19,6 +20,78 @@ export const lambdaHandler = Handler.handler(()=>router);
19
20
 
20
21
  ```
21
22
 
23
+ ### Errors
24
+ Lambda router has a number of built-in exceptions for common HTTP error codes. Rather than explicity sending an error response and returning from your endpoint code, you can just throw one of these exceptions.
25
+
26
+ If your application throws an error that does not extend from `@contextualize/lambda-router/PublicError`, a HTTP 500 status is returned by default with 'Internal Server Error'
27
+
28
+ ```js
29
+ router.post('/', (req, res)=>{
30
+ if(!("required" in req.body)){
31
+ // Ends execution
32
+ // Returns a HTTP 400 error to the user
33
+ // Error message is supplied to user in the response body as json
34
+ throw new BadRequestError("'required' must be in request body");
35
+ }
36
+
37
+ return res.status(200).json({
38
+ status: "ok"
39
+ });
40
+ })
41
+
42
+ ```
43
+
44
+ ### Path Parameters
45
+ Parameters can be passed in through the path using `{}`. Lambda Router will always prioritize named paths before attempting to use a parameterized path.
46
+ ```js
47
+ router.get('/{myParam}', (req, res)=>{
48
+ return res.status(200).json({
49
+ status: "ok"
50
+ myParam: req.params['myParam']
51
+ });
52
+ });
53
+
54
+
55
+ router.get('/list', (req, res)=>{
56
+ return res.status(200).json({
57
+ status: "ok"
58
+ list: ['stuff', 'and', 'things']
59
+ });
60
+ });
61
+ ```
62
+
63
+ ### Middleware
64
+ Middleware allow you to run a block of code before all subsequent paths.
65
+
66
+
67
+ ```js
68
+ import { Router, Handler } from '@contextualize/lambda-router';
69
+
70
+ const router = new Router();
71
+
72
+ router.get('/', (req,res)=>{
73
+ return res.status(200).json({
74
+ message: "Welcome!"
75
+ });
76
+ })
77
+
78
+ // Check user token
79
+ router.use((req)=>{
80
+ const token = req.headers['Authorization'];
81
+
82
+ // Do some token validation
83
+ })
84
+
85
+ router.get('/protected', (req, res)=>{
86
+ return res.status(200).json({
87
+ message: "You are authenticated"
88
+ });
89
+ })
90
+
91
+ export const lambdaHandler = Handler.handler(()=>router);
92
+
93
+ ```
94
+
22
95
  ## Background Handler
23
96
  Some lambdas may require tasks to run in the background after a request has been handled. `Handler.backgroundHandler` allows you to defer promises for after the lambda has returned a response to the client.
24
97
 
@@ -30,7 +103,8 @@ const router = new Router();
30
103
  router.get('/', (req, res)=>{
31
104
  req.handler.background(async ()=>{
32
105
 
33
- // Assume this a request that is going to take a long time
106
+ // Assume this is a request that is going to take a long time
107
+ // i.e. uploading a file, running a put query
34
108
  await fetch('https://example.com')
35
109
  })
36
110