lambder 1.0.138 → 1.0.141
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/Readme.md +1 -1
- package/dist/LambderMSW.js +3 -5
- package/dist/LambderResolver.js +1 -1
- package/dist/LambderResponseBuilder.d.ts +1 -1
- package/dist/LambderResponseBuilder.js +13 -11
- package/dist/LambderUtils.js +9 -8
- package/dist/node-polyfills.d.ts +2 -2
- package/dist/node-polyfills.js +22 -17
- package/package.json +1 -1
- package/src/LambderMSW.ts +5 -12
- package/src/LambderResolver.ts +1 -1
- package/src/LambderResponseBuilder.ts +15 -15
- package/src/LambderUtils.ts +9 -8
- package/src/node-polyfills.ts +17 -18
package/Readme.md
CHANGED
|
@@ -435,7 +435,7 @@ lambder.addApi("getCompanyName", async (ctx, res) => {
|
|
|
435
435
|
return res.die.status404(data, headers);
|
|
436
436
|
return res.die.cors();
|
|
437
437
|
return res.die.fileBase64(fileBase64, mimeType, headers);
|
|
438
|
-
return res.die.file(filePath, headers, fallbackFilePath);
|
|
438
|
+
return await res.die.file(filePath, headers, fallbackFilePath);
|
|
439
439
|
return await res.die.ejsFile(filePath, pageData, headers);
|
|
440
440
|
return await res.die.ejsTemplate(template, pageData, headers);
|
|
441
441
|
return res.die.api(payload, { versionExpired, sessionExpired, notAuthorized, message, errorMessage }, headers);
|
package/dist/LambderMSW.js
CHANGED
|
@@ -31,7 +31,8 @@ export default class LambderMSW {
|
|
|
31
31
|
return this.http.post(this.apiPath, async ({ request }) => {
|
|
32
32
|
let body;
|
|
33
33
|
try {
|
|
34
|
-
|
|
34
|
+
const clonedRequest = request.clone();
|
|
35
|
+
body = await clonedRequest.json();
|
|
35
36
|
}
|
|
36
37
|
catch (parseErr) {
|
|
37
38
|
// If JSON parsing fails, return undefined to let other handlers try
|
|
@@ -40,15 +41,12 @@ export default class LambderMSW {
|
|
|
40
41
|
}
|
|
41
42
|
// Check if body is valid and has apiName
|
|
42
43
|
if (!body || typeof body.apiName !== 'string') {
|
|
43
|
-
// Invalid request format, let other handlers try
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
|
-
console.log("LambderMSW called for:", body.apiName, "matching against:", apiName);
|
|
47
|
-
// Check if this is the API we're mocking
|
|
48
46
|
if (body.apiName !== apiName) {
|
|
49
|
-
// If this handler doesn't match, return undefined to let MSW try other handlers
|
|
50
47
|
return;
|
|
51
48
|
}
|
|
49
|
+
console.log("LambderMSW called for:", body.apiName);
|
|
52
50
|
try {
|
|
53
51
|
// Add artificial delay if specified
|
|
54
52
|
if (options?.delay) {
|
package/dist/LambderResolver.js
CHANGED
|
@@ -16,7 +16,7 @@ export default class LambderResolver extends LambderResponseBuilder {
|
|
|
16
16
|
status404: this.autoResolve(this.status404),
|
|
17
17
|
cors: this.autoResolve(this.cors),
|
|
18
18
|
fileBase64: this.autoResolve(this.fileBase64),
|
|
19
|
-
file: this.
|
|
19
|
+
file: this.autoResolvePromise(this.file),
|
|
20
20
|
ejsFile: this.autoResolvePromise(this.ejsFile),
|
|
21
21
|
ejsTemplate: this.autoResolvePromise(this.ejsTemplate),
|
|
22
22
|
api: this.autoResolve(this.api),
|
|
@@ -44,7 +44,7 @@ export default class LambderResponseBuilder<TContract extends ApiContractShape =
|
|
|
44
44
|
status404(data: string, headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
45
45
|
cors(): LambderResolverResponse;
|
|
46
46
|
fileBase64(fileBase64: string, mimeType: string, headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
47
|
-
file(filePath: string, headers?: Record<string, string | string[]>, fallbackFilePath?: string): LambderResolverResponse
|
|
47
|
+
file(filePath: string, headers?: Record<string, string | string[]>, fallbackFilePath?: string): Promise<LambderResolverResponse>;
|
|
48
48
|
ejsTemplate(template: string, pageData: Record<string, any>, headers?: Record<string, string | string[]>): Promise<LambderResolverResponse>;
|
|
49
49
|
ejsFile(filePath: string, pageData: Record<string, any>, headers?: Record<string, string | string[]>): Promise<LambderResolverResponse>;
|
|
50
50
|
api<T = any>(payload: T | null, { versionExpired, sessionExpired, notAuthorized, message, errorMessage, logList, }?: LambderApiResponseConfig, headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
@@ -21,9 +21,9 @@ export default class LambderResponseBuilder {
|
|
|
21
21
|
this.ctx = ctx;
|
|
22
22
|
}
|
|
23
23
|
;
|
|
24
|
-
readPublicFileSync(filePath) {
|
|
25
|
-
const fs = getFS();
|
|
26
|
-
const path = getPath();
|
|
24
|
+
async readPublicFileSync(filePath) {
|
|
25
|
+
const fs = await getFS();
|
|
26
|
+
const path = await getPath();
|
|
27
27
|
if (!fs || !path) {
|
|
28
28
|
throw new Error("File system operations are not available in browser environment");
|
|
29
29
|
}
|
|
@@ -36,9 +36,9 @@ export default class LambderResponseBuilder {
|
|
|
36
36
|
return fs.readFileSync(absolutePath);
|
|
37
37
|
}
|
|
38
38
|
;
|
|
39
|
-
checkPublicFileExist(filePath) {
|
|
40
|
-
const fs = getFS();
|
|
41
|
-
const path = getPath();
|
|
39
|
+
async checkPublicFileExist(filePath) {
|
|
40
|
+
const fs = await getFS();
|
|
41
|
+
const path = await getPath();
|
|
42
42
|
if (!fs || !path) {
|
|
43
43
|
return false;
|
|
44
44
|
}
|
|
@@ -144,15 +144,17 @@ export default class LambderResponseBuilder {
|
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
;
|
|
147
|
-
file(filePath, headers, fallbackFilePath) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
147
|
+
async file(filePath, headers, fallbackFilePath) {
|
|
148
|
+
const doesFileExist = await this.checkPublicFileExist(filePath);
|
|
149
|
+
if (!doesFileExist && fallbackFilePath) {
|
|
150
|
+
const doesFallbackExist = await this.checkPublicFileExist(fallbackFilePath);
|
|
151
|
+
if (doesFallbackExist) {
|
|
152
|
+
return await this.file(fallbackFilePath, headers);
|
|
151
153
|
}
|
|
152
154
|
return this.json({ error: "File not found: " + filePath });
|
|
153
155
|
}
|
|
154
156
|
const mimeType = mimeTypeResolver.lookup(filePath);
|
|
155
|
-
const body = this.readPublicFileSync(filePath);
|
|
157
|
+
const body = await this.readPublicFileSync(filePath);
|
|
156
158
|
if (body === "forbidden-public-path") {
|
|
157
159
|
throw { error: "Forbidden public path: " + filePath };
|
|
158
160
|
}
|
package/dist/LambderUtils.js
CHANGED
|
@@ -6,9 +6,9 @@ export default class LambderUtils {
|
|
|
6
6
|
this.ejsPath = ejsPath;
|
|
7
7
|
}
|
|
8
8
|
;
|
|
9
|
-
readEjsFileSync(filePath) {
|
|
10
|
-
const fs = getFS();
|
|
11
|
-
const path = getPath();
|
|
9
|
+
async readEjsFileSync(filePath) {
|
|
10
|
+
const fs = await getFS();
|
|
11
|
+
const path = await getPath();
|
|
12
12
|
if (!fs || !path) {
|
|
13
13
|
throw new Error("File system operations are not available in browser environment");
|
|
14
14
|
}
|
|
@@ -24,9 +24,9 @@ export default class LambderUtils {
|
|
|
24
24
|
return String(fs.readFileSync(absolutePath));
|
|
25
25
|
}
|
|
26
26
|
;
|
|
27
|
-
checkEjsFileExist(filePath) {
|
|
28
|
-
const fs = getFS();
|
|
29
|
-
const path = getPath();
|
|
27
|
+
async checkEjsFileExist(filePath) {
|
|
28
|
+
const fs = await getFS();
|
|
29
|
+
const path = await getPath();
|
|
30
30
|
if (!fs || !path) {
|
|
31
31
|
return false;
|
|
32
32
|
}
|
|
@@ -52,10 +52,11 @@ export default class LambderUtils {
|
|
|
52
52
|
}
|
|
53
53
|
;
|
|
54
54
|
async renderEjsFile(filePath, pageData) {
|
|
55
|
-
|
|
55
|
+
const doesFileExist = await this.checkEjsFileExist(filePath);
|
|
56
|
+
if (!doesFileExist) {
|
|
56
57
|
return "File not found: " + filePath;
|
|
57
58
|
}
|
|
58
|
-
const template = this.readEjsFileSync(filePath);
|
|
59
|
+
const template = await this.readEjsFileSync(filePath);
|
|
59
60
|
return this.renderEjs(template, pageData);
|
|
60
61
|
}
|
|
61
62
|
;
|
package/dist/node-polyfills.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function getFS(): any
|
|
2
|
-
export declare function getPath(): any
|
|
1
|
+
export declare function getFS(): Promise<any>;
|
|
2
|
+
export declare function getPath(): Promise<any>;
|
package/dist/node-polyfills.js
CHANGED
|
@@ -2,24 +2,29 @@
|
|
|
2
2
|
// This file provides optional Node.js modules that fail gracefully in browser environments
|
|
3
3
|
let fs = null;
|
|
4
4
|
let path = null;
|
|
5
|
-
|
|
6
|
-
try {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// Use eval to prevent bundlers from trying to resolve these at build time
|
|
10
|
-
const requireFunc = eval('typeof require !== "undefined" ? require : null');
|
|
11
|
-
if (requireFunc) {
|
|
12
|
-
fs = requireFunc('fs');
|
|
13
|
-
path = requireFunc('path');
|
|
5
|
+
export async function getFS() {
|
|
6
|
+
try {
|
|
7
|
+
if (fs) {
|
|
8
|
+
return fs;
|
|
14
9
|
}
|
|
10
|
+
fs = await import('fs');
|
|
11
|
+
return fs;
|
|
12
|
+
}
|
|
13
|
+
catch (e) {
|
|
14
|
+
// Silently fail - we're in a browser environment
|
|
15
|
+
return null;
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
export async function getPath() {
|
|
19
|
+
try {
|
|
20
|
+
if (path) {
|
|
21
|
+
return path;
|
|
22
|
+
}
|
|
23
|
+
path = await import('path');
|
|
24
|
+
return path;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
// Silently fail - we're in a browser environment
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
25
30
|
}
|
package/package.json
CHANGED
package/src/LambderMSW.ts
CHANGED
|
@@ -65,7 +65,8 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
65
65
|
let body: any;
|
|
66
66
|
|
|
67
67
|
try {
|
|
68
|
-
|
|
68
|
+
const clonedRequest = request.clone();
|
|
69
|
+
body = await clonedRequest.json();
|
|
69
70
|
} catch (parseErr) {
|
|
70
71
|
// If JSON parsing fails, return undefined to let other handlers try
|
|
71
72
|
console.warn("LambderMSW: Failed to parse request body as JSON");
|
|
@@ -73,18 +74,10 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
// Check if body is valid and has apiName
|
|
76
|
-
if (!body || typeof body.apiName !== 'string') {
|
|
77
|
-
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
console.log("LambderMSW called for:", body.apiName, "matching against:", apiName);
|
|
77
|
+
if (!body || typeof body.apiName !== 'string') { return; }
|
|
78
|
+
if (body.apiName !== apiName) { return; }
|
|
82
79
|
|
|
83
|
-
|
|
84
|
-
if (body.apiName !== apiName) {
|
|
85
|
-
// If this handler doesn't match, return undefined to let MSW try other handlers
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
80
|
+
console.log("LambderMSW called for:", body.apiName);
|
|
88
81
|
|
|
89
82
|
try {
|
|
90
83
|
// Add artificial delay if specified
|
package/src/LambderResolver.ts
CHANGED
|
@@ -57,7 +57,7 @@ export default class LambderResolver<
|
|
|
57
57
|
status404: this.autoResolve(this.status404),
|
|
58
58
|
cors: this.autoResolve(this.cors),
|
|
59
59
|
fileBase64: this.autoResolve(this.fileBase64),
|
|
60
|
-
file: this.
|
|
60
|
+
file: this.autoResolvePromise(this.file),
|
|
61
61
|
ejsFile: this.autoResolvePromise(this.ejsFile),
|
|
62
62
|
ejsTemplate: this.autoResolvePromise(this.ejsTemplate),
|
|
63
63
|
api: this.autoResolve(this.api),
|
|
@@ -61,9 +61,9 @@ export default class LambderResponseBuilder<TContract extends ApiContractShape =
|
|
|
61
61
|
this.ctx = ctx;
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
-
private readPublicFileSync(filePath: string){
|
|
65
|
-
const fs = getFS();
|
|
66
|
-
const path = getPath();
|
|
64
|
+
private async readPublicFileSync(filePath: string){
|
|
65
|
+
const fs = await getFS();
|
|
66
|
+
const path = await getPath();
|
|
67
67
|
|
|
68
68
|
if (!fs || !path) {
|
|
69
69
|
throw new Error("File system operations are not available in browser environment");
|
|
@@ -76,13 +76,11 @@ export default class LambderResponseBuilder<TContract extends ApiContractShape =
|
|
|
76
76
|
return fs.readFileSync(absolutePath);
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
private checkPublicFileExist(filePath: string){
|
|
80
|
-
const fs = getFS();
|
|
81
|
-
const path = getPath();
|
|
79
|
+
private async checkPublicFileExist(filePath: string){
|
|
80
|
+
const fs = await getFS();
|
|
81
|
+
const path = await getPath();
|
|
82
82
|
|
|
83
|
-
if (!fs || !path) {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
83
|
+
if (!fs || !path) { return false; }
|
|
86
84
|
|
|
87
85
|
const publicPath = path.resolve(this.publicPath);
|
|
88
86
|
const absolutePath = path.join(this.publicPath, filePath);
|
|
@@ -199,19 +197,21 @@ export default class LambderResponseBuilder<TContract extends ApiContractShape =
|
|
|
199
197
|
});
|
|
200
198
|
};
|
|
201
199
|
|
|
202
|
-
file(
|
|
200
|
+
async file(
|
|
203
201
|
filePath: string,
|
|
204
202
|
headers?: Record<string, string|string[]>,
|
|
205
203
|
fallbackFilePath?: string,
|
|
206
|
-
):LambderResolverResponse{
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
204
|
+
):Promise<LambderResolverResponse>{
|
|
205
|
+
const doesFileExist = await this.checkPublicFileExist(filePath);
|
|
206
|
+
if(!doesFileExist && fallbackFilePath){
|
|
207
|
+
const doesFallbackExist = await this.checkPublicFileExist(fallbackFilePath);
|
|
208
|
+
if(doesFallbackExist){
|
|
209
|
+
return await this.file(fallbackFilePath, headers);
|
|
210
210
|
}
|
|
211
211
|
return this.json({ error: "File not found: " + filePath })
|
|
212
212
|
}
|
|
213
213
|
const mimeType = mimeTypeResolver.lookup(filePath);
|
|
214
|
-
const body = this.readPublicFileSync(filePath);
|
|
214
|
+
const body = await this.readPublicFileSync(filePath);
|
|
215
215
|
if (body === "forbidden-public-path") {
|
|
216
216
|
throw { error: "Forbidden public path: " + filePath };
|
|
217
217
|
}
|
package/src/LambderUtils.ts
CHANGED
|
@@ -12,9 +12,9 @@ export default class LambderUtils {
|
|
|
12
12
|
this.ejsPath = ejsPath;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
private readEjsFileSync(filePath: string){
|
|
16
|
-
const fs = getFS();
|
|
17
|
-
const path = getPath();
|
|
15
|
+
private async readEjsFileSync(filePath: string){
|
|
16
|
+
const fs = await getFS();
|
|
17
|
+
const path = await getPath();
|
|
18
18
|
|
|
19
19
|
if (!fs || !path) {
|
|
20
20
|
throw new Error("File system operations are not available in browser environment");
|
|
@@ -27,9 +27,9 @@ export default class LambderUtils {
|
|
|
27
27
|
return String(fs.readFileSync(absolutePath));
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
private checkEjsFileExist(filePath: string){
|
|
31
|
-
const fs = getFS();
|
|
32
|
-
const path = getPath();
|
|
30
|
+
private async checkEjsFileExist(filePath: string){
|
|
31
|
+
const fs = await getFS();
|
|
32
|
+
const path = await getPath();
|
|
33
33
|
|
|
34
34
|
if (!fs || !path) {
|
|
35
35
|
return false;
|
|
@@ -59,10 +59,11 @@ export default class LambderUtils {
|
|
|
59
59
|
filePath: string,
|
|
60
60
|
pageData: Record<string, any>,
|
|
61
61
|
):Promise<string>{
|
|
62
|
-
|
|
62
|
+
const doesFileExist = await this.checkEjsFileExist(filePath);
|
|
63
|
+
if(!doesFileExist){
|
|
63
64
|
return "File not found: " + filePath;
|
|
64
65
|
}
|
|
65
|
-
const template = this.readEjsFileSync(filePath);
|
|
66
|
+
const template = await this.readEjsFileSync(filePath);
|
|
66
67
|
return this.renderEjs(template, pageData);
|
|
67
68
|
};
|
|
68
69
|
|
package/src/node-polyfills.ts
CHANGED
|
@@ -4,25 +4,24 @@
|
|
|
4
4
|
let fs: any = null;
|
|
5
5
|
let path: any = null;
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
path = requireFunc('path');
|
|
16
|
-
}
|
|
7
|
+
export async function getFS(): Promise<any> {
|
|
8
|
+
try {
|
|
9
|
+
if(fs){ return fs; }
|
|
10
|
+
fs = await import('fs');
|
|
11
|
+
return fs;
|
|
12
|
+
} catch (e) {
|
|
13
|
+
// Silently fail - we're in a browser environment
|
|
14
|
+
return null;
|
|
17
15
|
}
|
|
18
|
-
} catch (e) {
|
|
19
|
-
// Silently fail - we're in a browser environment
|
|
20
16
|
}
|
|
21
17
|
|
|
22
|
-
export function
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
export async function getPath(): Promise<any> {
|
|
19
|
+
try {
|
|
20
|
+
if(path){ return path; }
|
|
21
|
+
path = await import('path');
|
|
22
|
+
return path;
|
|
23
|
+
} catch (e) {
|
|
24
|
+
// Silently fail - we're in a browser environment
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
28
27
|
}
|