zudoku 0.0.0-zabced09d → 0.0.0-zc17c4c22

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.
Files changed (128) hide show
  1. package/dist/config/validators/InputNavigationSchema.d.ts +56 -56
  2. package/dist/config/validators/ProtectedRoutesSchema.d.ts +1 -1
  3. package/dist/config/validators/validate.d.ts +1 -1
  4. package/dist/flat-config.d.ts +24 -24
  5. package/dist/lib/components/Heading.d.ts +1 -1
  6. package/dist/lib/components/index.d.ts +18 -74
  7. package/dist/lib/components/index.js +19 -36
  8. package/dist/lib/components/index.js.map +1 -1
  9. package/dist/lib/hooks/index.d.ts +7 -30
  10. package/dist/lib/hooks/index.js +7 -15
  11. package/dist/lib/hooks/index.js.map +1 -1
  12. package/dist/lib/oas/graphql/circular.d.ts +1 -1
  13. package/dist/lib/oas/graphql/circular.js +18 -35
  14. package/dist/lib/oas/graphql/circular.js.map +1 -1
  15. package/dist/lib/oas/graphql/circular.test.js +33 -2
  16. package/dist/lib/oas/graphql/circular.test.js.map +1 -1
  17. package/dist/lib/oas/parser/index.js +14 -5
  18. package/dist/lib/oas/parser/index.js.map +1 -1
  19. package/dist/lib/util/flattenAllOf.d.ts +0 -2
  20. package/dist/lib/util/flattenAllOf.js +0 -46
  21. package/dist/lib/util/flattenAllOf.js.map +1 -1
  22. package/dist/lib/util/flattenAllOf.test.js +2 -1
  23. package/dist/lib/util/flattenAllOf.test.js.map +1 -1
  24. package/dist/lib/util/flattenAllOfProcessor.d.ts +2 -0
  25. package/dist/lib/util/flattenAllOfProcessor.js +48 -0
  26. package/dist/lib/util/flattenAllOfProcessor.js.map +1 -0
  27. package/dist/vite/api/SchemaManager.js +1 -1
  28. package/dist/vite/api/SchemaManager.js.map +1 -1
  29. package/dist/vite/api/SchemaManager.test.js +1 -1
  30. package/dist/vite/api/SchemaManager.test.js.map +1 -1
  31. package/lib/{ClaudeLogo-DJ9bU-sO.js → ClaudeLogo-C6q-Xn_l.js} +26 -22
  32. package/lib/{ClaudeLogo-DJ9bU-sO.js.map → ClaudeLogo-C6q-Xn_l.js.map} +1 -1
  33. package/lib/{MdxPage-stpAoBtx.js → MdxPage-B1G4W1TK.js} +8 -8
  34. package/lib/{MdxPage-stpAoBtx.js.map → MdxPage-B1G4W1TK.js.map} +1 -1
  35. package/lib/{Mermaid-Koc3z8mU.js → Mermaid-B1xNo-pf.js} +3 -2
  36. package/lib/{Mermaid-Koc3z8mU.js.map → Mermaid-B1xNo-pf.js.map} +1 -1
  37. package/lib/{OAuthErrorPage-DJ811Bn_.js → OAuthErrorPage-01Ke086W.js} +20 -18
  38. package/lib/{OAuthErrorPage-DJ811Bn_.js.map → OAuthErrorPage-01Ke086W.js.map} +1 -1
  39. package/lib/{OasProvider-CS_ASmBB.js → OasProvider-oHPiMJZg.js} +3 -3
  40. package/lib/{OasProvider-CS_ASmBB.js.map → OasProvider-oHPiMJZg.js.map} +1 -1
  41. package/lib/{OperationList-Dq_AB4W9.js → OperationList-CZ4OK8Pm.js} +948 -945
  42. package/lib/OperationList-CZ4OK8Pm.js.map +1 -0
  43. package/lib/{RouteGuard--A04ESy8.js → RouteGuard-B1lCR0C_.js} +5 -5
  44. package/lib/{RouteGuard--A04ESy8.js.map → RouteGuard-B1lCR0C_.js.map} +1 -1
  45. package/lib/{SchemaList-BJZJv1gD.js → SchemaList-DoQFkJgM.js} +7 -7
  46. package/lib/{SchemaList-BJZJv1gD.js.map → SchemaList-DoQFkJgM.js.map} +1 -1
  47. package/lib/{SchemaView-U4JMYB3N.js → SchemaView-D2k6ZJck.js} +3 -3
  48. package/lib/{SchemaView-U4JMYB3N.js.map → SchemaView-D2k6ZJck.js.map} +1 -1
  49. package/lib/{SignUp-DCBViNUi.js → SignUp-8kDBaLbO.js} +31 -26
  50. package/lib/{SignUp-DCBViNUi.js.map → SignUp-8kDBaLbO.js.map} +1 -1
  51. package/lib/{SyntaxHighlight-Dshjn3Zf.js → SyntaxHighlight-hZOFnYl0.js} +3 -3
  52. package/lib/{SyntaxHighlight-Dshjn3Zf.js.map → SyntaxHighlight-hZOFnYl0.js.map} +1 -1
  53. package/lib/{Toc-Cgz6CPiE.js → Toc-qEIii_-W.js} +2 -2
  54. package/lib/{Toc-Cgz6CPiE.js.map → Toc-qEIii_-W.js.map} +1 -1
  55. package/lib/{index-CL8eDnQW.js → Zudoku-DUsdmPME.js} +2250 -2268
  56. package/lib/Zudoku-DUsdmPME.js.map +1 -0
  57. package/lib/{ZudokuContext-BZB1TWdT.js → ZudokuContext-BBI06sOx.js} +5 -5
  58. package/lib/{ZudokuContext-BZB1TWdT.js.map → ZudokuContext-BBI06sOx.js.map} +1 -1
  59. package/lib/{circular-BmMJjG1v.js → circular-D9tSKG2c.js} +1327 -1346
  60. package/lib/{circular-BmMJjG1v.js.map → circular-D9tSKG2c.js.map} +1 -1
  61. package/lib/createServer-BprC4n85.js +13036 -0
  62. package/lib/createServer-BprC4n85.js.map +1 -0
  63. package/lib/{errors-b9I-fAOY.js → errors-7hgPDs1h.js} +3 -3
  64. package/lib/{errors-b9I-fAOY.js.map → errors-7hgPDs1h.js.map} +1 -1
  65. package/lib/{firebase-BCXX7Qv5.js → firebase-Dwn-2ju-.js} +13 -13
  66. package/lib/{firebase-BCXX7Qv5.js.map → firebase-Dwn-2ju-.js.map} +1 -1
  67. package/lib/{hook-BGlHBdET.js → hook-ZEd1Es7D.js} +2 -2
  68. package/lib/{hook-BGlHBdET.js.map → hook-ZEd1Es7D.js.map} +1 -1
  69. package/lib/{index-O9RHI87z.js → index-CyIW9rHv.js} +463 -461
  70. package/lib/{index-O9RHI87z.js.map → index-CyIW9rHv.js.map} +1 -1
  71. package/lib/index-DAWHN3cH.js +86 -0
  72. package/lib/index-DAWHN3cH.js.map +1 -0
  73. package/lib/{index-UOLtazB8.js → index-Dxdhrp-I.js} +2 -2
  74. package/lib/{index-UOLtazB8.js.map → index-Dxdhrp-I.js.map} +1 -1
  75. package/lib/{index.esm-B_0dvNjB.js → index.esm-Ca5zvoff.js} +20 -20
  76. package/lib/{index.esm-B_0dvNjB.js.map → index.esm-Ca5zvoff.js.map} +1 -1
  77. package/lib/{index.esm-C5CBsVzN.js → index.esm-DG4KaDKR.js} +2 -2
  78. package/lib/index.esm-DG4KaDKR.js.map +1 -0
  79. package/lib/{invariant-BJAl77rw.js → invariant-B_t_F2s_.js} +3 -3
  80. package/lib/{invariant-BJAl77rw.js.map → invariant-B_t_F2s_.js.map} +1 -1
  81. package/lib/ui/SyntaxHighlight.js +3 -3
  82. package/lib/useExposedProps-CzTDfXfq.js +30 -0
  83. package/lib/useExposedProps-CzTDfXfq.js.map +1 -0
  84. package/lib/zudoku.__internal.js +1492 -1030
  85. package/lib/zudoku.__internal.js.map +1 -1
  86. package/lib/zudoku.auth-auth0.js +1 -1
  87. package/lib/zudoku.auth-azureb2c.js +4 -4
  88. package/lib/zudoku.auth-clerk.js +2 -2
  89. package/lib/zudoku.auth-firebase.js +5 -5
  90. package/lib/zudoku.auth-openid.js +5 -5
  91. package/lib/zudoku.auth-supabase.js +4 -4
  92. package/lib/zudoku.components.js +31 -29
  93. package/lib/zudoku.components.js.map +1 -1
  94. package/lib/zudoku.hooks.js +24 -11
  95. package/lib/zudoku.hooks.js.map +1 -1
  96. package/lib/zudoku.mermaid.js +4 -3
  97. package/lib/zudoku.mermaid.js.map +1 -1
  98. package/lib/zudoku.plugin-api-catalog.js +36 -32
  99. package/lib/zudoku.plugin-api-catalog.js.map +1 -1
  100. package/lib/zudoku.plugin-api-keys.js +131 -130
  101. package/lib/zudoku.plugin-api-keys.js.map +1 -1
  102. package/lib/zudoku.plugin-custom-pages.js +1 -1
  103. package/lib/zudoku.plugin-markdown.js +1 -1
  104. package/lib/zudoku.plugin-openapi.js +2 -2
  105. package/lib/zudoku.plugin-search-pagefind.js +2 -2
  106. package/package.json +4 -3
  107. package/src/lib/components/index.ts +19 -39
  108. package/src/lib/hooks/index.ts +7 -16
  109. package/src/lib/oas/graphql/circular.test.ts +37 -2
  110. package/src/lib/oas/graphql/circular.ts +25 -51
  111. package/src/lib/oas/parser/index.ts +17 -6
  112. package/src/lib/util/flattenAllOf.test.ts +2 -1
  113. package/src/lib/util/flattenAllOf.ts +0 -57
  114. package/src/lib/util/flattenAllOfProcessor.ts +58 -0
  115. package/src/zuplo/enrich-with-zuplo-mcp.ts +168 -0
  116. package/src/zuplo/enrich-with-zuplo.ts +254 -0
  117. package/src/zuplo/policy-types.ts +46 -0
  118. package/src/zuplo/with-zuplo-processors.ts +35 -0
  119. package/src/zuplo/with-zuplo.ts +14 -0
  120. package/lib/OperationList-Dq_AB4W9.js.map +0 -1
  121. package/lib/___vite-browser-external_commonjs-proxy-BttVsNON.js +0 -9
  122. package/lib/___vite-browser-external_commonjs-proxy-BttVsNON.js.map +0 -1
  123. package/lib/createServer-CLSZ7hWJ.js +0 -16693
  124. package/lib/createServer-CLSZ7hWJ.js.map +0 -1
  125. package/lib/index-CL8eDnQW.js.map +0 -1
  126. package/lib/index-DBjOT2H1.js +0 -133
  127. package/lib/index-DBjOT2H1.js.map +0 -1
  128. package/lib/index.esm-C5CBsVzN.js.map +0 -1
