lambder 2.0.11 → 2.0.12
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/package.json +1 -1
- package/tests/type-safety.test.ts +0 -564
package/package.json
CHANGED
|
@@ -1,564 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type Safety Tests
|
|
3
|
-
*
|
|
4
|
-
* This file tests that the type system works correctly.
|
|
5
|
-
* If this file compiles without errors, the types are working!
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { describe, it, expect } from 'vitest';
|
|
9
|
-
import { z } from 'zod';
|
|
10
|
-
import Lambder from '../src/Lambder.js';
|
|
11
|
-
import LambderCaller from '../src/LambderCaller.js';
|
|
12
|
-
|
|
13
|
-
// ============================================================================
|
|
14
|
-
// Test 1: LambderCaller Type Safety
|
|
15
|
-
// ============================================================================
|
|
16
|
-
|
|
17
|
-
describe('LambderCaller Type Safety', () => {
|
|
18
|
-
it('should have correct types', () => {
|
|
19
|
-
// Define contract via chaining
|
|
20
|
-
const lambder = new Lambder({
|
|
21
|
-
publicPath: './public',
|
|
22
|
-
apiPath: '/api'
|
|
23
|
-
})
|
|
24
|
-
.addApi('getUser', {
|
|
25
|
-
input: z.object({ userId: z.string() }),
|
|
26
|
-
output: z.object({ id: z.string(), name: z.string() })
|
|
27
|
-
}, async (ctx, resolver) => {
|
|
28
|
-
return resolver.api({ id: ctx.apiPayload.userId, name: 'Test' });
|
|
29
|
-
})
|
|
30
|
-
.addApi('createUser', {
|
|
31
|
-
input: z.object({ name: z.string(), email: z.string() }),
|
|
32
|
-
output: z.object({ id: z.string(), name: z.string(), email: z.string() })
|
|
33
|
-
}, async (ctx, resolver) => {
|
|
34
|
-
return resolver.api({ id: '1', ...ctx.apiPayload });
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
type TestApiContract = typeof lambder.ApiContract;
|
|
38
|
-
|
|
39
|
-
const caller = new LambderCaller<TestApiContract>({
|
|
40
|
-
apiPath: '/api',
|
|
41
|
-
isCorsEnabled: false
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// Type assertions to verify compile-time types
|
|
45
|
-
// If this compiles, the types are correct
|
|
46
|
-
|
|
47
|
-
// ✅ These should compile without errors
|
|
48
|
-
type GetUserInput = Parameters<typeof caller.api<'getUser'>>[1];
|
|
49
|
-
type CreateUserInput = Parameters<typeof caller.api<'createUser'>>[1];
|
|
50
|
-
|
|
51
|
-
// Verify the caller was created successfully
|
|
52
|
-
expect(caller).toBeDefined();
|
|
53
|
-
expect(caller.api).toBeDefined();
|
|
54
|
-
|
|
55
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
56
|
-
// await caller.api('getUser', { id: '123' }); // Error: should be userId
|
|
57
|
-
// await caller.api('createUser', { name: 'Bob' }); // Error: missing email
|
|
58
|
-
// await caller.api('getUser', { userId: 123 }); // Error: userId should be string
|
|
59
|
-
// await caller.api('nonExistent', {}); // Error: API doesn't exist
|
|
60
|
-
// await caller.api('listUsers', { something: true }); // Error: should be undefined
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// ============================================================================
|
|
65
|
-
// Test 2: Lambder Type Safety
|
|
66
|
-
// ============================================================================
|
|
67
|
-
|
|
68
|
-
describe('Lambder Type Safety', () => {
|
|
69
|
-
it('should have correct types', () => {
|
|
70
|
-
const lambder = new Lambder({
|
|
71
|
-
publicPath: './public',
|
|
72
|
-
apiPath: '/api'
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
// ✅ Should work: Typed API
|
|
76
|
-
lambder.addApi('getUser', {
|
|
77
|
-
input: z.object({ userId: z.string() }),
|
|
78
|
-
output: z.object({ id: z.string(), name: z.string() })
|
|
79
|
-
}, async (ctx, resolver) => {
|
|
80
|
-
// ctx.apiPayload should be typed as { userId: string }
|
|
81
|
-
const userId: string = ctx.apiPayload.userId; // Should work
|
|
82
|
-
|
|
83
|
-
// Return correct type
|
|
84
|
-
return resolver.api({ id: userId, name: 'Test' });
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// ✅ Should work: Typed session API
|
|
88
|
-
lambder.addSessionApi('createUser', {
|
|
89
|
-
input: z.object({ name: z.string(), email: z.string() }),
|
|
90
|
-
output: z.object({ id: z.string(), name: z.string(), email: z.string() })
|
|
91
|
-
}, async (ctx, resolver) => {
|
|
92
|
-
// ctx.apiPayload should be typed as { name: string, email: string }
|
|
93
|
-
const name: string = ctx.apiPayload.name;
|
|
94
|
-
const email: string = ctx.apiPayload.email;
|
|
95
|
-
|
|
96
|
-
return resolver.api({ id: '1', name, email });
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// ✅ Should work: Void input API
|
|
100
|
-
lambder.addApi('listUsers', {
|
|
101
|
-
input: z.void(),
|
|
102
|
-
output: z.array(z.object({ id: z.string(), name: z.string() }))
|
|
103
|
-
}, async (ctx, resolver) => {
|
|
104
|
-
// ctx.apiPayload should be void/undefined
|
|
105
|
-
return resolver.api([
|
|
106
|
-
{ id: '1', name: 'User 1' },
|
|
107
|
-
{ id: '2', name: 'User 2' }
|
|
108
|
-
]);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// ✅ Should work: RegExp (untyped)
|
|
112
|
-
lambder.addApi('admin.anyAction', {
|
|
113
|
-
input: z.any(),
|
|
114
|
-
output: z.any()
|
|
115
|
-
}, async (ctx, resolver) => {
|
|
116
|
-
// ctx.apiPayload is any (untyped)
|
|
117
|
-
return resolver.api({});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Verify the lambder was created successfully
|
|
121
|
-
expect(lambder).toBeDefined();
|
|
122
|
-
expect(lambder.addApi).toBeDefined();
|
|
123
|
-
|
|
124
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
125
|
-
// lambder.addApi('getUser', async (ctx, resolver) => {
|
|
126
|
-
// const id = ctx.apiPayload.id; // Error: should be userId
|
|
127
|
-
// return resolver.api({ id, name: 'Test' });
|
|
128
|
-
// });
|
|
129
|
-
// lambder.addApi('getUser', async (ctx, resolver) => {
|
|
130
|
-
// return resolver.api({ id: '1' }); // Error: missing name property
|
|
131
|
-
// });
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
// ============================================================================
|
|
136
|
-
// Test 3: Backward Compatibility (No Contract)
|
|
137
|
-
// ============================================================================
|
|
138
|
-
|
|
139
|
-
describe('Backward Compatibility (No Contract)', () => {
|
|
140
|
-
it('should work without type contract', () => {
|
|
141
|
-
// Without contract type - should work as before (untyped)
|
|
142
|
-
const caller = new LambderCaller({
|
|
143
|
-
apiPath: '/api',
|
|
144
|
-
isCorsEnabled: false
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
const lambder = new Lambder({
|
|
148
|
-
publicPath: './public',
|
|
149
|
-
apiPath: '/api'
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// Should work - untyped
|
|
153
|
-
lambder.addApi('anyApi', {
|
|
154
|
-
input: z.any(),
|
|
155
|
-
output: z.any()
|
|
156
|
-
}, async (ctx, resolver) => {
|
|
157
|
-
// ctx.apiPayload is any
|
|
158
|
-
return resolver.api({ anything: ctx.apiPayload });
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
// Verify the instances were created successfully
|
|
162
|
-
expect(caller).toBeDefined();
|
|
163
|
-
expect(lambder).toBeDefined();
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// ============================================================================
|
|
168
|
-
// Test 4: apiRaw Method
|
|
169
|
-
// ============================================================================
|
|
170
|
-
|
|
171
|
-
describe('apiRaw Method Type Safety', () => {
|
|
172
|
-
it('should have correct types for apiRaw', () => {
|
|
173
|
-
const lambder = new Lambder({
|
|
174
|
-
publicPath: './public',
|
|
175
|
-
apiPath: '/api'
|
|
176
|
-
})
|
|
177
|
-
.addApi('getUser', {
|
|
178
|
-
input: z.object({ userId: z.string() }),
|
|
179
|
-
output: z.object({ id: z.string(), name: z.string() })
|
|
180
|
-
}, async (ctx, resolver) => {
|
|
181
|
-
return resolver.api({ id: ctx.apiPayload.userId, name: 'Test' });
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
type TestApiContract = typeof lambder.ApiContract;
|
|
185
|
-
|
|
186
|
-
const caller = new LambderCaller<TestApiContract>({
|
|
187
|
-
apiPath: '/api',
|
|
188
|
-
isCorsEnabled: false
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
// Verify the caller was created successfully
|
|
192
|
-
expect(caller).toBeDefined();
|
|
193
|
-
expect(caller.apiRaw).toBeDefined();
|
|
194
|
-
|
|
195
|
-
// Type checking happens at compile time
|
|
196
|
-
// If this compiles, apiRaw has correct types
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
// ============================================================================
|
|
201
|
-
// Test 5: Complex Types
|
|
202
|
-
// ============================================================================
|
|
203
|
-
|
|
204
|
-
type ComplexApiContract = {
|
|
205
|
-
update: {
|
|
206
|
-
input: { id: string } & Partial<{ name: string, email: string }>,
|
|
207
|
-
output: { id: string, name: string, email: string }
|
|
208
|
-
},
|
|
209
|
-
search: {
|
|
210
|
-
input: { query: string, filters?: { role?: string, active?: boolean } },
|
|
211
|
-
output: Array<{ id: string, name: string }>
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
describe('Complex Types', () => {
|
|
216
|
-
it('should handle complex type patterns', () => {
|
|
217
|
-
const caller = new LambderCaller<ComplexApiContract>({
|
|
218
|
-
apiPath: '/api',
|
|
219
|
-
isCorsEnabled: false
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// Verify the caller was created successfully
|
|
223
|
-
expect(caller).toBeDefined();
|
|
224
|
-
|
|
225
|
-
// Type assertions to verify compile-time types
|
|
226
|
-
// If this compiles with correct types, the test passes
|
|
227
|
-
type UpdateInput = Parameters<typeof caller.api<'update'>>[1];
|
|
228
|
-
type SearchInput = Parameters<typeof caller.api<'search'>>[1];
|
|
229
|
-
|
|
230
|
-
// These would compile successfully with correct types:
|
|
231
|
-
// await caller.api('update', { id: '1', name: 'New Name' });
|
|
232
|
-
// await caller.api('update', { id: '1', email: 'new@email.com' });
|
|
233
|
-
// await caller.api('update', { id: '1' }); // Only id
|
|
234
|
-
// await caller.api('search', { query: 'test' });
|
|
235
|
-
// await caller.api('search', { query: 'test', filters: { role: 'admin' } });
|
|
236
|
-
// await caller.api('search', { query: 'test', filters: { role: 'admin', active: true } });
|
|
237
|
-
});
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
// ============================================================================
|
|
241
|
-
// Test 6: Output Type Enforcement
|
|
242
|
-
// ============================================================================
|
|
243
|
-
|
|
244
|
-
describe('Output Type Enforcement', () => {
|
|
245
|
-
it('should enforce output types in resolver.api()', () => {
|
|
246
|
-
const lambder = new Lambder({
|
|
247
|
-
publicPath: './public',
|
|
248
|
-
apiPath: '/api'
|
|
249
|
-
})
|
|
250
|
-
.addApi('getUser', {
|
|
251
|
-
input: z.object({ userId: z.string() }),
|
|
252
|
-
output: z.object({ id: z.string(), name: z.string() }).nullable()
|
|
253
|
-
}, async (ctx, resolver) => {
|
|
254
|
-
const userId = ctx.apiPayload.userId;
|
|
255
|
-
return resolver.api({ id: userId, name: 'John Doe' });
|
|
256
|
-
})
|
|
257
|
-
.addApi('deleteUser', {
|
|
258
|
-
input: z.object({ userId: z.string() }),
|
|
259
|
-
output: z.boolean()
|
|
260
|
-
}, async (ctx, resolver) => {
|
|
261
|
-
return resolver.api(true);
|
|
262
|
-
})
|
|
263
|
-
.addApi('listUsers', {
|
|
264
|
-
input: z.void(),
|
|
265
|
-
output: z.array(z.object({ id: z.string(), name: z.string() }))
|
|
266
|
-
}, async (ctx, resolver) => {
|
|
267
|
-
return resolver.api([
|
|
268
|
-
{ id: '1', name: 'User 1' },
|
|
269
|
-
{ id: '2', name: 'User 2' }
|
|
270
|
-
]);
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
expect(lambder).toBeDefined();
|
|
274
|
-
|
|
275
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
276
|
-
|
|
277
|
-
// Wrong output type:
|
|
278
|
-
// lambder.addApi('getUser', async (ctx, resolver) => {
|
|
279
|
-
// return resolver.api("wrong"); // Error: string not assignable to { id: string, name: string }
|
|
280
|
-
// });
|
|
281
|
-
|
|
282
|
-
// Missing required property:
|
|
283
|
-
// lambder.addApi('getUser', async (ctx, resolver) => {
|
|
284
|
-
// return resolver.api({ id: '123' }); // Error: missing 'name' property
|
|
285
|
-
// });
|
|
286
|
-
|
|
287
|
-
// Wrong property type:
|
|
288
|
-
// lambder.addApi('getUser', async (ctx, resolver) => {
|
|
289
|
-
// return resolver.api({ id: 123, name: 'Test' }); // Error: id should be string
|
|
290
|
-
// });
|
|
291
|
-
|
|
292
|
-
// Wrong boolean output:
|
|
293
|
-
// lambder.addApi('deleteUser', async (ctx, resolver) => {
|
|
294
|
-
// return resolver.api("true"); // Error: string not assignable to boolean
|
|
295
|
-
// });
|
|
296
|
-
|
|
297
|
-
// Wrong array element type:
|
|
298
|
-
// lambder.addApi('listUsers', async (ctx, resolver) => {
|
|
299
|
-
// return resolver.api([{ id: '1' }]); // Error: missing 'name' property
|
|
300
|
-
// });
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
it('should enforce output types in resolver.die.api()', () => {
|
|
304
|
-
const lambder = new Lambder({
|
|
305
|
-
publicPath: './public',
|
|
306
|
-
apiPath: '/api'
|
|
307
|
-
})
|
|
308
|
-
.addApi('getUser', {
|
|
309
|
-
input: z.object({ userId: z.string() }),
|
|
310
|
-
output: z.object({ id: z.string(), name: z.string() })
|
|
311
|
-
}, async (ctx, resolver) => {
|
|
312
|
-
return resolver.die.api({ id: ctx.apiPayload.userId, name: 'Test' });
|
|
313
|
-
})
|
|
314
|
-
.addApi('deleteUser', {
|
|
315
|
-
input: z.object({ userId: z.string() }),
|
|
316
|
-
output: z.boolean()
|
|
317
|
-
}, async (ctx, resolver) => {
|
|
318
|
-
return resolver.die.api(true);
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
expect(lambder).toBeDefined();
|
|
322
|
-
|
|
323
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
324
|
-
// lambder.addApi('getUser', async (ctx, resolver) => {
|
|
325
|
-
// return resolver.die.api("wrong"); // Error: wrong type
|
|
326
|
-
// });
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
it('should enforce output types in addSessionApi()', () => {
|
|
330
|
-
const lambder = new Lambder({
|
|
331
|
-
publicPath: './public',
|
|
332
|
-
apiPath: '/api'
|
|
333
|
-
})
|
|
334
|
-
.addSessionApi('createUser', {
|
|
335
|
-
input: z.object({ name: z.string(), email: z.string() }),
|
|
336
|
-
output: z.object({ id: z.string(), name: z.string(), email: z.string() })
|
|
337
|
-
}, async (ctx, resolver) => {
|
|
338
|
-
const { name, email } = ctx.apiPayload;
|
|
339
|
-
return resolver.api({ id: '1', name, email });
|
|
340
|
-
})
|
|
341
|
-
.addSessionApi('deleteUser', {
|
|
342
|
-
input: z.object({ userId: z.string() }),
|
|
343
|
-
output: z.boolean()
|
|
344
|
-
}, async (ctx, resolver) => {
|
|
345
|
-
return resolver.api(true);
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
expect(lambder).toBeDefined();
|
|
349
|
-
|
|
350
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
351
|
-
// lambder.addSessionApi('createUser', async (ctx, resolver) => {
|
|
352
|
-
// return resolver.api({ id: '1' }); // Error: missing name and email
|
|
353
|
-
// });
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
// ============================================================================
|
|
358
|
-
// Test 7: Complex Output Types
|
|
359
|
-
// ============================================================================
|
|
360
|
-
|
|
361
|
-
type ComplexOutputContract = {
|
|
362
|
-
// Primitive outputs
|
|
363
|
-
getCount: { input: void, output: number },
|
|
364
|
-
getMessage: { input: void, output: string },
|
|
365
|
-
isActive: { input: void, output: boolean },
|
|
366
|
-
|
|
367
|
-
// Nested objects
|
|
368
|
-
getStats: {
|
|
369
|
-
input: void,
|
|
370
|
-
output: {
|
|
371
|
-
users: { total: number, active: number },
|
|
372
|
-
products: { total: number, inStock: number }
|
|
373
|
-
}
|
|
374
|
-
},
|
|
375
|
-
|
|
376
|
-
// Union types
|
|
377
|
-
findUser: {
|
|
378
|
-
input: { email: string },
|
|
379
|
-
output: { id: string, name: string } | null
|
|
380
|
-
},
|
|
381
|
-
|
|
382
|
-
// Array of complex objects
|
|
383
|
-
getOrders: {
|
|
384
|
-
input: { userId: string },
|
|
385
|
-
output: Array<{
|
|
386
|
-
id: string,
|
|
387
|
-
items: Array<{ name: string, price: number }>,
|
|
388
|
-
total: number
|
|
389
|
-
}>
|
|
390
|
-
}
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
describe('Complex Output Types', () => {
|
|
394
|
-
it('should enforce primitive output types', () => {
|
|
395
|
-
const lambder = new Lambder({
|
|
396
|
-
publicPath: './public',
|
|
397
|
-
apiPath: '/api'
|
|
398
|
-
})
|
|
399
|
-
.addApi('getCount', {
|
|
400
|
-
input: z.void(),
|
|
401
|
-
output: z.number()
|
|
402
|
-
}, async (ctx, resolver) => {
|
|
403
|
-
return resolver.api(42);
|
|
404
|
-
})
|
|
405
|
-
.addApi('getMessage', {
|
|
406
|
-
input: z.void(),
|
|
407
|
-
output: z.string()
|
|
408
|
-
}, async (ctx, resolver) => {
|
|
409
|
-
return resolver.api("Hello World");
|
|
410
|
-
})
|
|
411
|
-
.addApi('isActive', {
|
|
412
|
-
input: z.void(),
|
|
413
|
-
output: z.boolean()
|
|
414
|
-
}, async (ctx, resolver) => {
|
|
415
|
-
return resolver.api(true);
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
expect(lambder).toBeDefined();
|
|
419
|
-
|
|
420
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
421
|
-
// lambder.addApi('getCount', async (ctx, resolver) => {
|
|
422
|
-
// return resolver.api("42"); // Error: string not assignable to number
|
|
423
|
-
// });
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
it('should enforce nested object types', () => {
|
|
427
|
-
const lambder = new Lambder({
|
|
428
|
-
publicPath: './public',
|
|
429
|
-
apiPath: '/api'
|
|
430
|
-
})
|
|
431
|
-
.addApi('getStats', {
|
|
432
|
-
input: z.void(),
|
|
433
|
-
output: z.object({
|
|
434
|
-
users: z.object({ total: z.number(), active: z.number() }),
|
|
435
|
-
products: z.object({ total: z.number(), inStock: z.number() })
|
|
436
|
-
})
|
|
437
|
-
}, async (ctx, resolver) => {
|
|
438
|
-
return resolver.api({
|
|
439
|
-
users: { total: 100, active: 75 },
|
|
440
|
-
products: { total: 50, inStock: 40 }
|
|
441
|
-
});
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
expect(lambder).toBeDefined();
|
|
445
|
-
|
|
446
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
447
|
-
// lambder.addApi('getStats', async (ctx, resolver) => {
|
|
448
|
-
// return resolver.api({
|
|
449
|
-
// users: { total: 100 } // Error: missing 'active'
|
|
450
|
-
// });
|
|
451
|
-
// });
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
it('should enforce union types correctly', () => {
|
|
455
|
-
const lambder = new Lambder({
|
|
456
|
-
publicPath: './public',
|
|
457
|
-
apiPath: '/api'
|
|
458
|
-
})
|
|
459
|
-
.addApi('findUser', {
|
|
460
|
-
input: z.object({ email: z.string() }),
|
|
461
|
-
output: z.object({ id: z.string(), name: z.string() }).nullable()
|
|
462
|
-
}, async (ctx, resolver) => {
|
|
463
|
-
return resolver.api({ id: '123', name: 'John' });
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
expect(lambder).toBeDefined();
|
|
467
|
-
|
|
468
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
469
|
-
// lambder.addApi('findUser', async (ctx, resolver) => {
|
|
470
|
-
// return resolver.api(undefined); // Error: undefined not in union
|
|
471
|
-
// });
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
it('should enforce complex nested array types', () => {
|
|
475
|
-
const lambder = new Lambder({
|
|
476
|
-
publicPath: './public',
|
|
477
|
-
apiPath: '/api'
|
|
478
|
-
})
|
|
479
|
-
.addApi('getOrders', {
|
|
480
|
-
input: z.object({ userId: z.string() }),
|
|
481
|
-
output: z.array(z.object({
|
|
482
|
-
id: z.string(),
|
|
483
|
-
items: z.array(z.object({ name: z.string(), price: z.number() })),
|
|
484
|
-
total: z.number()
|
|
485
|
-
}))
|
|
486
|
-
}, async (ctx, resolver) => {
|
|
487
|
-
return resolver.api([
|
|
488
|
-
{
|
|
489
|
-
id: '1',
|
|
490
|
-
items: [
|
|
491
|
-
{ name: 'Item 1', price: 10 },
|
|
492
|
-
{ name: 'Item 2', price: 20 }
|
|
493
|
-
],
|
|
494
|
-
total: 30
|
|
495
|
-
}
|
|
496
|
-
]);
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
expect(lambder).toBeDefined();
|
|
500
|
-
|
|
501
|
-
// ❌ These would cause TypeScript errors (commented out):
|
|
502
|
-
// lambder.addApi('getOrders', async (ctx, resolver) => {
|
|
503
|
-
// return resolver.api([
|
|
504
|
-
// {
|
|
505
|
-
// id: '1',
|
|
506
|
-
// items: [{ name: 'Item 1' }], // Error: missing 'price'
|
|
507
|
-
// total: 30
|
|
508
|
-
// }
|
|
509
|
-
// ]);
|
|
510
|
-
// });
|
|
511
|
-
});
|
|
512
|
-
});
|
|
513
|
-
|
|
514
|
-
// ============================================================================
|
|
515
|
-
// Test 8: Type Inference
|
|
516
|
-
// ============================================================================
|
|
517
|
-
|
|
518
|
-
describe('Type Inference', () => {
|
|
519
|
-
it('should infer types correctly from contract', () => {
|
|
520
|
-
const lambder = new Lambder({
|
|
521
|
-
publicPath: './public',
|
|
522
|
-
apiPath: '/api'
|
|
523
|
-
})
|
|
524
|
-
.addApi('getUser', {
|
|
525
|
-
input: z.object({ userId: z.string() }),
|
|
526
|
-
output: z.object({ id: z.string(), name: z.string() })
|
|
527
|
-
}, async (ctx, resolver) => {
|
|
528
|
-
// ctx.apiPayload type should be inferred as { userId: string }
|
|
529
|
-
const userId: string = ctx.apiPayload.userId;
|
|
530
|
-
|
|
531
|
-
// Variable with explicit type matching contract
|
|
532
|
-
const user: { id: string, name: string } = {
|
|
533
|
-
id: userId,
|
|
534
|
-
name: 'Test User'
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
// Should accept the correctly typed variable
|
|
538
|
-
return resolver.api(user);
|
|
539
|
-
})
|
|
540
|
-
.addApi('createUser', {
|
|
541
|
-
input: z.object({ name: z.string(), email: z.string() }),
|
|
542
|
-
output: z.object({ id: z.string(), name: z.string(), email: z.string() })
|
|
543
|
-
}, async (ctx, resolver) => {
|
|
544
|
-
// Input type inference
|
|
545
|
-
const name: string = ctx.apiPayload.name;
|
|
546
|
-
const email: string = ctx.apiPayload.email;
|
|
547
|
-
|
|
548
|
-
// Output type inference
|
|
549
|
-
const result: { id: string, name: string, email: string } = {
|
|
550
|
-
id: '123',
|
|
551
|
-
name,
|
|
552
|
-
email
|
|
553
|
-
};
|
|
554
|
-
|
|
555
|
-
return resolver.api(result);
|
|
556
|
-
});
|
|
557
|
-
|
|
558
|
-
expect(lambder).toBeDefined();
|
|
559
|
-
});
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
// ============================================================================
|
|
563
|
-
// If this file compiles without errors, the type system is working! ✅
|
|
564
|
-
// ============================================================================
|