@trojs/openapi-dereference 0.2.4 → 1.0.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.
- package/package.json +26 -13
- package/src/dereference.js +37 -37
- package/src/klona.js +23 -22
- package/src/resolveRef.js +17 -17
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trojs/openapi-dereference",
|
|
3
3
|
"description": "OpenAPI dereference",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Pieter Wigboldus",
|
|
7
7
|
"url": "https://trojs.org/"
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"scripts": {
|
|
11
|
-
"lint": "eslint
|
|
12
|
-
"lint:report": "eslint src/*.js
|
|
13
|
-
"lint:fix": "eslint
|
|
11
|
+
"lint": "eslint",
|
|
12
|
+
"lint:report": "eslint src/*.js -f json -o report.json",
|
|
13
|
+
"lint:fix": "eslint --fix",
|
|
14
14
|
"lint:rules": "eslint --print-config file.js > eslintconfig.json",
|
|
15
15
|
"test": "c8 node --test src/*.test.js",
|
|
16
16
|
"test:watch": "c8 node --watch --test src/*.test.js",
|
|
@@ -28,15 +28,19 @@
|
|
|
28
28
|
"main": "src/index.js",
|
|
29
29
|
"types": "src/types.d.ts",
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@
|
|
31
|
+
"@eslint/js": "^9.15.0",
|
|
32
|
+
"@stylistic/eslint-plugin": "^2.11.0",
|
|
33
|
+
"@stylistic/eslint-plugin-js": "^2.11.0",
|
|
32
34
|
"c8": "^10.0.0",
|
|
33
|
-
"eslint": "^
|
|
34
|
-
"eslint-config-
|
|
35
|
-
"eslint-plugin-import": "^2.
|
|
36
|
-
"eslint-plugin-jsdoc": "^50.
|
|
37
|
-
"eslint-plugin-n": "^
|
|
38
|
-
"eslint-plugin-
|
|
39
|
-
"eslint-plugin-
|
|
35
|
+
"eslint": "^9.15.0",
|
|
36
|
+
"eslint-config-prettier": "^9.1.0",
|
|
37
|
+
"eslint-plugin-import": "^2.31.0",
|
|
38
|
+
"eslint-plugin-jsdoc": "^50.5.0",
|
|
39
|
+
"eslint-plugin-n": "^17.14.0",
|
|
40
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
41
|
+
"eslint-plugin-promise": "^7.1.0",
|
|
42
|
+
"eslint-plugin-sonarjs": "^2.0.4",
|
|
43
|
+
"globals": "^15.12.0",
|
|
40
44
|
"jscpd": "^4.0.0",
|
|
41
45
|
"json-schema": "^0.4.0"
|
|
42
46
|
},
|
|
@@ -45,7 +49,7 @@
|
|
|
45
49
|
"url": "https://github.com/trojs/openapi-dereference"
|
|
46
50
|
},
|
|
47
51
|
"engines": {
|
|
48
|
-
"node": ">=
|
|
52
|
+
"node": ">= 20"
|
|
49
53
|
},
|
|
50
54
|
"keywords": [
|
|
51
55
|
"openapi",
|
|
@@ -62,5 +66,14 @@
|
|
|
62
66
|
"funding": {
|
|
63
67
|
"type": "github",
|
|
64
68
|
"url": "https://github.com/sponsors/w3nl"
|
|
69
|
+
},
|
|
70
|
+
"overrides": {
|
|
71
|
+
"eslint-plugin-sonarjs": {
|
|
72
|
+
"eslint": "^9.15.0",
|
|
73
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
74
|
+
"@typescript-eslint/utils": "^8.0.0",
|
|
75
|
+
"eslint-plugin-import": "^2.31.0",
|
|
76
|
+
"eslint-plugin-react-hooks": "^5.0.0"
|
|
77
|
+
}
|
|
65
78
|
}
|
|
66
79
|
}
|
package/src/dereference.js
CHANGED
|
@@ -17,46 +17,46 @@ const cache = new Map()
|
|
|
17
17
|
* @returns {DereferencedJSONSchema}
|
|
18
18
|
*/
|
|
19
19
|
export const dereferenceSync = (schema) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const visitedNodes = new Set()
|
|
25
|
-
const cloned = klona(schema)
|
|
26
|
-
|
|
27
|
-
const resolve = (current, path) => {
|
|
28
|
-
if (typeof current === 'object' && current !== null) {
|
|
29
|
-
// make sure we don't visit the same node twice
|
|
30
|
-
if (visitedNodes.has(current)) {
|
|
31
|
-
return current
|
|
32
|
-
}
|
|
33
|
-
visitedNodes.add(current)
|
|
20
|
+
if (cache.has(schema)) {
|
|
21
|
+
return cache.get(schema)
|
|
22
|
+
}
|
|
34
23
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
24
|
+
const visitedNodes = new Set()
|
|
25
|
+
const cloned = klona(schema)
|
|
26
|
+
|
|
27
|
+
const resolve = (current, path) => {
|
|
28
|
+
if (typeof current === 'object' && current !== null) {
|
|
29
|
+
// make sure we don't visit the same node twice
|
|
30
|
+
if (visitedNodes.has(current)) {
|
|
31
|
+
return current
|
|
32
|
+
}
|
|
33
|
+
visitedNodes.add(current)
|
|
34
|
+
|
|
35
|
+
if (Array.isArray(current)) {
|
|
36
|
+
// array
|
|
37
|
+
for (let index = 0; index < current.length; index++) {
|
|
38
|
+
current[index] = resolve(current[index], `${path}/${index}`)
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
// object
|
|
42
|
+
if ('$ref' in current && typeof current.$ref === 'string') {
|
|
43
|
+
let ref = current
|
|
44
|
+
do {
|
|
45
|
+
ref = resolveRefSync(cloned, ref.$ref)
|
|
46
|
+
} while (ref?.$ref)
|
|
47
|
+
return ref
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const key in current) {
|
|
51
|
+
current[key] = resolve(current[key], `${path}/${key}`)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
48
54
|
}
|
|
49
55
|
|
|
50
|
-
|
|
51
|
-
current[key] = resolve(current[key], `${path}/${key}`)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
56
|
+
return current
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const result = resolve(cloned, '#')
|
|
60
|
-
cache.set(schema, result)
|
|
61
|
-
return result
|
|
59
|
+
const result = resolve(cloned, '#')
|
|
60
|
+
cache.set(schema, result)
|
|
61
|
+
return result
|
|
62
62
|
}
|
package/src/klona.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* eslint-disable sonarjs/cognitive-complexity */
|
|
2
|
+
/* eslint-disable sonarjs/no-nested-assignment */
|
|
2
3
|
/* eslint-disable no-restricted-syntax */
|
|
3
4
|
/**
|
|
4
5
|
* klona/json - MIT License
|
|
@@ -8,30 +9,30 @@
|
|
|
8
9
|
* @returns {any}
|
|
9
10
|
*/
|
|
10
11
|
export function klona (val) {
|
|
11
|
-
|
|
12
|
+
let index, out, tmp
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
if (Array.isArray(val)) {
|
|
15
|
+
out = Array((index = val.length))
|
|
16
|
+
while (index--) out[index] = (tmp = val[index]) && typeof tmp === 'object' ? klona(tmp) : tmp
|
|
17
|
+
return out
|
|
18
|
+
}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
if (Object.prototype.toString.call(val) === '[object Object]') {
|
|
21
|
+
out = {} // null
|
|
22
|
+
for (index in val) {
|
|
23
|
+
if (index === '__proto__') {
|
|
24
|
+
Object.defineProperty(out, index, {
|
|
25
|
+
value: klona(val[index]),
|
|
26
|
+
configurable: true,
|
|
27
|
+
enumerable: true,
|
|
28
|
+
writable: true
|
|
29
|
+
})
|
|
30
|
+
} else {
|
|
31
|
+
out[index] = (tmp = val[index]) && typeof tmp === 'object' ? klona(tmp) : tmp
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return out
|
|
32
35
|
}
|
|
33
|
-
return out
|
|
34
|
-
}
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
return val
|
|
37
38
|
}
|
package/src/resolveRef.js
CHANGED
|
@@ -12,26 +12,26 @@ const cache = new Map()
|
|
|
12
12
|
* @returns {unknown}
|
|
13
13
|
*/
|
|
14
14
|
export const resolveRefSync = (schema, ref) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
if (!cache.has(schema)) {
|
|
16
|
+
cache.set(schema, new Map())
|
|
17
|
+
}
|
|
18
|
+
const schemaCache = cache.get(schema)
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
if (schemaCache.has(ref)) {
|
|
21
|
+
return schemaCache.get(ref)
|
|
22
|
+
}
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
const path = ref.split('/').slice(1)
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
let current = schema
|
|
27
|
+
for (const segment of path) {
|
|
28
|
+
if (!current || typeof current !== 'object') {
|
|
29
|
+
// we've reached a dead end
|
|
30
|
+
current = null
|
|
31
|
+
}
|
|
32
|
+
current = current[segment] ?? null
|
|
31
33
|
}
|
|
32
|
-
current = current[segment] ?? null
|
|
33
|
-
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
schemaCache.set(ref, current)
|
|
36
|
+
return current
|
|
37
37
|
}
|