lambder 2.0.11 → 2.0.13
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/LambderResolver.d.ts +1 -1
- package/dist/LambderResolver.js +1 -1
- package/dist/LambderResponseBuilder.d.ts +3 -2
- package/dist/LambderResponseBuilder.js +2 -2
- package/package.json +1 -1
- package/src/LambderResolver.ts +2 -2
- package/src/LambderResponseBuilder.ts +10 -3
- package/tests/UNTESTED_FEATURES.md +1 -1
- package/tests/redirect.test.ts +88 -0
- package/tests/type-safety.test.ts +0 -564
|
@@ -7,7 +7,7 @@ interface DieResolverMethods<TOutput> {
|
|
|
7
7
|
json: MethodType<LambderResponseBuilder, 'json'>;
|
|
8
8
|
xml: MethodType<LambderResponseBuilder, 'xml'>;
|
|
9
9
|
html: MethodType<LambderResponseBuilder, 'html'>;
|
|
10
|
-
|
|
10
|
+
redirect: MethodType<LambderResponseBuilder, 'redirect'>;
|
|
11
11
|
status404: MethodType<LambderResponseBuilder, 'status404'>;
|
|
12
12
|
cors: MethodType<LambderResponseBuilder, 'cors'>;
|
|
13
13
|
fileBase64: MethodType<LambderResponseBuilder, 'fileBase64'>;
|
package/dist/LambderResolver.js
CHANGED
|
@@ -12,7 +12,7 @@ export default class LambderResolver extends LambderResponseBuilder {
|
|
|
12
12
|
json: this.autoResolve(this.json),
|
|
13
13
|
xml: this.autoResolve(this.xml),
|
|
14
14
|
html: this.autoResolve(this.html),
|
|
15
|
-
|
|
15
|
+
redirect: this.autoResolve(this.redirect),
|
|
16
16
|
status404: this.autoResolve(this.status404),
|
|
17
17
|
cors: this.autoResolve(this.cors),
|
|
18
18
|
fileBase64: this.autoResolve(this.fileBase64),
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import LambderUtils from "./LambderUtils.js";
|
|
2
2
|
import { LambderRenderContext } from "./LambderContext.js";
|
|
3
|
+
export type HttpStatusCode = 200 | 201 | 202 | 204 | 300 | 301 | 302 | 303 | 304 | 307 | 308 | 400 | 401 | 403 | 404 | 405 | 409 | 410 | 413 | 415 | 422 | 429 | 500 | 501 | 502 | 503 | 504;
|
|
3
4
|
export type LambderResolverResponse = {
|
|
4
|
-
statusCode:
|
|
5
|
+
statusCode: HttpStatusCode;
|
|
5
6
|
multiValueHeaders?: Record<string, string[]>;
|
|
6
7
|
body: string | null;
|
|
7
8
|
isBase64Encoded?: boolean;
|
|
@@ -39,7 +40,7 @@ export default class LambderResponseBuilder<TResponse = any> {
|
|
|
39
40
|
json(data: Record<string, any>, headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
40
41
|
xml(data: string): LambderResolverResponse;
|
|
41
42
|
html(data: string, headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
42
|
-
|
|
43
|
+
redirect(url: string, statusCode?: HttpStatusCode, headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
43
44
|
status404(data: string, headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
44
45
|
versionExpired(headers?: Record<string, string | string[]>): LambderResolverResponse;
|
|
45
46
|
cors(): LambderResolverResponse;
|
|
@@ -116,9 +116,9 @@ export default class LambderResponseBuilder {
|
|
|
116
116
|
});
|
|
117
117
|
}
|
|
118
118
|
;
|
|
119
|
-
|
|
119
|
+
redirect(url, statusCode = 302, headers) {
|
|
120
120
|
return this.raw({
|
|
121
|
-
statusCode:
|
|
121
|
+
statusCode: statusCode,
|
|
122
122
|
multiValueHeaders: { "Location": [url], ...convertToMultiHeader(headers) },
|
|
123
123
|
body: null
|
|
124
124
|
});
|
package/package.json
CHANGED
package/src/LambderResolver.ts
CHANGED
|
@@ -9,7 +9,7 @@ interface DieResolverMethods<TOutput> {
|
|
|
9
9
|
json: MethodType<LambderResponseBuilder, 'json'>;
|
|
10
10
|
xml: MethodType<LambderResponseBuilder, 'xml'>;
|
|
11
11
|
html: MethodType<LambderResponseBuilder, 'html'>;
|
|
12
|
-
|
|
12
|
+
redirect: MethodType<LambderResponseBuilder, 'redirect'>;
|
|
13
13
|
status404: MethodType<LambderResponseBuilder, 'status404'>;
|
|
14
14
|
cors: MethodType<LambderResponseBuilder, 'cors'>;
|
|
15
15
|
fileBase64: MethodType<LambderResponseBuilder, 'fileBase64'>;
|
|
@@ -49,7 +49,7 @@ export default class LambderResolver<TOutput = any> extends LambderResponseBuild
|
|
|
49
49
|
json: this.autoResolve(this.json),
|
|
50
50
|
xml: this.autoResolve(this.xml),
|
|
51
51
|
html: this.autoResolve(this.html),
|
|
52
|
-
|
|
52
|
+
redirect: this.autoResolve(this.redirect),
|
|
53
53
|
status404: this.autoResolve(this.status404),
|
|
54
54
|
cors: this.autoResolve(this.cors),
|
|
55
55
|
fileBase64: this.autoResolve(this.fileBase64),
|
|
@@ -15,8 +15,14 @@ const CORS_HEADERS = convertToMultiHeader({
|
|
|
15
15
|
"Access-Control-Allow-Methods": "OPTIONS,POST",
|
|
16
16
|
});
|
|
17
17
|
|
|
18
|
+
export type HttpStatusCode =
|
|
19
|
+
| 200 | 201 | 202 | 204
|
|
20
|
+
| 300 | 301 | 302 | 303 | 304 | 307 | 308
|
|
21
|
+
| 400 | 401 | 403 | 404 | 405 | 409 | 410 | 413 | 415 | 422 | 429
|
|
22
|
+
| 500 | 501 | 502 | 503 | 504;
|
|
23
|
+
|
|
18
24
|
export type LambderResolverResponse = {
|
|
19
|
-
statusCode:
|
|
25
|
+
statusCode: HttpStatusCode,
|
|
20
26
|
multiValueHeaders?: Record<string, string[]>,
|
|
21
27
|
body: string | null,
|
|
22
28
|
isBase64Encoded?: boolean,
|
|
@@ -157,12 +163,13 @@ export default class LambderResponseBuilder<TResponse = any> {
|
|
|
157
163
|
});
|
|
158
164
|
};
|
|
159
165
|
|
|
160
|
-
|
|
166
|
+
redirect(
|
|
161
167
|
url: string,
|
|
168
|
+
statusCode: HttpStatusCode = 302,
|
|
162
169
|
headers?: Record<string, string|string[]>,
|
|
163
170
|
):LambderResolverResponse{
|
|
164
171
|
return this.raw({
|
|
165
|
-
statusCode:
|
|
172
|
+
statusCode: statusCode,
|
|
166
173
|
multiValueHeaders: { "Location" : [url], ...convertToMultiHeader(headers) },
|
|
167
174
|
body:null
|
|
168
175
|
});
|
|
@@ -98,7 +98,7 @@ Based on existing test files:
|
|
|
98
98
|
- [ ] `res.fileBase64()`
|
|
99
99
|
- [ ] `res.ejsFile()`
|
|
100
100
|
- [ ] `res.ejsTemplate()`
|
|
101
|
-
- [ ] `res.
|
|
101
|
+
- [ ] `res.redirect()` (redirects)
|
|
102
102
|
- [ ] `res.status404()`
|
|
103
103
|
- [ ] `res.cors()`
|
|
104
104
|
- [ ] `res.die.*` methods (skip afterRender hooks)
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import Lambder from '../src/Lambder.js';
|
|
3
|
+
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
|
|
4
|
+
|
|
5
|
+
const createMockEvent = (path: string, method: string = 'GET'): APIGatewayProxyEvent => ({
|
|
6
|
+
body: null,
|
|
7
|
+
headers: {
|
|
8
|
+
Host: 'localhost',
|
|
9
|
+
},
|
|
10
|
+
multiValueHeaders: {},
|
|
11
|
+
httpMethod: method,
|
|
12
|
+
isBase64Encoded: false,
|
|
13
|
+
path,
|
|
14
|
+
pathParameters: null,
|
|
15
|
+
queryStringParameters: null,
|
|
16
|
+
multiValueQueryStringParameters: null,
|
|
17
|
+
stageVariables: null,
|
|
18
|
+
requestContext: {} as any,
|
|
19
|
+
resource: '',
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const createMockContext = (): Context => ({
|
|
23
|
+
callbackWaitsForEmptyEventLoop: false,
|
|
24
|
+
functionName: 'test',
|
|
25
|
+
functionVersion: '1',
|
|
26
|
+
invokedFunctionArn: 'arn',
|
|
27
|
+
memoryLimitInMB: '128',
|
|
28
|
+
awsRequestId: '123',
|
|
29
|
+
logGroupName: 'group',
|
|
30
|
+
logStreamName: 'stream',
|
|
31
|
+
getRemainingTimeInMillis: () => 1000,
|
|
32
|
+
done: () => {},
|
|
33
|
+
fail: () => {},
|
|
34
|
+
succeed: () => {},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('Redirect Response', () => {
|
|
38
|
+
it('should redirect with default status code 302', async () => {
|
|
39
|
+
const lambder = new Lambder({
|
|
40
|
+
publicPath: './public',
|
|
41
|
+
apiPath: '/api'
|
|
42
|
+
})
|
|
43
|
+
.addRoute('/old-path', (ctx, res) => {
|
|
44
|
+
return res.redirect('/new-path');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const handler = lambder.getHandler();
|
|
48
|
+
const event = createMockEvent('/old-path');
|
|
49
|
+
const result = await handler(event, createMockContext());
|
|
50
|
+
|
|
51
|
+
expect(result.statusCode).toBe(302);
|
|
52
|
+
expect(result.multiValueHeaders?.Location).toEqual(['/new-path']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should redirect with custom status code', async () => {
|
|
56
|
+
const lambder = new Lambder({
|
|
57
|
+
publicPath: './public',
|
|
58
|
+
apiPath: '/api'
|
|
59
|
+
})
|
|
60
|
+
.addRoute('/moved-permanently', (ctx, res) => {
|
|
61
|
+
return res.redirect('/new-location', 301);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const handler = lambder.getHandler();
|
|
65
|
+
const event = createMockEvent('/moved-permanently');
|
|
66
|
+
const result = await handler(event, createMockContext());
|
|
67
|
+
|
|
68
|
+
expect(result.statusCode).toBe(301);
|
|
69
|
+
expect(result.multiValueHeaders?.Location).toEqual(['/new-location']);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should redirect using die.redirect', async () => {
|
|
73
|
+
const lambder = new Lambder({
|
|
74
|
+
publicPath: './public',
|
|
75
|
+
apiPath: '/api'
|
|
76
|
+
})
|
|
77
|
+
.addRoute('/die-redirect', (ctx, res) => {
|
|
78
|
+
return res.die.redirect('/somewhere-else');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const handler = lambder.getHandler();
|
|
82
|
+
const event = createMockEvent('/die-redirect');
|
|
83
|
+
const result = await handler(event, createMockContext());
|
|
84
|
+
|
|
85
|
+
expect(result.statusCode).toBe(302);
|
|
86
|
+
expect(result.multiValueHeaders?.Location).toEqual(['/somewhere-else']);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -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
|
-
// ============================================================================
|