@mandujs/core 0.18.3 → 0.18.6
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 +8 -2
- package/src/bundler/build.ts +53 -16
- package/src/bundler/css.ts +337 -302
- package/src/bundler/dev.ts +63 -6
- package/src/bundler/types.ts +6 -0
- package/src/config/mandu.ts +1 -1
- package/src/config/validate.ts +1 -1
- package/src/contract/registry.ts +591 -568
- package/src/resource/generator.ts +5 -4
- package/src/router/fs-scanner.ts +4 -4
- package/src/runtime/escape.ts +12 -0
- package/src/runtime/server.ts +1 -1
- package/src/runtime/ssr.ts +27 -10
- package/src/runtime/streaming-ssr.ts +34 -7
package/src/contract/registry.ts
CHANGED
|
@@ -1,568 +1,591 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mandu Contract Registry
|
|
3
|
-
* Build-time contract index for tooling, docs, and guard
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { RoutesManifest } from "../spec/schema";
|
|
7
|
-
import type { ContractSchema, MethodRequestSchema } from "./schema";
|
|
8
|
-
import path from "path";
|
|
9
|
-
import { createHash } from "crypto";
|
|
10
|
-
|
|
11
|
-
const HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const;
|
|
12
|
-
|
|
13
|
-
export interface ContractRegistryEntry {
|
|
14
|
-
id: string;
|
|
15
|
-
routeId: string;
|
|
16
|
-
file: string;
|
|
17
|
-
methods: string[];
|
|
18
|
-
request: Record<string, {
|
|
19
|
-
query: boolean;
|
|
20
|
-
body: boolean;
|
|
21
|
-
params: boolean;
|
|
22
|
-
headers: boolean;
|
|
23
|
-
}>;
|
|
24
|
-
response: number[];
|
|
25
|
-
schemas?: {
|
|
26
|
-
request?: Record<string, {
|
|
27
|
-
query?: SchemaSummary;
|
|
28
|
-
body?: SchemaSummary;
|
|
29
|
-
params?: SchemaSummary;
|
|
30
|
-
headers?: SchemaSummary;
|
|
31
|
-
}>;
|
|
32
|
-
response?: Record<number, SchemaSummary | undefined>;
|
|
33
|
-
};
|
|
34
|
-
hash: string | null;
|
|
35
|
-
description?: string;
|
|
36
|
-
tags?: string[];
|
|
37
|
-
normalize?: ContractSchema["normalize"];
|
|
38
|
-
coerceQueryParams?: ContractSchema["coerceQueryParams"];
|
|
39
|
-
version?: string;
|
|
40
|
-
meta?: Record<string, unknown>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export type SchemaSummary =
|
|
44
|
-
| {
|
|
45
|
-
type: "object";
|
|
46
|
-
keys: string[];
|
|
47
|
-
required: string[];
|
|
48
|
-
}
|
|
49
|
-
| {
|
|
50
|
-
type: "enum";
|
|
51
|
-
values: Array<string | number>;
|
|
52
|
-
}
|
|
53
|
-
| {
|
|
54
|
-
type: "literal";
|
|
55
|
-
value: string | number | boolean | null;
|
|
56
|
-
}
|
|
57
|
-
| {
|
|
58
|
-
type: "other";
|
|
59
|
-
typeName?: string;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export interface ContractRegistry {
|
|
63
|
-
version: 1;
|
|
64
|
-
generatedAt: string;
|
|
65
|
-
contracts: ContractRegistryEntry[];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface ContractRegistryResult {
|
|
69
|
-
registry: ContractRegistry;
|
|
70
|
-
warnings: string[];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface ContractRegistryChange {
|
|
74
|
-
id: string;
|
|
75
|
-
routeId: string;
|
|
76
|
-
severity: "major" | "minor" | "patch";
|
|
77
|
-
changes: string[];
|
|
78
|
-
before?: ContractRegistryEntry;
|
|
79
|
-
after?: ContractRegistryEntry;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface ContractRegistryDiff {
|
|
83
|
-
added: ContractRegistryEntry[];
|
|
84
|
-
removed: ContractRegistryEntry[];
|
|
85
|
-
changed: ContractRegistryChange[];
|
|
86
|
-
summary: {
|
|
87
|
-
major: number;
|
|
88
|
-
minor: number;
|
|
89
|
-
patch: number;
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
request
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
):
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
next
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
if (
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
const
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
changes
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
if (
|
|
557
|
-
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Mandu Contract Registry
|
|
3
|
+
* Build-time contract index for tooling, docs, and guard
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { RoutesManifest } from "../spec/schema";
|
|
7
|
+
import type { ContractSchema, MethodRequestSchema } from "./schema";
|
|
8
|
+
import path from "path";
|
|
9
|
+
import { createHash } from "crypto";
|
|
10
|
+
|
|
11
|
+
const HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const;
|
|
12
|
+
|
|
13
|
+
export interface ContractRegistryEntry {
|
|
14
|
+
id: string;
|
|
15
|
+
routeId: string;
|
|
16
|
+
file: string;
|
|
17
|
+
methods: string[];
|
|
18
|
+
request: Record<string, {
|
|
19
|
+
query: boolean;
|
|
20
|
+
body: boolean;
|
|
21
|
+
params: boolean;
|
|
22
|
+
headers: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
response: number[];
|
|
25
|
+
schemas?: {
|
|
26
|
+
request?: Record<string, {
|
|
27
|
+
query?: SchemaSummary;
|
|
28
|
+
body?: SchemaSummary;
|
|
29
|
+
params?: SchemaSummary;
|
|
30
|
+
headers?: SchemaSummary;
|
|
31
|
+
}>;
|
|
32
|
+
response?: Record<number, SchemaSummary | undefined>;
|
|
33
|
+
};
|
|
34
|
+
hash: string | null;
|
|
35
|
+
description?: string;
|
|
36
|
+
tags?: string[];
|
|
37
|
+
normalize?: ContractSchema["normalize"];
|
|
38
|
+
coerceQueryParams?: ContractSchema["coerceQueryParams"];
|
|
39
|
+
version?: string;
|
|
40
|
+
meta?: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type SchemaSummary =
|
|
44
|
+
| {
|
|
45
|
+
type: "object";
|
|
46
|
+
keys: string[];
|
|
47
|
+
required: string[];
|
|
48
|
+
}
|
|
49
|
+
| {
|
|
50
|
+
type: "enum";
|
|
51
|
+
values: Array<string | number>;
|
|
52
|
+
}
|
|
53
|
+
| {
|
|
54
|
+
type: "literal";
|
|
55
|
+
value: string | number | boolean | null;
|
|
56
|
+
}
|
|
57
|
+
| {
|
|
58
|
+
type: "other";
|
|
59
|
+
typeName?: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export interface ContractRegistry {
|
|
63
|
+
version: 1;
|
|
64
|
+
generatedAt: string;
|
|
65
|
+
contracts: ContractRegistryEntry[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ContractRegistryResult {
|
|
69
|
+
registry: ContractRegistry;
|
|
70
|
+
warnings: string[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ContractRegistryChange {
|
|
74
|
+
id: string;
|
|
75
|
+
routeId: string;
|
|
76
|
+
severity: "major" | "minor" | "patch";
|
|
77
|
+
changes: string[];
|
|
78
|
+
before?: ContractRegistryEntry;
|
|
79
|
+
after?: ContractRegistryEntry;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface ContractRegistryDiff {
|
|
83
|
+
added: ContractRegistryEntry[];
|
|
84
|
+
removed: ContractRegistryEntry[];
|
|
85
|
+
changed: ContractRegistryChange[];
|
|
86
|
+
summary: {
|
|
87
|
+
major: number;
|
|
88
|
+
minor: number;
|
|
89
|
+
patch: number;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Zod 내부 구조를 duck typing으로 접근하기 위한 인터페이스 */
|
|
94
|
+
interface ZodLike {
|
|
95
|
+
_def?: {
|
|
96
|
+
typeName?: string;
|
|
97
|
+
innerType?: ZodLike;
|
|
98
|
+
schema?: ZodLike;
|
|
99
|
+
type?: ZodLike;
|
|
100
|
+
shape?: (() => Record<string, ZodLike>) | Record<string, ZodLike>;
|
|
101
|
+
values?: unknown[] | Record<string, unknown>;
|
|
102
|
+
value?: unknown;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function loadContract(contractPath: string, rootDir: string): Promise<ContractSchema | null> {
|
|
107
|
+
try {
|
|
108
|
+
const fullPath = path.join(rootDir, contractPath);
|
|
109
|
+
const module = await import(fullPath);
|
|
110
|
+
return module.default ?? null;
|
|
111
|
+
} catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function computeFileHash(filePath: string): Promise<string | null> {
|
|
117
|
+
try {
|
|
118
|
+
const content = await Bun.file(filePath).text();
|
|
119
|
+
return createHash("sha256").update(content).digest("hex");
|
|
120
|
+
} catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function extractRequestInfo(contract: ContractSchema): ContractRegistryEntry["request"] {
|
|
126
|
+
const requestInfo: ContractRegistryEntry["request"] = {};
|
|
127
|
+
|
|
128
|
+
for (const method of HTTP_METHODS) {
|
|
129
|
+
const methodSchema = contract.request[method] as MethodRequestSchema | undefined;
|
|
130
|
+
if (!methodSchema) continue;
|
|
131
|
+
|
|
132
|
+
requestInfo[method] = {
|
|
133
|
+
query: Boolean(methodSchema.query),
|
|
134
|
+
body: Boolean(methodSchema.body),
|
|
135
|
+
params: Boolean(methodSchema.params),
|
|
136
|
+
headers: Boolean(methodSchema.headers),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return requestInfo;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function unwrapSchema(schema: ZodLike): ZodLike {
|
|
144
|
+
let current = schema;
|
|
145
|
+
let depth = 0;
|
|
146
|
+
|
|
147
|
+
while (current && current._def && depth < 10) {
|
|
148
|
+
const typeName = current._def.typeName;
|
|
149
|
+
if (typeName === "ZodOptional" || typeName === "ZodDefault") {
|
|
150
|
+
current = current._def.innerType ?? current;
|
|
151
|
+
} else if (typeName === "ZodEffects") {
|
|
152
|
+
current = current._def.schema ?? current;
|
|
153
|
+
} else if (typeName === "ZodNullable") {
|
|
154
|
+
current = current._def.innerType ?? current;
|
|
155
|
+
} else if (typeName === "ZodBranded" || typeName === "ZodCatch") {
|
|
156
|
+
current = current._def.type ?? current;
|
|
157
|
+
} else {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
depth += 1;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return current;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function isOptionalSchema(schema: ZodLike): boolean {
|
|
167
|
+
let current = schema;
|
|
168
|
+
let depth = 0;
|
|
169
|
+
while (current && current._def && depth < 10) {
|
|
170
|
+
const typeName = current._def.typeName;
|
|
171
|
+
if (typeName === "ZodOptional" || typeName === "ZodDefault") {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
if (typeName === "ZodEffects" || typeName === "ZodNullable" || typeName === "ZodBranded" || typeName === "ZodCatch") {
|
|
175
|
+
current = current._def.schema ?? current._def.innerType ?? current._def.type ?? current;
|
|
176
|
+
depth += 1;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function summarizeSchema(schema: ZodLike): SchemaSummary | undefined {
|
|
185
|
+
if (!schema || !schema._def) return undefined;
|
|
186
|
+
|
|
187
|
+
const base = unwrapSchema(schema);
|
|
188
|
+
const def = base?._def;
|
|
189
|
+
if (!def) return undefined;
|
|
190
|
+
|
|
191
|
+
const typeName = def.typeName;
|
|
192
|
+
|
|
193
|
+
if (typeName === "ZodObject") {
|
|
194
|
+
const shape = typeof def.shape === "function" ? def.shape() : (def.shape as Record<string, ZodLike> | undefined);
|
|
195
|
+
const keys = Object.keys(shape ?? {}).sort();
|
|
196
|
+
const required = keys.filter((key) => !isOptionalSchema((shape ?? {})[key]));
|
|
197
|
+
return {
|
|
198
|
+
type: "object",
|
|
199
|
+
keys,
|
|
200
|
+
required: required.sort(),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (typeName === "ZodEnum") {
|
|
205
|
+
const values = Array.isArray(def.values) ? def.values.slice() : [];
|
|
206
|
+
values.sort();
|
|
207
|
+
return {
|
|
208
|
+
type: "enum",
|
|
209
|
+
values: values as Array<string | number>,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (typeName === "ZodNativeEnum") {
|
|
214
|
+
const rawValues = def.values && !Array.isArray(def.values) ? Object.values(def.values) : [];
|
|
215
|
+
const values = rawValues.filter((v: unknown) => typeof v === "string" || typeof v === "number") as Array<string | number>;
|
|
216
|
+
values.sort();
|
|
217
|
+
return {
|
|
218
|
+
type: "enum",
|
|
219
|
+
values,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (typeName === "ZodLiteral") {
|
|
224
|
+
return {
|
|
225
|
+
type: "literal",
|
|
226
|
+
value: def.value as string | number | boolean | null,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
type: "other",
|
|
232
|
+
typeName,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function extractSchemaSummaries(contract: ContractSchema): ContractRegistryEntry["schemas"] {
|
|
237
|
+
const request: Record<string, {
|
|
238
|
+
query?: SchemaSummary;
|
|
239
|
+
body?: SchemaSummary;
|
|
240
|
+
params?: SchemaSummary;
|
|
241
|
+
headers?: SchemaSummary;
|
|
242
|
+
}> = {};
|
|
243
|
+
const response: Record<number, SchemaSummary> = {};
|
|
244
|
+
|
|
245
|
+
for (const method of HTTP_METHODS) {
|
|
246
|
+
const methodSchema = contract.request[method] as MethodRequestSchema | undefined;
|
|
247
|
+
if (!methodSchema) continue;
|
|
248
|
+
|
|
249
|
+
request[method] = {
|
|
250
|
+
query: methodSchema.query ? summarizeSchema(methodSchema.query as ZodLike) : undefined,
|
|
251
|
+
body: methodSchema.body ? summarizeSchema(methodSchema.body as ZodLike) : undefined,
|
|
252
|
+
params: methodSchema.params ? summarizeSchema(methodSchema.params as ZodLike) : undefined,
|
|
253
|
+
headers: methodSchema.headers ? summarizeSchema(methodSchema.headers as ZodLike) : undefined,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
for (const [statusCode, schema] of Object.entries(contract.response)) {
|
|
258
|
+
const code = Number(statusCode);
|
|
259
|
+
if (Number.isNaN(code)) continue;
|
|
260
|
+
if (!schema) continue;
|
|
261
|
+
|
|
262
|
+
// 일부 핸들러는 응답 스키마를 { schema: ZodSchema } 형태로 래핑해서 반환함
|
|
263
|
+
// summarizeSchema는 원시 Zod 스키마를 기대하므로 내부 schema 필드 언래핑
|
|
264
|
+
const actualSchema = (schema as { schema?: unknown }).schema ?? schema;
|
|
265
|
+
const summary = summarizeSchema(actualSchema as ZodLike);
|
|
266
|
+
if (summary) {
|
|
267
|
+
response[code] = summary;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
request,
|
|
273
|
+
response,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function extractMethods(contract: ContractSchema): string[] {
|
|
278
|
+
return HTTP_METHODS.filter((method) => Boolean(contract.request[method]));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function extractResponseCodes(contract: ContractSchema): number[] {
|
|
282
|
+
return Object.keys(contract.response)
|
|
283
|
+
.filter((key) => /^\d+$/.test(key))
|
|
284
|
+
.map((key) => Number(key))
|
|
285
|
+
.sort((a, b) => a - b);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export async function buildContractRegistry(
|
|
289
|
+
manifest: RoutesManifest,
|
|
290
|
+
rootDir: string
|
|
291
|
+
): Promise<ContractRegistryResult> {
|
|
292
|
+
const warnings: string[] = [];
|
|
293
|
+
const contracts: ContractRegistryEntry[] = [];
|
|
294
|
+
|
|
295
|
+
for (const route of manifest.routes) {
|
|
296
|
+
if (!route.contractModule) continue;
|
|
297
|
+
|
|
298
|
+
const contract = await loadContract(route.contractModule, rootDir);
|
|
299
|
+
if (!contract) {
|
|
300
|
+
warnings.push(`Failed to load contract: ${route.contractModule} (routeId: ${route.id})`);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const contractPath = path.join(rootDir, route.contractModule);
|
|
305
|
+
const hash = await computeFileHash(contractPath);
|
|
306
|
+
const id = contract.name ?? route.id;
|
|
307
|
+
|
|
308
|
+
contracts.push({
|
|
309
|
+
id,
|
|
310
|
+
routeId: route.id,
|
|
311
|
+
file: route.contractModule,
|
|
312
|
+
methods: extractMethods(contract),
|
|
313
|
+
request: extractRequestInfo(contract),
|
|
314
|
+
response: extractResponseCodes(contract),
|
|
315
|
+
schemas: extractSchemaSummaries(contract),
|
|
316
|
+
hash,
|
|
317
|
+
description: contract.description,
|
|
318
|
+
tags: contract.tags,
|
|
319
|
+
normalize: contract.normalize,
|
|
320
|
+
coerceQueryParams: contract.coerceQueryParams,
|
|
321
|
+
version: contract.version,
|
|
322
|
+
meta: contract.meta,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
registry: {
|
|
328
|
+
version: 1,
|
|
329
|
+
generatedAt: new Date().toISOString(),
|
|
330
|
+
contracts,
|
|
331
|
+
},
|
|
332
|
+
warnings,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export async function writeContractRegistry(
|
|
337
|
+
registryPath: string,
|
|
338
|
+
registry: ContractRegistry
|
|
339
|
+
): Promise<void> {
|
|
340
|
+
await Bun.write(registryPath, JSON.stringify(registry, null, 2));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export async function readContractRegistry(
|
|
344
|
+
registryPath: string
|
|
345
|
+
): Promise<ContractRegistry | null> {
|
|
346
|
+
try {
|
|
347
|
+
const file = Bun.file(registryPath);
|
|
348
|
+
const exists = await file.exists();
|
|
349
|
+
if (!exists) return null;
|
|
350
|
+
const content = await file.text();
|
|
351
|
+
return JSON.parse(content) as ContractRegistry;
|
|
352
|
+
} catch {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function diffArray<T>(prev: T[], next: T[]): { added: T[]; removed: T[] } {
|
|
358
|
+
const prevSet = new Set(prev);
|
|
359
|
+
const nextSet = new Set(next);
|
|
360
|
+
const added = next.filter((item) => !prevSet.has(item));
|
|
361
|
+
const removed = prev.filter((item) => !nextSet.has(item));
|
|
362
|
+
return { added, removed };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function diffRequestShapes(
|
|
366
|
+
prev: ContractRegistryEntry["request"],
|
|
367
|
+
next: ContractRegistryEntry["request"]
|
|
368
|
+
): { major: string[]; minor: string[] } {
|
|
369
|
+
const major: string[] = [];
|
|
370
|
+
const minor: string[] = [];
|
|
371
|
+
const methods = new Set([...Object.keys(prev), ...Object.keys(next)]);
|
|
372
|
+
|
|
373
|
+
for (const method of methods) {
|
|
374
|
+
const before = prev[method];
|
|
375
|
+
const after = next[method];
|
|
376
|
+
if (!before || !after) continue;
|
|
377
|
+
|
|
378
|
+
for (const key of ["query", "body", "params", "headers"] as const) {
|
|
379
|
+
const beforeHas = Boolean(before[key]);
|
|
380
|
+
const afterHas = Boolean(after[key]);
|
|
381
|
+
if (beforeHas && !afterHas) {
|
|
382
|
+
major.push(`${method}.${key} removed`);
|
|
383
|
+
} else if (!beforeHas && afterHas) {
|
|
384
|
+
minor.push(`${method}.${key} added`);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return { major, minor };
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function diffSchemaSummary(
|
|
393
|
+
prev?: SchemaSummary,
|
|
394
|
+
next?: SchemaSummary
|
|
395
|
+
): { major: string[]; minor: string[] } {
|
|
396
|
+
if (!prev || !next) return { major: [], minor: [] };
|
|
397
|
+
|
|
398
|
+
if (prev.type !== next.type) {
|
|
399
|
+
return {
|
|
400
|
+
major: [`schema type changed: ${prev.type} -> ${next.type}`],
|
|
401
|
+
minor: [],
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (prev.type === "object" && next.type === "object") {
|
|
406
|
+
const prevKeys = new Set(prev.keys);
|
|
407
|
+
const nextKeys = new Set(next.keys);
|
|
408
|
+
|
|
409
|
+
const removed = prev.keys.filter((k) => !nextKeys.has(k));
|
|
410
|
+
const added = next.keys.filter((k) => !prevKeys.has(k));
|
|
411
|
+
|
|
412
|
+
const prevRequired = new Set(prev.required);
|
|
413
|
+
const nextRequired = new Set(next.required);
|
|
414
|
+
|
|
415
|
+
const major: string[] = [];
|
|
416
|
+
const minor: string[] = [];
|
|
417
|
+
|
|
418
|
+
if (removed.length > 0) {
|
|
419
|
+
major.push(`fields removed: ${removed.join(", ")}`);
|
|
420
|
+
}
|
|
421
|
+
if (added.length > 0) {
|
|
422
|
+
minor.push(`fields added: ${added.join(", ")}`);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
for (const key of prev.keys) {
|
|
426
|
+
if (!nextKeys.has(key)) continue;
|
|
427
|
+
const wasRequired = prevRequired.has(key);
|
|
428
|
+
const nowRequired = nextRequired.has(key);
|
|
429
|
+
if (wasRequired && !nowRequired) {
|
|
430
|
+
minor.push(`field optionalized: ${key}`);
|
|
431
|
+
} else if (!wasRequired && nowRequired) {
|
|
432
|
+
major.push(`field required: ${key}`);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return { major, minor };
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (prev.type === "enum" && next.type === "enum") {
|
|
440
|
+
const prevValues = new Set(prev.values.map(String));
|
|
441
|
+
const nextValues = new Set(next.values.map(String));
|
|
442
|
+
const removed = prev.values.filter((v) => !nextValues.has(String(v)));
|
|
443
|
+
const added = next.values.filter((v) => !prevValues.has(String(v)));
|
|
444
|
+
return {
|
|
445
|
+
major: removed.length > 0 ? [`enum values removed: ${removed.join(", ")}`] : [],
|
|
446
|
+
minor: added.length > 0 ? [`enum values added: ${added.join(", ")}`] : [],
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (prev.type === "literal" && next.type === "literal") {
|
|
451
|
+
if (prev.value !== next.value) {
|
|
452
|
+
return { major: [`literal changed: ${String(prev.value)} -> ${String(next.value)}`], minor: [] };
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (prev.type === "other" && next.type === "other") {
|
|
457
|
+
if (prev.typeName !== next.typeName) {
|
|
458
|
+
return { major: [`schema changed: ${prev.typeName ?? "unknown"} -> ${next.typeName ?? "unknown"}`], minor: [] };
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return { major: [], minor: [] };
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
export function diffContractRegistry(
|
|
466
|
+
prev: ContractRegistry,
|
|
467
|
+
next: ContractRegistry
|
|
468
|
+
): ContractRegistryDiff {
|
|
469
|
+
const prevMap = new Map(prev.contracts.map((c) => [c.id, c]));
|
|
470
|
+
const nextMap = new Map(next.contracts.map((c) => [c.id, c]));
|
|
471
|
+
|
|
472
|
+
const added: ContractRegistryEntry[] = [];
|
|
473
|
+
const removed: ContractRegistryEntry[] = [];
|
|
474
|
+
const changed: ContractRegistryChange[] = [];
|
|
475
|
+
|
|
476
|
+
for (const [id, nextEntry] of nextMap.entries()) {
|
|
477
|
+
const prevEntry = prevMap.get(id);
|
|
478
|
+
if (!prevEntry) {
|
|
479
|
+
added.push(nextEntry);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const changes: string[] = [];
|
|
484
|
+
let severity: ContractRegistryChange["severity"] = "patch";
|
|
485
|
+
|
|
486
|
+
const methodDiff = diffArray(prevEntry.methods, nextEntry.methods);
|
|
487
|
+
if (methodDiff.removed.length > 0) {
|
|
488
|
+
changes.push(`methods removed: ${methodDiff.removed.join(", ")}`);
|
|
489
|
+
severity = "major";
|
|
490
|
+
}
|
|
491
|
+
if (methodDiff.added.length > 0) {
|
|
492
|
+
changes.push(`methods added: ${methodDiff.added.join(", ")}`);
|
|
493
|
+
if (severity !== "major") severity = "minor";
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const responseDiff = diffArray(prevEntry.response, nextEntry.response);
|
|
497
|
+
if (responseDiff.removed.length > 0) {
|
|
498
|
+
changes.push(`responses removed: ${responseDiff.removed.join(", ")}`);
|
|
499
|
+
severity = "major";
|
|
500
|
+
}
|
|
501
|
+
if (responseDiff.added.length > 0) {
|
|
502
|
+
changes.push(`responses added: ${responseDiff.added.join(", ")}`);
|
|
503
|
+
if (severity !== "major") severity = "minor";
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const requestDiff = diffRequestShapes(prevEntry.request, nextEntry.request);
|
|
507
|
+
if (requestDiff.major.length > 0) {
|
|
508
|
+
changes.push(...requestDiff.major);
|
|
509
|
+
severity = "major";
|
|
510
|
+
}
|
|
511
|
+
if (requestDiff.minor.length > 0) {
|
|
512
|
+
changes.push(...requestDiff.minor);
|
|
513
|
+
if (severity !== "major") severity = "minor";
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const prevReqSchemas = prevEntry.schemas?.request ?? {};
|
|
517
|
+
const nextReqSchemas = nextEntry.schemas?.request ?? {};
|
|
518
|
+
for (const method of Object.keys(prevReqSchemas)) {
|
|
519
|
+
if (!nextReqSchemas[method]) continue;
|
|
520
|
+
const prevParts = prevReqSchemas[method] ?? {};
|
|
521
|
+
const nextParts = nextReqSchemas[method] ?? {};
|
|
522
|
+
for (const part of ["query", "body", "params", "headers"] as const) {
|
|
523
|
+
if (!prevParts[part] || !nextParts[part]) continue;
|
|
524
|
+
const diff = diffSchemaSummary(prevParts[part], nextParts[part]);
|
|
525
|
+
if (diff.major.length > 0) {
|
|
526
|
+
changes.push(...diff.major.map((msg) => `${method}.${part}: ${msg}`));
|
|
527
|
+
severity = "major";
|
|
528
|
+
}
|
|
529
|
+
if (diff.minor.length > 0) {
|
|
530
|
+
changes.push(...diff.minor.map((msg) => `${method}.${part}: ${msg}`));
|
|
531
|
+
if (severity !== "major") severity = "minor";
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const prevResSchemas = prevEntry.schemas?.response ?? {};
|
|
537
|
+
const nextResSchemas = nextEntry.schemas?.response ?? {};
|
|
538
|
+
for (const [code, prevSchema] of Object.entries(prevResSchemas)) {
|
|
539
|
+
const status = Number(code);
|
|
540
|
+
const nextSchema = nextResSchemas[status];
|
|
541
|
+
if (!nextSchema) continue;
|
|
542
|
+
const diff = diffSchemaSummary(prevSchema, nextSchema);
|
|
543
|
+
if (diff.major.length > 0) {
|
|
544
|
+
changes.push(...diff.major.map((msg) => `response.${status}: ${msg}`));
|
|
545
|
+
severity = "major";
|
|
546
|
+
}
|
|
547
|
+
if (diff.minor.length > 0) {
|
|
548
|
+
changes.push(...diff.minor.map((msg) => `response.${status}: ${msg}`));
|
|
549
|
+
if (severity !== "major") severity = "minor";
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (prevEntry.description !== nextEntry.description) {
|
|
554
|
+
changes.push("description changed");
|
|
555
|
+
}
|
|
556
|
+
if (JSON.stringify(prevEntry.tags ?? []) !== JSON.stringify(nextEntry.tags ?? [])) {
|
|
557
|
+
changes.push("tags changed");
|
|
558
|
+
}
|
|
559
|
+
if (prevEntry.version !== nextEntry.version) {
|
|
560
|
+
changes.push("version changed");
|
|
561
|
+
}
|
|
562
|
+
if (prevEntry.hash !== nextEntry.hash && changes.length === 0) {
|
|
563
|
+
changes.push("contract content changed");
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
if (changes.length > 0) {
|
|
567
|
+
changed.push({
|
|
568
|
+
id,
|
|
569
|
+
routeId: nextEntry.routeId,
|
|
570
|
+
severity,
|
|
571
|
+
changes,
|
|
572
|
+
before: prevEntry,
|
|
573
|
+
after: nextEntry,
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
for (const [id, prevEntry] of prevMap.entries()) {
|
|
579
|
+
if (!nextMap.has(id)) {
|
|
580
|
+
removed.push(prevEntry);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
const summary = {
|
|
585
|
+
major: changed.filter((c) => c.severity === "major").length + removed.length,
|
|
586
|
+
minor: changed.filter((c) => c.severity === "minor").length + added.length,
|
|
587
|
+
patch: changed.filter((c) => c.severity === "patch").length,
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
return { added, removed, changed, summary };
|
|
591
|
+
}
|