Skip to content

Commit 1d51835

Browse files
committed
feat: parse json schema support
1 parent 65f5659 commit 1d51835

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

.vitepress/theme/components/Route.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import MarkdownIt from 'markdown-it';
125125
import type { Route } from '../types';
126126
import CopyButton from './CopyButton.vue';
127+
import { parseJsonSchema } from './parse-json-schema';
127128
128129
const props = defineProps<{
129130
namespace: string,
@@ -134,7 +135,11 @@ const props = defineProps<{
134135
description: string | null,
135136
siteUrl: string | null,
136137
image: string | null,
137-
}[] },
138+
}[],
139+
param?: any,
140+
query?: any,
141+
location?: string,
142+
},
138143
test?: {
139144
code: number,
140145
message?: string,
@@ -143,9 +148,10 @@ const props = defineProps<{
143148
144149
const demoUrl = props.data.example ? ('https://rsshub.app' + props.data.example) : null;
145150
const path = typeof props.data.path === "string" ? props.data.path : props.data.path[0];
151+
const param = parseJsonSchema(props.data.param);
146152
const paramMatch = path.match?.(/(?<=:).*?(?=\/|$)/g)?.map((item) => {
147153
const name = item.replace(/:|\?|\+|\*/g, '');
148-
let parameter = props.data.parameters?.[name];
154+
let parameter = param?.[name] || props.data.parameters?.[name];
149155
if (typeof parameter === "string") {
150156
parameter = {
151157
description: parameter,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { JSONSchema4 } from "json-schema";
2+
3+
export type ParamDesc = {
4+
description?: string;
5+
default?: string;
6+
options?: {
7+
value: string;
8+
label: string;
9+
}[];
10+
optional?: boolean;
11+
};
12+
13+
export function parseJsonSchema(
14+
schema?: JSONSchema4
15+
): Record<string, ParamDesc> {
16+
if (schema?.type !== "object") {
17+
return {};
18+
}
19+
const properties = schema.properties || {};
20+
const required = Array.isArray(schema.required) ? schema.required : [];
21+
const result: Record<string, ParamDesc> = {};
22+
23+
for (const [key, value] of Object.entries(properties)) {
24+
if (typeof value === "boolean") continue;
25+
26+
const propertySchema = value as JSONSchema4;
27+
const isOptional = !required.includes(key);
28+
29+
// Extract default value
30+
const defaultValue =
31+
propertySchema.default !== undefined
32+
? String(propertySchema.default)
33+
: undefined;
34+
35+
// Extract options from enum
36+
const options = propertySchema.anyOf
37+
? propertySchema.anyOf
38+
.filter((val) => "const" in val && "description" in val)
39+
.map((val) => ({
40+
label: String(val.description),
41+
value: String(val.const),
42+
}))
43+
: undefined;
44+
45+
result[key] = {
46+
optional: isOptional,
47+
default: defaultValue,
48+
description: propertySchema.description,
49+
...(options && { options }),
50+
};
51+
}
52+
return result;
53+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"docs:preview": "vitepress preview"
77
},
88
"devDependencies": {
9+
"@types/json-schema": "7.0.15",
910
"markdown-it": "14.1.0",
1011
"vitepress": "1.6.4",
1112
"vue": "3.5.25"

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)