@scalar/oas-utils 0.0.2 → 0.0.4

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,17 @@
1
1
  # @scalar/oas-utils
2
2
 
3
+ ## 0.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - cecf074: Migrate to oas utils for basic spec operations
8
+
9
+ ## 0.0.3
10
+
11
+ ### Patch Changes
12
+
13
+ - 313997c: chore: align node versions to basis 18
14
+
3
15
  ## 0.0.2
4
16
 
5
17
  ### Patch Changes
@@ -1,3 +1,3 @@
1
1
  /** Fetches an OAS spec file from a given URL. */
2
- export declare const fetchSpecFromUrl: (url: string, proxy?: string, parseObject?: boolean) => Promise<string | import("./types").AnyObject>;
2
+ export declare function fetchSpecFromUrl(url: string, proxy?: string): Promise<string>;
3
3
  //# sourceMappingURL=fetch-spec.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetch-spec.d.ts","sourceRoot":"","sources":["../src/fetch-spec.ts"],"names":[],"mappings":"AAEA,iDAAiD;AACjD,eAAO,MAAM,gBAAgB,QACtB,MAAM,UACH,MAAM,yEAgCf,CAAA"}
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"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './fetch-spec';
1
+ export { fetchSpecFromUrl } from './fetch-spec';
2
2
  export * from './parse';
3
3
  export * from './types';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC/C,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
package/dist/index.js CHANGED
@@ -1,23 +1,50 @@
1
1
  import { parse, stringify } from 'yaml';
2
2
 
3
3
  const yaml = {
4
- parse,
4
+ /** Parse and throw if the return value is not an object */
5
+ parse: (val) => {
6
+ const yamlObject = parse(val);
7
+ if (typeof yamlObject !== "object")
8
+ throw Error("Invalid YAML object");
9
+ return yamlObject;
10
+ },
11
+ /** Parse and return a fallback on failure */
12
+ parseSafe(val, fallback) {
13
+ try {
14
+ return yaml.parse(val);
15
+ } catch (err) {
16
+ return typeof fallback === "function" ? fallback(err) : fallback;
17
+ }
18
+ },
5
19
  stringify
6
20
  };
