@scalar/oas-utils 0.1.13 → 0.1.15
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 +13 -0
- package/dist/fetchSpecFromUrl.d.ts.map +1 -1
- package/dist/getExampleFromSchema.d.ts.map +1 -1
- package/dist/index.js +55 -1
- package/dist/normalizeMimeType.d.ts +7 -1
- package/dist/normalizeMimeType.d.ts.map +1 -1
- package/dist/normalizeMimeTypeObject.d.ts.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @scalar/oas-utils
|
|
2
2
|
|
|
3
|
+
## 0.1.15
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f472998: feat: generated example values based on the given format
|
|
8
|
+
|
|
9
|
+
## 0.1.14
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 7205137: fix: response body does not show a preview when the content-type is undefined
|
|
14
|
+
- d369ac4: feat: union types in getExampleFromSchema
|
|
15
|
+
|
|
3
16
|
## 0.1.13
|
|
4
17
|
|
|
5
18
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchSpecFromUrl.d.ts","sourceRoot":"","sources":["../src/fetchSpecFromUrl.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchSpecFromUrl.d.ts","sourceRoot":"","sources":["../src/fetchSpecFromUrl.ts"],"names":[],"mappings":"AAYA,iEAAiE;AACjE,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAyBjB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../src/getExampleFromSchema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../src/getExampleFromSchema.ts"],"names":[],"mappings":"AAsCA;;GAEG;AACH,eAAO,MAAM,oBAAoB,WACvB,OAAO,MAAM,EAAE,GAAG,CAAC,YACjB;IACR;;;QAGI;IACJ,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;IACvB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,MAAM,EAAE,GAAG,CAAC,CAAA;CAChC,UACM,MAAM,KACZ,GAiQF,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -86,10 +86,15 @@ const parseJsonOrYaml = (value) => {
|
|
|
86
86
|
});
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
+
const OLD_PROXY_URL = "https://api.scalar.com/request-proxy";
|
|
90
|
+
const NEW_PROXY_URL = "https://proxy.scalar.com";
|
|
89
91
|
function redirectToProxy(proxy, url) {
|
|
90
92
|
return `${proxy}?scalar_url=${encodeURI(url)}`;
|
|
91
93
|
}
|
|
92
94
|
async function fetchSpecFromUrl(url, proxy) {
|
|
95
|
+
if (proxy === OLD_PROXY_URL) {
|
|
96
|
+
proxy = NEW_PROXY_URL;
|
|
97
|
+
}
|
|
93
98
|
const response = await fetch(proxy ? redirectToProxy(proxy, url) : url);
|
|
94
99
|
if (response.status !== 200) {
|
|
95
100
|
console.error(
|
|
@@ -104,10 +109,44 @@ async function fetchSpecFromUrl(url, proxy) {
|
|
|
104
109
|
return formatJsonOrYamlString(await response.text());
|
|
105
110
|
}
|
|
106
111
|
|
|
112
|
+
function guessFromFormat(schema, fallback = "") {
|
|
113
|
+
const exampleValues = {
|
|
114
|
+
// 'date-time': '1970-01-01T00:00:00Z',
|
|
115
|
+
"date-time": (/* @__PURE__ */ new Date()).toISOString(),
|
|
116
|
+
// 'date': '1970-01-01',
|
|
117
|
+
"date": (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
|
|
118
|
+
"email": "hello@example.com",
|
|
119
|
+
"hostname": "example.com",
|
|
120
|
+
// https://tools.ietf.org/html/rfc6531#section-3.3
|
|
121
|
+
"idn-email": "jane.doe@example.com",
|
|
122
|
+
// https://tools.ietf.org/html/rfc5890#section-2.3.2.3
|
|
123
|
+
"idn-hostname": "example.com",
|
|
124
|
+
"ipv4": "127.0.0.1",
|
|
125
|
+
"ipv6": "51d4:7fab:bfbf:b7d7:b2cb:d4b4:3dad:d998",
|
|
126
|
+
"iri-reference": "/entitiy/1",
|
|
127
|
+
// https://tools.ietf.org/html/rfc3987
|
|
128
|
+
"iri": "https://example.com/entity/123",
|
|
129
|
+
"json-pointer": "/nested/objects",
|
|
130
|
+
"password": "super-secret",
|
|
131
|
+
"regex": "/[a-z]/",
|
|
132
|
+
// https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
|
|
133
|
+
"relative-json-pointer": "1/nested/objects",
|
|
134
|
+
// full-time in https://tools.ietf.org/html/rfc3339#section-5.6
|
|
135
|
+
// 'time': '00:00:00Z',
|
|
136
|
+
"time": (/* @__PURE__ */ new Date()).toISOString().split("T")[1].split(".")[0],
|
|
137
|
+
// either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
|
|
138
|
+
"uri-reference": "../folder",
|
|
139
|
+
"uri-template": "https://example.com/{id}",
|
|
140
|
+
"uri": "https://example.com",
|
|
141
|
+
"uuid": "123e4567-e89b-12d3-a456-426614174000"
|
|
142
|
+
};
|
|
143
|
+
return exampleValues[schema.format] ?? fallback;
|
|
144
|
+
}
|
|
107
145
|
const getExampleFromSchema = (schema, options, level = 0) => {
|
|
108
146
|
if (level > 5) {
|
|
109
147
|
return null;
|
|
110
148
|
}
|
|
149
|
+
const makeUpRandomData = !!options?.emptyString;
|
|
111
150
|
if (options?.mode === "write" && schema.readOnly) {
|
|
112
151
|
return void 0;
|
|
113
152
|
}
|
|
@@ -236,7 +275,7 @@ const getExampleFromSchema = (schema, options, level = 0) => {
|
|
|
236
275
|
return [];
|
|
237
276
|
}
|
|
238
277
|
const exampleValues = {
|
|
239
|
-
string: options?.emptyString
|
|
278
|
+
string: makeUpRandomData ? guessFromFormat(schema, options?.emptyString) : "",
|
|
240
279
|
boolean: true,
|
|
241
280
|
integer: schema.min ?? 1,
|
|
242
281
|
number: schema.min ?? 1,
|
|
@@ -260,6 +299,15 @@ const getExampleFromSchema = (schema, options, level = 0) => {
|
|
|
260
299
|
});
|
|
261
300
|
return example;
|
|
262
301
|
}
|
|
302
|
+
if (Array.isArray(schema.type)) {
|
|
303
|
+
if (schema.type.includes("null")) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
const exampleValue = exampleValues[schema.type[0]];
|
|
307
|
+
if (exampleValue !== void 0) {
|
|
308
|
+
return exampleValue;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
263
311
|
console.warn(`[getExampleFromSchema] Unknown property type "${schema.type}".`);
|
|
264
312
|
return null;
|
|
265
313
|
};
|
|
@@ -374,6 +422,9 @@ function json2xml(data, tab) {
|
|
|
374
422
|
}
|
|
375
423
|
|
|
376
424
|
function normalizeMimeType(contentType) {
|
|
425
|
+
if (typeof contentType !== "string") {
|
|
426
|
+
return void 0;
|
|
427
|
+
}
|
|
377
428
|
return contentType.replace(/;.*$/, "").replace(/\/.+\+/, "/").trim();
|
|
378
429
|
}
|
|
379
430
|
|
|
@@ -386,6 +437,9 @@ function normalizeMimeTypeObject(content) {
|
|
|
386
437
|
};
|
|
387
438
|
Object.keys(newContent).forEach((key) => {
|
|
388
439
|
const newKey = normalizeMimeType(key);
|
|
440
|
+
if (newKey === void 0) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
389
443
|
newContent[newKey] = newContent[key];
|
|
390
444
|
if (key !== newKey) {
|
|
391
445
|
delete newContent[key];
|
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import type { ContentType } from './types';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes a MIME type to a standard format.
|
|
4
|
+
*
|
|
5
|
+
* Input: application/problem+json; charset=utf-8
|
|
6
|
+
* Output: application/json
|
|
7
|
+
*/
|
|
8
|
+
export declare function normalizeMimeType(contentType?: string): ContentType | undefined;
|
|
3
9
|
//# sourceMappingURL=normalizeMimeType.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeMimeType.d.ts","sourceRoot":"","sources":["../src/normalizeMimeType.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"normalizeMimeType.d.ts","sourceRoot":"","sources":["../src/normalizeMimeType.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,CAAC,EAAE,MAAM,2BAcrD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeMimeTypeObject.d.ts","sourceRoot":"","sources":["../src/normalizeMimeTypeObject.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC;;;;;;;;
|
|
1
|
+
{"version":3,"file":"normalizeMimeTypeObject.d.ts","sourceRoot":"","sources":["../src/normalizeMimeTypeObject.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC;;;;;;;;cA8BzE"}
|