lambder 1.0.17 → 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 +4 -2
- package/dist/index.js +17 -6
- package/package.json +1 -1
- package/src/index.ts +18 -13
package/dist/index.d.ts
CHANGED
|
@@ -44,14 +44,16 @@ 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;
|
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,6 +72,7 @@ export default class Lambder {
|
|
|
68
72
|
// Check DDB;
|
|
69
73
|
return false;
|
|
70
74
|
}
|
|
75
|
+
;
|
|
71
76
|
addRoute(condition, actionFn) {
|
|
72
77
|
this._actionList.push({
|
|
73
78
|
conditionFn: async (renderContext) => (renderContext.method === "GET" &&
|
|
@@ -77,6 +82,7 @@ export default class Lambder {
|
|
|
77
82
|
actionFn: async (renderContext, resolver) => await actionFn(renderContext, resolver)
|
|
78
83
|
});
|
|
79
84
|
}
|
|
85
|
+
;
|
|
80
86
|
addSessionRoute(condition, actionFn) {
|
|
81
87
|
this._actionList.push({
|
|
82
88
|
conditionFn: async (renderContext) => (renderContext.method === "GET" &&
|
|
@@ -86,6 +92,7 @@ export default class Lambder {
|
|
|
86
92
|
actionFn: async (renderContext, resolver) => await actionFn(renderContext, resolver)
|
|
87
93
|
});
|
|
88
94
|
}
|
|
95
|
+
;
|
|
89
96
|
addApi(condition, actionFn) {
|
|
90
97
|
this._actionList.push({
|
|
91
98
|
conditionFn: async (renderContext) => (renderContext.method === "POST" && renderContext.url === this.apiPath &&
|
|
@@ -95,6 +102,7 @@ export default class Lambder {
|
|
|
95
102
|
actionFn: async (renderContext, resolver) => await actionFn(renderContext, resolver)
|
|
96
103
|
});
|
|
97
104
|
}
|
|
105
|
+
;
|
|
98
106
|
addSessionApi(condition, actionFn) {
|
|
99
107
|
this._actionList.push({
|
|
100
108
|
conditionFn: async (renderContext) => (renderContext.method === "POST" && renderContext.url === this.apiPath &&
|
|
@@ -109,6 +117,7 @@ export default class Lambder {
|
|
|
109
117
|
}
|
|
110
118
|
});
|
|
111
119
|
}
|
|
120
|
+
;
|
|
112
121
|
async addHook(hookEvent, hookFn, priority = 0) {
|
|
113
122
|
if (hookEvent === "created") {
|
|
114
123
|
await hookFn(this);
|
|
@@ -149,9 +158,11 @@ export default class Lambder {
|
|
|
149
158
|
for (const hook of this._hookList["fallback"]) {
|
|
150
159
|
await hook.hookFn(renderContext, resolver);
|
|
151
160
|
}
|
|
152
|
-
if (this.
|
|
153
|
-
|
|
154
|
-
|
|
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));
|
|
155
166
|
}
|
|
156
167
|
else {
|
|
157
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,7 +122,7 @@ 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({
|
|
@@ -132,7 +136,7 @@ export default class Lambder {
|
|
|
132
136
|
),
|
|
133
137
|
actionFn: async (renderContext:RenderContext, resolver: Resolver) => await actionFn(renderContext, resolver)
|
|
134
138
|
});
|
|
135
|
-
}
|
|
139
|
+
};
|
|
136
140
|
|
|
137
141
|
addSessionRoute(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
|
|
138
142
|
this._actionList.push({
|
|
@@ -146,7 +150,7 @@ export default class Lambder {
|
|
|
146
150
|
),
|
|
147
151
|
actionFn: async (renderContext:RenderContext, resolver: Resolver) => await actionFn(renderContext, resolver)
|
|
148
152
|
});
|
|
149
|
-
}
|
|
153
|
+
};
|
|
150
154
|
|
|
151
155
|
addApi(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
|
|
152
156
|
this._actionList.push({
|
|
@@ -160,7 +164,7 @@ export default class Lambder {
|
|
|
160
164
|
),
|
|
161
165
|
actionFn: async (renderContext:RenderContext, resolver: Resolver) => await actionFn(renderContext, resolver)
|
|
162
166
|
});
|
|
163
|
-
}
|
|
167
|
+
};
|
|
164
168
|
|
|
165
169
|
addSessionApi(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
|
|
166
170
|
this._actionList.push({
|
|
@@ -178,7 +182,7 @@ export default class Lambder {
|
|
|
178
182
|
return await actionFn(renderContext, resolver);
|
|
179
183
|
}
|
|
180
184
|
});
|
|
181
|
-
}
|
|
185
|
+
};
|
|
182
186
|
|
|
183
187
|
|
|
184
188
|
async addHook(hookEvent: 'created', hookFn: HookCreatedFunction, priority?: number): Promise<void>;
|
|
@@ -217,8 +221,8 @@ export default class Lambder {
|
|
|
217
221
|
if(renderContext.method === "OPTIONS") return resolver.cors();
|
|
218
222
|
|
|
219
223
|
const firstMatchedAction = await (async () => {
|
|
220
|
-
for
|
|
221
|
-
if
|
|
224
|
+
for(const action of this._actionList){
|
|
225
|
+
if(await action.conditionFn(renderContext)) return action;
|
|
222
226
|
}
|
|
223
227
|
})();
|
|
224
228
|
|
|
@@ -235,9 +239,10 @@ export default class Lambder {
|
|
|
235
239
|
for(const hook of this._hookList["fallback"]){
|
|
236
240
|
await hook.hookFn(renderContext, resolver);
|
|
237
241
|
}
|
|
238
|
-
if(this.
|
|
239
|
-
|
|
240
|
-
|
|
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));
|
|
241
246
|
}else{
|
|
242
247
|
resolve({ statusCode: 204, body: "Fallback handler not set.", })
|
|
243
248
|
}
|