7
- const loadJsonOrYaml = (value) => {
8
- if (typeof value === "string") {
21
+ const json = {
22
+ /** Parse and throw if the return value is not an object */
23
+ parse: (val) => {
24
+ const jsonObject = JSON.parse(val);
25
+ if (typeof jsonObject !== "object")
26
+ throw Error("Invalid JSON object");
27
+ return jsonObject;
28
+ },
29
+ /** Parse and return a fallback on failure */
30
+ parseSafe(val, fallback) {
9
31
  try {
10
- return JSON.parse(value);
11
- } catch (error) {
12
- if (value.length > 0 && ["{", "["].includes(value[0])) {
13
- throw error;
14
- }
15
- return yaml.parse(value);
32
+ return json.parse(val);
33
+ } catch (err) {
34
+ return typeof fallback === "function" ? fallback(err) : fallback;
16
35
  }
17
- }
18
- return value;
36
+ },
37
+ stringify: (val) => JSON.stringify(val)
38
+ };
39
+ const isJsonString = (value) => {
40
+ if (typeof value !== "string")
41
+ return false;
42
+ return !!json.parseSafe(value, false);
43
+ };
44
+ const transformToJson = (value) => {
45
+ return JSON.stringify(json.parseSafe(value, yaml.parseSafe(value, value)));
19
46
  };
20
- function loadJsonOrYamlString(value) {
47
+ function formatJsonOrYamlString(value) {
21
48
  const trimmed = value.trim();
22
49
  if (trimmed[0] !== "{")
23
50
  return value;
@@ -27,8 +54,21 @@ function loadJsonOrYamlString(value) {
27
54
  return value;
28
55
  }
29
56
  }
57
+ const parseJsonOrYaml = (value) => {
58
+ if (typeof value !== "string")
59
+ return value;
60
+ const jsonObject = json.parseSafe(value, null);
61
+ if (jsonObject)
62
+ return jsonObject;
63
+ if (value.length > 0 && ["{", "["].includes(value[0])) {
64
+ throw Error("Invalid JSON or YAML");
65
+ }
66
+ return yaml.parseSafe(value, (err) => {
67
+ throw Error(err);
68
+ });
69
+ };
30
70
 
31
- const fetchSpecFromUrl = async (url, proxy, parseObject = false) => {
71
+ async function fetchSpecFromUrl(url, proxy) {
32
72
  const response = proxy ? await fetch(proxy, {
33
73
  method: "POST",
34
74
  headers: {
@@ -46,7 +86,7 @@ const fetchSpecFromUrl = async (url, proxy, parseObject = false) => {
46
86
  );
47
87
  }
48
88
  const payload = proxy ? String((await response.json()).data) : await response.text();
49
- return parseObject ? loadJsonOrYaml(payload) : loadJsonOrYamlString(payload);
50
- };
89
+ return formatJsonOrYamlString(payload);
90
+ }
51
91
 
52
- export { fetchSpecFromUrl, loadJsonOrYaml, loadJsonOrYamlString, yaml };
92
+ export { fetchSpecFromUrl, formatJsonOrYamlString, isJsonString, json, parseJsonOrYaml, transformToJson, yaml };
package/dist/parse.d.ts CHANGED
@@ -1,11 +1,33 @@
1
- import { parse, stringify } from 'yaml';
1
+ import { stringify } from 'yaml';
2
2
  import { type AnyObject } from './types';
3
+ type PrimitiveOrObject = object | string | null | number | boolean | undefined;
4
+ /** Yaml handling with optional safeparse */
3
5
  export declare const yaml: {
4
- parse: typeof parse;
6
+ /** Parse and throw if the return value is not an object */
7
+ parse: (val: string) => AnyObject;
8
+ /** Parse and return a fallback on failure */
9
+ parseSafe<T extends PrimitiveOrObject>(val: string, fallback: T | ((err: any) => T)): AnyObject | T;
5
10
  stringify: typeof stringify;
6
11
  };
7
- /** Parses a JSON or Yaml object or string into an object */
8
- export declare const loadJsonOrYaml: (value: string | AnyObject) => AnyObject;
12
+ /** JSON handling with optional safeparse */
13
+ export declare const json: {
14
+ /** Parse and throw if the return value is not an object */
15
+ parse: (val: string) => AnyObject;
16
+ /** Parse and return a fallback on failure */
17
+ parseSafe<T extends PrimitiveOrObject>(val: string, fallback: T | ((err: any) => T)): AnyObject | T;
18
+ stringify: (val: object) => string;
19
+ };
20
+ /**
21
+ * Check if value is a valid JSON string
22
+ */
23
+ export declare const isJsonString: (value?: any) => boolean;
24
+ /**
25
+ * This helper is used to transform the content of the swagger file to JSON, even it was YAML.
26
+ */
27
+ export declare const transformToJson: (value: string) => string;
9
28
  /** Validates a JSON string if provided. Otherwise returns the raw Yaml */
10
- export declare function loadJsonOrYamlString(value: string): string;
29
+ export declare function formatJsonOrYamlString(value: string): string;
30
+ /** Parse JSON or YAML into an object */
31
+ export declare const parseJsonOrYaml: (value: string | AnyObject) => AnyObject;
32
+ export {};
11
33
  //# sourceMappingURL=parse.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAA;AAEvC,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAA;AAExC,eAAO,MAAM,IAAI;;;CAGhB,CAAA;AAED,4DAA4D;AAC5D,eAAO,MAAM,cAAc,UAAW,MAAM,GAAG,SAAS,KAAG,SAgB1D,CAAA;AAED,0EAA0E;AAC1E,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,UAYjD"}
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,SAAS,EAAE,MAAM,MAAM,CAAA;AAEvC,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAA;AAExC,KAAK,iBAAiB,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAA;AAE9E,4CAA4C;AAC5C,eAAO,MAAM,IAAI;IACf,2DAA2D;iBAC9C,MAAM;IAKnB,6CAA6C;gDAEtC,MAAM,uBACU,GAAG,KAAK,CAAC,IAC7B,SAAS,GAAG,CAAC;;CAQjB,CAAA;AAED,4CAA4C;AAC5C,eAAO,MAAM,IAAI;IACf,2DAA2D;iBAC9C,MAAM,KAAG,SAAS;IAK/B,6CAA6C;gDAEtC,MAAM,uBACU,GAAG,KAAK,CAAC,IAC7B,SAAS,GAAG,CAAC;qBAOC,MAAM;CACxB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,YAAY,WAAY,GAAG,YAIvC,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,eAAe,UAAW,MAAM,WAG5C,CAAA;AAED,0EAA0E;AAC1E,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,UAYnD;AAED,wCAAwC;AACxC,eAAO,MAAM,eAAe,UAAW,MAAM,GAAG,SAAS,KAAG,SAc3D,CAAA"}
package/package.json CHANGED
@@ -11,9 +11,9 @@
11
11
  "specification",
12
12
  "yaml"
13
13
  ],
14
- "version": "0.0.2",
14
+ "version": "0.0.4",
15
15
  "engines": {
16
- "node": ">=20"
16
+ "node": ">=18"
17
17
  },
18
18
  "type": "module",
19
19
  "main": "dist/index.js",
@@ -48,7 +48,7 @@
48
48
  "tsc-alias": "^1.8.8",
49
49
  "vite": "^5.1.1",
50
50
  "vitest": "^1.2.2",
51
- "@scalar/build-tooling": "0.0.2"
51
+ "@scalar/build-tooling": "0.0.4"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",