lambder 1.0.56 → 1.0.57

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.d.ts CHANGED
@@ -19,6 +19,7 @@ type RenderContext = {
19
19
  get: Record<string, any>;
20
20
  post: Record<string, any>;
21
21
  cookie: Record<string, any>;
22
+ apiName: string;
22
23
  headers: APIGatewayProxyEventHeaders;
23
24
  session: Session | null;
24
25
  lambdaContext: Context;
@@ -32,7 +33,7 @@ type HookFallbackFunction = (ctx: RenderContext, resolver: Resolver) => void | P
32
33
  type GlobalErrorHandlerFunction = (err: Error, ctx: RenderContext | null, response: ResponseBuilder) => ResolverResponse | Promise<ResolverResponse>;
33
34
  type RouteFallbackHandlerFunction = (ctx: RenderContext, resolver: Resolver) => ResolverResponse;
34
35
  type ApiFallbackHandlerFunction = (ctx: RenderContext, resolver: Resolver) => ResolverResponse;
35
- export declare const createContext: (event: APIGatewayProxyEvent, lambdaContext: Context) => RenderContext;
36
+ export declare const createContext: (event: APIGatewayProxyEvent, lambdaContext: Context, apiPath?: string | null) => RenderContext;
36
37
  export default class Lambder {
37
38
  private apiPath;
38
39
  private apiVersion;
@@ -59,8 +60,8 @@ export default class Lambder {
59
60
  addModule(moduleFn: Function): Promise<void>;
60
61
  addRoute(condition: Path | ConditionFunction | RegExp, actionFn: ActionFunction): void;
61
62
  addSessionRoute(condition: Path | ConditionFunction | RegExp, actionFn: ActionFunction): void;
62
- addApi(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
63
- addSessionApi(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
63
+ addApi(apiName: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
64
+ addSessionApi(apiName: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
64
65
  addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<void>;
65
66
  addHook(hookEvent: 'beforeRender', hookFn: HookBeforeRenderFunction, priority?: number): Promise<void>;
66
67
  addHook(hookEvent: 'afterRender', hookFn: HookAfterRenderFunction, priority?: number): Promise<void>;
package/dist/Lambder.js CHANGED
@@ -3,7 +3,7 @@ import cookieParser from "cookie";
3
3
  import { match } from "path-to-regexp";
4
4
  import Resolver from "./Resolver.js";
5
5
  import ResponseBuilder from "./ResponseBuilder.js";
6
- export const createContext = (event, lambdaContext) => {
6
+ export const createContext = (event, lambdaContext, apiPath = null) => {
7
7
  const host = event.headers.Host || event.headers.host || "";
8
8
  const path = event.path;
9
9
  const pathParams = null;
@@ -13,6 +13,7 @@ export const createContext = (event, lambdaContext) => {
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;
16
17
  try {
17
18
  const decodedBody = event.isBase64Encoded ? (event.body ? Buffer.from(event.body, "base64").toString() : "{}") : (event.body || "{}");
18
19
  try {
@@ -26,7 +27,7 @@ export const createContext = (event, lambdaContext) => {
26
27
  if (/^(((\d+\.)+\d+)|(([a-f\d]+:)+[a-f\d]+))$/.test(host)) {
27
28
  throw new Error("[404] Host cannot be an ip address: " + host);
28
29
  }
29
- return { host, path, pathParams, method, get, post, cookie, headers, session, lambdaContext };
30
+ return { host, path, pathParams, method, get, post, cookie, apiName, headers, session, lambdaContext };
30
31
  };
31
32
  export default class Lambder {
32
33
  apiPath;
@@ -141,22 +142,20 @@ export default class Lambder {
141
142
  });
142
143
  }
143
144
  ;
144
- addApi(condition, actionFn) {
145
+ addApi(apiName, actionFn) {
145
146
  this.actionList.push({
146
- conditionFn: (ctx) => (ctx.method === "POST" && ctx.path === this.apiPath &&
147
- ((typeof condition === "string" && ctx.post?.api === condition) ||
148
- (typeof condition === "function" && condition(ctx)) ||
149
- (condition?.constructor == RegExp && condition.test(ctx.post?.api)))),
147
+ conditionFn: (ctx) => ((typeof apiName === "string" && ctx.apiName === apiName) ||
148
+ (typeof apiName === "function" && apiName(ctx)) ||
149
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName))),
150
150
  actionFn: async (ctx, resolver) => await actionFn(ctx, resolver),
151
151
  });
152
152
  }
153
153
  ;
154
- addSessionApi(condition, actionFn) {
154
+ addSessionApi(apiName, actionFn) {
155
155
  this.actionList.push({
156
- conditionFn: (ctx) => (ctx.method === "POST" && ctx.path === this.apiPath &&
157
- ((typeof condition === "string" && ctx.post?.api === condition) ||
158
- (typeof condition === "function" && condition(ctx)) ||
159
- (condition?.constructor == RegExp && condition.test(ctx.post?.api)))),
156
+ conditionFn: (ctx) => ((typeof apiName === "string" && ctx.apiName === apiName) ||
157
+ (typeof apiName === "function" && apiName(ctx)) ||
158
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName))),
160
159
  actionFn: async (ctx, resolver) => {
161
160
  const isSessionValid = this.validateSession(ctx.session);
162
161
  if (!isSessionValid)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.56",
3
+ "version": "1.0.57",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/Lambder.ts CHANGED
@@ -26,6 +26,7 @@ type RenderContext = {
26
26
  get: Record<string, any>;
27
27
  post: Record<string, any>;
28
28
  cookie: Record<string, any>;
29
+ apiName: string;
29
30
  headers: APIGatewayProxyEventHeaders;
30
31
  session: Session|null;
31
32
  lambdaContext: Context;
@@ -53,6 +54,7 @@ type HookListType = {
53
54
  export const createContext = (
54
55
  event: APIGatewayProxyEvent,
55
56
  lambdaContext: Context,
57
+ apiPath: string|null = null
56
58
  ):RenderContext => {
57
59
  const host = event.headers.Host || event.headers.host || "";
58
60
  const path = event.path;
@@ -63,13 +65,14 @@ export const createContext = (
63
65
  const headers = event.headers;
64
66
  let post: Record<string, any> = {};
65
67
  const session = null;
68
+ const apiName = (method === "POST" && apiPath && path === apiPath && post.apiName) ? post.apiName : null;
66
69
  try {
67
70
  const decodedBody = event.isBase64Encoded ? ( event.body ? Buffer.from(event.body,"base64").toString() : "{}" ) : ( event.body || "{}" );
68
71
  try { post = JSON.parse(decodedBody) || {}; }
69
72
  catch(e){ post = querystring.parse(decodedBody) || {}; }
70
73
  }catch(e){}
71
74
  if(/^(((\d+\.)+\d+)|(([a-f\d]+:)+[a-f\d]+))$/.test(host)){ throw new Error("[404] Host cannot be an ip address: " + host); }
72
- return { host, path, pathParams, method, get, post, cookie, headers, session, lambdaContext };
75
+ return { host, path, pathParams, method, get, post, cookie, apiName, headers, session, lambdaContext };
73
76
  }
74
77
 
75
78
 
@@ -196,29 +199,23 @@ export default class Lambder {
196
199
  });
197
200
  };
198
201
 
199
- addApi(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
202
+ addApi(apiName: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
200
203
  this.actionList.push({
201
204
  conditionFn: (ctx:RenderContext) => (
202
- ctx.method === "POST" && ctx.path === this.apiPath &&
203
- (
204
- (typeof condition === "string" && ctx.post?.api === condition) ||
205
- (typeof condition === "function" && condition(ctx)) ||
206
- (condition?.constructor == RegExp && condition.test(ctx.post?.api))
207
- )
205
+ (typeof apiName === "string" && ctx.apiName === apiName) ||
206
+ (typeof apiName === "function" && apiName(ctx)) ||
207
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName))
208
208
  ),
209
209
  actionFn: async (ctx:RenderContext, resolver: Resolver) => await actionFn(ctx, resolver),
210
210
  });
211
211
  };
212
212
 
213
- addSessionApi(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
213
+ addSessionApi(apiName: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
214
214
  this.actionList.push({
215
215
  conditionFn: (ctx:RenderContext) => (
216
- ctx.method === "POST" && ctx.path === this.apiPath &&
217
- (
218
- (typeof condition === "string" && ctx.post?.api === condition) ||
219
- (typeof condition === "function" && condition(ctx)) ||
220
- (condition?.constructor == RegExp && condition.test(ctx.post?.api))
221
- )
216
+ (typeof apiName === "string" && ctx.apiName === apiName) ||
217
+ (typeof apiName === "function" && apiName(ctx)) ||
218
+ (apiName?.constructor == RegExp && apiName.test(ctx.apiName))
222
219
  ),
223
220
  actionFn: async (ctx:RenderContext, resolver: Resolver) => {
224
221
  const isSessionValid = this.validateSession(ctx.session);