Skip to content

Commit a19ec46

Browse files
committed
Derive unique version paths and labels from query params for schema inputs
1 parent 2ba0be8 commit a19ec46

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

docs/pages/docs/guides/processors.mdx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,28 @@ argument, allowing you to filter or transform the schema differently for each va
170170
This is useful when you have a single schema containing multiple API versions or groups that you
171171
want to split into separate pages.
172172

173+
Plain strings work out of the box. Version paths and dropdown labels are auto-generated from the
174+
query parameter values. For more control, use the object form with explicit `path` and `label`.
175+
173176
```ts title=zudoku.config.ts
174177
const config = {
175178
apis: {
176179
type: "file",
177-
input: ["openapi.json?prefix=/v2", "openapi.json?prefix=/v1"],
180+
input: [
181+
// Object form: explicit path and label
182+
{
183+
input: "openapi.json?prefix=/v2",
184+
path: "latest",
185+
label: "Latest (v2)",
186+
},
187+
// Object form: explicit path, label auto-generated from params
188+
{
189+
input: "openapi.json?prefix=/v1.1",
190+
path: "v1.1",
191+
},
192+
// Plain string: both path and label auto-generated from params
193+
"openapi.json?prefix=/v1",
194+
],
178195
path: "/api",
179196
},
180197
};

packages/zudoku/src/lib/plugins/openapi/ApiHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const ApiHeader = ({
6060
{showVersions && (
6161
<span className="text-xl text-muted-foreground ms-1.5">
6262
{" "}
63-
({version})
63+
({currentVersion?.label ?? version})
6464
</span>
6565
)}
6666
</Heading>

packages/zudoku/src/vite/api/SchemaManager.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ describe("SchemaManager", () => {
274274

275275
expect(schemas?.[0]?.importKey).not.toBe(schemas?.[1]?.importKey);
276276

277+
// Auto-generated version paths are unique when using query params
278+
expect(schemas?.[0]?.path).not.toBe(schemas?.[1]?.path);
279+
// Auto-generated labels from param values
280+
expect(schemas?.[0]?.label).toBe("/v1");
281+
expect(schemas?.[1]?.label).toBe("/v2");
282+
277283
const v1Paths = Object.keys(schemas?.[0]?.schema.paths ?? {});
278284
const v2Paths = Object.keys(schemas?.[1]?.schema.paths ?? {});
279285
expect(v1Paths).toEqual(["/v1/users", "/v1/posts"]);

packages/zudoku/src/vite/api/SchemaManager.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ const paramsSuffix = (params: Record<string, string>) =>
5050
? `-${slugify(new URLSearchParams(params).toString(), { separator: "_" })}`
5151
: "";
5252

53+
const paramsPath = (params: Record<string, string>) =>
54+
Object.keys(params).length > 0
55+
? Object.values(params)
56+
.map((v) => slugify(v))
57+
.join("-")
58+
: "";
59+
5360
const normalizeInputs = (
5461
inputs:
5562
| string
@@ -188,13 +195,17 @@ export class SchemaManager {
188195
const versionPath =
189196
existingSchema?.path && existingSchema.path.length > 0
190197
? existingSchema.path
191-
: schemaVersion;
198+
: paramsPath(params) || schemaVersion;
192199

193200
const processed = {
194201
schema: processedSchema,
195202
version: schemaVersion,
196203
path: versionPath,
197-
label: existingSchema?.label,
204+
label:
205+
existingSchema?.label ??
206+
(Object.keys(params).length > 0
207+
? Object.values(params).join(", ")
208+
: undefined),
198209
inputPath: filePath,
199210
params,
200211
importKey,

0 commit comments

Comments
 (0)