@scalar/oas-utils 0.1.11 → 0.1.13

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,18 @@
1
1
  # @scalar/oas-utils
2
2
 
3
+ ## 0.1.13
4
+
5
+ ### Patch Changes
6
+
7
+ - 5aa656f: feat: [BREAKING CHANGE] use new HTTP proxy (https://github.com/scalar/scalar/pull/1703)
8
+
9
+ ## 0.1.12
10
+
11
+ ### Patch Changes
12
+
13
+ - 61b8ddc: fix: doesn’t render preview for mimetype variations like application/foobar+json
14
+ - 165b901: feat: dynamic values for generated examples
15
+
3
16
  ## 0.1.11
4
17
 
5
18
  ### Patch Changes
@@ -0,0 +1,3 @@
1
+ /** Fetches an OpenAPI/Swagger specification from a given URL. */
2
+ export declare function fetchSpecFromUrl(url: string, proxy?: string): Promise<string>;
3
+ //# sourceMappingURL=fetchSpecFromUrl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetchSpecFromUrl.d.ts","sourceRoot":"","sources":["../src/fetchSpecFromUrl.ts"],"names":[],"mappings":"AAOA,iEAAiE;AACjE,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAmBjB"}
@@ -17,5 +17,9 @@ export declare const getExampleFromSchema: (schema: Record<string, any>, options
17
17
  * @default undefined
18
18
  */
19
19
  mode?: 'read' | 'write';
20
+ /**
21
+ * Dynamic values to add to the example.
22
+ */
23
+ variables?: Record<string, any>;
20
24
  }, level?: number) => any;
21
25
  //# sourceMappingURL=getExampleFromSchema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../src/getExampleFromSchema.ts"],"names":[],"mappings":"AAAA;;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;CACxB,UACM,MAAM,KACZ,GA+NF,CAAA"}
