@contextualize/lambda-router 0.0.19 → 0.0.21

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/handler.d.ts CHANGED
@@ -11,9 +11,30 @@ export default class Handler {
11
11
  private canDoBackground;
12
12
  private backgroundProcesses;
13
13
  private static executeRouter;
14
- static handler(setup: HandlerSetup, cleanup?: HandlerCleanup): (event: APIGatewayEvent) => Promise<APIGatewayResponse<any>>;
14
+ /**
15
+ * Register a background process that can past a resolution being returned to the lambda handler
16
+ */
15
17
  background(process: BackgroundProcess | (() => BackgroundProcess)): void;
18
+ /**
19
+ * Wait for the promise to be resolved before killing the lambda.
20
+ *
21
+ * Can be run in standard handler
22
+ */
23
+ await(process: BackgroundProcess | (() => BackgroundProcess)): void;
16
24
  waitForBackgroundProcesses(): Promise<void>;
17
- static serverHandler(setup: HandlerSetup, cleanup?: HandlerCleanup, props?: Parameters<typeof Server.serve>[1]): Promise<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse> | import("https").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>>;
25
+ /**
26
+ * A standard handler. No response will be returned until all processes have completed
27
+ */
28
+ static handler(setup: HandlerSetup, cleanup?: HandlerCleanup): (event: APIGatewayEvent) => Promise<APIGatewayResponse<any>>;
29
+ /**
30
+ * An advanced handler that allows promises to keep running in the background.
31
+ * Allows for quick responses when triggering long running tasks.
32
+ */
18
33
  static backgroundHandler(setup: HandlerSetup, cleanup?: HandlerCleanup): import("aws-lambda").Handler<any, any>;
34
+ /**
35
+ * Starts a node native HTTP/S server
36
+ *
37
+ * **Alternatively:** Start the lambda using `lambda-router serve`
38
+ */
39
+ static serverHandler(setup: HandlerSetup, cleanup?: HandlerCleanup, props?: Parameters<typeof Server.serve>[1]): Promise<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse> | import("https").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>>;
19
40
  }
package/dist/handler.js CHANGED
@@ -18,25 +18,24 @@ class Handler {
18
18
  result = new response_1.default();
19
19
  result.status(500).json({ message: "An unhandled error was encountered" });
20
20
  }
21
- handler.isInHandler = false;
22
21
  return result;
23
22
  }
24
- static handler(setup, cleanup) {
25
- const handler = new Handler();
26
- handler.canDoBackground = false;
27
- return async (event) => {
28
- const result = await this.executeRouter(event, handler, setup);
29
- if (cleanup) {
30
- await cleanup(event);
31
- }
32
- return result;
33
- };
34
- }
23
+ /**
24
+ * Register a background process that can past a resolution being returned to the lambda handler
25
+ */
35
26
  background(process) {
36
27
  console.log("Registering background process");
37
28
  if (!this.canDoBackground) {
38
29
  throw new Error("Lambda handler not configured to use background processes");
39
30
  }
31
+ this.await(process);
32
+ }
33
+ /**
34
+ * Wait for the promise to be resolved before killing the lambda.
35
+ *
36
+ * Can be run in standard handler
37
+ */
38
+ await(process) {
40
39
  if (!this.isInHandler) {
41
40
  throw new Error("Cannot push a new background process outside of router execution");
42
41
  }
@@ -46,6 +45,7 @@ class Handler {
46
45
  this.backgroundProcesses.push(process);
47
46
  }
48
47
  async waitForBackgroundProcesses() {
48
+ this.isInHandler = false;
49
49
  this.canDoBackground = false;
50
50
  if (this.backgroundProcesses.length > 0) {
51
51
  console.log("Awaiting background processes");
@@ -58,21 +58,29 @@ class Handler {
58
58
  });
59
59
  }
60
60
  }
