@scalar/oas-utils 0.2.53 → 0.2.54

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 CHANGED
@@ -1,5 +1,20 @@
1
1
  # @scalar/oas-utils
2
2
 
3
+ ## 0.2.54
4
+
5
+ ### Patch Changes
6
+
7
+ - 2ff5905: chore: cache results of getExampleFromSchema
8
+ - 9a2d5ca: fix: schema model console log typo
9
+ - e911047: Add default exports
10
+ - Updated dependencies [e911047]
11
+ - Updated dependencies [d02d70c]
12
+ - Updated dependencies [3acbf26]
13
+ - @scalar/object-utils@1.1.10
14
+ - @scalar/openapi-types@0.1.2
15
+ - @scalar/types@0.0.14
16
+ - @scalar/themes@0.9.36
17
+
3
18
  ## 0.2.53
4
19
 
5
20
  ### Patch Changes
@@ -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('Recieved: ', data);
7
+ console.log('Received: ', data);
8
8
  console.groupEnd();
9
9
  }
10
10
  if (throwError && !result.success)
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This function takes a properties object and generates an example response content.
2
+ * This function takes an OpenAPI schema and generates an example from it
3
3
  */
4
4
  export declare const getExampleFromSchema: (schema: Record<string, any>, options?: {
5
5
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../../src/spec-getters/getExampleFromSchema.ts"],"names":[],"mappings":"AAyCA;;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,GAgRF,CAAA"}
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 = 10;
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 a properties object and generates an example response content.
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.53",
19
+ "version": "0.2.54",
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.9",
96
- "@scalar/openapi-types": "0.1.1",
97
- "@scalar/types": "0.0.13",
98
- "@scalar/themes": "0.9.35"
109
+ "@scalar/object-utils": "1.1.10",
110
+ "@scalar/themes": "0.9.36",
111
+ "@scalar/openapi-types": "0.1.2",
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.10",
107
- "@scalar/openapi-parser": "0.8.5",
108
- "@scalar/openapi-types": "0.1.1"
120
+ "@scalar/build-tooling": "0.1.11",
121
+ "@scalar/openapi-parser": "0.8.6",
122
+ "@scalar/openapi-types": "0.1.2"
109
123
  },
110
124
  "scripts": {
111
125
  "build": "scalar-build-rollup",