lambder 1.0.16 → 1.0.18
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/index.d.ts +7 -4
- package/dist/index.js +28 -8
- package/package.json +1 -1
- package/src/index.ts +33 -14
package/dist/index.d.ts
CHANGED
|
@@ -44,19 +44,22 @@ export default class Lambder {
|
|
|
44
44
|
private _actionList;
|
|
45
45
|
private _hookList;
|
|
46
46
|
private globalErrorHandler;
|
|
47
|
-
private
|
|
47
|
+
private routeFallbackHandler;
|
|
48
|
+
private apiFallbackHandler;
|
|
48
49
|
constructor({ publicPath, apiPath, apiVersion, isCorsEnabled, }: {
|
|
49
50
|
publicPath: string;
|
|
50
51
|
apiPath?: string;
|
|
51
52
|
apiVersion?: string;
|
|
52
53
|
isCorsEnabled?: boolean;
|
|
53
54
|
});
|
|
54
|
-
|
|
55
|
+
setRouteFallbackHandler(routeFallbackHandler: (renderContext: RenderContext, resolver: Resolver) => ResolverResponse): void;
|
|
56
|
+
setApiFallbackHandler(apiFallbackHandler: (renderContext: RenderContext, resolver: Resolver) => ResolverResponse): void;
|
|
55
57
|
setGlobalErrorHandler(globalErrorHandler: (err: Error) => ResolverResponse): void;
|
|
56
58
|
addModule(moduleFn: Function): Promise<void>;
|
|
57
59
|
private validateSession;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
addRoute(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
|
|
61
|
+
addSessionRoute(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
|
|
62
|
+
addApi(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
|
|
60
63
|
addSessionApi(condition: string | ConditionFunction | RegExp, actionFn: ActionFunction): void;
|
|
61
64
|
addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<void>;
|
|
62
65
|
addHook(hookEvent: 'beforeRender', hookFn: HookBeforeRenderFunction, priority?: number): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -33,7 +33,8 @@ export default class Lambder {
|
|
|
33
33
|
_actionList;
|
|
34
34
|
_hookList;
|
|
35
35
|
globalErrorHandler = null;
|
|
36
|
-
|
|
36
|
+
routeFallbackHandler = null;
|
|
37
|
+
apiFallbackHandler = null;
|
|
37
38
|
constructor({ publicPath, apiPath, apiVersion, isCorsEnabled, }) {
|
|
38
39
|
this.publicPath = publicPath || "/incorrect-path-not-found";
|
|
39
40
|
this.apiPath = apiPath ?? "/api";
|
|
@@ -47,8 +48,11 @@ export default class Lambder {
|
|
|
47
48
|
"completed": []
|
|
48
49
|
};
|
|
49
50
|
}
|
|
50
|
-
|
|
51
|
-
this.
|
|
51
|
+
setRouteFallbackHandler(routeFallbackHandler) {
|
|
52
|
+
this.routeFallbackHandler = routeFallbackHandler;
|
|
53
|
+
}
|
|
54
|
+
setApiFallbackHandler(apiFallbackHandler) {
|
|
55
|
+
this.apiFallbackHandler = apiFallbackHandler;
|
|
52
56
|
}
|
|
53
57
|
setGlobalErrorHandler(globalErrorHandler) {
|
|
54
58
|
this.globalErrorHandler = globalErrorHandler;
|
|
@@ -68,7 +72,18 @@ export default class Lambder {
|
|
|
68
72
|
// Check DDB;
|
|
69
73
|
return false;
|
|
70
74
|
}
|
|
71
|
-
|
|
75
|
+
;
|
|
76
|
+
addRoute(condition, actionFn) {
|
|
77
|
+
this._actionList.push({
|
|
78
|
+
conditionFn: async (renderContext) => (renderContext.method === "GET" &&
|
|
79
|
+
((typeof condition === "string" && renderContext.url === condition) ||
|
|
80
|
+
(typeof condition === "function" && (await condition(renderContext))) ||
|
|
81
|
+
(condition?.constructor == RegExp && condition.test(renderContext.url)))),
|
|
82
|
+
actionFn: async (renderContext, resolver) => await actionFn(renderContext, resolver)
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
;
|
|
86
|
+
addSessionRoute(condition, actionFn) {
|
|
72
87
|
this._actionList.push({
|
|
73
88
|
conditionFn: async (renderContext) => (renderContext.method === "GET" &&
|
|
74
89
|
((typeof condition === "string" && renderContext.url === condition) ||
|
|
@@ -77,7 +92,8 @@ export default class Lambder {
|
|
|
77
92
|
actionFn: async (renderContext, resolver) => await actionFn(renderContext, resolver)
|
|
78
93
|
});
|
|
79
94
|
}
|
|
80
|
-
|
|
95
|
+
;
|
|
96
|
+
addApi(condition, actionFn) {
|
|
81
97
|
this._actionList.push({
|
|
82
98
|
conditionFn: async (renderContext) => (renderContext.method === "POST" && renderContext.url === this.apiPath &&
|
|
83
99
|
((typeof condition === "string" && renderContext.post?.api === condition) ||
|
|
@@ -86,6 +102,7 @@ export default class Lambder {
|
|
|
86
102
|
actionFn: async (renderContext, resolver) => await actionFn(renderContext, resolver)
|
|
87
103
|
});
|
|
88
104
|
}
|
|
105
|
+
;
|
|
89
106
|
addSessionApi(condition, actionFn) {
|
|
90
107
|
this._actionList.push({
|
|
91
108
|
conditionFn: async (renderContext) => (renderContext.method === "POST" && renderContext.url === this.apiPath &&
|
|
@@ -100,6 +117,7 @@ export default class Lambder {
|
|
|
100
117
|
}
|
|
101
118
|
});
|
|
102
119
|
}
|
|
120
|
+
;
|
|
103
121
|
async addHook(hookEvent, hookFn, priority = 0) {
|
|
104
122
|
if (hookEvent === "created") {
|
|
105
123
|
await hookFn(this);
|
|
@@ -140,9 +158,11 @@ export default class Lambder {
|
|
|
140
158
|
for (const hook of this._hookList["fallback"]) {
|
|
141
159
|
await hook.hookFn(renderContext, resolver);
|
|
142
160
|
}
|
|
143
|
-
if (this.
|
|
144
|
-
|
|
145
|
-
|
|
161
|
+
if (renderContext.url === this.apiPath && this.apiFallbackHandler) {
|
|
162
|
+
resolve(await this.apiFallbackHandler(renderContext, resolver));
|
|
163
|
+
}
|
|
164
|
+
else if (this.routeFallbackHandler) {
|
|
165
|
+
resolve(await this.routeFallbackHandler(renderContext, resolver));
|
|
146
166
|
}
|
|
147
167
|
else {
|
|
148
168
|
resolve({ statusCode: 204, body: "Fallback handler not set.", });
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -80,7 +80,8 @@ export default class Lambder {
|
|
|
80
80
|
"completed": { priority: number, hookFn: HookCompletedFunction }[]
|
|
81
81
|
};
|
|
82
82
|
private globalErrorHandler: Function|null = null;
|
|
83
|
-
private
|
|
83
|
+
private routeFallbackHandler: Function|null = null;
|
|
84
|
+
private apiFallbackHandler: Function|null = null;
|
|
84
85
|
|
|
85
86
|
constructor(
|
|
86
87
|
{ publicPath, apiPath, apiVersion, isCorsEnabled, }:
|
|
@@ -100,8 +101,11 @@ export default class Lambder {
|
|
|
100
101
|
};
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
|
|
104
|
-
this.
|
|
104
|
+
setRouteFallbackHandler(routeFallbackHandler: (renderContext:RenderContext, resolver: Resolver)=>ResolverResponse){
|
|
105
|
+
this.routeFallbackHandler = routeFallbackHandler;
|
|
106
|
+
}
|
|
107
|
+
setApiFallbackHandler(apiFallbackHandler: (renderContext:RenderContext, resolver: Resolver)=>ResolverResponse){
|
|
108
|
+
this.apiFallbackHandler = apiFallbackHandler;
|
|
105
109
|
}
|
|
106
110
|
setGlobalErrorHandler(globalErrorHandler: (err: Error)=>ResolverResponse){
|
|
107
111
|
this.globalErrorHandler = globalErrorHandler;
|
|
@@ -118,9 +122,9 @@ export default class Lambder {
|
|
|
118
122
|
if(session.expiresTimeStamp > Date.now()) return false;
|
|
119
123
|
// Check DDB;
|
|
120
124
|
return false;
|
|
121
|
-
}
|
|
125
|
+
};
|
|
122
126
|
|
|
123
|
-
|
|
127
|
+
addRoute(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
|
|
124
128
|
this._actionList.push({
|
|
125
129
|
conditionFn: async (renderContext:RenderContext) => (
|
|
126
130
|
renderContext.method === "GET" &&
|
|
@@ -132,9 +136,23 @@ export default class Lambder {
|
|
|
132
136
|
),
|
|
133
137
|
actionFn: async (renderContext:RenderContext, resolver: Resolver) => await actionFn(renderContext, resolver)
|
|
134
138
|
});
|
|
135
|
-
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
addSessionRoute(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
|
|
142
|
+
this._actionList.push({
|
|
143
|
+
conditionFn: async (renderContext:RenderContext) => (
|
|
144
|
+
renderContext.method === "GET" &&
|
|
145
|
+
(
|
|
146
|
+
(typeof condition === "string" && renderContext.url === condition) ||
|
|
147
|
+
(typeof condition === "function" && (await condition(renderContext))) ||
|
|
148
|
+
(condition?.constructor == RegExp && condition.test(renderContext.url))
|
|
149
|
+
)
|
|
150
|
+
),
|
|
151
|
+
actionFn: async (renderContext:RenderContext, resolver: Resolver) => await actionFn(renderContext, resolver)
|
|
152
|
+
});
|
|
153
|
+
};
|
|
136
154
|
|
|
137
|
-
|
|
155
|
+
addApi(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
|
|
138
156
|
this._actionList.push({
|
|
139
157
|
conditionFn: async (renderContext:RenderContext) => (
|
|
140
158
|
renderContext.method === "POST" && renderContext.url === this.apiPath &&
|
|
@@ -146,7 +164,7 @@ export default class Lambder {
|
|
|
146
164
|
),
|
|
147
165
|
actionFn: async (renderContext:RenderContext, resolver: Resolver) => await actionFn(renderContext, resolver)
|
|
148
166
|
});
|
|
149
|
-
}
|
|
167
|
+
};
|
|
150
168
|
|
|
151
169
|
addSessionApi(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
|
|
152
170
|
this._actionList.push({
|
|
@@ -164,7 +182,7 @@ export default class Lambder {
|
|
|
164
182
|
return await actionFn(renderContext, resolver);
|
|
165
183
|
}
|
|
166
184
|
});
|
|
167
|
-
}
|
|
185
|
+
};
|
|
168
186
|
|
|
169
187
|
|
|
170
188
|
async addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<void>;
|
|
@@ -203,8 +221,8 @@ export default class Lambder {
|
|
|
203
221
|
if(renderContext.method === "OPTIONS") return resolver.cors();
|
|
204
222
|
|
|
205
223
|
const firstMatchedAction = await (async () => {
|
|
206
|
-
for
|
|
207
|
-
if
|
|
224
|
+
for(const action of this._actionList){
|
|
225
|
+
if(await action.conditionFn(renderContext)) return action;
|
|
208
226
|
}
|
|
209
227
|
})();
|
|
210
228
|
|
|
@@ -221,9 +239,10 @@ export default class Lambder {
|
|
|
221
239
|
for(const hook of this._hookList["fallback"]){
|
|
222
240
|
await hook.hookFn(renderContext, resolver);
|
|
223
241
|
}
|
|
224
|
-
if(this.
|
|
225
|
-
|
|
226
|
-
|
|
242
|
+
if(renderContext.url === this.apiPath && this.apiFallbackHandler){
|
|
243
|
+
resolve(await this.apiFallbackHandler(renderContext, resolver));
|
|
244
|
+
}else if(this.routeFallbackHandler){
|
|
245
|
+
resolve(await this.routeFallbackHandler(renderContext, resolver));
|
|
227
246
|
}else{
|
|
228
247
|
resolve({ statusCode: 204, body: "Fallback handler not set.", })
|
|
229
248
|
}
|