1
+ {"version":3,"file":"getExampleFromSchema.d.ts","sourceRoot":"","sources":["../src/getExampleFromSchema.ts"],"names":[],"mappings":"AAAA;;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,GA8OF,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,16 +1,17 @@
1
1
  export { createHash } from './createHash';
2
- export { fetchSpecFromUrl } from './fetch-spec';
2
+ export { defaultStateFactory, ssrState } from './ssrState';
3
+ export { fetchSpecFromUrl } from './fetchSpecFromUrl';
3
4
  export { getExampleFromSchema } from './getExampleFromSchema';
4
5
  export { getHarRequest } from './getHarRequest';
5
6
  export { getParametersFromOperation } from './getParametersFromOperation';
6
7
  export { getRequestBodyFromOperation } from './getRequestBodyFromOperation';
7
8
  export { getRequestFromOperation } from './getRequestFromOperation';
8
- export type { HttpStatusCode, HttpStatusCodes } from './httpStatusCodes';
9
9
  export { httpStatusCodes } from './httpStatusCodes';
10
- export { normalizeMimeTypeObject } from './normalizeMimeTypeObject';
11
10
  export { json2xml } from './json2xml';
12
- export { formatJsonOrYamlString, isJsonString, json, parseJsonOrYaml, transformToJson, yaml, } from './parse';
11
+ export { normalizeMimeTypeObject } from './normalizeMimeTypeObject';
13
12
  export { prettyPrintJson } from './prettyPrintJson';
14
- export { defaultStateFactory, ssrState } from './ssrState';
13
+ export * from './normalizeMimeType';
15
14
  export * from './types';
15
+ export type { HttpStatusCode, HttpStatusCodes } from './httpStatusCodes';
16
+ export { formatJsonOrYamlString, isJsonString, json, parseJsonOrYaml, transformToJson, yaml, } from './parse';
16
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAA;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAA;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAA;AACnE,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAA;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,IAAI,EACJ,eAAe,EACf,eAAe,EACf,IAAI,GACL,MAAM,SAAS,CAAA;AAChB,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC1D,cAAc,SAAS,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAA;AACzE,OAAO,EAAE,2BAA2B,EAAE,MAAM,+BAA+B,CAAA;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAA;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAA;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,qBAAqB,CAAA;AACnC,cAAc,SAAS,CAAA;AACvB,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxE,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,IAAI,EACJ,eAAe,EACf,eAAe,EACf,IAAI,GACL,MAAM,SAAS,CAAA"}
package/dist/index.js CHANGED
@@ -15,6 +15,9 @@ const createHash = (input) => {
15
15
  return hash;
16
16
  };
17
17
 
18
+ const defaultStateFactory = () => ({});
19
+ const ssrState = typeof window !== "undefined" ? window.__SCALAR__ ?? defaultStateFactory() : defaultStateFactory();
20
+
18
21
  const yaml = {
19
22
  /** Parse and throw if the return value is not an object */
20
23
  parse: (val) => {
@@ -83,25 +86,22 @@ const parseJsonOrYaml = (value) => {
83
86
  });
84
87
  };
85
88
 
89
+ function redirectToProxy(proxy, url) {
90
+ return `${proxy}?scalar_url=${encodeURI(url)}`;
91
+ }
86
92
  async function fetchSpecFromUrl(url, proxy) {
87
- const response = proxy ? await fetch(proxy, {
88
- method: "POST",
89
- headers: {
90
- "Content-Type": "application/json"
91
- },
92
- body: JSON.stringify({
93
- method: "GET",
94
- url
95
- })
96
- }) : await fetch(url);
93
+ const response = await fetch(proxy ? redirectToProxy(proxy, url) : url);
97
94
  if (response.status !== 200) {
98
- const proxyWarning = proxy ? "" : "Trying to fetch the spec file without a proxy. The CORS headers must be set properly or the request will fail.";
99
95
  console.error(
100
- `[fetchSpecFromUrl] Failed to fetch the spec at ${url}. ${proxyWarning}`
96
+ `[fetchSpecFromUrl] Failed to fetch the specification at ${url} (Status: ${response.status})`
101
97
  );
98
+ if (!proxy) {
99
+ console.warn(
100
+ `[fetchSpecFromUrl] Tried to fetch the specification (url: ${url}) without a proxy. Are the CORS headers configured to allow cross-domain requests? https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS`
101
+ );
102
+ }
102
103
  }
103
- const payload = proxy ? String((await response.json()).data) : await response.text();
104
- return formatJsonOrYamlString(payload);
104
+ return formatJsonOrYamlString(await response.text());
105
105
  }
106
106
 
107
107
  const getExampleFromSchema = (schema, options, level = 0) => {
@@ -114,6 +114,15 @@ const getExampleFromSchema = (schema, options, level = 0) => {
114
114
  if (options?.mode === "read" && schema.writeOnly) {
115
115
  return void 0;
116
116
  }
117
+ if (schema["x-variable"]) {
118
+ const value = options?.variables?.[schema["x-variable"]];
119
+ if (value !== void 0) {
120
+ if (schema.type === "number" || schema.type === "integer") {
121
+ return parseInt(value, 10);
122
+ }
123
+ return value;
124
+ }
125
+ }
117
126
  if (Array.isArray(schema.examples) && schema.examples.length > 0) {
118
127
  return schema.examples[0];
119
128
  }
@@ -364,6 +373,10 @@ function json2xml(data, tab) {
364
373
  return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
365
374
  }
366
375
 
376
+ function normalizeMimeType(contentType) {
377
+ return contentType.replace(/;.*$/, "").replace(/\/.+\+/, "/").trim();
378
+ }
379
+
367
380
  function normalizeMimeTypeObject(content) {
368
381
  if (!content) {
369
382
  return content;
@@ -372,7 +385,7 @@ function normalizeMimeTypeObject(content) {
372
385
  ...content
373
386
  };
374
387
  Object.keys(newContent).forEach((key) => {
375
- const newKey = key.replace(/;.*$/, "").replace(/\/.+\+/, "/").trim();
388
+ const newKey = normalizeMimeType(key);
376
389
  newContent[newKey] = newContent[key];
377
390
  if (key !== newKey) {
378
391
  delete newContent[key];
@@ -823,7 +836,4 @@ const httpStatusCodes = {
823
836
  }
824
837
  };
825
838
 
826
- const defaultStateFactory = () => ({});
827
- const ssrState = typeof window !== "undefined" ? window.__SCALAR__ ?? defaultStateFactory() : defaultStateFactory();
828
-
829
- export { createHash, defaultStateFactory, fetchSpecFromUrl, formatJsonOrYamlString, getExampleFromSchema, getHarRequest, getParametersFromOperation, getRequestBodyFromOperation, getRequestFromOperation, httpStatusCodes, isJsonString, json, json2xml, normalizeMimeTypeObject, parseJsonOrYaml, prettyPrintJson, ssrState, transformToJson, yaml };
839
+ export { createHash, defaultStateFactory, fetchSpecFromUrl, formatJsonOrYamlString, getExampleFromSchema, getHarRequest, getParametersFromOperation, getRequestBodyFromOperation, getRequestFromOperation, httpStatusCodes, isJsonString, json, json2xml, normalizeMimeType, normalizeMimeTypeObject, parseJsonOrYaml, prettyPrintJson, ssrState, transformToJson, yaml };
@@ -0,0 +1,3 @@
1
+ import type { ContentType } from './types';
2
+ export declare function normalizeMimeType(contentType: string): ContentType;
3
+ //# sourceMappingURL=normalizeMimeType.d.ts.map
@@ -0,0 +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,eAUpD"}
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeMimeTypeObject.d.ts","sourceRoot":"","sources":["../src/normalizeMimeTypeObject.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE1C;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC;;;;;;;;cA6BzE"}
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;;;;;;;;cAuBzE"}
package/dist/parse.d.ts CHANGED
@@ -25,7 +25,7 @@ export declare const isJsonString: (value?: any) => boolean;
25
25
  * This helper is used to transform the content of the swagger file to JSON, even it was YAML.
26
26
  */
27
27
  export declare const transformToJson: (value: string) => string;
28
- /** Validates a JSON string if provided. Otherwise returns the raw Yaml */
28
+ /** Validates a JSON string if provided. Otherwise returns the raw YAML */
29
29
  export declare function formatJsonOrYamlString(value: string): string;
30
30
  /** Parse JSON or YAML into an object */
31
31
  export declare const parseJsonOrYaml: (value: string | AnyObject) => AnyObject;
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "specification",
17
17
  "yaml"
18
18
  ],
19
- "version": "0.1.11",
19
+ "version": "0.1.13",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -1,3 +0,0 @@
1
- /** Fetches an OAS spec file from a given URL. */
2
- export declare function fetchSpecFromUrl(url: string, proxy?: string): Promise<string>;
3
- //# sourceMappingURL=fetch-spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetch-spec.d.ts","sourceRoot":"","sources":["../src/fetch-spec.ts"],"names":[],"mappings":"AAEA,iDAAiD;AACjD,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CA8BjB"}