lambder 1.0.62 → 1.0.64

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.
@@ -18,7 +18,9 @@
18
18
  "**/.git/": true,
19
19
  },
20
20
  "cSpell.words": [
21
- "lambder",
21
+ "lambder",
22
+ "LMBDRAUTHID",
23
+ "LMBDRTOKEN"
22
24
  ],
23
25
  }
24
26
 
package/dist/Lambder.js CHANGED
@@ -69,7 +69,8 @@ export default class Lambder {
69
69
  testPatternMatch(pattern, path) {
70
70
  return (match(pattern, { decode: decodeURIComponent }))(path) !== false;
71
71
  }
72
- async validateSession(session) {
72
+ async validateSession(ctx) {
73
+ const { session, apiName, post, cookie } = ctx;
73
74
  if (!session)
74
75
  return false;
75
76
  if (!session.userId || !session.userIdHash || !session.sessionHash || !session.csrfToken)
@@ -78,6 +79,14 @@ export default class Lambder {
78
79
  return false;
79
80
  if (session.expiresTimeStamp > Date.now())
80
81
  return false;
82
+ if (apiName) {
83
+ if (!post.token)
84
+ return false;
85
+ if (post.token !== cookie.LMBDRTOKEN)
86
+ return false;
87
+ if (post.token !== session.csrfToken)
88
+ return false;
89
+ }
81
90
  // Check DDB;
82
91
  return false;
83
92
  }
@@ -128,7 +137,7 @@ export default class Lambder {
128
137
  (typeof condition === "function" && condition(ctx)) ||
129
138
  (condition?.constructor == RegExp && condition.test(ctx.path)))),
130
139
  actionFn: async (ctx, resolver) => {
131
- const isSessionValid = this.validateSession(ctx.session);
140
+ const isSessionValid = this.validateSession(ctx);
132
141
  if (!isSessionValid)
133
142
  throw new Error("Session not found");
134
143
  if (typeof condition === "string") {
@@ -157,7 +166,7 @@ export default class Lambder {
157
166
  (typeof apiName === "function" && apiName(ctx)) ||
158
167
  (apiName?.constructor == RegExp && apiName.test(ctx.apiName))),
159
168
  actionFn: async (ctx, resolver) => {
160
- const isSessionValid = this.validateSession(ctx.session);
169
+ const isSessionValid = this.validateSession(ctx);
161
170
  if (!isSessionValid)
162
171
  throw new Error("Session not found");
163
172
  return await actionFn(ctx, resolver);
@@ -1,3 +1,4 @@
1
+ import { LambderApiResponse } from './LambderResponseBuilder';
1
2
  type VoidFunction = () => void | Promise<void>;
2
3
  type FetchTracker = {
3
4
  apiName: string;
@@ -47,7 +48,8 @@ export default class LambderCaller {
47
48
  fetchStartedHandler?: FetchStartEventHandler;
48
49
  fetchEndedHandler?: FetchEndEventHandler;
49
50
  });
50
- api<T>(apiName: string, payload?: any, headers?: Record<string, any>, options?: {
51
+ apiRaw<T>(apiName: string, payload?: any, options?: {
52
+ headers?: Record<string, any>;
51
53
  versionExpiredHandler?: VoidFunction;
52
54
  sessionExpiredHandler?: VoidFunction;
53
55
  messageHandler?: MessageHandler;
@@ -56,6 +58,7 @@ export default class LambderCaller {
56
58
  errorHandler?: ErrorHandler;
57
59
  fetchStartedHandler?: FetchStartEventHandler;
58
60
  fetchEndedHandler?: FetchEndEventHandler;
59
- }): Promise<T | null>;
61
+ }): Promise<LambderApiResponse<T> | null>;
62
+ api<T>(...params: Parameters<typeof LambderCaller.prototype.apiRaw>): Promise<T | null>;
60
63
  }
61
64
  export {};
@@ -27,7 +27,8 @@ export default class LambderCaller {
27
27
  this.fetchEndedHandler = fetchEndedHandler;
28
28
  }
29
29
  ;
30
- async api(apiName, payload, headers, options) {
30
+ async apiRaw(apiName, payload, options) {
31
+ const headers = options?.headers;
31
32
  const fetchTracker = { apiName, done: false, fetchEndCalled: false };
32
33
  try {
33
34
  this.fetchTrackerList.push(fetchTracker);
@@ -37,7 +38,7 @@ export default class LambderCaller {
37
38
  activeFetchList: this.fetchTrackerList.filter(v => !v.done)
38
39
  });
39
40
  const version = this.apiVersion;
40
- const token = Cookies.get("LMBRTOKEN") || "";
41
+ const token = Cookies.get("LMBDRTOKEN") || "";
41
42
  const siteHost = window.location.hostname;
42
43
  let data = await fetch(this.apiPath, {
43
44
  method: 'POST', mode: 'same-origin', cache: 'no-cache',
@@ -68,8 +69,8 @@ export default class LambderCaller {
68
69
  return null;
69
70
  }
70
71
  if (data && data.sessionExpired) {
71
- Cookies.set('LMBRAUTHID', '', { expires: -1 });
72
- Cookies.set('LMBRTOKEN', '', { expires: -1 });
72
+ Cookies.set('LMBDRAUTHID', '', { expires: -1 });
73
+ Cookies.set('LMBDRTOKEN', '', { expires: -1 });
73
74
  if (this.sessionExpiredHandler) {
74
75
  await this.sessionExpiredHandler();
75
76
  }
@@ -93,7 +94,7 @@ export default class LambderCaller {
93
94
  if (data && data.errorMessage && this.errorMessageHandler) {
94
95
  await this.errorMessageHandler(data.errorMessage);
95
96
  }
96
- return data.payload ?? null;
97
+ return data;
97
98
  }
98
99
  catch (err) {
99
100
  const wrappedError = err instanceof Error ? err : new Error("Error: " + String(err));
@@ -112,4 +113,11 @@ export default class LambderCaller {
112
113
  }
113
114
  }
114
115
  ;
116
+ // Use the same type for api but adjust the return type
117
+ async api(...params) {
118
+ const result = await this.apiRaw(...params);
119
+ if (!result)
120
+ return null;
121
+ return result.payload ?? null;
122
+ }
115
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.62",
3
+ "version": "1.0.64",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/Lambder.ts CHANGED
@@ -123,11 +123,18 @@ export default class Lambder {
123
123
  return (match(pattern, { decode: decodeURIComponent }))(path) !== false;
124
124
  }
125
125
 
126
- private async validateSession (session: Session|null): Promise<boolean>{
126
+ private async validateSession (ctx: RenderContext): Promise<boolean>{
127
+ const { session, apiName, post, cookie } = ctx;
127
128
  if(!session) return false;
128
129
  if(!session.userId || !session.userIdHash || !session.sessionHash || !session.csrfToken) return false;
129
130
  if(!session.createdTimeStamp || !session.expiresTimeStamp) return false;
130
131
  if(session.expiresTimeStamp > Date.now()) return false;
132
+
133
+ if(apiName){
134
+ if(!post.token) return false;
135
+ if(post.token !== cookie.LMBDRTOKEN) return false;
136
+ if(post.token !== session.csrfToken) return false;
137
+ }
131
138
  // Check DDB;
132
139
  return false;
133
140
  };
@@ -183,7 +190,7 @@ export default class Lambder {
183
190
  )
184
191
  ),
185
192
  actionFn: async (ctx:RenderContext, resolver: Resolver) => {
186
- const isSessionValid = this.validateSession(ctx.session);
193
+ const isSessionValid = this.validateSession(ctx);
187
194
  if(!isSessionValid) throw new Error("Session not found");
188
195
  if(typeof condition === "string"){
189
196
  ctx.pathParams = this.getPatternMatch(condition, ctx.path);
@@ -214,7 +221,7 @@ export default class Lambder {
214
221
  (apiName?.constructor == RegExp && apiName.test(ctx.apiName))
215
222
  ),
216
223
  actionFn: async (ctx:RenderContext, resolver: Resolver) => {
217
- const isSessionValid = this.validateSession(ctx.session);
224
+ const isSessionValid = this.validateSession(ctx);
218
225
  if(!isSessionValid) throw new Error("Session not found");
219
226
  return await actionFn(ctx, resolver);
220
227
  }
@@ -81,7 +81,8 @@ export default class LambderCaller {
81
81
 
82
82
  };
83
83
 
84
- async api<T>(apiName: string, payload?: any, headers?: Record<string, any>, options?: {
84
+ async apiRaw<T>(apiName: string, payload?: any, options?: {
85
+ headers?: Record<string, any>
85
86
  versionExpiredHandler?: VoidFunction,
86
87
  sessionExpiredHandler?: VoidFunction,
87
88
  messageHandler?: MessageHandler,
@@ -90,7 +91,8 @@ export default class LambderCaller {
90
91
  errorHandler?: ErrorHandler,
91
92
  fetchStartedHandler?: FetchStartEventHandler,
92
93
  fetchEndedHandler?: FetchEndEventHandler,
93
- }): Promise<T|null>{
94
+ }): Promise<LambderApiResponse<T>|null>{
95
+ const headers = options?.headers;
94
96
  const fetchTracker: FetchTracker = { apiName, done: false, fetchEndCalled: false };
95
97
  try {
96
98
  this.fetchTrackerList.push(fetchTracker);
@@ -99,7 +101,7 @@ export default class LambderCaller {
99
101
  activeFetchList: this.fetchTrackerList.filter(v=>!v.done)
100
102
  });
101
103
  const version = this.apiVersion;
102
- const token = Cookies.get("LMBRTOKEN") || "";
104
+ const token = Cookies.get("LMBDRTOKEN") || "";
103
105
  const siteHost = window.location.hostname;
104
106
  let data = await fetch(this.apiPath, {
105
107
  method: 'POST', mode: 'same-origin', cache: 'no-cache',
@@ -128,8 +130,8 @@ export default class LambderCaller {
128
130
  return null;
129
131
  }
130
132
  if(data && data.sessionExpired){
131
- Cookies.set('LMBRAUTHID', '', { expires: -1 });
132
- Cookies.set('LMBRTOKEN', '', { expires: -1 });
133
+ Cookies.set('LMBDRAUTHID', '', { expires: -1 });
134
+ Cookies.set('LMBDRTOKEN', '', { expires: -1 });
133
135
  if(this.sessionExpiredHandler){
134
136
  await this.sessionExpiredHandler();
135
137
  }else if(this.errorHandler){
@@ -151,7 +153,7 @@ export default class LambderCaller {
151
153
  if(data && data.errorMessage && this.errorMessageHandler){
152
154
  await this.errorMessageHandler(data.errorMessage);
153
155
  }
154
- return data.payload ?? null;
156
+ return data;
155
157
  }catch(err){
156
158
  const wrappedError = err instanceof Error ? err : new Error("Error: " + String(err));
157
159
  fetchTracker.done = true;
@@ -166,4 +168,12 @@ export default class LambderCaller {
166
168
  return null;
167
169
  }
168
170
  };
171
+
172
+ // Use the same type for api but adjust the return type
173
+ async api<T>(...params: Parameters<typeof LambderCaller.prototype.apiRaw>): Promise<T|null> {
174
+ const result = await this.apiRaw<T>(...params);
175
+ if (!result) return null;
176
+ return result.payload ?? null;
177
+ }
178
+
169
179
  }