@scalar/oas-utils 0.2.50 → 0.2.51

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,16 @@
1
1
  # @scalar/oas-utils
2
2
 
3
+ ## 0.2.51
4
+
5
+ ### Patch Changes
6
+
7
+ - ef49617: chore: improve performance of getExampleFromSchema
8
+ - Updated dependencies [8f12149]
9
+ - Updated dependencies [1026d81]
10
+ - Updated dependencies [07b5439]
11
+ - @scalar/types@0.0.12
12
+ - @scalar/themes@0.9.33
13
+
3
14
  ## 0.2.50
4
15
 
5
16
  ### Patch Changes
@@ -3,7 +3,7 @@ const defaultStateFactory = () => ({});
3
3
  * This allows us to access the server state in the front-end
4
4
  */
5
5
  const ssrState = typeof window !== 'undefined'
6
- ? window.__SCALAR__ ?? defaultStateFactory()
6
+ ? (window.__SCALAR__ ?? defaultStateFactory())
7
7
  : defaultStateFactory();
8
8
 
9
9
  export { defaultStateFactory, ssrState };
@@ -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,GA4QF,CAAA"}
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,40 +1,40 @@
1
1
  /** Hard limit for rendering circular references */
2
- const MAX_LEVELS_DEEP = 5;
2
+ const MAX_LEVELS_DEEP = 10;
3
+ const genericExampleValues = {
4
+ // 'date-time': '1970-01-01T00:00:00Z',
5
+ 'date-time': new Date().toISOString(),
6
+ // 'date': '1970-01-01',
7
+ 'date': new Date().toISOString().split('T')[0],
8
+ 'email': 'hello@example.com',
9
+ 'hostname': 'example.com',
10
+ // https://tools.ietf.org/html/rfc6531#section-3.3
11
+ 'idn-email': 'jane.doe@example.com',
12
+ // https://tools.ietf.org/html/rfc5890#section-2.3.2.3
13
+ 'idn-hostname': 'example.com',
14
+ 'ipv4': '127.0.0.1',
15
+ 'ipv6': '51d4:7fab:bfbf:b7d7:b2cb:d4b4:3dad:d998',
16
+ 'iri-reference': '/entitiy/1',
17
+ // https://tools.ietf.org/html/rfc3987
18
+ 'iri': 'https://example.com/entity/123',
19
+ 'json-pointer': '/nested/objects',
20
+ 'password': 'super-secret',
21
+ 'regex': '/[a-z]/',
22
+ // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
23
+ 'relative-json-pointer': '1/nested/objects',
24
+ // full-time in https://tools.ietf.org/html/rfc3339#section-5.6
25
+ // 'time': '00:00:00Z',
26
+ 'time': new Date().toISOString().split('T')[1].split('.')[0],
27
+ // either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
28
+ 'uri-reference': '../folder',
29
+ 'uri-template': 'https://example.com/{id}',
30
+ 'uri': 'https://example.com',
31
+ 'uuid': '123e4567-e89b-12d3-a456-426614174000',
32
+ };
3
33
  /**
4
34
  * We can use the `format` to generate some random values.
5
35
  */
6
36
  function guessFromFormat(schema, fallback = '') {
7
- const exampleValues = {
8
- // 'date-time': '1970-01-01T00:00:00Z',
9
- 'date-time': new Date().toISOString(),
10
- // 'date': '1970-01-01',
11
- 'date': new Date().toISOString().split('T')[0],
12
- 'email': 'hello@example.com',
13
- 'hostname': 'example.com',
14
- // https://tools.ietf.org/html/rfc6531#section-3.3
15
- 'idn-email': 'jane.doe@example.com',
16
- // https://tools.ietf.org/html/rfc5890#section-2.3.2.3
17
- 'idn-hostname': 'example.com',
18
- 'ipv4': '127.0.0.1',
19
- 'ipv6': '51d4:7fab:bfbf:b7d7:b2cb:d4b4:3dad:d998',
20
- 'iri-reference': '/entitiy/1',
21
- // https://tools.ietf.org/html/rfc3987
22
- 'iri': 'https://example.com/entity/123',
23
- 'json-pointer': '/nested/objects',
24
- 'password': 'super-secret',
25
- 'regex': '/[a-z]/',
26
- // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
27
- 'relative-json-pointer': '1/nested/objects',
28
- // full-time in https://tools.ietf.org/html/rfc3339#section-5.6
29
- // 'time': '00:00:00Z',
30
- 'time': new Date().toISOString().split('T')[1].split('.')[0],
31
- // either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
32
- 'uri-reference': '../folder',
33
- 'uri-template': 'https://example.com/{id}',
34
- 'uri': 'https://example.com',
35
- 'uuid': '123e4567-e89b-12d3-a456-426614174000',
36
- };
37
- return exampleValues[schema.format] ?? fallback;
37
+ return genericExampleValues[schema.format] ?? fallback;
38
38
  }
