lambder 1.0.113 → 1.0.114

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/Readme.md CHANGED
@@ -5,7 +5,7 @@ Lambder is a highly opinionated dynamic serverless framework designed to facilit
5
5
  ## Features
6
6
 
7
7
  - **Simple API & Route Declaration**: Define your APIs and routes using concise and expressive syntax.
8
- - **Session Management**: IN PROGRESS: Built-in session management to secure and personalize user experiences.
8
+ - **Session Management**: Built-in session management to secure and personalize user experiences.
9
9
  - **Flexible Hooks System**: Employ hooks to execute code at different stages of the request lifecycle, enabling fine-grained control over the application flow.
10
10
  - **Error Handling**: Comprehensive error handling capabilities, including global error handlers and route-specific fallbacks.
11
11
  - **Seamless Integration**: Designed to work effortlessly with AWS Lambda and API Gateway, providing a straightforward path to deploy serverless applications.
@@ -30,11 +30,21 @@ import * as path from 'path';
30
30
 
31
31
  const lambder = new Lambder({
32
32
  apiPath: "/secure",
33
- isCorsEnabled: true,
34
33
  publicPath: path.resolve(`./public`),
35
34
  // ejsPath: path.resolve(`./ejs-templates`),
36
35
  });
37
36
 
37
+
38
+ // Enable session
39
+ lambder.enableDdbSession({
40
+ tableName: "website-session",
41
+ tableRegion: "us-east-1",
42
+ sessionSalt: "8p6Vt+4b1w3N8d/dcJ47QF3DRkp9koFg0G" // Change salt
43
+ });
44
+
45
+ // Enable Cors
46
+ lambder.setIsCorsEnabled(true);
47
+
38
48
  // Define a simple api
