@scalar/openapi-to-markdown 1.1.0 → 1.3.0
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.
- package/CHANGELOG.md +34 -0
- package/dist/get-markdown-examples.d.ts +12 -1
- package/dist/get-markdown-examples.d.ts.map +1 -1
- package/dist/get-markdown-examples.js +83 -4
- package/dist/load-document.d.ts.map +1 -1
- package/dist/load-document.js +120 -42
- package/dist/object-kinds.d.ts +14 -0
- package/dist/object-kinds.d.ts.map +1 -0
- package/dist/object-kinds.js +70 -0
- package/dist/render-document.d.ts.map +1 -1
- package/dist/render-document.js +6 -2
- package/dist/render-examples.d.ts +1 -1
- package/dist/render-examples.d.ts.map +1 -1
- package/dist/render-examples.js +32 -11
- package/dist/render-operation-details.d.ts +2 -2
- package/dist/render-operation-details.d.ts.map +1 -1
- package/dist/render-operation-details.js +5 -5
- package/dist/render-operation.d.ts.map +1 -1
- package/dist/render-operation.js +10 -11
- package/dist/render-schema.d.ts +20 -2
- package/dist/render-schema.d.ts.map +1 -1
- package/dist/render-schema.js +175 -67
- package/dist/restore-boolean-schemas.d.ts +8 -1
- package/dist/restore-boolean-schemas.d.ts.map +1 -1
- package/dist/restore-boolean-schemas.js +42 -38
- package/dist/select-document.d.ts +3 -3
- package/dist/select-document.d.ts.map +1 -1
- package/dist/select-document.js +56 -35
- package/package.json +5 -5
package/dist/select-document.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HTTP_METHODS } from '@scalar/helpers/http/http-methods';
|
|
2
2
|
import { isObject } from '@scalar/helpers/object/is-object';
|
|
3
|
-
import { getPathItemOperation, getResolvedPathItem } from '@scalar/workspace-store/helpers/for-each-path-item-operation';
|
|
3
|
+
import { forEachPathItemOperation, getPathItemOperation, getResolvedPathItem, setPathItemOperation, } from '@scalar/workspace-store/helpers/for-each-path-item-operation';
|
|
4
4
|
import { getResolvedRef } from '@scalar/workspace-store/helpers/get-resolved-ref';
|
|
5
5
|
const HTTP_METHOD_SET = new Set(HTTP_METHODS);
|
|
6
6
|
const normalizeHttpMethod = (method) => {
|
|
@@ -28,17 +28,18 @@ const parseJsonPointer = (pointer) => normalizeJsonPointer(pointer)
|
|
|
28
28
|
.map((segment) => segment.replaceAll('~1', '/').replaceAll('~0', '~'));
|
|
29
29
|
const getOperationSelectorFromPointer = (pointer) => {
|
|
30
30
|
const segments = parseJsonPointer(pointer);
|
|
31
|
-
if (segments
|
|
31
|
+
if (segments[0] !== 'paths' ||
|
|
32
|
+
!(segments.length === 3 || (segments.length === 4 && segments[2] === 'additionalOperations'))) {
|
|
32
33
|
throw new Error(`JSON pointer "${pointer}" must target an operation object under "/paths/{path}/{method}"`);
|
|
33
34
|
}
|
|
34
35
|
const path = segments[1];
|
|
35
|
-
const method = segments[2];
|
|
36
|
-
if (!path || !method) {
|
|
36
|
+
const method = segments.length === 4 ? segments[3] : segments[2];
|
|
37
|
+
if (!path || !method || (segments.length === 3 ? !HTTP_METHOD_SET.has(method) : HTTP_METHOD_SET.has(method))) {
|
|
37
38
|
throw new Error(`JSON pointer "${pointer}" must target an operation object under "/paths/{path}/{method}"`);
|
|
38
39
|
}
|
|
39
40
|
return {
|
|
40
41
|
path,
|
|
41
|
-
method
|
|
42
|
+
method,
|
|
42
43
|
};
|
|
43
44
|
};
|
|
44
45
|
const getPathEntries = (document) => {
|
|
@@ -51,12 +52,20 @@ const getPathEntries = (document) => {
|
|
|
51
52
|
return pathItem ? [[path, pathItem]] : [];
|
|
52
53
|
});
|
|
53
54
|
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
/** Keep path metadata while excluding every unselected fixed or additional operation. */
|
|
56
|
+
const filterPathItemOperations = (pathItem, methods) => {
|
|
57
|
+
const selected = Object.fromEntries(Object.entries(pathItem).filter(([key]) => !HTTP_METHOD_SET.has(key) && key !== 'additionalOperations'));
|
|
58
|
+
forEachPathItemOperation(pathItem, (method, operation) => {
|
|
59
|
+
if (methods.includes(method)) {
|
|
60
|
+
setPathItemOperation(selected, method, operation);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return selected;
|
|
64
|
+
};
|
|
65
|
+
/** Exact authored methods take precedence over the legacy uppercase fixed-method aliases. */
|
|
66
|
+
const resolveMethod = (pathItem, method) => getPathItemOperation(pathItem, method) ? method : normalizeHttpMethod(method);
|
|
58
67
|
const findOperationByPathAndMethod = (document, selector) => {
|
|
59
|
-
const method =
|
|
68
|
+
const method = resolveMethod(getResolvedPathItem(document.paths?.[selector.path]), selector.method);
|
|
60
69
|
if (!method) {
|
|
61
70
|
throw new Error(`Invalid HTTP method "${selector.method}". Supported methods: ${HTTP_METHODS.join(', ')}`);
|
|
62
71
|
}
|
|
@@ -69,20 +78,22 @@ const findOperationByPathAndMethod = (document, selector) => {
|
|
|
69
78
|
method,
|
|
70
79
|
};
|
|
71
80
|
};
|
|
72
|
-
const findOperationsByOperationId = (document, operationId) => getPathEntries(document).flatMap(([path, pathItem]) =>
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return [{ path, method }];
|
|
82
|
-
}));
|
|
81
|
+
const findOperationsByOperationId = (document, operationId) => getPathEntries(document).flatMap(([path, pathItem]) => {
|
|
82
|
+
const matches = [];
|
|
83
|
+
forEachPathItemOperation(pathItem, (method, operation) => {
|
|
84
|
+
if (getResolvedRef(operation)?.operationId === operationId) {
|
|
85
|
+
matches.push({ path, method });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return matches;
|
|
89
|
+
});
|
|
83
90
|
const resolveOperationMatch = (document, selector) => {
|
|
84
91
|
if ('pointer' in selector) {
|
|
85
|
-
|
|
92
|
+
const match = getOperationSelectorFromPointer(selector.pointer);
|
|
93
|
+
if (!getPathItemOperation(document.paths?.[match.path], match.method)) {
|
|
94
|
+
throw new Error(`Operation not found at JSON pointer "${selector.pointer}"`);
|
|
95
|
+
}
|
|
96
|
+
return match;
|
|
86
97
|
}
|
|
87
98
|
if ('operationId' in selector) {
|
|
88
99
|
const matches = findOperationsByOperationId(document, selector.operationId);
|
|
@@ -106,7 +117,7 @@ const filterDocumentByOperation = (document, selector) => {
|
|
|
106
117
|
return {
|
|
107
118
|
...document,
|
|
108
119
|
paths: {
|
|
109
|
-
[match.path]:
|
|
120
|
+
[match.path]: filterPathItemOperations(pathItem, [match.method]),
|
|
110
121
|
},
|
|
111
122
|
};
|
|
112
123
|
};
|
|
@@ -157,9 +168,14 @@ export const selectDocument = (document, options = {}) => {
|
|
|
157
168
|
}
|
|
158
169
|
selected.tags = metadata.length ? metadata : [{ name: options.tag }];
|
|
159
170
|
for (const [path, item] of getPathEntries(document)) {
|
|
160
|
-
const methods =
|
|
171
|
+
const methods = [];
|
|
172
|
+
forEachPathItemOperation(item, (method, operation) => {
|
|
173
|
+
if (getResolvedRef(operation)?.tags?.includes(options.tag)) {
|
|
174
|
+
methods.push(method);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
161
177
|
if (methods.length) {
|
|
162
|
-
selected.paths[path] =
|
|
178
|
+
selected.paths[path] = filterPathItemOperations(item, methods);
|
|
163
179
|
}
|
|
164
180
|
}
|
|
165
181
|
if (!metadata.length && !Object.keys(selected.paths ?? {}).length) {
|
|
@@ -182,26 +198,31 @@ export const selectDocument = (document, options = {}) => {
|
|
|
182
198
|
Object.keys(selector).length !== 2) {
|
|
183
199
|
throw new Error('Invalid webhook selector. Use { name, method }');
|
|
184
200
|
}
|
|
185
|
-
const
|
|
201
|
+
const item = getResolvedPathItem(document.webhooks?.[selector.name]);
|
|
202
|
+
const method = resolveMethod(item, selector.method);
|
|
186
203
|
if (!method) {
|
|
187
204
|
throw new Error(`Invalid HTTP method "${selector.method}"`);
|
|
188
205
|
}
|
|
189
|
-
const item = getResolvedPathItem(document.webhooks?.[selector.name]);
|
|
190
206
|
if (!item || !getPathItemOperation(item, method)) {
|
|
191
207
|
throw new Error(`Webhook "${selector.name}" with method "${method.toUpperCase()}" was not found`);
|
|
192
208
|
}
|
|
193
|
-
selected.webhooks = { [selector.name]:
|
|
209
|
+
selected.webhooks = { [selector.name]: filterPathItemOperations(item, [method]) };
|
|
194
210
|
}
|
|
195
211
|
const securityNames = new Set();
|
|
196
212
|
const tagNames = new Set();
|
|
197
213
|
for (const items of [selected.paths, selected.webhooks]) {
|
|
198
214
|
for (const [path, itemRef] of Object.entries(items ?? {})) {
|
|
199
215
|
const item = getResolvedPathItem(itemRef);
|
|
200
|
-
const scoped = {
|
|
201
|
-
|
|
202
|
-
|
|
216
|
+
const scoped = {
|
|
217
|
+
...item,
|
|
218
|
+
additionalOperations: item.additionalOperations ? { ...item.additionalOperations } : undefined,
|
|
219
|
+
parameters: undefined,
|
|
220
|
+
servers: undefined,
|
|
221
|
+
};
|
|
222
|
+
forEachPathItemOperation(item, (method, operationRef) => {
|
|
223
|
+
const operation = getResolvedRef(operationRef);
|
|
203
224
|
if (!operation) {
|
|
204
|
-
|
|
225
|
+
return;
|
|
205
226
|
}
|
|
206
227
|
const parameters = new Map();
|
|
207
228
|
for (const ref of [...(item.parameters ?? []), ...(operation.parameters ?? [])]) {
|
|
@@ -219,14 +240,14 @@ export const selectDocument = (document, options = {}) => {
|
|
|
219
240
|
for (const name of operation.tags ?? []) {
|
|
220
241
|
tagNames.add(name);
|
|
221
242
|
}
|
|
222
|
-
scoped
|
|
243
|
+
setPathItemOperation(scoped, method, {
|
|
223
244
|
...operation,
|
|
224
245
|
parameters: [...parameters.values()],
|
|
225
246
|
servers: operation.servers ?? item.servers ?? document.servers,
|
|
226
247
|
security,
|
|
227
248
|
tags: options.tag !== undefined ? [options.tag] : operation.tags,
|
|
228
|
-
};
|
|
229
|
-
}
|
|
249
|
+
});
|
|
250
|
+
});
|
|
230
251
|
items[path] = scoped;
|
|
231
252
|
}
|
|
232
253
|
}
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"llm",
|
|
17
17
|
"swagger"
|
|
18
18
|
],
|
|
19
|
-
"version": "1.
|
|
19
|
+
"version": "1.3.0",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=22"
|
|
22
22
|
},
|
|
@@ -41,10 +41,10 @@
|
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@scalar/code-highlight": "0.4.7",
|
|
44
|
-
"@scalar/helpers": "0.
|
|
45
|
-
"@scalar/json-magic": "0.15.
|
|
46
|
-
"@scalar/openapi-upgrader": "0.
|
|
47
|
-
"@scalar/workspace-store": "0.
|
|
44
|
+
"@scalar/helpers": "0.15.0",
|
|
45
|
+
"@scalar/json-magic": "0.15.2",
|
|
46
|
+
"@scalar/openapi-upgrader": "0.4.0",
|
|
47
|
+
"@scalar/workspace-store": "0.67.0",
|
|
48
48
|
"rehype-parse": "^9.0.1",
|
|
49
49
|
"rehype-remark": "^10.0.1",
|
|
50
50
|
"rehype-sanitize": "^6.0.0",
|