transit-kit 0.6.1 → 0.6.2
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.
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
import z from "zod";
|
|
2
2
|
import { hasValue } from "../server/utils/typeGuards";
|
|
3
3
|
import { isJsonResponseSchema } from "../server/handlers/api/responses/jsonResponse";
|
|
4
|
+
function prepareForJsonSchema(schema) {
|
|
5
|
+
// 1. Handle Dates (The Core Issue)
|
|
6
|
+
if (schema instanceof z.ZodDate) {
|
|
7
|
+
return z.string().datetime().describe("ISO 8601 date-time string");
|
|
8
|
+
}
|
|
9
|
+
// 2. Handle Objects
|
|
10
|
+
if (schema instanceof z.ZodObject) {
|
|
11
|
+
const newShape = {};
|
|
12
|
+
for (const key in schema.shape) {
|
|
13
|
+
newShape[key] = prepareForJsonSchema(schema.shape[key]);
|
|
14
|
+
}
|
|
15
|
+
return z.object(newShape);
|
|
16
|
+
}
|
|
17
|
+
// 3. Handle Arrays
|
|
18
|
+
if (schema instanceof z.ZodArray) {
|
|
19
|
+
return z.array(prepareForJsonSchema(schema.element));
|
|
20
|
+
}
|
|
21
|
+
// 4. Handle Optionals
|
|
22
|
+
if (schema instanceof z.ZodOptional) {
|
|
23
|
+
return prepareForJsonSchema(schema.unwrap()).optional();
|
|
24
|
+
}
|
|
25
|
+
// 5. Handle Nullables
|
|
26
|
+
if (schema instanceof z.ZodNullable) {
|
|
27
|
+
return prepareForJsonSchema(schema.unwrap()).nullable();
|
|
28
|
+
}
|
|
29
|
+
// 7. Handle Unions
|
|
30
|
+
if (schema instanceof z.ZodUnion) {
|
|
31
|
+
return z.union(schema.options.map((option) => prepareForJsonSchema(option)));
|
|
32
|
+
}
|
|
33
|
+
// Return as-is if no transformation is needed
|
|
34
|
+
return schema;
|
|
35
|
+
}
|
|
4
36
|
function extractPathAndParameters(path) {
|
|
5
37
|
const parameters = path.match(/:([a-zA-Z0-9_]+)/g)?.map((param) => {
|
|
6
38
|
return {
|
|
@@ -17,7 +49,7 @@ function extractPathAndParameters(path) {
|
|
|
17
49
|
return { openApiPath, parameters };
|
|
18
50
|
}
|
|
19
51
|
function extractQueryParameters(querySchema) {
|
|
20
|
-
const querySchemaObject = z.toJSONSchema(querySchema);
|
|
52
|
+
const querySchemaObject = z.toJSONSchema(prepareForJsonSchema(querySchema));
|
|
21
53
|
if (querySchemaObject.properties) {
|
|
22
54
|
return Object.entries(querySchemaObject.properties).map(([name, schema]) => ({
|
|
23
55
|
name: name,
|
|
@@ -63,7 +95,7 @@ function translateToOpenAPIPathItem(definition) {
|
|
|
63
95
|
required: true,
|
|
64
96
|
content: {
|
|
65
97
|
"application/json": {
|
|
66
|
-
schema: z.toJSONSchema(requestBodySchema), // Type assertion
|
|
98
|
+
schema: z.toJSONSchema(prepareForJsonSchema(requestBodySchema)), // Type assertion
|
|
67
99
|
},
|
|
68
100
|
},
|
|
69
101
|
},
|
|
@@ -74,7 +106,7 @@ function translateToOpenAPIPathItem(definition) {
|
|
|
74
106
|
.map(([statusCode, responseDef]) => {
|
|
75
107
|
if (isJsonResponseSchema(responseDef)) {
|
|
76
108
|
const zodSchema = responseDef.dataSchema;
|
|
77
|
-
const responseSchema = z.toJSONSchema(zodSchema);
|
|
109
|
+
const responseSchema = z.toJSONSchema(prepareForJsonSchema(zodSchema));
|
|
78
110
|
return {
|
|
79
111
|
[statusCode]: {
|
|
80
112
|
description: `Response for status code ${statusCode}`,
|