39
49
  lambder.addApi("getCompanyPage", async ({ apiPayload }, res) => {
40
50
  const companyName = apiPayload.companyName;
@@ -42,6 +52,13 @@ lambder.addApi("getCompanyPage", async ({ apiPayload }, res) => {
42
52
  return res.api(data);
43
53
  });
44
54
 
55
+ // Start a session from an API
56
+ lambder.addApi("loginUser", async (ctx, res) => {
57
+ const user = await fetchUserData();
58
+ await lambder.getSessionController(ctx).startSession(user.id);
59
+ return res.api({ success: true });
60
+ });
61
+
45
62
  // Define a simple route
46
63
  lambder.addRoute("/hello-world", (ctx, res) => {
47
64
  return res.html("Hello World");
@@ -114,6 +131,29 @@ export const handler = (event, context) => {
114
131
  ### Adding APIs
115
132
 
116
133
  For more details on route matching, please check [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) package.
134
+ ```typescript
135
+ // Add routes
136
+ lambder.addRoute(pathAsString, async (ctx, res) => {});
137
+ lambder.addRoute(pathAsRegex, async (ctx, res) => {});
138
+ lambder.addRoute(matchFunction, async (ctx, res) => {});
139
+
140
+ // Add apis
141
+ lambder.addApi(apiNameAsString, async (ctx, res) => {});
142
+ lambder.addApi(apiNameAsRegex, async (ctx, res) => {});
143
+ lambder.addApi(matchFunction, async (ctx, res) => {});
144
+
145
+ // Add only session accessible routes
146
+ lambder.addSessionRoute(pathAsString, async (ctx, res) => {});
147
+ lambder.addSessionRoute(pathAsRegex, async (ctx, res) => {});
148
+ lambder.addSessionRoute(matchFunction, async (ctx, res) => {});
149
+
150
+ // Add only session accessible apis
151
+ lambder.addSessionApi(apiNameAsString, async (ctx, res) => {});
152
+ lambder.addSessionApi(apiNameAsRegex, async (ctx, res) => {});
153
+ lambder.addSessionApi(matchFunction, async (ctx, res) => {});
154
+
155
+ ```
156
+
117
157
 
118
158
  ```typescript
119
159
  // Define a simple api
@@ -128,6 +168,21 @@ lambder.addApi("getCompanyPage", async (ctx, res) => {
128
168
  });
129
169
  ```
130
170
 
171
+ ```typescript
172
+ // Define an only session accessible api
173
+ lambder.addSessionApi("getCompanyPage", async (ctx, res) => {
174
+ const {
175
+ host, path, get, post, cookie, headers,
176
+ session, apiName, apiPayload
177
+ } = ctx;
178
+
179
+ const companyName = session.data.companyName;
180
+ const data = await fetchDataSomehow(companyName);
181
+ return res.api(data);
182
+ });
183
+ ```
184
+
185
+
131
186
 
132
187
  ### Adding Routes
133
188
  ```typescript
@@ -161,6 +216,32 @@ lambder.addModule(async (lambder: Lambder): Promise<void> => {
161
216
  });
162
217
  ```
163
218
 
219
+ ### Session Management
220
+
221
+ ```typescript
222
+ lambder.addApi("getCompanyPage", async (ctx, res) => {
223
+ const sessionController = lambder.getSessionController(ctx);
224
+ const userId = "37234";
225
+ await sessionController.startSession(userId, { "business": "Session data goes here" });
226
+ console.log(ctx.session?.userKey); // "37234"
227
+ console.log(ctx.session?.data?.business); // "Session data goes here"
228
+
229
+ await sessionController.updateSessionData({ "business2": "Session data updated" });
230
+ console.log(ctx.session?.userKey); // "37234"
231
+ console.log(ctx.session?.data?.business); // undefined
232
+ console.log(ctx.session?.data?.business2); // "Session data updated"
233
+
234
+ await sessionController.deleteSession(); // End session
235
+ console.log(ctx.session?.userKey); // undefined
236
+ console.log(ctx.session?.data?.business); // undefined
237
+
238
+ await sessionController.deleteSessionAll(); // End session for this user in all devices
239
+ console.log(ctx.session?.userKey); // undefined
240
+ console.log(ctx.session?.data?.business); // undefined
241
+
242
+ });
243
+ ```
244
+
164
245
  ### EJS Templates:
165
246
 
166
247
  EJS templates have the variables `page` and `partial` available:
package/dist/Lambder.d.ts CHANGED
@@ -49,9 +49,8 @@ export default class Lambder {
49
49
  apiPath?: string;
50
50
  ejsPath?: string;
51
51
  apiVersion?: string;
52
- isCorsEnabled?: boolean;
53
52
  });
54
- enableCORS(isCorsEnabled: boolean): void;
53
+ setIsCorsEnabled(isCorsEnabled: boolean): void;
55
54
  enableDdbSession({ tableName, tableRegion, sessionSalt }: {
56
55
  tableName: string;
57
56
  tableRegion: string;
@@ -84,7 +83,7 @@ export default class Lambder {
84
83
  addHook(hookEvent: 'fallback', hookFn: HookFallbackFunction, priority?: number): Promise<void>;
85
84
  getSessionController(ctx: LambderRenderContext): {
86
85
  startSession: (userKey: string, data?: any, ttlInSeconds?: number) => Promise<LambderSessionContext>;
87
- editSession: (newData: any) => Promise<LambderSessionContext>;
86
+ updateSessionData: (newData: any) => Promise<LambderSessionContext>;
88
87
  deleteSession: () => Promise<void>;
89
88
  deleteSessionAll: () => Promise<void>;
90
89
  };
package/dist/Lambder.js CHANGED
@@ -60,7 +60,7 @@ export default class Lambder {
60
60
  };
61
61
  this.utils = new LambderUtils({ ejsPath });
62
62
  }
63
- enableCORS(isCorsEnabled) {
63
+ setIsCorsEnabled(isCorsEnabled) {
64
64
  this.isCorsEnabled = isCorsEnabled;
65
65
  }
66
66
  enableDdbSession({ tableName, tableRegion, sessionSalt }, { partitionKey, sortKey } = { partitionKey: "pk", sortKey: "sk" }) {
@@ -221,12 +221,12 @@ export default class Lambder {
221
221
  ctx.session = await this.lambderSession.createSession(userKey, data, ttlInSeconds);
222
222
  return ctx.session;
223
223
  },
224
- editSession: async (newData) => {
224
+ updateSessionData: async (newData) => {
225
225
  if (!this.lambderSession)
226
226
  throw "Session is not enabled. Use lambder.enableDdbSession(...) to configure.";
227
227
  if (!ctx.session)
228
228
  throw "Session not found.";
229
- ctx.session = await this.lambderSession.editSession(ctx.session, newData);
229
+ ctx.session = await this.lambderSession.updateSessionData(ctx.session, newData);
230
230
  return ctx.session;
231
231
  },
232
232
  deleteSession: async () => {
@@ -28,7 +28,7 @@ export default class LambderSession {
28
28
  private ddbQueryAllByPartitionKey;
29
29
  private ddbDeleteAllByPartitionKey;
30
30
  createSession(userKey: string, data?: any, ttlInSeconds?: number): Promise<LambderSessionContext>;
31
- editSession(session: LambderSessionContext, newData?: any): Promise<LambderSessionContext>;
31
+ updateSessionData(session: LambderSessionContext, newData?: any): Promise<LambderSessionContext>;
32
32
  getSession(sessionToken: string): Promise<LambderSessionContext | null>;
33
33
  isSessionValid(session: any, sessionToken: any, csrfToken: any, skipCsrfTokenCheck?: boolean): boolean;
34
34
  deleteSession(session: Record<string, any>): Promise<boolean>;
@@ -80,7 +80,7 @@ export default class LambderSession {
80
80
  await this.ddbPutItem(session);
81
81
  return session;
82
82
  }
83
- async editSession(session, newData) {
83
+ async updateSessionData(session, newData) {
84
84
  if (!session)
85
85
  throw "Invalid session";
86
86
  session.data = newData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.113",
3
+ "version": "1.0.114",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/Lambder.ts CHANGED
@@ -96,7 +96,7 @@ export default class Lambder {
96
96
 
97
97
  constructor(
98
98
  { publicPath, apiPath, ejsPath, apiVersion }:
99
- { publicPath: string, apiPath?: string, ejsPath?: string, apiVersion?: string, isCorsEnabled?: boolean, }
99
+ { publicPath: string, apiPath?: string, ejsPath?: string, apiVersion?: string }
100
100
  ){
101
101
  this.publicPath = publicPath || "/incorrect-path-not-found";
102
102
  this.ejsPath = ejsPath || "/incorrect-ejs-path-not-found";
@@ -113,7 +113,7 @@ export default class Lambder {
113
113
  this.utils = new LambderUtils({ ejsPath });
114
114
  }
115
115
 
116
- enableCORS(isCorsEnabled: boolean){
116
+ setIsCorsEnabled(isCorsEnabled: boolean){
117
117
  this.isCorsEnabled = isCorsEnabled;
118
118
  }
119
119
 
@@ -295,10 +295,10 @@ export default class Lambder {
295
295
  ctx.session = await this.lambderSession.createSession(userKey, data, ttlInSeconds);
296
296
  return ctx.session;
297
297
  },
298
- editSession: async (newData: any): Promise<LambderSessionContext> => {
298
+ updateSessionData: async (newData: any): Promise<LambderSessionContext> => {
299
299
  if(!this.lambderSession) throw "Session is not enabled. Use lambder.enableDdbSession(...) to configure.";
300
300
  if(!ctx.session) throw "Session not found.";
301
- ctx.session = await this.lambderSession.editSession(ctx.session, newData);
301
+ ctx.session = await this.lambderSession.updateSessionData(ctx.session, newData);
302
302
  return ctx.session;
303
303
  },
304
304
  deleteSession: async () => {
@@ -116,7 +116,7 @@ export default class LambderSession{
116
116
  return session;
117
117
  }
118
118
 
119
- public async editSession(
119
+ public async updateSessionData(
120
120
  session: LambderSessionContext,
121
121
  newData?: any
122
122
  ): Promise<LambderSessionContext> {