transit-kit 0.6.0 → 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}`,
@@ -42,6 +42,7 @@ export function createServer(config) {
42
42
  endpointDefinitions: [],
43
43
  registerApiEndpoint(endpoint) {
44
44
  registerApiEndpoint(app, endpoint);
45
+ this.endpointDefinitions.push(endpoint.definition);
45
46
  },
46
47
  start() {
47
48
  app.listen(port);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transit-kit",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "A declarative TypeScript framework for building type-safe Express.js APIs with automatic OpenAPI generation",
5
5
  "keywords": [
6
6
  "express",