@scalar/oas-utils 0.11.0 → 0.12.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.
Files changed (126) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/helpers/index.d.ts +0 -82
  3. package/dist/helpers/index.d.ts.map +1 -1
  4. package/dist/helpers/index.js +0 -78
  5. package/dist/migrations/migrate-to-indexdb.d.ts.map +1 -1
  6. package/dist/migrations/migrate-to-indexdb.js +1 -1
  7. package/dist/migrations/v-2.5.0/types.generated.d.ts +428 -34
  8. package/dist/migrations/v-2.5.0/types.generated.d.ts.map +1 -1
  9. package/package.json +8 -62
  10. package/dist/entities/cookie/cookie.d.ts +0 -15
  11. package/dist/entities/cookie/cookie.d.ts.map +0 -1
  12. package/dist/entities/cookie/cookie.js +0 -12
  13. package/dist/entities/cookie/index.d.ts +0 -2
  14. package/dist/entities/cookie/index.d.ts.map +0 -1
  15. package/dist/entities/cookie/index.js +0 -1
  16. package/dist/entities/environment/environment.d.ts +0 -12
  17. package/dist/entities/environment/environment.d.ts.map +0 -1
  18. package/dist/entities/environment/environment.js +0 -8
  19. package/dist/entities/environment/index.d.ts +0 -2
  20. package/dist/entities/environment/index.d.ts.map +0 -1
  21. package/dist/entities/environment/index.js +0 -1
  22. package/dist/entities/hotkeys/hotkeys.d.ts +0 -15
  23. package/dist/entities/hotkeys/hotkeys.d.ts.map +0 -1
  24. package/dist/entities/hotkeys/hotkeys.js +0 -118
  25. package/dist/entities/hotkeys/index.d.ts +0 -2
  26. package/dist/entities/hotkeys/index.d.ts.map +0 -1
  27. package/dist/entities/hotkeys/index.js +0 -1
  28. package/dist/entities/shared/index.d.ts +0 -2
  29. package/dist/entities/shared/index.d.ts.map +0 -1
  30. package/dist/entities/shared/index.js +0 -1
  31. package/dist/entities/shared/utility.d.ts +0 -5
  32. package/dist/entities/shared/utility.d.ts.map +0 -1
  33. package/dist/entities/shared/utility.js +0 -9
  34. package/dist/entities/spec/collection.d.ts +0 -133
  35. package/dist/entities/spec/collection.d.ts.map +0 -1
  36. package/dist/entities/spec/collection.js +0 -95
  37. package/dist/entities/spec/index.d.ts +0 -12
  38. package/dist/entities/spec/index.d.ts.map +0 -1
  39. package/dist/entities/spec/index.js +0 -10
  40. package/dist/entities/spec/operation.d.ts +0 -99
  41. package/dist/entities/spec/operation.d.ts.map +0 -1
  42. package/dist/entities/spec/operation.js +0 -6
  43. package/dist/entities/spec/parameters.d.ts +0 -44
  44. package/dist/entities/spec/parameters.d.ts.map +0 -1
  45. package/dist/entities/spec/parameters.js +0 -41
  46. package/dist/entities/spec/request-examples.d.ts +0 -380
  47. package/dist/entities/spec/request-examples.d.ts.map +0 -1
  48. package/dist/entities/spec/request-examples.js +0 -449
  49. package/dist/entities/spec/requests.d.ts +0 -137
  50. package/dist/entities/spec/requests.d.ts.map +0 -1
  51. package/dist/entities/spec/requests.js +0 -123
  52. package/dist/entities/spec/server.d.ts +0 -15
  53. package/dist/entities/spec/server.d.ts.map +0 -1
  54. package/dist/entities/spec/server.js +0 -67
  55. package/dist/entities/spec/spec-objects.d.ts +0 -171
  56. package/dist/entities/spec/spec-objects.d.ts.map +0 -1
  57. package/dist/entities/spec/spec-objects.js +0 -134
  58. package/dist/entities/spec/x-scalar-environments.d.ts +0 -22
  59. package/dist/entities/spec/x-scalar-environments.d.ts.map +0 -1
  60. package/dist/entities/spec/x-scalar-environments.js +0 -22
  61. package/dist/entities/spec/x-scalar-secrets.d.ts +0 -6
  62. package/dist/entities/spec/x-scalar-secrets.d.ts.map +0 -1
  63. package/dist/entities/spec/x-scalar-secrets.js +0 -8
  64. package/dist/entities/workspace/index.d.ts +0 -2
  65. package/dist/entities/workspace/index.d.ts.map +0 -1
  66. package/dist/entities/workspace/index.js +0 -1
  67. package/dist/entities/workspace/workspace.d.ts +0 -236
  68. package/dist/entities/workspace/workspace.d.ts.map +0 -1
  69. package/dist/entities/workspace/workspace.js +0 -49
  70. package/dist/helpers/fetch-document.d.ts +0 -7
  71. package/dist/helpers/fetch-document.d.ts.map +0 -1
  72. package/dist/helpers/fetch-document.js +0 -32
  73. package/dist/helpers/fetch-with-proxy-fallback.d.ts +0 -15
  74. package/dist/helpers/fetch-with-proxy-fallback.d.ts.map +0 -1
  75. package/dist/helpers/fetch-with-proxy-fallback.js +0 -29
  76. package/dist/helpers/normalize-mime-type-object.d.ts +0 -8
  77. package/dist/helpers/normalize-mime-type-object.d.ts.map +0 -1
  78. package/dist/helpers/normalize-mime-type-object.js +0 -21
  79. package/dist/helpers/normalize-mime-type.d.ts +0 -10
  80. package/dist/helpers/normalize-mime-type.d.ts.map +0 -1
  81. package/dist/helpers/normalize-mime-type.js +0 -12
  82. package/dist/helpers/operation-stability.d.ts +0 -19
  83. package/dist/helpers/operation-stability.d.ts.map +0 -1
  84. package/dist/helpers/operation-stability.js +0 -24
  85. package/dist/helpers/schema-model.d.ts +0 -5
  86. package/dist/helpers/schema-model.d.ts.map +0 -1
  87. package/dist/helpers/schema-model.js +0 -14
  88. package/dist/helpers/security/get-schemes.d.ts +0 -97
  89. package/dist/helpers/security/get-schemes.d.ts.map +0 -1
  90. package/dist/helpers/security/get-schemes.js +0 -8
  91. package/dist/helpers/security/has-token.d.ts +0 -4
  92. package/dist/helpers/security/has-token.d.ts.map +0 -1
  93. package/dist/helpers/security/has-token.js +0 -19
  94. package/dist/helpers/security/index.d.ts +0 -3
  95. package/dist/helpers/security/index.d.ts.map +0 -1
  96. package/dist/helpers/security/index.js +0 -2
  97. package/dist/helpers/servers.d.ts +0 -24
  98. package/dist/helpers/servers.d.ts.map +0 -1
  99. package/dist/helpers/servers.js +0 -146
  100. package/dist/helpers/should-ignore-entity.d.ts +0 -8
  101. package/dist/helpers/should-ignore-entity.d.ts.map +0 -1
  102. package/dist/helpers/should-ignore-entity.js +0 -4
  103. package/dist/migrations/generate-types.d.ts +0 -2
  104. package/dist/migrations/generate-types.d.ts.map +0 -1
  105. package/dist/migrations/generate-types.js +0 -53
  106. package/dist/spec-getters/get-example-from-schema.d.ts +0 -36
  107. package/dist/spec-getters/get-example-from-schema.d.ts.map +0 -1
  108. package/dist/spec-getters/get-example-from-schema.js +0 -521
  109. package/dist/spec-getters/get-parameters-from-operation.d.ts +0 -17
  110. package/dist/spec-getters/get-parameters-from-operation.d.ts.map +0 -1
  111. package/dist/spec-getters/get-parameters-from-operation.js +0 -40
  112. package/dist/spec-getters/get-request-body-from-operation.d.ts +0 -14
  113. package/dist/spec-getters/get-request-body-from-operation.d.ts.map +0 -1
  114. package/dist/spec-getters/get-request-body-from-operation.js +0 -176
  115. package/dist/spec-getters/get-server-variable-examples.d.ts +0 -4
  116. package/dist/spec-getters/get-server-variable-examples.d.ts.map +0 -1
  117. package/dist/spec-getters/get-server-variable-examples.js +0 -12
  118. package/dist/spec-getters/index.d.ts +0 -5
  119. package/dist/spec-getters/index.d.ts.map +0 -1
  120. package/dist/spec-getters/index.js +0 -4
  121. package/dist/transforms/import-spec.d.ts +0 -64
  122. package/dist/transforms/import-spec.d.ts.map +0 -1
  123. package/dist/transforms/import-spec.js +0 -397
  124. package/dist/transforms/index.d.ts +0 -2
  125. package/dist/transforms/index.d.ts.map +0 -1
  126. package/dist/transforms/index.js +0 -1
