@scalar/oas-utils 0.2.53 → 0.2.55
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 +21 -0
- package/dist/helpers/parse.js +1 -1
- package/dist/helpers/schema-model.js +1 -1
- package/dist/spec-getters/getExampleFromSchema.d.ts +1 -1
- package/dist/spec-getters/getExampleFromSchema.d.ts.map +1 -1
- package/dist/spec-getters/getExampleFromSchema.js +29 -14
- package/package.json +36 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @scalar/oas-utils
|
|
2
2
|
|
|
3
|
+
## 0.2.55
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 0ddd4f3: feat: added virtual text component
|
|
8
|
+
|
|
9
|
+
## 0.2.54
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 2ff5905: chore: cache results of getExampleFromSchema
|
|
14
|
+
- 9a2d5ca: fix: schema model console log typo
|
|
15
|
+
- e911047: Add default exports
|
|
16
|
+
- Updated dependencies [e911047]
|
|
17
|
+
- Updated dependencies [d02d70c]
|
|
18
|
+
- Updated dependencies [3acbf26]
|
|
19
|
+
- @scalar/object-utils@1.1.10
|
|
20
|
+
- @scalar/openapi-types@0.1.2
|
|
21
|
+
- @scalar/types@0.0.14
|
|
22
|
+
- @scalar/themes@0.9.36
|
|
23
|
+
|
|
3
24
|
## 0.2.53
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
package/dist/helpers/parse.js
CHANGED
|
@@ -59,7 +59,7 @@ const transformToJson = (value) => {
|
|
|
59
59
|
function formatJsonOrYamlString(value) {
|
|
60
60
|
// If we don't start with a bracket assume yaml
|
|
61
61
|
const trimmed = value.trim();
|
|
62
|
-
if (trimmed[0] !== '{')
|
|
62
|
+
if (trimmed[0] !== '{' && trimmed[0] !== '[')
|
|
63
63
|
return value;
|
|
64
64
|
try {
|
|
65
65
|
// JSON
|
|
@@ -4,7 +4,7 @@ function schemaModel(data, schema, throwError = true, errorLocation) {
|
|
|
4
4
|
if (!result.success) {
|
|
5
5
|
console.group('Schema Error' + (errorLocation ? ` - ${errorLocation}` : ''));
|
|
6
6
|
console.warn(JSON.stringify(result.error.format(), null, 2));
|
|
7
|
-
console.log('
|
|
7
|
+
console.log('Received: ', data);
|
|
8
8
|
console.groupEnd();
|
|
9
9
|
}
|
|
10
10
|
if (throwError && !result.success)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../../src/spec-getters/getExampleFromSchema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../../src/spec-getters/getExampleFromSchema.ts"],"names":[],"mappings":"AAwDA;;GAEG;AACH,eAAO,MAAM,oBAAoB,WACvB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,YACjB;IACR;;;OAGG;IACH,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,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC/B;;OAEG;IACH,8BAA8B,CAAC,EAAE,OAAO,CAAA;CACzC,UACM,MAAM,iBACE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,SAC3B,MAAM,KACZ,GA2RF,CAAA"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Hard limit for rendering circular references */
|
|
2
|
-
const MAX_LEVELS_DEEP =
|
|
2
|
+
const MAX_LEVELS_DEEP = 5;
|
|
3
3
|
const genericExampleValues = {
|
|
4
4
|
// 'date-time': '1970-01-01T00:00:00Z',
|
|
5
5
|
'date-time': new Date().toISOString(),
|
|
@@ -36,10 +36,25 @@ const genericExampleValues = {
|
|
|
36
36
|
function guessFromFormat(schema, fallback = '') {
|
|
37
37
|
return genericExampleValues[schema.format] ?? fallback;
|
|
38
38
|
}
|
|
39
|
+
/** Map of all the results */
|
|
40
|
+
const resultCache = new WeakMap();
|
|
41
|
+
/** Store result in the cache, and return the result */
|
|
42
|
+
function cache(schema, result) {
|
|
43
|
+
// Avoid unnecessary WeakMap operations for primitive values
|
|
44
|
+
if (typeof result !== 'object' || result === null) {
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
resultCache.set(schema, result);
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
39
50
|
/**
|
|
40
|
-
* This function takes
|
|
51
|
+
* This function takes an OpenAPI schema and generates an example from it
|
|
41
52
|
*/
|
|
42
53
|
const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) => {
|
|
54
|
+
// Check if the result is already cached
|
|
55
|
+
if (resultCache.has(schema)) {
|
|
56
|
+
return resultCache.get(schema);
|
|
57
|
+
}
|
|
43
58
|
// Check whether it’s a circular reference
|
|
44
59
|
if (level === MAX_LEVELS_DEEP + 1) {
|
|
45
60
|
try {
|
|
@@ -67,24 +82,24 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
|
|
|
67
82
|
if (schema.type === 'number' || schema.type === 'integer') {
|
|
68
83
|
return parseInt(value, 10);
|
|
69
84
|
}
|
|
70
|
-
return value;
|
|
85
|
+
return cache(schema, value);
|
|
71
86
|
}
|
|
72
87
|
}
|
|
73
88
|
// Use the first example, if there’s an array
|
|
74
89
|
if (Array.isArray(schema.examples) && schema.examples.length > 0) {
|
|
75
|
-
return schema.examples[0];
|
|
90
|
+
return cache(schema, schema.examples[0]);
|
|
76
91
|
}
|
|
77
92
|
// Use an example, if there’s one
|
|
78
93
|
if (schema.example !== undefined) {
|
|
79
|
-
return schema.example;
|
|
94
|
+
return cache(schema, schema.example);
|
|
80
95
|
}
|
|
81
96
|
// Use a default value, if there’s one
|
|
82
97
|
if (schema.default !== undefined) {
|
|
83
|
-
return schema.default;
|
|
98
|
+
return cache(schema, schema.default);
|
|
84
99
|
}
|
|
85
100
|
// enum: [ 'available', 'pending', 'sold' ]
|
|
86
101
|
if (Array.isArray(schema.enum) && schema.enum.length > 0) {
|
|
87
|
-
return schema.enum[0];
|
|
102
|
+
return cache(schema, schema.enum[0]);
|
|
88
103
|
}
|
|
89
104
|
// Check if the property is required
|
|
90
105
|
const isObjectOrArray = schema.type === 'object' ||
|
|
@@ -144,14 +159,14 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
|
|
|
144
159
|
.map((item) => getExampleFromSchema(item, options, level + 1, schema))
|
|
145
160
|
.filter((item) => item !== undefined));
|
|
146
161
|
}
|
|
147
|
-
return response;
|
|
162
|
+
return cache(schema, response);
|
|
148
163
|
}
|
|
149
164
|
// Array
|
|
150
165
|
if (schema.type === 'array' || schema.items !== undefined) {
|
|
151
166
|
const itemsXmlTagName = schema?.items?.xml?.name;
|
|
152
167
|
const wrapItems = !!(options?.xml && schema.xml?.wrapped && itemsXmlTagName);
|
|
153
168
|
if (schema.example !== undefined) {
|
|
154
|
-
return wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example;
|
|
169
|
+
return cache(schema, wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example);
|
|
155
170
|
}
|
|
156
171
|
// Check whether the array has a anyOf, oneOf, or allOf rule
|
|
157
172
|
if (schema.items) {
|
|
@@ -171,9 +186,9 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
|
|
|
171
186
|
const exampleFromRule = schemas
|
|
172
187
|
.map((item) => getExampleFromSchema(item, options, level + 1, schema))
|
|
173
188
|
.filter((item) => item !== undefined);
|
|
174
|
-
return wrapItems
|
|
189
|
+
return cache(schema, wrapItems
|
|
175
190
|
? [{ [itemsXmlTagName]: exampleFromRule }]
|
|
176
|
-
: exampleFromRule;
|
|
191
|
+
: exampleFromRule);
|
|
177
192
|
}
|
|
178
193
|
}
|
|
179
194
|
if (schema.items?.type) {
|
|
@@ -194,7 +209,7 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
|
|
|
194
209
|
array: [],
|
|
195
210
|
};
|
|
196
211
|
if (schema.type !== undefined && exampleValues[schema.type] !== undefined) {
|
|
197
|
-
return exampleValues[schema.type];
|
|
212
|
+
return cache(schema, exampleValues[schema.type]);
|
|
198
213
|
}
|
|
199
214
|
const discriminateSchema = schema.oneOf || schema.anyOf;
|
|
200
215
|
// Check if property has the `oneOf` | `anyOf` key
|
|
@@ -222,7 +237,7 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
|
|
|
222
237
|
? [...(example ?? {}), ...newExample]
|
|
223
238
|
: newExample;
|
|
224
239
|
});
|
|
225
|
-
return example;
|
|
240
|
+
return cache(schema, example);
|
|
226
241
|
}
|
|
227
242
|
// Check if schema is a union type
|
|
228
243
|
if (Array.isArray(schema.type)) {
|
|
@@ -233,7 +248,7 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
|
|
|
233
248
|
// Return an example for the first type in the union
|
|
234
249
|
const exampleValue = exampleValues[schema.type[0]];
|
|
235
250
|
if (exampleValue !== undefined) {
|
|
236
|
-
return exampleValue;
|
|
251
|
+
return cache(schema, exampleValue);
|
|
237
252
|
}
|
|
238
253
|
}
|
|
239
254
|
// Warn if the type is unknown …
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"specification",
|
|
17
17
|
"yaml"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.2.
|
|
19
|
+
"version": "0.2.55",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=18"
|
|
22
22
|
},
|
|
@@ -25,59 +25,73 @@
|
|
|
25
25
|
"exports": {
|
|
26
26
|
"./transforms": {
|
|
27
27
|
"import": "./dist/transforms/index.js",
|
|
28
|
-
"types": "./dist/transforms/index.d.ts"
|
|
28
|
+
"types": "./dist/transforms/index.d.ts",
|
|
29
|
+
"default": "./dist/transforms/index.js"
|
|
29
30
|
},
|
|
30
31
|
"./spec-getters": {
|
|
31
32
|
"import": "./dist/spec-getters/index.js",
|
|
32
|
-
"types": "./dist/spec-getters/index.d.ts"
|
|
33
|
+
"types": "./dist/spec-getters/index.d.ts",
|
|
34
|
+
"default": "./dist/spec-getters/index.js"
|
|
33
35
|
},
|
|
34
36
|
"./migrations": {
|
|
35
37
|
"import": "./dist/migrations/index.js",
|
|
36
|
-
"types": "./dist/migrations/index.d.ts"
|
|
38
|
+
"types": "./dist/migrations/index.d.ts",
|
|
39
|
+
"default": "./dist/migrations/index.js"
|
|
37
40
|
},
|
|
38
41
|
"./migrations/v-2.1.0": {
|
|
39
42
|
"import": "./dist/migrations/v-2.1.0/index.js",
|
|
40
|
-
"types": "./dist/migrations/v-2.1.0/index.d.ts"
|
|
43
|
+
"types": "./dist/migrations/v-2.1.0/index.d.ts",
|
|
44
|
+
"default": "./dist/migrations/v-2.1.0/index.js"
|
|
41
45
|
},
|
|
42
46
|
"./migrations/v-0.0.0": {
|
|
43
47
|
"import": "./dist/migrations/v-0.0.0/index.js",
|
|
44
|
-
"types": "./dist/migrations/v-0.0.0/index.d.ts"
|
|
48
|
+
"types": "./dist/migrations/v-0.0.0/index.d.ts",
|
|
49
|
+
"default": "./dist/migrations/v-0.0.0/index.js"
|
|
45
50
|
},
|
|
46
51
|
"./helpers": {
|
|
47
52
|
"import": "./dist/helpers/index.js",
|
|
48
|
-
"types": "./dist/helpers/index.d.ts"
|
|
53
|
+
"types": "./dist/helpers/index.d.ts",
|
|
54
|
+
"default": "./dist/helpers/index.js"
|
|
49
55
|
},
|
|
50
56
|
"./entities": {
|
|
51
57
|
"import": "./dist/entities/index.js",
|
|
52
|
-
"types": "./dist/entities/index.d.ts"
|
|
58
|
+
"types": "./dist/entities/index.d.ts",
|
|
59
|
+
"default": "./dist/entities/index.js"
|
|
53
60
|
},
|
|
54
61
|
"./entities/workspace": {
|
|
55
62
|
"import": "./dist/entities/workspace/index.js",
|
|
56
|
-
"types": "./dist/entities/workspace/index.d.ts"
|
|
63
|
+
"types": "./dist/entities/workspace/index.d.ts",
|
|
64
|
+
"default": "./dist/entities/workspace/index.js"
|
|
57
65
|
},
|
|
58
66
|
"./entities/spec": {
|
|
59
67
|
"import": "./dist/entities/spec/index.js",
|
|
60
|
-
"types": "./dist/entities/spec/index.d.ts"
|
|
68
|
+
"types": "./dist/entities/spec/index.d.ts",
|
|
69
|
+
"default": "./dist/entities/spec/index.js"
|
|
61
70
|
},
|
|
62
71
|
"./entities/shared": {
|
|
63
72
|
"import": "./dist/entities/shared/index.js",
|
|
64
|
-
"types": "./dist/entities/shared/index.d.ts"
|
|
73
|
+
"types": "./dist/entities/shared/index.d.ts",
|
|
74
|
+
"default": "./dist/entities/shared/index.js"
|
|
65
75
|
},
|
|
66
76
|
"./entities/hotkeys": {
|
|
67
77
|
"import": "./dist/entities/hotkeys/index.js",
|
|
68
|
-
"types": "./dist/entities/hotkeys/index.d.ts"
|
|
78
|
+
"types": "./dist/entities/hotkeys/index.d.ts",
|
|
79
|
+
"default": "./dist/entities/hotkeys/index.js"
|
|
69
80
|
},
|
|
70
81
|
"./entities/environment": {
|
|
71
82
|
"import": "./dist/entities/environment/index.js",
|
|
72
|
-
"types": "./dist/entities/environment/index.d.ts"
|
|
83
|
+
"types": "./dist/entities/environment/index.d.ts",
|
|
84
|
+
"default": "./dist/entities/environment/index.js"
|
|
73
85
|
},
|
|
74
86
|
"./entities/cookie": {
|
|
75
87
|
"import": "./dist/entities/cookie/index.js",
|
|
76
|
-
"types": "./dist/entities/cookie/index.d.ts"
|
|
88
|
+
"types": "./dist/entities/cookie/index.d.ts",
|
|
89
|
+
"default": "./dist/entities/cookie/index.js"
|
|
77
90
|
},
|
|
78
91
|
"./diff": {
|
|
79
92
|
"import": "./dist/diff/index.js",
|
|
80
|
-
"types": "./dist/diff/index.d.ts"
|
|
93
|
+
"types": "./dist/diff/index.d.ts",
|
|
94
|
+
"default": "./dist/diff/index.js"
|
|
81
95
|
}
|
|
82
96
|
},
|
|
83
97
|
"files": [
|
|
@@ -92,10 +106,10 @@
|
|
|
92
106
|
"nanoid": "^5.0.7",
|
|
93
107
|
"yaml": "^2.4.5",
|
|
94
108
|
"zod": "^3.23.8",
|
|
95
|
-
"@scalar/object-utils": "1.1.
|
|
96
|
-
"@scalar/openapi-types": "0.1.
|
|
97
|
-
"@scalar/
|
|
98
|
-
"@scalar/
|
|
109
|
+
"@scalar/object-utils": "1.1.10",
|
|
110
|
+
"@scalar/openapi-types": "0.1.2",
|
|
111
|
+
"@scalar/themes": "0.9.36",
|
|
112
|
+
"@scalar/types": "0.0.14"
|
|
99
113
|
},
|
|
100
114
|
"devDependencies": {
|
|
101
115
|
"rollup": "^4.16.4",
|
|
@@ -103,9 +117,9 @@
|
|
|
103
117
|
"vite": "^5.2.10",
|
|
104
118
|
"vitest": "^1.6.0",
|
|
105
119
|
"zod-to-ts": "^1.2.0",
|
|
106
|
-
"@scalar/build-tooling": "0.1.
|
|
107
|
-
"@scalar/openapi-
|
|
108
|
-
"@scalar/openapi-
|
|
120
|
+
"@scalar/build-tooling": "0.1.11",
|
|
121
|
+
"@scalar/openapi-types": "0.1.2",
|
|
122
|
+
"@scalar/openapi-parser": "0.8.6"
|
|
109
123
|
},
|
|
110
124
|
"scripts": {
|
|
111
125
|
"build": "scalar-build-rollup",
|