39
39
  /**
40
40
  * This function takes a properties object and generates an example response content.
@@ -53,12 +53,9 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
53
53
  // Sometimes, we just want the structure and no values.
54
54
  // But if `emptyString` is set, we do want to see some values.
55
55
  const makeUpRandomData = !!options?.emptyString;
56
- // Check if the property is read-only
57
- if (options?.mode === 'write' && schema.readOnly) {
58
- return undefined;
59
- }
60
- // Check if the property is write-only
61
- if (options?.mode === 'read' && schema.writeOnly) {
56
+ // Check if the property is read-only/write-only
57
+ if ((options?.mode === 'write' && schema.readOnly) ||
58
+ (options?.mode === 'read' && schema.writeOnly)) {
62
59
  return undefined;
63
60
  }
64
61
  // Use given variables as values
@@ -86,7 +83,7 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
86
83
  return schema.default;
87
84
  }
88
85
  // enum: [ 'available', 'pending', 'sold' ]
89
- if (schema.enum !== undefined) {
86
+ if (Array.isArray(schema.enum) && schema.enum.length > 0) {
90
87
  return schema.enum[0];
91
88
  }
92
89
  // Check if the property is required
@@ -108,14 +105,18 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
108
105
  const response = {};
109
106
  // Regular properties
110
107
  if (schema.properties !== undefined) {
111
- Object.keys(schema.properties).forEach((propertyName) => {
112
- const property = schema.properties[propertyName];
113
- const propertyXmlTagName = options?.xml ? property.xml?.name : undefined;
114
- response[propertyXmlTagName ?? propertyName] = getExampleFromSchema(property, options, level + 1, schema, propertyName);
115
- if (typeof response[propertyXmlTagName ?? propertyName] === 'undefined') {
116
- delete response[propertyXmlTagName ?? propertyName];
108
+ for (const propertyName in schema.properties) {
109
+ if (Object.prototype.hasOwnProperty.call(schema.properties, propertyName)) {
110
+ const property = schema.properties[propertyName];
111
+ const propertyXmlTagName = options?.xml
112
+ ? property.xml?.name
113
+ : undefined;
114
+ response[propertyXmlTagName ?? propertyName] = getExampleFromSchema(property, options, level + 1, schema, propertyName);
115
+ if (typeof response[propertyXmlTagName ?? propertyName] === 'undefined') {
116
+ delete response[propertyXmlTagName ?? propertyName];
117
+ }
117
118
  }
118
- });
119
+ }
119
120
  }
120
121
  // Additional properties
121
122
  if (schema.additionalProperties !== undefined) {
@@ -236,7 +237,7 @@ const getExampleFromSchema = (schema, options, level = 0, parentSchema, name) =>
236
237
  }
237
238
  }
238
239
  // Warn if the type is unknown …
239
- console.warn(`[getExampleFromSchema] Unknown property type "${schema.type}".`);
240
+ // console.warn(`[getExampleFromSchema] Unknown property type "${schema.type}".`)
240
241
  // … and just return null for now.
241
242
  return null;
242
243
  };
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "specification",
17
17
  "yaml"
18
18
  ],
19
- "version": "0.2.50",
19
+ "version": "0.2.51",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -94,8 +94,8 @@
94
94
  "zod": "^3.23.8",
95
95
  "@scalar/object-utils": "1.1.9",
96
96
  "@scalar/openapi-types": "0.1.1",
97
- "@scalar/themes": "0.9.32",
98
- "@scalar/types": "0.0.11"
97
+ "@scalar/types": "0.0.12",
98
+ "@scalar/themes": "0.9.33"
99
99
  },
100
100
  "devDependencies": {
101
101
  "rollup": "^4.16.4",
@@ -104,7 +104,7 @@
104
104
  "vitest": "^1.6.0",
105
105
  "zod-to-ts": "^1.2.0",
106
106
  "@scalar/build-tooling": "0.1.10",
107
- "@scalar/openapi-parser": "0.8.4",
107
+ "@scalar/openapi-parser": "0.8.5",
108
108
  "@scalar/openapi-types": "0.1.1"
109
109
  },
110
110
  "scripts": {