lambder 1.0.73 → 1.0.76

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/Lambder.js CHANGED
@@ -13,7 +13,6 @@ export const createContext = (event, lambdaContext, apiPath) => {
13
13
  const headers = event.headers;
14
14
  let post = {};
15
15
  const session = null;
16
- const apiName = (method === "POST" && apiPath && path === apiPath && post.apiName) ? post.apiName : null;
17
16
  try {
18
17
  const decodedBody = event.isBase64Encoded ? (event.body ? Buffer.from(event.body, "base64").toString() : "{}") : (event.body || "{}");
19
18
  try {
@@ -24,6 +23,8 @@ export const createContext = (event, lambdaContext, apiPath) => {
24
23
  }
25
24
  }
26
25
  catch (e) { }
26
+ const isAPICall = method === "POST" && apiPath && path === apiPath && post.apiName;
27
+ const apiName = isAPICall ? post.apiName : null;
27
28
  if (/^(((\d+\.)+\d+)|(([a-f\d]+:)+[a-f\d]+))$/.test(host)) {
28
29
  throw new Error("[404] Host cannot be an ip address: " + host);
29
30
  }
@@ -153,18 +154,18 @@ export default class Lambder {
153
154
  ;
154
155
  addApi(apiName, actionFn) {
155
156
  this.actionList.push({
156
- conditionFn: (ctx) => ((typeof apiName === "string" && ctx.apiName === apiName) ||
157
+ conditionFn: (ctx) => (!!ctx.apiName && ((typeof apiName === "string" && ctx.apiName === apiName) ||
157
158
  (typeof apiName === "function" && apiName(ctx)) ||
158
- (apiName?.constructor == RegExp && apiName.test(ctx.apiName))),
159
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName)))),
159
160
  actionFn: async (ctx, resolver) => await actionFn(ctx, resolver),
160
161
  });
161
162
  }
162
163
  ;
163
164
  addSessionApi(apiName, actionFn) {
164
165
  this.actionList.push({
165
- conditionFn: (ctx) => ((typeof apiName === "string" && ctx.apiName === apiName) ||
166
+ conditionFn: (ctx) => (!!ctx.apiName && ((typeof apiName === "string" && ctx.apiName === apiName) ||
166
167
  (typeof apiName === "function" && apiName(ctx)) ||
167
- (apiName?.constructor == RegExp && apiName.test(ctx.apiName))),
168
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName)))),
168
169
  actionFn: async (ctx, resolver) => {
169
170
  const isSessionValid = this.validateSession(ctx);
170
171
  if (!isSessionValid)
@@ -15,7 +15,7 @@ export default class LambderCaller {
15
15
  fetchEndedHandler;
16
16
  constructor({ isCorsEnabled = false, apiPath, apiVersion, versionExpiredHandler, sessionExpiredHandler, messageHandler, errorMessageHandler, notAuthorizedHandler, errorHandler, fetchStartedHandler, fetchEndedHandler, }) {
17
17
  this.isCorsEnabled = isCorsEnabled;
18
- this.apiPath = apiPath;
18
+ this.apiPath = apiPath ?? "/api";
19
19
  this.apiVersion = apiVersion;
20
20
  this.versionExpiredHandler = versionExpiredHandler;
21
21
  this.sessionExpiredHandler = sessionExpiredHandler;
@@ -54,7 +54,7 @@ export default class LambderCaller {
54
54
  if (this.fetchEndedHandler) {
55
55
  fetchTracker.fetchEndCalled = true;
56
56
  await this.fetchEndedHandler({
57
- fetchParams: { apiName, payload, headers, },
57
+ fetchParams: { apiName, payload, headers },
58
58
  fetchResult: data,
59
59
  activeFetchList: this.fetchTrackerList.filter(v => !v.done),
60
60
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.73",
3
+ "version": "1.0.76",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/Lambder.ts CHANGED
@@ -61,12 +61,14 @@ export const createContext = (
61
61
  const headers = event.headers;
62
62
  let post: Record<string, any> = {};
63
63
  const session = null;
64
- const apiName:string = (method === "POST" && apiPath && path === apiPath && post.apiName) ? post.apiName : null;
65
64
  try {
66
65
  const decodedBody = event.isBase64Encoded ? ( event.body ? Buffer.from(event.body,"base64").toString() : "{}" ) : ( event.body || "{}" );
67
66
  try { post = JSON.parse(decodedBody) || {}; }
68
67
  catch(e){ post = querystring.parse(decodedBody) || {}; }
69
68
  }catch(e){}
69
+ const isAPICall = method === "POST" && apiPath && path === apiPath && post.apiName;
70
+ const apiName:string = isAPICall ? post.apiName : null;
71
+
70
72
  if(/^(((\d+\.)+\d+)|(([a-f\d]+:)+[a-f\d]+))$/.test(host)){ throw new Error("[404] Host cannot be an ip address: " + host); }
71
73
  return { host, path, pathParams, method, get, post, cookie, apiName, headers, session, lambdaContext };
72
74
  }
@@ -205,9 +207,11 @@ export default class Lambder {
205
207
  addApi(apiName: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
206
208
  this.actionList.push({
207
209
  conditionFn: (ctx:LambderRenderContext) => (
208
- (typeof apiName === "string" && ctx.apiName === apiName) ||
209
- (typeof apiName === "function" && apiName(ctx)) ||
210
- (apiName?.constructor == RegExp && apiName.test(ctx.apiName))
210
+ !!ctx.apiName && (
211
+ (typeof apiName === "string" && ctx.apiName === apiName) ||
212
+ (typeof apiName === "function" && apiName(ctx)) ||
213
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName))
214
+ )
211
215
  ),
212
216
  actionFn: async (ctx:LambderRenderContext, resolver: LambderResolver) => await actionFn(ctx, resolver),
213
217
  });
@@ -216,9 +220,11 @@ export default class Lambder {
216
220
  addSessionApi(apiName: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
217
221
  this.actionList.push({
218
222
  conditionFn: (ctx:LambderRenderContext) => (
219
- (typeof apiName === "string" && ctx.apiName === apiName) ||
220
- (typeof apiName === "function" && apiName(ctx)) ||
221
- (apiName?.constructor == RegExp && apiName.test(ctx.apiName))
223
+ !!ctx.apiName && (
224
+ (typeof apiName === "string" && ctx.apiName === apiName) ||
225
+ (typeof apiName === "function" && apiName(ctx)) ||
226
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName))
227
+ )
222
228
  ),
223
229
  actionFn: async (ctx:LambderRenderContext, resolver: LambderResolver) => {
224
230
  const isSessionValid = this.validateSession(ctx);
@@ -65,7 +65,7 @@ export default class LambderCaller {
65
65
  }
66
66
  ){
67
67
  this.isCorsEnabled = isCorsEnabled;
68
- this.apiPath = apiPath;
68
+ this.apiPath = apiPath ?? "/api";
69
69
  this.apiVersion = apiVersion;
70
70
 
71
71
  this.versionExpiredHandler = versionExpiredHandler;
@@ -116,7 +116,7 @@ export default class LambderCaller {
116
116
  if(this.fetchEndedHandler){
117
117
  fetchTracker.fetchEndCalled = true;
118
118
  await this.fetchEndedHandler({
119
- fetchParams: { apiName, payload, headers, },
119
+ fetchParams: { apiName, payload, headers },
120
120
  fetchResult: data,
121
121
  activeFetchList: this.fetchTrackerList.filter(v=>!v.done),
122
122
  });