lambder 1.0.112 → 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 +83 -2
- package/dist/Lambder.d.ts +2 -3
- package/dist/Lambder.js +5 -5
- package/dist/LambderCaller.d.ts +5 -2
- package/dist/LambderCaller.js +11 -5
- package/dist/LambderSession.d.ts +1 -1
- package/dist/LambderSession.js +1 -1
- package/package.json +1 -1
- package/src/Lambder.ts +6 -6
- package/src/LambderCaller.ts +15 -6
- package/src/LambderSession.ts +1 -1
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**:
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
229
|
+
ctx.session = await this.lambderSession.updateSessionData(ctx.session, newData);
|
|
230
230
|
return ctx.session;
|
|
231
231
|
},
|
|
232
232
|
deleteSession: async () => {
|
|
@@ -303,7 +303,7 @@ export default class Lambder {
|
|
|
303
303
|
response.multiValueHeaders = {
|
|
304
304
|
...(response.multiValueHeaders || {}),
|
|
305
305
|
"Set-Cookie": [
|
|
306
|
-
`${this.sessionTokenCookieKey}=${ctx.session.sessionToken}; Expires=${new Date(ctx.session.expiresAt * 1000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
306
|
+
`${this.sessionTokenCookieKey}=${ctx.session.sessionToken}; Expires=${new Date(ctx.session.expiresAt * 1000).toUTCString()}; Path=/; HttpOnly; SameSite=Lax; Secure`,
|
|
307
307
|
`${this.sessionCsrfCookieKey}=${ctx.session.csrfToken}; Expires=${new Date(ctx.session.expiresAt * 1000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
308
308
|
],
|
|
309
309
|
};
|
|
@@ -313,7 +313,7 @@ export default class Lambder {
|
|
|
313
313
|
response.multiValueHeaders = {
|
|
314
314
|
...(response.multiValueHeaders || {}),
|
|
315
315
|
"Set-Cookie": [
|
|
316
|
-
`${this.sessionTokenCookieKey}=0; Expires=${new Date(Date.now() - 100000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
316
|
+
`${this.sessionTokenCookieKey}=0; Expires=${new Date(Date.now() - 100000).toUTCString()}; Path=/; HttpOnly; SameSite=Lax; Secure`,
|
|
317
317
|
`${this.sessionCsrfCookieKey}=0; Expires=${new Date(Date.now() - 100000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
318
318
|
],
|
|
319
319
|
};
|
package/dist/LambderCaller.d.ts
CHANGED
|
@@ -35,10 +35,12 @@ export default class LambderCaller {
|
|
|
35
35
|
private errorHandler?;
|
|
36
36
|
private fetchStartedHandler?;
|
|
37
37
|
private fetchEndedHandler?;
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
private sessionTokenCookieKey;
|
|
39
|
+
private sessionCsrfCookieKey;
|
|
40
|
+
constructor({ apiPath, apiVersion, isCorsEnabled, versionExpiredHandler, sessionExpiredHandler, messageHandler, errorMessageHandler, notAuthorizedHandler, errorHandler, fetchStartedHandler, fetchEndedHandler, }: {
|
|
40
41
|
apiPath: string;
|
|
41
42
|
apiVersion?: string;
|
|
43
|
+
isCorsEnabled: boolean;
|
|
42
44
|
versionExpiredHandler?: VoidFunction;
|
|
43
45
|
sessionExpiredHandler?: VoidFunction;
|
|
44
46
|
messageHandler?: MessageHandler;
|
|
@@ -48,6 +50,7 @@ export default class LambderCaller {
|
|
|
48
50
|
fetchStartedHandler?: FetchStartEventHandler;
|
|
49
51
|
fetchEndedHandler?: FetchEndEventHandler;
|
|
50
52
|
});
|
|
53
|
+
setSessionCookieKey(sessionTokenCookieKey: string, sessionCsrfCookieKey: string): void;
|
|
51
54
|
apiRaw<T = any>(apiName: string, payload?: any, options?: {
|
|
52
55
|
headers?: Record<string, any>;
|
|
53
56
|
versionExpiredHandler?: VoidFunction;
|
package/dist/LambderCaller.js
CHANGED
|
@@ -13,10 +13,12 @@ export default class LambderCaller {
|
|
|
13
13
|
errorHandler;
|
|
14
14
|
fetchStartedHandler;
|
|
15
15
|
fetchEndedHandler;
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
sessionTokenCookieKey = "LMDRSESSIONTKID";
|
|
17
|
+
sessionCsrfCookieKey = "LMDRSESSIONCSTK";
|
|
18
|
+
constructor({ apiPath, apiVersion, isCorsEnabled = false, versionExpiredHandler, sessionExpiredHandler, messageHandler, errorMessageHandler, notAuthorizedHandler, errorHandler, fetchStartedHandler, fetchEndedHandler, }) {
|
|
18
19
|
this.apiPath = apiPath ?? "/api";
|
|
19
20
|
this.apiVersion = apiVersion;
|
|
21
|
+
this.isCorsEnabled = isCorsEnabled;
|
|
20
22
|
this.versionExpiredHandler = versionExpiredHandler;
|
|
21
23
|
this.sessionExpiredHandler = sessionExpiredHandler;
|
|
22
24
|
this.messageHandler = messageHandler;
|
|
@@ -27,6 +29,10 @@ export default class LambderCaller {
|
|
|
27
29
|
this.fetchEndedHandler = fetchEndedHandler;
|
|
28
30
|
}
|
|
29
31
|
;
|
|
32
|
+
setSessionCookieKey(sessionTokenCookieKey, sessionCsrfCookieKey) {
|
|
33
|
+
this.sessionTokenCookieKey = sessionTokenCookieKey;
|
|
34
|
+
this.sessionCsrfCookieKey = sessionCsrfCookieKey;
|
|
35
|
+
}
|
|
30
36
|
async apiRaw(apiName, payload, options) {
|
|
31
37
|
const headers = options?.headers;
|
|
32
38
|
const fetchTracker = { apiName, done: false, fetchEndCalled: false };
|
|
@@ -38,7 +44,7 @@ export default class LambderCaller {
|
|
|
38
44
|
activeFetchList: this.fetchTrackerList.filter(v => !v.done)
|
|
39
45
|
});
|
|
40
46
|
const version = this.apiVersion;
|
|
41
|
-
const token = Cookies.get(
|
|
47
|
+
const token = Cookies.get(this.sessionCsrfCookieKey) || "";
|
|
42
48
|
const siteHost = window.location.hostname;
|
|
43
49
|
let data = await fetch(this.apiPath, {
|
|
44
50
|
method: 'POST', mode: 'same-origin', cache: 'no-cache',
|
|
@@ -69,8 +75,8 @@ export default class LambderCaller {
|
|
|
69
75
|
return null;
|
|
70
76
|
}
|
|
71
77
|
if (data && data.sessionExpired) {
|
|
72
|
-
Cookies.set(
|
|
73
|
-
Cookies.set(
|
|
78
|
+
Cookies.set(this.sessionTokenCookieKey, '', { expires: -1 });
|
|
79
|
+
Cookies.set(this.sessionCsrfCookieKey, '', { expires: -1 });
|
|
74
80
|
if (this.sessionExpiredHandler) {
|
|
75
81
|
await this.sessionExpiredHandler();
|
|
76
82
|
}
|
package/dist/LambderSession.d.ts
CHANGED
|
@@ -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
|
-
|
|
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>;
|
package/dist/LambderSession.js
CHANGED
package/package.json
CHANGED
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
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
301
|
+
ctx.session = await this.lambderSession.updateSessionData(ctx.session, newData);
|
|
302
302
|
return ctx.session;
|
|
303
303
|
},
|
|
304
304
|
deleteSession: async () => {
|
|
@@ -377,7 +377,7 @@ export default class Lambder {
|
|
|
377
377
|
response.multiValueHeaders = {
|
|
378
378
|
...(response.multiValueHeaders || {}),
|
|
379
379
|
"Set-Cookie": [
|
|
380
|
-
`${this.sessionTokenCookieKey}=${ctx.session.sessionToken}; Expires=${new Date(ctx.session.expiresAt * 1000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
380
|
+
`${this.sessionTokenCookieKey}=${ctx.session.sessionToken}; Expires=${new Date(ctx.session.expiresAt * 1000).toUTCString()}; Path=/; HttpOnly; SameSite=Lax; Secure`,
|
|
381
381
|
`${this.sessionCsrfCookieKey}=${ctx.session.csrfToken}; Expires=${new Date(ctx.session.expiresAt * 1000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
382
382
|
],
|
|
383
383
|
};
|
|
@@ -386,7 +386,7 @@ export default class Lambder {
|
|
|
386
386
|
response.multiValueHeaders = {
|
|
387
387
|
...(response.multiValueHeaders || {}),
|
|
388
388
|
"Set-Cookie": [
|
|
389
|
-
`${this.sessionTokenCookieKey}=0; Expires=${new Date(Date.now() - 100000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
389
|
+
`${this.sessionTokenCookieKey}=0; Expires=${new Date(Date.now() - 100000).toUTCString()}; Path=/; HttpOnly; SameSite=Lax; Secure`,
|
|
390
390
|
`${this.sessionCsrfCookieKey}=0; Expires=${new Date(Date.now() - 100000).toUTCString()}; Path=/; SameSite=Lax; Secure`,
|
|
391
391
|
],
|
|
392
392
|
};
|
package/src/LambderCaller.ts
CHANGED
|
@@ -42,18 +42,22 @@ export default class LambderCaller {
|
|
|
42
42
|
private fetchStartedHandler?: FetchStartEventHandler;
|
|
43
43
|
private fetchEndedHandler?: FetchEndEventHandler;
|
|
44
44
|
|
|
45
|
+
private sessionTokenCookieKey = "LMDRSESSIONTKID";
|
|
46
|
+
private sessionCsrfCookieKey = "LMDRSESSIONCSTK";
|
|
47
|
+
|
|
45
48
|
constructor(
|
|
46
49
|
{
|
|
47
|
-
|
|
50
|
+
apiPath, apiVersion,
|
|
51
|
+
isCorsEnabled = false,
|
|
48
52
|
versionExpiredHandler, sessionExpiredHandler,
|
|
49
53
|
messageHandler, errorMessageHandler,
|
|
50
54
|
notAuthorizedHandler, errorHandler,
|
|
51
55
|
fetchStartedHandler, fetchEndedHandler,
|
|
52
56
|
}:
|
|
53
57
|
{
|
|
54
|
-
isCorsEnabled: boolean,
|
|
55
58
|
apiPath: string,
|
|
56
59
|
apiVersion?: string,
|
|
60
|
+
isCorsEnabled: boolean,
|
|
57
61
|
versionExpiredHandler?: VoidFunction,
|
|
58
62
|
sessionExpiredHandler?: VoidFunction,
|
|
59
63
|
messageHandler?: MessageHandler,
|
|
@@ -64,9 +68,9 @@ export default class LambderCaller {
|
|
|
64
68
|
fetchEndedHandler?: FetchEndEventHandler,
|
|
65
69
|
}
|
|
66
70
|
){
|
|
67
|
-
this.isCorsEnabled = isCorsEnabled;
|
|
68
71
|
this.apiPath = apiPath ?? "/api";
|
|
69
72
|
this.apiVersion = apiVersion;
|
|
73
|
+
this.isCorsEnabled = isCorsEnabled;
|
|
70
74
|
|
|
71
75
|
this.versionExpiredHandler = versionExpiredHandler;
|
|
72
76
|
this.sessionExpiredHandler = sessionExpiredHandler;
|
|
@@ -81,6 +85,11 @@ export default class LambderCaller {
|
|
|
81
85
|
|
|
82
86
|
};
|
|
83
87
|
|
|
88
|
+
setSessionCookieKey(sessionTokenCookieKey: string, sessionCsrfCookieKey: string){
|
|
89
|
+
this.sessionTokenCookieKey = sessionTokenCookieKey;
|
|
90
|
+
this.sessionCsrfCookieKey = sessionCsrfCookieKey;
|
|
91
|
+
}
|
|
92
|
+
|
|
84
93
|
async apiRaw<T=any>(apiName: string, payload?: any, options?: {
|
|
85
94
|
headers?: Record<string, any>
|
|
86
95
|
versionExpiredHandler?: VoidFunction,
|
|
@@ -101,7 +110,7 @@ export default class LambderCaller {
|
|
|
101
110
|
activeFetchList: this.fetchTrackerList.filter(v=>!v.done)
|
|
102
111
|
});
|
|
103
112
|
const version = this.apiVersion;
|
|
104
|
-
const token = Cookies.get(
|
|
113
|
+
const token = Cookies.get(this.sessionCsrfCookieKey) || "";
|
|
105
114
|
const siteHost = window.location.hostname;
|
|
106
115
|
let data = await fetch(this.apiPath, {
|
|
107
116
|
method: 'POST', mode: 'same-origin', cache: 'no-cache',
|
|
@@ -130,8 +139,8 @@ export default class LambderCaller {
|
|
|
130
139
|
return null;
|
|
131
140
|
}
|
|
132
141
|
if(data && data.sessionExpired){
|
|
133
|
-
Cookies.set(
|
|
134
|
-
Cookies.set(
|
|
142
|
+
Cookies.set(this.sessionTokenCookieKey, '', { expires: -1 });
|
|
143
|
+
Cookies.set(this.sessionCsrfCookieKey, '', { expires: -1 });
|
|
135
144
|
if(this.sessionExpiredHandler){
|
|
136
145
|
await this.sessionExpiredHandler();
|
|
137
146
|
}else if(this.errorHandler){
|
package/src/LambderSession.ts
CHANGED