lambder 1.0.50 → 1.0.52

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
@@ -1,6 +1,7 @@
1
1
  import type { APIGatewayProxyEvent, APIGatewayProxyEventHeaders, Context } from "aws-lambda";
2
2
  import Resolver from "./Resolver.js";
3
3
  import ResponseBuilder, { ResolverResponse } from "./ResponseBuilder.js";
4
+ type Path = `/${string}`;
4
5
  type Session = {
5
6
  userId: string;
6
7
  userIdHash: string;
@@ -8,26 +9,16 @@ type Session = {
8
9
  csrfToken: string;
9
10
  createdTimeStamp: number;
10
11
  expiresTimeStamp: number;
11
- data: {
12
- [key: string]: any;
13
- };
12
+ data: Record<string, any>;
14
13
  };
15
14
  type RenderContext = {
16
15
  host: string;
17
16
  path: string;
18
- pathParams: {
19
- [key: string]: any;
20
- } | null;
17
+ pathParams: Record<string, any> | null;
21
18
  method: string;
22
- get: {
23
- [key: string]: any;
24
- };
25
- post: {
26
- [key: string]: any;
27
- };
28
- cookie: {
29
- [key: string]: any;
30
- };
19
+ get: Record<string, any>;
20
+ post: Record<string, any>;
21
+ cookie: Record<string, any>;
31
22
  headers: APIGatewayProxyEventHeaders;
32
23
  session: Session | null;
33
24
  lambdaContext: Context;
@@ -62,15 +53,13 @@ export default class Lambder {
62
53
  setRouteFallbackHandler(routeFallbackHandler: RouteFallbackHandlerFunction): void;
63
54
  setApiFallbackHandler(apiFallbackHandler: ApiFallbackHandlerFunction): void;
64
55
  setGlobalErrorHandler(globalErrorHandler: GlobalErrorHandlerFunction): void;
65
- getPatternMatch(pattern: string, path: string): {
66
- [key: string]: any;
67
- };
56
+ getPatternMatch(pattern: string, path: string): Record<string, any>;
68
57
  testPatternMatch(pattern: string, path: string): boolean;
69
58
  private validateSession;
70
59
  private handleNoMatchedAction;
71
60
  addModule(moduleFn: Function): Promise<void>;
72
- addRoute(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
73
- addSessionRoute(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
61
+ addRoute(condition: Path | ConditionFunction | RegExp, actionFn: ActionFunction): void;
62
+ addSessionRoute(condition: Path | ConditionFunction | RegExp, actionFn: ActionFunction): void;
74
63
  addApi(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
75
64
  addSessionApi(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
76
65
  addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<void>;
@@ -7,9 +7,7 @@ type FetchTracker = {
7
7
  type EventHandlerFetchParams = {
8
8
  api: string;
9
9
  payload?: any;
10
- headers?: {
11
- [key: string]: any;
12
- };
10
+ headers?: Record<string, any>;
13
11
  };
14
12
  type FetchStartEventHandler = (params: {
15
13
  fetchParams: EventHandlerFetchParams;
@@ -49,9 +47,7 @@ export default class LambderCaller {
49
47
  fetchStartedHandler?: FetchStartEventHandler;
50
48
  fetchEndedHandler?: FetchEndEventHandler;
51
49
  });
52
- api(apiName: string, payload?: any, headers?: {
53
- [key: string]: any;
54
- }, options?: {
50
+ api(apiName: string, payload?: any, headers?: Record<string, any>, options?: {
55
51
  versionExpiredHandler?: VoidFunction;
56
52
  sessionExpiredHandler?: VoidFunction;
57
53
  messageHandler?: MessageHandler;
@@ -1,11 +1,8 @@
1
1
  export type ResolverResponse = {
2
2
  statusCode: number;
3
- multiValueHeaders?: {
4
- [key: string]: string[];
5
- };
3
+ multiValueHeaders?: Record<string, string[]>;
6
4
  body: string | null;
7
5
  isBase64Encoded?: boolean;
8
- [key: string]: any;
9
6
  };
10
7
  export default class ResponseBuilder {
11
8
  private isCorsEnabled;
@@ -19,28 +16,14 @@ export default class ResponseBuilder {
19
16
  private readFileSync;
20
17
  private checkFileExist;
21
18
  raw(param: ResolverResponse): ResolverResponse;
22
- json(data: {
23
- [key: string]: any;
24
- }, headers?: {
25
- [key: string]: string | string[];
26
- }): ResolverResponse;
19
+ json(data: Record<string, any>, headers?: Record<string, string | string[]>): ResolverResponse;
27
20
  xml(data: string): ResolverResponse;
28
- html(data: string, headers?: {
29
- [key: string]: string | string[];
30
- }): ResolverResponse;
31
- status301(url: string, headers?: {
32
- [key: string]: string | string[];
33
- }): ResolverResponse;
34
- status404(data: string, headers?: {
35
- [key: string]: string | string[];
36
- }): ResolverResponse;
21
+ html(data: string, headers?: Record<string, string | string[]>): ResolverResponse;
22
+ status301(url: string, headers?: Record<string, string | string[]>): ResolverResponse;
23
+ status404(data: string, headers?: Record<string, string | string[]>): ResolverResponse;
37
24
  cors(): ResolverResponse;
38
- fileBase64(fileBase64: string, mimeType: string, headers?: {
39
- [key: string]: string | string[];
40
- }): ResolverResponse;
41
- file(filePath: string, headers?: {
42
- [key: string]: string | string[];
43
- }, fallbackFilePath?: string): ResolverResponse;
25
+ fileBase64(fileBase64: string, mimeType: string, headers?: Record<string, string | string[]>): ResolverResponse;
26
+ file(filePath: string, headers?: Record<string, string | string[]>, fallbackFilePath?: string): ResolverResponse;
44
27
  api({ payload, versionExpired, sessionExpired, notAuthorized, message, errorMessage, ...other }: {
45
28
  payload?: any;
46
29
  versionExpired?: boolean;
@@ -48,7 +31,5 @@ export default class ResponseBuilder {
48
31
  notAuthorized?: boolean;
49
32
  message?: any;
50
33
  errorMessage?: any;
51
- }, headers?: {
52
- [key: string]: string | string[];
53
- }): ResolverResponse;
34
+ }, headers?: Record<string, string | string[]>): ResolverResponse;
54
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.50",
3
+ "version": "1.0.52",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/Lambder.ts CHANGED
@@ -6,6 +6,8 @@ import type { APIGatewayProxyEvent, APIGatewayProxyEventHeaders, Context } from
6
6
  import Resolver from "./Resolver.js";
7
7
  import ResponseBuilder, { ResolverResponse } from "./ResponseBuilder.js";
8
8
 
9
+ type Path = `/${string}`;
10
+
9
11
  type Session = {
10
12
  userId: string,
11
13
  userIdHash: string;
@@ -13,17 +15,17 @@ type Session = {
13
15
  csrfToken: string;
14
16
  createdTimeStamp: number;
15
17
  expiresTimeStamp: number;
16
- data: { [key:string]: any };
18
+ data: Record<string, any>;
17
19
  };
18
20
 
19
21
  type RenderContext = {
20
22
  host: string;
21
23
  path: string;
22
- pathParams: { [key: string]: any } | null;
24
+ pathParams: Record<string, any> | null;
23
25
  method: string;
24
- get: { [key: string]: any };
25
- post: { [key: string]: any };
26
- cookie: { [key: string]: any };
26
+ get: Record<string, any>;
27
+ post: Record<string, any>;
28
+ cookie: Record<string, any>;
27
29
  headers: APIGatewayProxyEventHeaders;
28
30
  session: Session|null;
29
31
  lambdaContext: Context;
@@ -56,11 +58,11 @@ export const createContext = (
56
58
  const host = event.headers.Host || event.headers.host || "";
57
59
  const path = event.path;
58
60
  const pathParams = null;
59
- const get: { [key:string]: any } = event.queryStringParameters || {};
61
+ const get: Record<string, any> = event.queryStringParameters || {};
60
62
  const method = event.httpMethod;
61
63
  const cookie = cookieParser.parse(event.headers.Cookie || event.headers.cookie || "");
62
64
  const headers = event.headers;
63
- let post:{ [key:string]: any } = {};
65
+ let post: Record<string, any> = {};
64
66
  const session = null;
65
67
  try {
66
68
  const decodedBody = event.isBase64Encoded ? ( event.body ? Buffer.from(event.body,"base64").toString() : "{}" ) : ( event.body || "{}" );
@@ -116,7 +118,7 @@ export default class Lambder {
116
118
  setGlobalErrorHandler(globalErrorHandler: GlobalErrorHandlerFunction){
117
119
  this.globalErrorHandler = globalErrorHandler;
118
120
  }
119
- getPatternMatch(pattern: string, path: string): { [key:string]: any }{
121
+ getPatternMatch(pattern: string, path: string): Record<string, any> {
120
122
  const result = (match(pattern, { decode: decodeURIComponent }))(path);
121
123
  if(!result) return {};
122
124
  return result?.params || {};
@@ -152,7 +154,7 @@ export default class Lambder {
152
154
  async addModule(moduleFn: Function): Promise<void>{
153
155
  await moduleFn(this);
154
156
  }
155
- addRoute(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
157
+ addRoute(condition: Path|ConditionFunction|RegExp, actionFn: ActionFunction):void{
156
158
  this.actionList.push({
157
159
  conditionFn: (ctx:RenderContext) => (
158
160
  ctx.method === "GET" &&
@@ -173,7 +175,7 @@ export default class Lambder {
173
175
  });
174
176
  };
175
177
 
176
- addSessionRoute(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
178
+ addSessionRoute(condition: Path|ConditionFunction|RegExp, actionFn: ActionFunction):void{
177
179
  this.actionList.push({
178
180
  conditionFn: (ctx:RenderContext) => (
179
181
  ctx.method === "GET" &&
@@ -5,7 +5,7 @@ type FetchTracker = { apiName: string, done: boolean, fetchEndCalled: boolean };
5
5
  type EventHandlerFetchParams = {
6
6
  api: string,
7
7
  payload?: any,
8
- headers?: { [key:string]: any }
8
+ headers?: Record<string, any>
9
9
  };
10
10
 
11
11
  type FetchStartEventHandler = (params: {
@@ -80,7 +80,7 @@ export default class LambderCaller {
80
80
 
81
81
  };
82
82
 
83
- async api(apiName: string, payload?: any, headers?: { [key:string]: any }, options?: {
83
+ async api(apiName: string, payload?: any, headers?: Record<string, any>, options?: {
84
84
  versionExpiredHandler?: VoidFunction,
85
85
  sessionExpiredHandler?: VoidFunction,
86
86
  messageHandler?: MessageHandler,
@@ -3,8 +3,8 @@ import * as path from "path";
3
3
  import mimeTypeResolver from "mime-types";
4
4
 
5
5
  const convertToMultiHeader = (
6
- headers: { [key:string]: string|string[] } | undefined
7
- ):{ [key:string]: string[] } =>
6
+ headers: Record<string, string|string[]> | undefined
7
+ ):Record<string, string[]> =>
8
8
  Object.fromEntries(Object.entries(headers||{}).map(([k,v])=>[ k, Array.isArray(v) ? v : [v] ]));
9
9
 
10
10
  const CORS_HEADERS = convertToMultiHeader({
@@ -16,10 +16,9 @@ const CORS_HEADERS = convertToMultiHeader({
16
16
 
17
17
  export type ResolverResponse = {
18
18
  statusCode: number,
19
- multiValueHeaders?: { [key:string]: string[] },
19
+ multiValueHeaders?: Record<string, string[]>,
20
20
  body: string | null,
21
21
  isBase64Encoded?: boolean,
22
- [key:string]: any,
23
22
  };
24
23
 
25
24
 
@@ -62,8 +61,8 @@ export default class ResponseBuilder {
62
61
  };
63
62
 
64
63
  json(
65
- data: { [key:string]: any },
66
- headers?: { [key:string]: string|string[] }
64
+ data: Record<string, any>,
65
+ headers?: Record<string, string|string[]>
67
66
  ):ResolverResponse{
68
67
  return this.raw({
69
68
  statusCode: 200,
@@ -87,7 +86,7 @@ export default class ResponseBuilder {
87
86
 
88
87
  html(
89
88
  data: string,
90
- headers?: { [key:string]: string|string[] },
89
+ headers?: Record<string, string|string[]>,
91
90
  ):ResolverResponse{
92
91
  return this.raw({
93
92
  statusCode: 200,
@@ -99,7 +98,7 @@ export default class ResponseBuilder {
99
98
 
100
99
  status301(
101
100
  url: string,
102
- headers?: { [key:string]: string|string[] },
101
+ headers?: Record<string, string|string[]>,
103
102
  ):ResolverResponse{
104
103
  return this.raw({
105
104
  statusCode: 301,
@@ -110,7 +109,7 @@ export default class ResponseBuilder {
110
109
 
111
110
  status404(
112
111
  data: string,
113
- headers?: { [key:string]: string|string[] },
112
+ headers?: Record<string, string|string[]>,
114
113
  ):ResolverResponse{
115
114
  return this.raw({
116
115
  statusCode: 404,
@@ -131,7 +130,7 @@ export default class ResponseBuilder {
131
130
  fileBase64 (
132
131
  fileBase64: string,
133
132
  mimeType: string,
134
- headers?: { [key:string]: string|string[] },
133
+ headers?: Record<string, string|string[]>,
135
134
  ):ResolverResponse{
136
135
  return this.raw({
137
136
  statusCode: 200,
@@ -143,7 +142,7 @@ export default class ResponseBuilder {
143
142
 
144
143
  file(
145
144
  filePath: string,
146
- headers?: { [key:string]: string|string[] },
145
+ headers?: Record<string, string|string[]>,
147
146
  fallbackFilePath?: string,
148
147
  ):ResolverResponse{
149
148
  if(!this.checkFileExist(filePath)){
@@ -173,7 +172,7 @@ export default class ResponseBuilder {
173
172
  message?: any;
174
173
  errorMessage?: any;
175
174
  },
176
- headers?: { [key:string]: string|string[] },
175
+ headers?: Record<string, string|string[]>,
177
176
  ): ResolverResponse {
178
177
  if(Object.keys(other).length) console.log("ERROR: Unexpected key in result: " + Object.keys(other).join(", "));
179
178
  return this.json({