lambder 1.0.46 → 1.0.48

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
@@ -62,6 +62,8 @@ export default class Lambder {
62
62
  setRouteFallbackHandler(routeFallbackHandler: RouteFallbackHandlerFunction): void;
63
63
  setApiFallbackHandler(apiFallbackHandler: ApiFallbackHandlerFunction): void;
64
64
  setGlobalErrorHandler(globalErrorHandler: GlobalErrorHandlerFunction): void;
65
+ getPatternMatch(pattern: string, path: string): object;
66
+ testPatternMatch(pattern: string, path: string): boolean;
65
67
  private validateSession;
66
68
  private handleNoMatchedAction;
67
69
  addModule(moduleFn: Function): Promise<void>;
package/dist/Lambder.js CHANGED
@@ -60,6 +60,15 @@ export default class Lambder {
60
60
  setGlobalErrorHandler(globalErrorHandler) {
61
61
  this.globalErrorHandler = globalErrorHandler;
62
62
  }
63
+ getPatternMatch(pattern, path) {
64
+ const result = (match(pattern, { decode: decodeURIComponent }))(path);
65
+ if (!result)
66
+ return {};
67
+ return result?.params;
68
+ }
69
+ testPatternMatch(pattern, path) {
70
+ return (match(pattern, { decode: decodeURIComponent }))(path) !== false;
71
+ }
63
72
  async validateSession(session) {
64
73
  if (!session)
65
74
  return false;
@@ -97,12 +106,12 @@ export default class Lambder {
97
106
  addRoute(condition, actionFn) {
98
107
  this.actionList.push({
99
108
  conditionFn: (ctx) => (ctx.method === "GET" &&
100
- ((typeof condition === "string" && ctx.path === condition) ||
109
+ ((typeof condition === "string" && this.testPatternMatch(condition, ctx.path)) ||
101
110
  (typeof condition === "function" && condition(ctx)) ||
102
111
  (condition?.constructor == RegExp && condition.test(ctx.path)))),
103
112
  actionFn: async (ctx, resolver) => {
104
113
  if (typeof condition === "string") {
105
- ctx.pathParams = (match(condition))(ctx.path) || {};
114
+ ctx.pathParams = this.getPatternMatch(condition, ctx.path);
106
115
  }
107
116
  else if (condition?.constructor == RegExp) {
108
117
  ctx.pathParams = ctx.path.match(condition);
@@ -115,7 +124,7 @@ export default class Lambder {
115
124
  addSessionRoute(condition, actionFn) {
116
125
  this.actionList.push({
117
126
  conditionFn: (ctx) => (ctx.method === "GET" &&
118
- ((typeof condition === "string" && ctx.path === condition) ||
127
+ ((typeof condition === "string" && this.testPatternMatch(condition, ctx.path)) ||
119
128
  (typeof condition === "function" && condition(ctx)) ||
120
129
  (condition?.constructor == RegExp && condition.test(ctx.path)))),
121
130
  actionFn: async (ctx, resolver) => {
@@ -123,7 +132,7 @@ export default class Lambder {
123
132
  if (!isSessionValid)
124
133
  throw new Error("Session not found");
125
134
  if (typeof condition === "string") {
126
- ctx.pathParams = (match(condition))(ctx.path) || {};
135
+ ctx.pathParams = this.getPatternMatch(condition, ctx.path);
127
136
  }
128
137
  else if (condition?.constructor == RegExp) {
129
138
  ctx.pathParams = ctx.path.match(condition);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.46",
3
+ "version": "1.0.48",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/Lambder.ts CHANGED
@@ -116,6 +116,14 @@ export default class Lambder {
116
116
  setGlobalErrorHandler(globalErrorHandler: GlobalErrorHandlerFunction){
117
117
  this.globalErrorHandler = globalErrorHandler;
118
118
  }
119
+ getPatternMatch(pattern: string, path: string){
120
+ const result = (match(pattern, { decode: decodeURIComponent }))(path);
121
+ if(!result) return {};
122
+ return result?.params;
123
+ }
124
+ testPatternMatch(pattern: string, path: string): boolean{
125
+ return (match(pattern, { decode: decodeURIComponent }))(path) !== false;
126
+ }
119
127
 
120
128
  private async validateSession (session: Session|null): Promise<boolean>{
121
129
  if(!session) return false;
@@ -149,14 +157,14 @@ export default class Lambder {
149
157
  conditionFn: (ctx:RenderContext) => (
150
158
  ctx.method === "GET" &&
151
159
  (
152
- (typeof condition === "string" && ctx.path === condition) ||
160
+ (typeof condition === "string" && this.testPatternMatch(condition, ctx.path)) ||
153
161
  (typeof condition === "function" && condition(ctx)) ||
154
162
  (condition?.constructor == RegExp && condition.test(ctx.path))
155
163
  )
156
164
  ),
157
165
  actionFn: async (ctx:RenderContext, resolver: Resolver) => {
158
166
  if(typeof condition === "string"){
159
- ctx.pathParams = (match(condition))(ctx.path) || {};
167
+ ctx.pathParams = this.getPatternMatch(condition, ctx.path);
160
168
  }else if(condition?.constructor == RegExp){
161
169
  ctx.pathParams = ctx.path.match(condition);
162
170
  }
@@ -170,7 +178,7 @@ export default class Lambder {
170
178
  conditionFn: (ctx:RenderContext) => (
171
179
  ctx.method === "GET" &&
172
180
  (
173
- (typeof condition === "string" && ctx.path === condition) ||
181
+ (typeof condition === "string" && this.testPatternMatch(condition, ctx.path)) ||
174
182
  (typeof condition === "function" && condition(ctx)) ||
175
183
  (condition?.constructor == RegExp && condition.test(ctx.path))
176
184
  )
@@ -179,7 +187,7 @@ export default class Lambder {
179
187
  const isSessionValid = this.validateSession(ctx.session);
180
188
  if(!isSessionValid) throw new Error("Session not found");
181
189
  if(typeof condition === "string"){
182
- ctx.pathParams = (match(condition))(ctx.path) || {};
190
+ ctx.pathParams = this.getPatternMatch(condition, ctx.path);
183
191
  }else if(condition?.constructor == RegExp){
184
192
  ctx.pathParams = ctx.path.match(condition);
185
193
  }