@scalar/oas-utils 0.0.2
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 +8 -0
- package/LICENSE +21 -0
- package/dist/fetch-spec.d.ts +3 -0
- package/dist/fetch-spec.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/dist/parse.d.ts +11 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +64 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Scalar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +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"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { parse, stringify } from 'yaml';
|
|
2
|
+
|
|
3
|
+
const yaml = {
|
|
4
|
+
parse,
|
|
5
|
+
stringify
|
|
6
|
+
};
|
|
7
|
+
const loadJsonOrYaml = (value) => {
|
|
8
|
+
if (typeof value === "string") {
|
|
9
|
+
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);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
function loadJsonOrYamlString(value) {
|
|
21
|
+
const trimmed = value.trim();
|
|
22
|
+
if (trimmed[0] !== "{")
|
|
23
|
+
return value;
|
|
24
|
+
try {
|
|
25
|
+
return JSON.stringify(JSON.parse(value), null, 2);
|
|
26
|
+
} catch {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const fetchSpecFromUrl = async (url, proxy, parseObject = false) => {
|
|
32
|
+
const response = proxy ? await fetch(proxy, {
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: {
|
|
35
|
+
"Content-Type": "application/json"
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
method: "GET",
|
|
39
|
+
url
|
|
40
|
+
})
|
|
41
|
+
}) : await fetch(url);
|
|
42
|
+
if (response.status !== 200) {
|
|
43
|
+
const proxyWarning = proxy ? "" : "Trying to fetch the spec file without a proxy. The CORS headers must be set properly or the request will fail.";
|
|
44
|
+
console.error(
|
|
45
|
+
`[fetchSpecFromUrl] Failed to fetch the spec at ${url}. ${proxyWarning}`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const payload = proxy ? String((await response.json()).data) : await response.text();
|
|
49
|
+
return parseObject ? loadJsonOrYaml(payload) : loadJsonOrYamlString(payload);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { fetchSpecFromUrl, loadJsonOrYaml, loadJsonOrYamlString, yaml };
|
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { parse, stringify } from 'yaml';
|
|
2
|
+
import { type AnyObject } from './types';
|
|
3
|
+
export declare const yaml: {
|
|
4
|
+
parse: typeof parse;
|
|
5
|
+
stringify: typeof stringify;
|
|
6
|
+
};
|
|
7
|
+
/** Parses a JSON or Yaml object or string into an object */
|
|
8
|
+
export declare const loadJsonOrYaml: (value: string | AnyObject) => AnyObject;
|
|
9
|
+
/** Validates a JSON string if provided. Otherwise returns the raw Yaml */
|
|
10
|
+
export declare function loadJsonOrYamlString(value: string): string;
|
|
11
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAE3C,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scalar/oas-utils",
|
|
3
|
+
"description": "Open API spec and Yaml handling utilities",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Scalar (https://github.com/scalar)",
|
|
6
|
+
"homepage": "https://github.com/scalar/scalar",
|
|
7
|
+
"bugs": "https://github.com/scalar/scalar/issues/new/choose",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"oas",
|
|
10
|
+
"fetching",
|
|
11
|
+
"specification",
|
|
12
|
+
"yaml"
|
|
13
|
+
],
|
|
14
|
+
"version": "0.0.2",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "dist/index.js",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./css/*.css": {
|
|
26
|
+
"import": "./dist/css/*.css",
|
|
27
|
+
"require": "./dist/css/*.css"
|
|
28
|
+
},
|
|
29
|
+
"./*.css": {
|
|
30
|
+
"import": "./dist/*.css",
|
|
31
|
+
"require": "./dist/*.css"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"CHANGELOG.md"
|
|
37
|
+
],
|
|
38
|
+
"module": "dist/index.js",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/scalar/scalar.git",
|
|
42
|
+
"directory": "packages/oas-utils"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"yaml": "^2.4.1"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"tsc-alias": "^1.8.8",
|
|
49
|
+
"vite": "^5.1.1",
|
|
50
|
+
"vitest": "^1.2.2",
|
|
51
|
+
"@scalar/build-tooling": "0.0.2"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "vite build && pnpm types:build && tsc-alias -p tsconfig.build.json",
|
|
55
|
+
"dev": "vite",
|
|
56
|
+
"lint:check": "eslint .",
|
|
57
|
+
"lint:fix": "eslint . --fix",
|
|
58
|
+
"preview": "vite preview",
|
|
59
|
+
"test": "vitest",
|
|
60
|
+
"test:unit": "vitest .",
|
|
61
|
+
"types:build": "tsc -p tsconfig.build.json",
|
|
62
|
+
"types:check": "tsc --noEmit --skipLibCheck --composite false"
|
|
63
|
+
}
|
|
64
|
+
}
|