@@ -156,16 +156,20 @@ describe("handleCircularRefs", () => {
156
156
  });
157
157
  });
158
158
 
159
- it("should deduplicate shared object instances with __$ref", () => {
159
+ it("should handle shared object instances with __$ref without marking circular", () => {
160
160
  const shared = { __$ref: "#/components/schemas/Foo", type: "string" };
161
161
  const obj = { a: shared, b: shared };
162
162
  const result = handleCircularRefs(obj);
163
163
 
164
+ // Both should return the cached result, not mark as circular
164
165
  expect(result.a).toEqual({
165
166
  __$ref: "#/components/schemas/Foo",
166
167
  type: "string",
167
168
  });
168
- expect(result.b).toBe(`${SCHEMA_REF_PREFIX}#/components/schemas/Foo`);
169
+ expect(result.b).toEqual({
170
+ __$ref: "#/components/schemas/Foo",
171
+ type: "string",
172
+ });
169
173
  });
170
174
 
171
175
  it("should mark circular ref with property name from path", () => {
@@ -183,4 +187,35 @@ describe("handleCircularRefs", () => {
183
187
 
184
188
  expect(result.properties.child.properties.back).toContain(CIRCULAR_REF);
185
189
  });
190
+
191
+ // Exact reproduction of #1869 - shared object instances with __$ref
192
+ it("should NOT mark shared object instances with __$ref as circular (issue #1869)", () => {
193
+ // When dereferencing, the SAME object instance is returned for all refs to the same schema
194
+ const timestampSchema = {
195
+ __$ref: "#/components/schemas/timestamp",
196
+ type: "string",
197
+ format: "date-time",
198
+ };
199
+
200
+ // Both created_at and updated_at point to the SAME object instance
201
+ const obj = {
202
+ type: "object",
203
+ properties: {
204
+ created_at: timestampSchema,
205
+ updated_at: timestampSchema,
206
+ },
207
+ };
208
+
209
+ const result = handleCircularRefs(obj);
210
+
211
+ // The first one should be fully expanded
212
+ expect(result.properties.created_at).toEqual({
213
+ __$ref: "#/components/schemas/timestamp",
214
+ type: "string",
215
+ format: "date-time",
216
+ });
217
+ // The second one should ALSO be fully expanded (not marked as circular)
218
+ expect(typeof result.properties.updated_at).toBe("object");
219
+ expect(result.properties.updated_at).not.toContain(CIRCULAR_REF);
220
+ });
186
221
  });
@@ -1,6 +1,5 @@
1
1
  import { GraphQLScalarType } from "graphql/index.js";
2
2
  import { GraphQLJSON } from "graphql-type-json";
3
- import type { RecordAny } from "../../util/traverse.js";
4
3
 
5
4
  export const CIRCULAR_REF = "$[Circular Reference]";
6
5
  export const SCHEMA_REF_PREFIX = "$ref:";
@@ -17,73 +16,48 @@ const OPENAPI_PROPS = new Set([
17
16
  export const handleCircularRefs = (
18
17
  // biome-ignore lint/suspicious/noExplicitAny: Allow any type
19
18
  obj: any,
20
- visited = new WeakSet(),
19
+ currentPath = new WeakSet(),
21
20
  refs = new WeakMap(),
22
21
  path: string[] = [],
23
- seenRefPaths = new Set<string>(),
22
+ currentRefPaths = new Set<string>(),
24
23
  // biome-ignore lint/suspicious/noExplicitAny: Allow any type
25
24
  ): any => {
26
25
  if (obj === null || typeof obj !== "object") return obj;
27
26
 
28
27
  const refPath = obj.__$ref;
28
+ const isCircular =
29
+ currentPath.has(obj) ||
30
+ (typeof refPath === "string" && currentRefPaths.has(refPath));
29
31
 
30
- // Check if this object has a __$ref marker (set during schema code generation)
31
- // If we've already fully processed this ref path, return a reference marker
32
- // instead of the full data to avoid JSON.stringify serializing duplicates
33
- if (typeof refPath === "string" && seenRefPaths.has(refPath)) {
34
- return SCHEMA_REF_PREFIX + refPath;
35
- }
36
-
37
- if (visited.has(obj)) {
38
- const cached = refs.get(obj);
39
- if (cached) {
40
- return typeof refPath === "string"
41
- ? // If already processed, return ref marker to avoid duplicate serialization
42
- SCHEMA_REF_PREFIX + refPath
43
- : cached;
44
- }
32
+ if (isCircular) {
33
+ if (typeof refPath === "string") return SCHEMA_REF_PREFIX + refPath;
45
34
  const circularProp = path.find((p) => !OPENAPI_PROPS.has(p)) || path[0];
46
-
47
35
  return [CIRCULAR_REF, circularProp].filter(Boolean).join(":");
48
36
  }
49
37
 
50
- visited.add(obj);
38
+ if (refs.has(obj)) return refs.get(obj);
51
39
 
52
- // Add refPath BEFORE recursing to detect cycles within this branch
53
- // This will be removed after processing to allow siblings with the same ref
54
- if (typeof refPath === "string") {
55
- seenRefPaths.add(refPath);
56
- }
40
+ currentPath.add(obj);
41
+ if (typeof refPath === "string") currentRefPaths.add(refPath);
57
42
 
58
- let result: RecordAny | RecordAny[];
59
- if (Array.isArray(obj)) {
60
- result = obj.map((item, index) =>
61
- handleCircularRefs(
62
- item,
63
- visited,
64
- refs,
65
- [...path, index.toString()],
66
- seenRefPaths,
67
- ),
43
+ const recurse = (value: unknown, key: string) =>
44
+ handleCircularRefs(
45
+ value,
46
+ currentPath,
47
+ refs,
48
+ [...path, key],
49
+ currentRefPaths,
68
50
  );
69
- } else {
70
- result = {};
71
- for (const [key, value] of Object.entries(obj)) {
72
- result[key] = handleCircularRefs(
73
- value,
74
- visited,
75
- refs,
76
- [...path, key],
77
- seenRefPaths,
51
+
52
+ const result = Array.isArray(obj)
53
+ ? obj.map((item, i) => recurse(item, i.toString()))
54
+ : Object.fromEntries(
55
+ Object.entries(obj).map(([k, v]) => [k, recurse(v, k)]),
78
56
  );
79
- }
80
- }
81
- refs.set(obj, result);
82
57
 
83
- // Remove refPath after processing so sibling refs aren't incorrectly marked
84
- if (typeof refPath === "string") {
85
- seenRefPaths.delete(refPath);
86
- }
58
+ refs.set(obj, result);
59
+ currentPath.delete(obj);
60
+ if (typeof refPath === "string") currentRefPaths.delete(refPath);
87
61
 
88
62
  return result;
89
63
  };
@@ -1,6 +1,7 @@
1
1
  import { GraphQLError } from "graphql/error/index.js";
2
2
  import { OpenAPIV3, type OpenAPIV3_1 } from "openapi-types";
3
- import { flattenAllOfProcessor } from "../../util/flattenAllOf.js";
3
+ import { flattenAllOf } from "../../util/flattenAllOf.js";
4
+ import { traverse } from "../../util/traverse.js";
4
5
  import { dereference, type JSONSchema } from "./dereference/index.js";
5
6
  import { upgradeSchema } from "./upgrade/index.js";
6
7
 
@@ -104,11 +105,21 @@ export const validate = async (schemaInput: unknown) => {
104
105
  const dereferenced = await dereference(schema);
105
106
  const upgraded = upgradeSchema(dereferenced);
106
107
 
107
- const flattened = await flattenAllOfProcessor({
108
- schema: upgraded,
109
- file: "schema.json",
110
- dereference: async (schema) => schema,
111
- });
108
+ const flattened = traverse(upgraded, (spec) => {
109
+ if (!spec || typeof spec !== "object" || Array.isArray(spec)) {
110
+ return spec;
111
+ }
112
+ const isSchemaObject =
113
+ "type" in spec ||
114
+ "properties" in spec ||
115
+ "allOf" in spec ||
116
+ "anyOf" in spec ||
117
+ "oneOf" in spec;
118
+
119
+ if (!isSchemaObject) return spec;
120
+
121
+ return flattenAllOf(spec) as typeof spec;
122
+ }) as OpenAPIDocument;
112
123
 
113
124
  return flattened;
114
125
  };
@@ -1,7 +1,8 @@
1
1
  import type { JSONSchema7 } from "json-schema";
2
2
  import { describe, expect, it, vi } from "vitest";
3
3
  import type { OpenAPIDocument } from "../oas/parser/index.js";
4
- import { flattenAllOf, flattenAllOfProcessor } from "./flattenAllOf.js";
4
+ import { flattenAllOf } from "./flattenAllOf.js";
5
+ import { flattenAllOfProcessor } from "./flattenAllOfProcessor.js";
5
6
  import invariant from "./invariant.js";
6
7
 
7
8
  describe("flattenAllOf", () => {
@@ -1,4 +1,3 @@
1
- import { $RefParser } from "@apidevtools/json-schema-ref-parser";
2
1
  import {
3
2
  createComparator,
4
3
  createMerger,
@@ -9,62 +8,6 @@ import {
9
8
  createIntersector,
10
9
  } from "@x0k/json-schema-merge/lib/array";
11
10
  import type { JSONSchema7Definition } from "json-schema";
12
- import type { Processor } from "../../config/validators/BuildSchema.js";
13
- import type { OpenAPIDocument } from "../oas/parser/index.js";
14
- import { type RecordAny, traverse } from "./traverse.js";
15
-
16
- export const flattenAllOfProcessor: Processor = async ({ schema, file }) => {
17
- try {
18
- // Resolve refs once - creates a lookup table without modifying the schema
19
- const parser = new $RefParser();
20
- await parser.resolve(schema);
21
- const $refs = parser.$refs;
22
-
23
- const flattened = traverse(schema, (spec) => {
24
- if (!spec || typeof spec !== "object" || Array.isArray(spec)) {
25
- return spec;
26
- }
27
-
28
- const isSchemaObject =
29
- "type" in spec ||
30
- "properties" in spec ||
31
- "allOf" in spec ||
32
- "anyOf" in spec ||
33
- "oneOf" in spec;
34
-
35
- if (!isSchemaObject) return spec;
36
-
37
- if ("allOf" in spec && Array.isArray(spec.allOf)) {
38
- const resolvedAllOf = spec.allOf.map((item) => {
39
- if (
40
- item &&
41
- typeof item === "object" &&
42
- "$ref" in item &&
43
- typeof item.$ref === "string"
44
- ) {
45
- try {
46
- return $refs.get(item.$ref) ?? item;
47
- } catch {
48
- return item;
49
- }
50
- }
51
- return item;
52
- });
53
- return flattenAllOf({ ...spec, allOf: resolvedAllOf }) as RecordAny;
54
- }
55
-
56
- return flattenAllOf(spec) as RecordAny;
57
- }) as OpenAPIDocument;
58
-
59
- return flattened;
60
- } catch (error) {
61
- // biome-ignore lint/suspicious/noConsole: Logging allowed here
62
- console.warn(
63
- `Failed to flatten \`allOf\` in ${file}: ${error instanceof Error ? error.message : error}`,
64
- );
65
- return schema;
66
- }
67
- };
68
11
 
69
12
  const { compareSchemaDefinitions, compareSchemaValues } = createComparator();
70
13
  const { mergeArrayOfSchemaDefinitions } = createMerger({
@@ -0,0 +1,58 @@
1
+ import { $RefParser } from "@apidevtools/json-schema-ref-parser";
2
+ import type { Processor } from "../../config/validators/BuildSchema.js";
3
+ import type { OpenAPIDocument } from "../oas/parser/index.js";
4
+ import { flattenAllOf } from "./flattenAllOf.js";
5
+ import { type RecordAny, traverse } from "./traverse.js";
6
+
7
+ export const flattenAllOfProcessor: Processor = async ({ schema, file }) => {
8
+ try {
9
+ // Resolve refs once - creates a lookup table without modifying the schema
10
+ const parser = new $RefParser();
11
+ await parser.resolve(schema);
12
+ const $refs = parser.$refs;
13
+
14
+ const flattened = traverse(schema, (spec) => {
15
+ if (!spec || typeof spec !== "object" || Array.isArray(spec)) {
16
+ return spec;
17
+ }
18
+
19
+ const isSchemaObject =
20
+ "type" in spec ||
21
+ "properties" in spec ||
22
+ "allOf" in spec ||
23
+ "anyOf" in spec ||
24
+ "oneOf" in spec;
25
+
26
+ if (!isSchemaObject) return spec;
27
+
28
+ if ("allOf" in spec && Array.isArray(spec.allOf)) {
29
+ const resolvedAllOf = spec.allOf.map((item) => {
30
+ if (
31
+ item &&
32
+ typeof item === "object" &&
33
+ "$ref" in item &&
34
+ typeof item.$ref === "string"
35
+ ) {
36
+ try {
37
+ return $refs.get(item.$ref) ?? item;
38
+ } catch {
39
+ return item;
40
+ }
41
+ }
42
+ return item;
43
+ });
44
+ return flattenAllOf({ ...spec, allOf: resolvedAllOf }) as RecordAny;
45
+ }
46
+
47
+ return flattenAllOf(spec) as RecordAny;
48
+ }) as OpenAPIDocument;
49
+
50
+ return flattened;
51
+ } catch (error) {
52
+ // biome-ignore lint/suspicious/noConsole: Logging allowed here
53
+ console.warn(
54
+ `Failed to flatten \`allOf\` in ${file}: ${error instanceof Error ? error.message : error}`,
55
+ );
56
+ return schema;
57
+ }
58
+ };
@@ -0,0 +1,168 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import type {
4
+ ExtensionMcpServer,
5
+ ExtensionMcpServerTool,
6
+ } from "@zuplo/mcp/openapi/types";
7
+ import {
8
+ DEFAULT_MCP_SERVER_NAME,
9
+ DEFAULT_MCP_SERVER_VERSION,
10
+ } from "@zuplo/mcp/server";
11
+ import type { OpenAPIV3_1 } from "openapi-types";
12
+ import type { ProcessorArg } from "../config/validators/BuildSchema.js";
13
+ import { traverse, traverseAsync } from "../lib/util/traverse.js";
14
+ import type { RecordAny } from "../lib/util/types.js";
15
+ import { operations } from "./enrich-with-zuplo.js";
16
+
17
+ // extracts x-mcp-server metadata from the operation using x-zuplo-mcp-tool
18
+ // as a first class citizen.
19
+ const extractOperationSchema = (
20
+ operation: OpenAPIV3_1.OperationObject & RecordAny,
21
+ ): ExtensionMcpServerTool | null => {
22
+ if (!operation.operationId) return null;
23
+
24
+ // Check if tool is explicitly disabled
25
+ const mcpToolConfig = operation["x-zuplo-mcp-tool"];
26
+ if (mcpToolConfig?.enabled === false) {
27
+ return null;
28
+ }
29
+
30
+ const tool: ExtensionMcpServerTool = {
31
+ // Use custom name from x-zuplo-mcp-tool or fallback to operationId
32
+ name: mcpToolConfig?.name || operation.operationId,
33
+
34
+ // Use custom description from x-zuplo-mcp-tool or fallback
35
+ // to operation description
36
+ description:
37
+ mcpToolConfig?.description ||
38
+ operation.summary ||
39
+ operation.description ||
40
+ `Operation ${operation.operationId}`,
41
+ };
42
+
43
+ // Grab valid request body JSON schema for the tool
44
+ const requestBody = operation.requestBody as
45
+ | OpenAPIV3_1.RequestBodyObject
46
+ | undefined;
47
+
48
+ const schema = requestBody?.content?.["application/json"]?.schema;
49
+ if (schema && typeof schema === "object") {
50
+ // TODO: @jpmcb - Zuplo also supports in-path parameters and query parameters
51
+ // as MCP "inputSchema" arguments. In order to document full argument params,
52
+ // Zudoku will need to more intelligently parse these elements of an operation.
53
+ tool.inputSchema = { body: schema };
54
+ }
55
+
56
+ return tool;
57
+ };
58
+
59
+ // Builds a lookup map of operationId -> operation for efficient access
60
+ const buildOperationLookup = (
61
+ document: OpenAPIV3_1.Document,
62
+ ): Map<string, OpenAPIV3_1.OperationObject> => {
63
+ const operationMap = new Map<string, OpenAPIV3_1.OperationObject>();
64
+
65
+ traverse(document, (node, path) => {
66
+ // Check if we're at a path item level (paths -> /some/path -> method)
67
+ // and validate it's in allowed operations
68
+ if (
69
+ !path ||
70
+ path.length < 2 ||
71
+ path[0] !== "paths" ||
72
+ !operations.includes(path[path.length - 1] as string)
73
+ ) {
74
+ return node;
75
+ }
76
+
77
+ if (node.operationId) {
78
+ operationMap.set(node.operationId, node);
79
+ }
80
+
81
+ return node;
82
+ });
83
+
84
+ return operationMap;
85
+ };
86
+
87
+ // Takes an OpenAPI document and returns the x-mcp-server tools list defined
88
+ // by an MCP server's options.files[x].operationIds array.
89
+ const findOperationsInDocument = (
90
+ document: OpenAPIV3_1.Document,
91
+ operationIds: string[],
92
+ ): ExtensionMcpServerTool[] => {
93
+ const tools: ExtensionMcpServerTool[] = [];
94
+ const operationLookup = buildOperationLookup(document);
95
+
96
+ operationIds.forEach((operationId) => {
97
+ const operation = operationLookup.get(operationId);
98
+ if (operation) {
99
+ const tool = extractOperationSchema(operation);
100
+ if (tool) {
101
+ tools.push(tool);
102
+ }
103
+ }
104
+ });
105
+
106
+ return tools;
107
+ };
108
+
109
+ // Enriches an OpenAPI schema with x-mcp-server data based on the Zuplo MCP server handler
110
+ export const enrichWithZuploMcpServerData = ({
111
+ rootDir,
112
+ }: {
113
+ rootDir: string;
114
+ }) => {
115
+ return async ({ schema }: ProcessorArg) => {
116
+ if (!schema.paths) return schema;
117
+ const modifiedSchema = { ...schema };
118
+ if (!modifiedSchema?.paths) return modifiedSchema;
119
+
120
+ await traverseAsync(modifiedSchema, async (node, nodePath) => {
121
+ // Check if we're at a "post" operation (paths -> /some/path -> "post").
122
+ // HTTP MCP servers are only allow post operations.
123
+ if (!nodePath || nodePath.length !== 3 || nodePath[2] !== "post") {
124
+ return node;
125
+ }
126
+
127
+ const operation = node as RecordAny;
128
+ if (!operation?.["x-zuplo-route"]) return node;
129
+
130
+ const handler = operation["x-zuplo-route"]?.handler;
131
+ if (handler?.export !== "mcpServerHandler" || !handler.options?.files)
132
+ return node;
133
+
134
+ const tools: ExtensionMcpServerTool[] = [];
135
+
136
+ for (const fileConfig of handler.options.files) {
137
+ if (!fileConfig.path || !fileConfig.operationIds) continue;
138
+
139
+ const resolvedPath = path.resolve(rootDir, "../", fileConfig.path);
140
+ const fileContent = await fs.readFile(resolvedPath, "utf-8");
141
+ const document = JSON.parse(fileContent);
142
+
143
+ if (document) {
144
+ const fileTools = findOperationsInDocument(
145
+ document,
146
+ fileConfig.operationIds,
147
+ );
148
+
149
+ tools.push(...fileTools);
150
+ }
151
+ }
152
+
153
+ const mcpExtension: ExtensionMcpServer = {
154
+ name: DEFAULT_MCP_SERVER_NAME,
155
+ version: DEFAULT_MCP_SERVER_VERSION,
156
+ };
157
+
158
+ if (tools.length > 0) {
159
+ mcpExtension.tools = tools;
160
+ }
161
+
162
+ node["x-mcp-server"] = mcpExtension;
163
+ return node;
164
+ });
165
+
166
+ return modifiedSchema;
167
+ };
168
+ };