61
- static serverHandler(setup, cleanup, props) {
61
+ /**
62
+ * A standard handler. No response will be returned until all processes have completed
63
+ */
64
+ static handler(setup, cleanup) {
62
65
  const handler = new Handler();
63
- handler.canDoBackground = true;
64
- return server_1.default.serve((async (event, response) => {
65
- let result = await this.executeRouter(event, handler, setup);
66
- response(result);
66
+ handler.canDoBackground = false;
67
+ return async (event) => {
68
+ const result = await this.executeRouter(event, handler, setup);
67
69
  await handler.waitForBackgroundProcesses();
68
70
  if (cleanup) {
69
71
  await cleanup(event);
70
72
  }
71
- }), props);
73
+ return result;
74
+ };
72
75
  }
76
+ /**
77
+ * An advanced handler that allows promises to keep running in the background.
78
+ * Allows for quick responses when triggering long running tasks.
79
+ */
73
80
  static backgroundHandler(setup, cleanup) {
74
- return awslambda.streamifyResponse(async (event, responseStream, _context) => {
81
+ return awslambda.streamifyResponse(async (event, responseStream, context) => {
75
82
  const handler = new Handler();
83
+ context.callbackWaitsForEmptyEventLoop = false;
76
84
  handler.canDoBackground = true;
77
85
  let result = await this.executeRouter(event, handler, setup);
78
86
  responseStream.write(JSON.stringify(result));
@@ -83,5 +91,23 @@ class Handler {
83
91
  }
84
92
  });
85
93
  }
94
+ /**
95
+ * Starts a node native HTTP/S server
96
+ *
97
+ * **Alternatively:** Start the lambda using `lambda-router serve`
98
+ */
99
+ static serverHandler(setup, cleanup, props) {
100
+ return server_1.default.serve((async (event, response) => {
101
+ const handler = new Handler();
102
+ handler.canDoBackground = true;
103
+ handler.isInHandler = true;
104
+ let result = await this.executeRouter(event, handler, setup);
105
+ response(result);
106
+ await handler.waitForBackgroundProcesses();
107
+ if (cleanup) {
108
+ await cleanup(event);
109
+ }
110
+ }), props);
111
+ }
86
112
  }
87
113
  exports.default = Handler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contextualize/lambda-router",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
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
@@ -65,10 +65,7 @@ Middleware allow you to run a block of code before all subsequent paths.
65
65
 
66
66
 
67
67
  ```js
68
- import { Router, Handler } from '@contextualize/lambda-router';
69
-
70
- const router = new Router();
71
-
68
+ // Not protected by token validation
72
69
  router.get('/', (req,res)=>{
73
70
  return res.status(200).json({
74
71
  message: "Welcome!"
@@ -82,14 +79,52 @@ router.use((req)=>{
82
79
  // Do some token validation
83
80
  })
84
81
 
82
+ // Protected by token validation
85
83
  router.get('/protected', (req, res)=>{
86
84
  return res.status(200).json({
87
85
  message: "You are authenticated"
88
86
  });
89
87
  })
88
+ ```
89
+
90
+ ### Subrouting
91
+ Subrouters allow you to organize like routes as well as define known states of the request a certain paths.
92
+
93
+ ```ts
94
+ const router = new Router();
95
+
96
+ router.get('/', (req, res)=>{
97
+
98
+ res.status(200).json({
99
+ message: 'Home'
100
+ })
101
+ })
102
+
103
+ type UserApp = {data: UserProfile}
104
+ const userRouter = new Router<UserApp>();
105
+
106
+
107
+ dataRouter.use(async (req)=>{
108
+ // Load user's profile from the database
109
+ req.app = {data: await User.load(req.params['username'])};
110
+ });
111
+
112
+ userRouter.get('/', (req, res)=>{
113
+ // Return user's data
114
+ res.status(200).json({
115
+ ...req.app.data
116
+ })
117
+ })
118
+
119
+ userRouter.get('/groups', (req, res)=>{
120
+ // Return user's groups
121
+ res.status(200).json({
122
+ groups: req.app.data.groups
123
+ })
124
+ })
90
125
 
91
- export const lambdaHandler = Handler.handler(()=>router);
92
126
 
127
+ router.subroute('/users/{username}', userRouter);
93
128
  ```
94
129
 
95
130
  ## Background Handler