lambder 1.0.133 → 1.0.134
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/LambderMSW.d.ts +11 -6
- package/dist/LambderMSW.js +5 -0
- package/package.json +1 -1
- package/src/LambderMSW.ts +11 -6
- package/test-type-enforcement.ts +111 -0
package/dist/LambderMSW.d.ts
CHANGED
|
@@ -23,27 +23,32 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
23
23
|
* @param apiName - The name of the API to mock
|
|
24
24
|
* @param handler - Function that returns the mock payload
|
|
25
25
|
* @param options - Additional response options (session expired, version expired, etc.)
|
|
26
|
+
*
|
|
27
|
+
* Note: TypeScript will check that the return type is assignable to the output type,
|
|
28
|
+
* but due to structural typing, extra properties are allowed. Enable strict checks
|
|
29
|
+
* in your tsconfig.json with "noUncheckedIndexedAccess" and "exactOptionalPropertyTypes"
|
|
30
|
+
* for better type safety.
|
|
26
31
|
*/
|
|
27
|
-
mockApi<TApiName extends keyof TContract
|
|
32
|
+
mockApi<TApiName extends keyof TContract>(apiName: TApiName, handler: (payload?: TContract[TApiName]['input']) => Promise<TContract[TApiName]['output']> | TContract[TApiName]['output'], options?: MockApiOptions): RequestHandler;
|
|
28
33
|
/**
|
|
29
34
|
* Mock an API endpoint that returns a session expired error
|
|
30
35
|
*/
|
|
31
|
-
mockSessionExpired<TApiName extends keyof TContract
|
|
36
|
+
mockSessionExpired<TApiName extends keyof TContract>(apiName: TApiName): RequestHandler;
|
|
32
37
|
/**
|
|
33
38
|
* Mock an API endpoint that returns a version expired error
|
|
34
39
|
*/
|
|
35
|
-
mockVersionExpired<TApiName extends keyof TContract
|
|
40
|
+
mockVersionExpired<TApiName extends keyof TContract>(apiName: TApiName): RequestHandler;
|
|
36
41
|
/**
|
|
37
42
|
* Mock an API endpoint that returns a not authorized error
|
|
38
43
|
*/
|
|
39
|
-
mockNotAuthorized<TApiName extends keyof TContract
|
|
44
|
+
mockNotAuthorized<TApiName extends keyof TContract>(apiName: TApiName): RequestHandler;
|
|
40
45
|
/**
|
|
41
46
|
* Mock an API endpoint that returns an error message
|
|
42
47
|
*/
|
|
43
|
-
mockError<TApiName extends keyof TContract
|
|
48
|
+
mockError<TApiName extends keyof TContract>(apiName: TApiName, errorMessage: string): RequestHandler;
|
|
44
49
|
/**
|
|
45
50
|
* Mock an API endpoint with a custom message
|
|
46
51
|
*/
|
|
47
|
-
mockWithMessage<TApiName extends keyof TContract
|
|
52
|
+
mockWithMessage<TApiName extends keyof TContract>(apiName: TApiName, handler: (payload?: TContract[TApiName]['input']) => Promise<TContract[TApiName]['output']> | TContract[TApiName]['output'], message: any): RequestHandler;
|
|
48
53
|
}
|
|
49
54
|
export {};
|
package/dist/LambderMSW.js
CHANGED
|
@@ -21,6 +21,11 @@ export default class LambderMSW {
|
|
|
21
21
|
* @param apiName - The name of the API to mock
|
|
22
22
|
* @param handler - Function that returns the mock payload
|
|
23
23
|
* @param options - Additional response options (session expired, version expired, etc.)
|
|
24
|
+
*
|
|
25
|
+
* Note: TypeScript will check that the return type is assignable to the output type,
|
|
26
|
+
* but due to structural typing, extra properties are allowed. Enable strict checks
|
|
27
|
+
* in your tsconfig.json with "noUncheckedIndexedAccess" and "exactOptionalPropertyTypes"
|
|
28
|
+
* for better type safety.
|
|
24
29
|
*/
|
|
25
30
|
mockApi(apiName, handler, options) {
|
|
26
31
|
return this.http.post(this.apiPath, async ({ request }) => {
|
package/package.json
CHANGED
package/src/LambderMSW.ts
CHANGED
|
@@ -50,8 +50,13 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
50
50
|
* @param apiName - The name of the API to mock
|
|
51
51
|
* @param handler - Function that returns the mock payload
|
|
52
52
|
* @param options - Additional response options (session expired, version expired, etc.)
|
|
53
|
+
*
|
|
54
|
+
* Note: TypeScript will check that the return type is assignable to the output type,
|
|
55
|
+
* but due to structural typing, extra properties are allowed. Enable strict checks
|
|
56
|
+
* in your tsconfig.json with "noUncheckedIndexedAccess" and "exactOptionalPropertyTypes"
|
|
57
|
+
* for better type safety.
|
|
53
58
|
*/
|
|
54
|
-
mockApi<TApiName extends keyof TContract
|
|
59
|
+
mockApi<TApiName extends keyof TContract>(
|
|
55
60
|
apiName: TApiName,
|
|
56
61
|
handler: (payload?: TContract[TApiName]['input']) => Promise<TContract[TApiName]['output']> | TContract[TApiName]['output'],
|
|
57
62
|
options?: MockApiOptions
|
|
@@ -122,7 +127,7 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
122
127
|
/**
|
|
123
128
|
* Mock an API endpoint that returns a session expired error
|
|
124
129
|
*/
|
|
125
|
-
mockSessionExpired<TApiName extends keyof TContract
|
|
130
|
+
mockSessionExpired<TApiName extends keyof TContract>(
|
|
126
131
|
apiName: TApiName
|
|
127
132
|
): RequestHandler {
|
|
128
133
|
return this.mockApi(
|
|
@@ -135,7 +140,7 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
135
140
|
/**
|
|
136
141
|
* Mock an API endpoint that returns a version expired error
|
|
137
142
|
*/
|
|
138
|
-
mockVersionExpired<TApiName extends keyof TContract
|
|
143
|
+
mockVersionExpired<TApiName extends keyof TContract>(
|
|
139
144
|
apiName: TApiName
|
|
140
145
|
): RequestHandler {
|
|
141
146
|
return this.mockApi(
|
|
@@ -148,7 +153,7 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
148
153
|
/**
|
|
149
154
|
* Mock an API endpoint that returns a not authorized error
|
|
150
155
|
*/
|
|
151
|
-
mockNotAuthorized<TApiName extends keyof TContract
|
|
156
|
+
mockNotAuthorized<TApiName extends keyof TContract>(
|
|
152
157
|
apiName: TApiName
|
|
153
158
|
): RequestHandler {
|
|
154
159
|
return this.mockApi(
|
|
@@ -161,7 +166,7 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
161
166
|
/**
|
|
162
167
|
* Mock an API endpoint that returns an error message
|
|
163
168
|
*/
|
|
164
|
-
mockError<TApiName extends keyof TContract
|
|
169
|
+
mockError<TApiName extends keyof TContract>(
|
|
165
170
|
apiName: TApiName,
|
|
166
171
|
errorMessage: string
|
|
167
172
|
): RequestHandler {
|
|
@@ -175,7 +180,7 @@ export default class LambderMSW<TContract extends ApiContractShape = any> {
|
|
|
175
180
|
/**
|
|
176
181
|
* Mock an API endpoint with a custom message
|
|
177
182
|
*/
|
|
178
|
-
mockWithMessage<TApiName extends keyof TContract
|
|
183
|
+
mockWithMessage<TApiName extends keyof TContract>(
|
|
179
184
|
apiName: TApiName,
|
|
180
185
|
handler: (payload?: TContract[TApiName]['input']) => Promise<TContract[TApiName]['output']> | TContract[TApiName]['output'],
|
|
181
186
|
message: any
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Test file to verify LambderMSW type enforcement
|
|
2
|
+
import LambderMSW from './src/LambderMSW';
|
|
3
|
+
import type { ApiContract } from './src/LambderApiContract';
|
|
4
|
+
|
|
5
|
+
// Define a test API contract
|
|
6
|
+
type TestContract = ApiContract<{
|
|
7
|
+
'public.getInitialPageData': {
|
|
8
|
+
input: void;
|
|
9
|
+
output: {
|
|
10
|
+
userLocationData: {
|
|
11
|
+
country: string;
|
|
12
|
+
region: string;
|
|
13
|
+
regionCode: string;
|
|
14
|
+
city: string;
|
|
15
|
+
zipCode: string;
|
|
16
|
+
lat: number;
|
|
17
|
+
lng: number;
|
|
18
|
+
};
|
|
19
|
+
sessionUser: {
|
|
20
|
+
id: string;
|
|
21
|
+
username: string;
|
|
22
|
+
email: string;
|
|
23
|
+
name: string;
|
|
24
|
+
bio: string;
|
|
25
|
+
// NOTE: 's' property should NOT be here
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}>;
|
|
30
|
+
|
|
31
|
+
const apiMocker = new LambderMSW<TestContract>({
|
|
32
|
+
apiPath: '/secure',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// TEST 1: Extra properties - TypeScript allows this due to structural typing
|
|
36
|
+
const handler1 = apiMocker.mockApi(
|
|
37
|
+
'public.getInitialPageData',
|
|
38
|
+
async () => {
|
|
39
|
+
return {
|
|
40
|
+
userLocationData: {
|
|
41
|
+
country: 'United States',
|
|
42
|
+
region: 'California',
|
|
43
|
+
regionCode: 'CA',
|
|
44
|
+
city: 'San Francisco',
|
|
45
|
+
zipCode: '94102',
|
|
46
|
+
lat: 37.7749,
|
|
47
|
+
lng: -122.4194,
|
|
48
|
+
},
|
|
49
|
+
sessionUser: {
|
|
50
|
+
id: 'mock-user-123',
|
|
51
|
+
username: 'mockuser',
|
|
52
|
+
email: 'mock@example.com',
|
|
53
|
+
name: 'Mock User',
|
|
54
|
+
bio: 'This is a mock user for development',
|
|
55
|
+
s: 43 // ⚠️ Extra property - TypeScript structural typing allows this
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// TEST 2: Missing required property - THIS WILL CAUSE AN ERROR!
|
|
62
|
+
const handler2 = apiMocker.mockApi(
|
|
63
|
+
'public.getInitialPageData',
|
|
64
|
+
async () => {
|
|
65
|
+
return {
|
|
66
|
+
userLocationData: {
|
|
67
|
+
country: 'United States',
|
|
68
|
+
region: 'California',
|
|
69
|
+
regionCode: 'CA',
|
|
70
|
+
city: 'San Francisco',
|
|
71
|
+
zipCode: '94102',
|
|
72
|
+
lat: 37.7749,
|
|
73
|
+
lng: -122.4194,
|
|
74
|
+
},
|
|
75
|
+
sessionUser: {
|
|
76
|
+
id: 'mock-user-123',
|
|
77
|
+
username: 'mockuser',
|
|
78
|
+
email: 'mock@example.com',
|
|
79
|
+
name: 'Mock User',
|
|
80
|
+
// bio: 'This is a mock user for development', // ❌ Missing required property - ERROR!
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// TEST 3: Wrong type - THIS WILL CAUSE AN ERROR!
|
|
87
|
+
const handler3 = apiMocker.mockApi(
|
|
88
|
+
'public.getInitialPageData',
|
|
89
|
+
async () => {
|
|
90
|
+
return {
|
|
91
|
+
userLocationData: {
|
|
92
|
+
country: 'United States',
|
|
93
|
+
region: 'California',
|
|
94
|
+
regionCode: 'CA',
|
|
95
|
+
city: 'San Francisco',
|
|
96
|
+
zipCode: '94102',
|
|
97
|
+
lat: 37.7749,
|
|
98
|
+
lng: -122.4194,
|
|
99
|
+
},
|
|
100
|
+
sessionUser: {
|
|
101
|
+
id: 123, // ❌ Wrong type - should be string - ERROR!
|
|
102
|
+
username: 'mockuser',
|
|
103
|
+
email: 'mock@example.com',
|
|
104
|
+
name: 'Mock User',
|
|
105
|
+
bio: 'This is a mock user for development',
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
console.log('Check for TypeScript errors above!');
|