@@ -1,521 +0,0 @@
1
- import { isDefined } from '@scalar/helpers/array/is-defined';
2
- import { getRaw } from '@scalar/json-magic/magic-proxy';
3
- import { getResolvedRef } from '@scalar/workspace-store/helpers/get-resolved-ref';
4
- import { unpackOverridesProxy } from '@scalar/workspace-store/helpers/overrides-proxy';
5
- /** Maximum recursion depth to prevent infinite loops in circular references */
6
- const MAX_LEVELS_DEEP = 10;
7
- /** Default name used for additional properties when no custom name is provided */
8
- const DEFAULT_ADDITIONAL_PROPERTIES_NAME = 'additionalProperty';
9
- /**
10
- * Pre-computed date/time values to avoid expensive Date operations on every call.
11
- * These are calculated once at module load time for better performance.
12
- */
13
- const currentISOString = new Date().toISOString();
14
- const currentDateString = currentISOString.split('T')[0];
15
- const currentTimeString = currentISOString.split('T')[1].split('.')[0];
16
- /**
17
- * Mapping of OpenAPI string formats to example values.
18
- * Used to generate realistic examples for different string formats.
19
- */
20
- const genericExampleValues = {
21
- 'date-time': currentISOString,
22
- 'date': currentDateString,
23
- 'email': 'hello@example.com',
24
- 'hostname': 'example.com',
25
- // https://tools.ietf.org/html/rfc6531#section-3.3
26
- 'idn-email': 'jane.doe@example.com',
27
- // https://tools.ietf.org/html/rfc5890#section-2.3.2.3
28
- 'idn-hostname': 'example.com',
29
- 'ipv4': '127.0.0.1',
30
- 'ipv6': '51d4:7fab:bfbf:b7d7:b2cb:d4b4:3dad:d998',
31
- 'iri-reference': '/entitiy/1',
32
- // https://tools.ietf.org/html/rfc3987
33
- 'iri': 'https://example.com/entity/123',
34
- 'json-pointer': '/nested/objects',
35
- 'password': 'super-secret',
36
- 'regex': '/[a-z]/',
37
- // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
38
- 'relative-json-pointer': '1/nested/objects',
39
- // full-time in https://tools.ietf.org/html/rfc3339#section-5.6
40
- 'time': currentTimeString,
41
- // either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
42
- 'uri-reference': '../folder',
43
- 'uri-template': 'https://example.com/{id}',
44
- 'uri': 'https://example.com',
45
- 'uuid': '123e4567-e89b-12d3-a456-426614174000',
46
- 'object-id': '6592008029c8c3e4dc76256c',
47
- };
48
- /**
49
- * Extract enum values from the propertyNames keyword of an object schema.
50
- * JSON Schema's propertyNames constrains which keys are valid in a map/dict.
51
- */
52
- const getPropertyNamesEnumValues = (schema) => {
53
- if (!('propertyNames' in schema) || !schema.propertyNames) {
54
- return undefined;
55
- }
56
- const resolved = getResolvedRef(schema.propertyNames);
57
- if (resolved && 'enum' in resolved && Array.isArray(resolved.enum) && resolved.enum.length > 0) {
58
- return resolved.enum;
59
- }
60
- return undefined;
61
- };
62
- /**
63
- * Generate example values for string types based on their format.
64
- * Special handling for binary format which returns a File object.
65
- */
66
- const guessFromFormat = (schema, makeUpRandomData = false, fallback = '') => {
67
- // Handle binary format specially - return a File object
68
- if ('type' in schema && schema.type === 'string' && 'format' in schema && schema.format === 'binary') {
69
- return new File([''], 'filename');
70
- }
71
- // Return format-specific example if we have one and are making up data
72
- if (makeUpRandomData && 'format' in schema && schema.format) {
73
- return genericExampleValues[schema.format] ?? fallback;
74
- }
75
- return fallback;
76
- };
77
- /**
78
- * WeakMap cache for memoizing resolved example results.
79
- * Uses the resolved schema object as the key for efficient lookups.
80
- */
81
- const resultCache = new WeakMap();
82
- /** Cache required property names per parent schema for O(1) membership checks */
83
- const requiredNamesCache = new WeakMap();
84
- /**
85
- * Retrieves the set of required property names from a schema.
86
- * Caches the result in a WeakMap for efficient lookups.
87
- */
88
- const getRequiredNames = (parentSchema) => {
89
- if (!parentSchema) {
90
- return undefined;
91
- }
92
- const cached = requiredNamesCache.get(parentSchema);
93
- if (cached) {
94
- return cached;
95
- }
96
- if ('required' in parentSchema) {
97
- const required = parentSchema.required;
98
- if (Array.isArray(required) && required.length > 0) {
99
- const set = new Set(required);
100
- requiredNamesCache.set(parentSchema, set);
101
- return set;
102
- }
103
- }
104
- return undefined;
105
- };
106
- /**
107
- * Cache the result for a schema if it is an object type.
108
- * Primitive values are not cached to avoid unnecessary WeakMap operations.
109
- */
110
- const cache = (schema, result) => {
111
- if (typeof result !== 'object' || result === null) {
112
- return result;
113
- }
114
- resultCache.set(getRaw(unpackOverridesProxy(schema)), result);
115
- return result;
116
- };
117
- /**
118
- * Check if a schema uses composition keywords (allOf, oneOf, anyOf).
119
- * These require special handling for merging or selecting schemas.
120
- */
121
- const isComposed = (schema) => !!(schema.allOf || schema.oneOf || schema.anyOf);
122
- /**
123
- * Determine if a property should be omitted based on the options.
124
- * Properties are omitted if they are not required and the option is enabled.
125
- */
126
- const shouldOmitProperty = (schema, parentSchema, propertyName, options) => {
127
- if (options?.omitEmptyAndOptionalProperties !== true) {
128
- return false;
129
- }
130
- // Never omit container types (objects/arrays) or composed schemas
131
- const isContainer = ('type' in schema && (schema.type === 'object' || schema.type === 'array')) || isComposed(schema);
132
- if (isContainer) {
133
- return false;
134
- }
135
- // Do not omit if explicit example-like values are present
136
- if (('examples' in schema && Array.isArray(schema.examples) && schema.examples.length > 0) ||
137
- ('example' in schema && schema.example !== undefined) ||
138
- ('default' in schema && schema.default !== undefined) ||
139
- ('const' in schema && schema.const !== undefined) ||
140
- ('enum' in schema && Array.isArray(schema.enum) && schema.enum.length > 0)) {
141
- return false;
142
- }
143
- // Check if the property is required
144
- const name = propertyName ?? schema.title ?? '';
145
- const requiredNames = getRequiredNames(parentSchema);
146
- const isRequired = requiredNames ? requiredNames.has(name) : false;
147
- return !isRequired;
148
- };
149
- /**
150
- * Merge two example values with predictable semantics.
151
- * Arrays are concatenated, objects are merged, otherwise the new value wins.
152
- */
153
- const mergeExamples = (baseValue, newValue) => {
154
- if (Array.isArray(baseValue) && Array.isArray(newValue)) {
155
- return [...baseValue, ...newValue];
156
- }
157
- if (baseValue && typeof baseValue === 'object' && newValue && typeof newValue === 'object') {
158
- return { ...baseValue, ...newValue };
159
- }
160
- return newValue;
161
- };
162
- /**
163
- * Build an example for an object schema, including properties, patternProperties,
164
- * additionalProperties, and composition (allOf/oneOf/anyOf) merging.
165
- */
166
- const handleObjectSchema = (schema, options, level, seen) => {
167
- const response = {};
168
- if ('properties' in schema && schema.properties) {
169
- const propertyNames = Object.keys(schema.properties);
170
- const limit = propertyNames.length;
171
- for (let i = 0; i < limit; i++) {
172
- const propertyName = propertyNames[i];
173
- const propertySchema = getResolvedRef(schema.properties[propertyName]);
174
- if (!propertySchema) {
175
- continue;
176
- }
177
- const propertyXmlName = options?.xml && 'xml' in propertySchema ? propertySchema.xml?.name : undefined;
178
- const value = getExampleFromSchema(propertySchema, options, {
179
- level: level + 1,
180
- parentSchema: schema,
181
- name: propertyName,
182
- seen,
183
- });
184
- if (typeof value !== 'undefined') {
185
- response[propertyXmlName ?? propertyName] = value;
186
- }
187
- }
188
- }
189
- if ('patternProperties' in schema && schema.patternProperties) {
190
- for (const pattern of Object.keys(schema.patternProperties)) {
191
- const propertySchema = getResolvedRef(schema.patternProperties[pattern]);
192
- if (!propertySchema) {
193
- continue;
194
- }
195
- response[pattern] = getExampleFromSchema(propertySchema, options, {
196
- level: level + 1,
197
- parentSchema: schema,
198
- name: pattern,
199
- seen,
200
- });
201
- }
202
- }
203
- if ('additionalProperties' in schema &&
204
- schema.additionalProperties !== undefined &&
205
- schema.additionalProperties !== false) {
206
- const additional = getResolvedRef(schema.additionalProperties);
207
- const isAnyType = schema.additionalProperties === true ||
208
- (typeof schema.additionalProperties === 'object' && Object.keys(schema.additionalProperties).length === 0);
209
- // Check for explicit x-additionalPropertiesName first
210
- const hasCustomName = typeof additional === 'object' &&
211
- 'x-additionalPropertiesName' in additional &&
212
- typeof additional['x-additionalPropertiesName'] === 'string' &&
213
- additional['x-additionalPropertiesName'].trim().length > 0;
214
- // Use propertyNames enum values as example keys when no custom name is set
215
- const propertyNamesEnum = hasCustomName ? undefined : getPropertyNamesEnumValues(schema);
216
- const additionalName = hasCustomName
217
- ? additional['x-additionalPropertiesName'].trim()
218
- : DEFAULT_ADDITIONAL_PROPERTIES_NAME;
219
- const additionalValue = isAnyType
220
- ? 'anything'
221
- : typeof additional === 'object'
222
- ? getExampleFromSchema(additional, options, {
223
- level: level + 1,
224
- seen,
225
- })
226
- : 'anything';
227
- if (propertyNamesEnum && propertyNamesEnum.length > 0) {
228
- // Use the first enum value as a realistic example key
229
- response[String(propertyNamesEnum[0])] = additionalValue;
230
- }
231
- else {
232
- response[additionalName] = additionalValue;
233
- }
234
- }
235
- // onOf
236
- if (schema.oneOf?.[0]) {
237
- Object.assign(response, getExampleFromSchema(getResolvedRef(schema.oneOf[0]), options, {
238
- level: level + 1,
239
- seen,
240
- }));
241
- }
242
- // anyOf
243
- else if (schema.anyOf?.[0]) {
244
- Object.assign(response, getExampleFromSchema(getResolvedRef(schema.anyOf[0]), options, {
245
- level: level + 1,
246
- seen,
247
- }));
248
- }
249
- // allOf
250
- else if (Array.isArray(schema.allOf) && schema.allOf.length > 0) {
251
- let merged = response;
252
- for (const item of schema.allOf) {
253
- const ex = getExampleFromSchema(getResolvedRef(item), options, {
254
- level: level + 1,
255
- parentSchema: schema,
256
- seen,
257
- });
258
- merged = mergeExamples(merged, ex);
259
- }
260
- if (merged && typeof merged === 'object') {
261
- Object.assign(response, merged);
262
- }
263
- }
264
- if (options?.xml && 'xml' in schema && schema.xml?.name && level === 0) {
265
- const wrapped = {};
266
- wrapped[schema.xml.name] = response;
267
- return cache(schema, wrapped);
268
- }
269
- return cache(schema, response);
270
- };
271
- /** Build an example for an array schema, including items, allOf, oneOf/anyOf, and XML wrapping */
272
- const handleArraySchema = (schema, options, level, seen) => {
273
- const items = 'items' in schema ? getResolvedRef(schema.items) : undefined;
274
- const itemsXmlTagName = items && typeof items === 'object' && 'xml' in items ? items.xml?.name : undefined;
275
- const wrapItems = !!(options?.xml && 'xml' in schema && schema.xml?.wrapped && itemsXmlTagName);
276
- if (schema.example !== undefined) {
277
- return cache(schema, wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example);
278
- }
279
- if (items && typeof items === 'object') {
280
- if (Array.isArray(items.allOf) && items.allOf.length > 0) {
281
- const allOf = items.allOf.filter(isDefined);
282
- const first = getResolvedRef(allOf[0]);
283
- if (first && typeof first === 'object' && 'type' in first && first.type === 'object') {
284
- const combined = { type: 'object', allOf };
285
- const merged = getExampleFromSchema(combined, options, {
286
- level: level + 1,
287
- parentSchema: schema,
288
- seen,
289
- });
290
- return cache(schema, wrapItems ? [{ [itemsXmlTagName]: merged }] : [merged]);
291
- }
292
- const examples = allOf
293
- .map((s) => getExampleFromSchema(getResolvedRef(s), options, {
294
- level: level + 1,
295
- parentSchema: schema,
296
- seen,
297
- }))
298
- .filter(isDefined);
299
- return cache(schema, wrapItems ? examples.map((e) => ({ [itemsXmlTagName]: e })) : examples);
300
- }
301
- const union = items.anyOf || items.oneOf;
302
- if (union && union.length > 0) {
303
- const first = union[0];
304
- const ex = getExampleFromSchema(getResolvedRef(first), options, {
305
- level: level + 1,
306
- parentSchema: schema,
307
- seen,
308
- });
309
- return cache(schema, wrapItems ? [{ [itemsXmlTagName]: ex }] : [ex]);
310
- }
311
- }
312
- const isObject = items && typeof items === 'object' && (('type' in items && items.type === 'object') || 'properties' in items);
313
- const isArray = items && typeof items === 'object' && (('type' in items && items.type === 'array') || 'items' in items);
314
- if (items && typeof items === 'object' && (('type' in items && items.type) || isObject || isArray)) {
315
- const ex = getExampleFromSchema(items, options, {
316
- level: level + 1,
317
- seen,
318
- });
319
- return cache(schema, wrapItems ? [{ [itemsXmlTagName]: ex }] : [ex]);
320
- }
321
- return cache(schema, []);
322
- };
323
- /** Return primitive example value for single-type schemas, or undefined if not primitive */
324
- const getPrimitiveValue = (schema, makeUpRandomData, emptyString) => {
325
- if ('type' in schema && schema.type && !Array.isArray(schema.type)) {
326
- switch (schema.type) {
327
- case 'string':
328
- return guessFromFormat(schema, makeUpRandomData, emptyString ?? '');
329
- case 'boolean':
330
- return true;
331
- case 'integer':
332
- return 'minimum' in schema && typeof schema.minimum === 'number' ? schema.minimum : 1;
333
- case 'number':
334
- return 'minimum' in schema && typeof schema.minimum === 'number' ? schema.minimum : 1;
335
- case 'array':
336
- return [];
337
- default:
338
- return undefined;
339
- }
340
- }
341
- return undefined;
342
- };
343
- /** Return primitive example value for union-type schemas (type: string[]) */
344
- const getUnionPrimitiveValue = (schema, makeUpRandomData, emptyString) => {
345
- if ('type' in schema && Array.isArray(schema.type)) {
346
- if (schema.type.includes('null')) {
347
- return null;
348
- }
349
- const first = schema.type[0];
350
- if (first) {
351
- switch (first) {
352
- case 'string':
353
- return guessFromFormat(schema, makeUpRandomData, emptyString ?? '');
354
- case 'boolean':
355
- return true;
356
- case 'integer':
357
- return 'minimum' in schema && typeof schema.minimum === 'number' ? schema.minimum : 1;
358
- case 'number':
359
- return 'minimum' in schema && typeof schema.minimum === 'number' ? schema.minimum : 1;
360
- case 'null':
361
- return null;
362
- default:
363
- return undefined;
364
- }
365
- }
366
- }
367
- return undefined;
368
- };
369
- /**
370
- * Generate an example value from a given OpenAPI SchemaObject.
371
- *
372
- * This function recursively processes OpenAPI schemas to create realistic example data.
373
- * It handles all OpenAPI schema types including primitives, objects, arrays, and
374
- * composition schemas (allOf, oneOf, anyOf).
375
- * Uses a tonne of caching for maximum performance.
376
- *
377
- * @param schema - The OpenAPI SchemaObject to generate an example from.
378
- * @param options - Options to customize example generation.
379
- * @param level - The current recursion depth.
380
- * @param parentSchema - The parent schema, if any.
381
- * @param name - The name of the property being processed.
382
- * @returns An example value for the given schema.
383
- *
384
- * @deprecated Use the one from `@scalar/api-client/v2/blocks/operation-code-sample` instead.
385
- */
386
- export const getExampleFromSchema = (schema, options, args) => {
387
- const { level = 0, parentSchema, name, seen = new WeakSet() } = args ?? {};
388
- // Resolve any $ref references to get the actual schema
389
- const _schema = getResolvedRef(schema);
390
- if (!isDefined(_schema)) {
391
- return undefined;
392
- }
393
- // Unpack from all proxies to get the raw schema object for cycle detection
394
- const targetValue = getRaw(unpackOverridesProxy(_schema));
395
- if (seen.has(targetValue)) {
396
- return undefined;
397
- }
398
- seen.add(targetValue);
399
- // Check cache first for performance - avoid recomputing the same schema
400
- if (resultCache.has(targetValue)) {
401
- seen.delete(targetValue);
402
- return resultCache.get(targetValue);
403
- }
404
- // Prevent infinite recursion in circular references
405
- if (level > MAX_LEVELS_DEEP) {
406
- seen.delete(targetValue);
407
- return '[Max Depth Exceeded]';
408
- }
409
- // Determine if we should generate realistic example data
410
- const makeUpRandomData = !!options?.emptyString;
411
- // Early exits for schemas that should not be included (deprecated, readOnly, writeOnly, omitEmptyAndOptionalProperties)
412
- if (_schema.deprecated ||
413
- (options?.mode === 'write' && _schema.readOnly) ||
414
- (options?.mode === 'read' && _schema.writeOnly) ||
415
- shouldOmitProperty(_schema, parentSchema, name, options)) {
416
- seen.delete(targetValue);
417
- return undefined;
418
- }
419
- // Handle custom variables (x-variable extension)
420
- if ('x-variable' in _schema && _schema['x-variable']) {
421
- const value = options?.variables?.[_schema['x-variable']];
422
- if (value !== undefined) {
423
- // Type coercion for numeric types
424
- if ('type' in _schema && (_schema.type === 'number' || _schema.type === 'integer')) {
425
- seen.delete(targetValue);
426
- return cache(_schema, Number(value));
427
- }
428
- seen.delete(targetValue);
429
- return cache(_schema, value);
430
- }
431
- }
432
- // Priority order: examples > example > default > const > enum
433
- if (Array.isArray(_schema.examples) && _schema.examples.length > 0) {
434
- seen.delete(targetValue);
435
- return cache(_schema, _schema.examples[0]);
436
- }
437
- if (_schema.example !== undefined) {
438
- seen.delete(targetValue);
439
- return cache(_schema, _schema.example);
440
- }
441
- if (_schema.default !== undefined) {
442
- seen.delete(targetValue);
443
- return cache(_schema, _schema.default);
444
- }
445
- if (_schema.const !== undefined) {
446
- seen.delete(targetValue);
447
- return cache(_schema, _schema.const);
448
- }
449
- if (Array.isArray(_schema.enum) && _schema.enum.length > 0) {
450
- seen.delete(targetValue);
451
- return cache(_schema, _schema.enum[0]);
452
- }
453
- // Handle object types - check for properties to identify objects
454
- if ('properties' in _schema || ('type' in _schema && _schema.type === 'object')) {
455
- const result = handleObjectSchema(_schema, options, level, seen);
456
- seen.delete(targetValue);
457
- return result;
458
- }
459
- // Handle array types
460
- if (('type' in _schema && _schema.type === 'array') || 'items' in _schema) {
461
- const result = handleArraySchema(_schema, options, level, seen);
462
- seen.delete(targetValue);
463
- return result;
464
- }
465
- // Handle primitive types without allocating temporary objects
466
- const primitive = getPrimitiveValue(_schema, makeUpRandomData, options?.emptyString);
467
- if (primitive !== undefined) {
468
- seen.delete(targetValue);
469
- return cache(_schema, primitive);
470
- }
471
- // Handle composition schemas (oneOf, anyOf) at root level
472
- const discriminate = _schema.oneOf || _schema.anyOf;
473
- if (Array.isArray(discriminate) && discriminate.length > 0) {
474
- // Find the first non-null type without allocating intermediate arrays
475
- for (const item of discriminate) {
476
- const resolved = getResolvedRef(item);
477
- if (resolved && (!('type' in resolved) || resolved.type !== 'null')) {
478
- seen.delete(targetValue);
479
- return cache(_schema, getExampleFromSchema(resolved, options, {
480
- level: level + 1,
481
- seen,
482
- }));
483
- }
484
- }
485
- seen.delete(targetValue);
486
- return cache(_schema, null);
487
- }
488
- // Handle allOf at root level (non-object/array schemas)
489
- if (Array.isArray(_schema.allOf) && _schema.allOf.length > 0) {
490
- let merged = undefined;
491
- const items = _schema.allOf;
492
- for (const item of items) {
493
- const ex = getExampleFromSchema(item, options, {
494
- level: level + 1,
495
- parentSchema: _schema,
496
- seen,
497
- });
498
- if (merged === undefined) {
499
- merged = ex;
500
- }
501
- else if (merged && typeof merged === 'object' && ex && typeof ex === 'object') {
502
- merged = mergeExamples(merged, ex);
503
- }
504
- else if (ex !== undefined) {
505
- // Prefer the latest defined primitive value
506
- merged = ex;
507
- }
508
- }
509
- seen.delete(targetValue);
510
- return cache(_schema, merged ?? null);
511
- }
512
- // Handle union types (array of types)
513
- const unionPrimitive = getUnionPrimitiveValue(_schema, makeUpRandomData, options?.emptyString);
514
- if (unionPrimitive !== undefined) {
515
- seen.delete(targetValue);
516
- return cache(_schema, unionPrimitive);
517
- }
518
- // Default fallback
519
- seen.delete(targetValue);
520
- return cache(_schema, null);
521
- };
@@ -1,17 +0,0 @@
1
- import type { OpenAPIV3_1 } from '@scalar/openapi-types';
2
- import type { Operation } from '../entities/spec/index.js';
3
- /**
4
- * Get the query parameters from an operation.
5
- *
6
- * Example: [ { name: 'foobar', value: '' } ]
7
- *
8
- * - OpenAPI 3.x: Possible values are "query", "header", "path" or "cookie".
9
- */
10
- export declare function getParametersFromOperation(operationParameters: Operation['parameters'], pathParameters: OpenAPIV3_1.ParameterObject[] | undefined, where: 'query' | 'header' | 'path' | 'cookie' | 'formData' | 'body', requiredOnly?: boolean): {
11
- name: string;
12
- description: string | null;
13
- value: any;
14
- required: boolean;
15
- enabled: boolean;
16
- }[];
17
- //# sourceMappingURL=get-parameters-from-operation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-parameters-from-operation.d.ts","sourceRoot":"","sources":["../../src/spec-getters/get-parameters-from-operation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAExD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAGhD;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,mBAAmB,EAAE,SAAS,CAAC,YAAY,CAAM,EACjD,cAAc,EAAE,WAAW,CAAC,eAAe,EAAE,YAAK,EAClD,KAAK,EAAE,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,GAAG,UAAU,GAAG,MAAM,EACnE,YAAY,GAAE,OAAc;;;;;;IAkC7B"}
@@ -1,40 +0,0 @@
1
- import { getExampleFromSchema } from './get-example-from-schema.js';
2
- /**
3
- * Get the query parameters from an operation.
4
- *
5
- * Example: [ { name: 'foobar', value: '' } ]
6
- *
7
- * - OpenAPI 3.x: Possible values are "query", "header", "path" or "cookie".
8
- */
9
- export function getParametersFromOperation(operationParameters = [], pathParameters = [], where, requiredOnly = true) {
10
- const parameters = [...(pathParameters || []), ...(operationParameters || [])];
11
- const params = parameters
12
- // query, path, header, cookie?
13
- .filter((parameter) => parameter.in === where)
14
- // don't add optional parameters
15
- .filter((parameter) => (requiredOnly && parameter.required) || !requiredOnly)
16
- // transform them
17
- .map((parameter) => ({
18
- name: parameter.name ?? 'Unknown Parameter',
19
- description: parameter.description ?? null,
20
- value: parameter.example
21
- ? parameter.example
22
- : parameter.schema
23
- ? getExampleFromSchema(parameter.schema, { mode: 'write' })
24
- : '',
25
- required: parameter.required ?? false,
26
- enabled: parameter.required ?? false,
27
- }));
28
- return params.sort((a, b) => {
29
- // Move a up if a is required and b is not
30
- if (a.required && !b.required) {
31
- return -1;
32
- }
33
- // Move b up if b is required and a is not
34
- if (!a.required && b.required) {
35
- return 1;
36
- }
37
- // Keep original order if both have the same required status
38
- return 0;
39
- });
40
- }
@@ -1,14 +0,0 @@
1
- import type { ContentType } from '@scalar/types/legacy';
2
- import type { Operation } from '../entities/spec/index.js';
3
- /**
4
- * Get the request body from the operation.
5
- */
6
- export declare function getRequestBodyFromOperation(operation: Pick<Operation, 'requestBody' | 'parameters'>, selectedExampleKey?: string | number, omitEmptyAndOptionalProperties?: boolean): {
7
- mimeType: ContentType;
8
- text?: string | undefined;
9
- params?: {
10
- name: string;
11
- value?: string | File;
12
- }[];
13
- } | null;
14
- //# sourceMappingURL=get-request-body-from-operation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-request-body-from-operation.d.ts","sourceRoot":"","sources":["../../src/spec-getters/get-request-body-from-operation.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAGvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AA6ChD;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,aAAa,GAAG,YAAY,CAAC,EACxD,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM,EACpC,8BAA8B,CAAC,EAAE,OAAO,GACvC;IACD,QAAQ,EAAE,WAAW,CAAA;IACrB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,MAAM,CAAC,EAAE;QACP,IAAI,EAAE,MAAM,CAAA;QACZ,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KACtB,EAAE,CAAA;CACJ,GAAG,IAAI,CA2KP"}