@trojs/openapi-dereference 1.0.1 → 1.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trojs/openapi-dereference",
3
3
  "description": "OpenAPI dereference",
4
- "version": "1.0.1",
4
+ "version": "1.1.1",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
@@ -12,8 +12,8 @@
12
12
  "lint:report": "eslint src/*.js -f json -o report.json",
13
13
  "lint:fix": "eslint --fix",
14
14
  "lint:rules": "eslint --print-config file.js > eslintconfig.json",
15
- "test": "c8 node --test src/*.test.js",
16
- "test:watch": "c8 node --watch --test src/*.test.js",
15
+ "test": "node --test --experimental-test-coverage --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=./coverage/lcov.info",
16
+ "test:watch": "node --watch --test --experimental-test-coverage --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=./coverage/lcov.info",
17
17
  "cpd": "node_modules/jscpd/bin/jscpd src",
18
18
  "vulnerabilities": "npm audit --omit=dev"
19
19
  },
@@ -28,19 +28,9 @@
28
28
  "main": "src/index.js",
29
29
  "types": "src/types.d.ts",
30
30
  "devDependencies": {
31
- "@eslint/js": "^9.15.0",
32
- "@stylistic/eslint-plugin": "^2.11.0",
33
- "@stylistic/eslint-plugin-js": "^2.11.0",
34
- "c8": "^10.0.0",
31
+ "@trojs/lint": "^0.2.13",
35
32
  "eslint": "^9.15.0",
36
- "eslint-config-prettier": "^10.0.0",
37
- "eslint-plugin-import": "^2.31.0",
38
- "eslint-plugin-jsdoc": "^50.5.0",
39
- "eslint-plugin-n": "^17.15.1",
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",
33
+ "globals": "^16.0.0",
44
34
  "jscpd": "^4.0.0",
45
35
  "json-schema": "^0.4.0"
46
36
  },
@@ -67,4 +57,4 @@
67
57
  "type": "github",
68
58
  "url": "https://github.com/sponsors/w3nl"
69
59
  }
70
- }
60
+ }
@@ -17,46 +17,46 @@ const cache = new Map()
17
17
  * @returns {DereferencedJSONSchema}
18
18
  */
19
19
  export const dereferenceSync = (schema) => {
20
- if (cache.has(schema)) {
21
- return cache.get(schema)
22
- }
20
+ if (cache.has(schema)) {
21
+ return cache.get(schema)
22
+ }
23
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)
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
- }
54
- }
24
+ const visitedNodes = new Set()
25
+ const cloned = klona(schema)
55
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)) {
56
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
+ }
57
54
  }
58
55
 
59
- const result = resolve(cloned, '#')
60
- cache.set(schema, result)
61
- return result
56
+ return current
57
+ }
58
+
59
+ const result = resolve(cloned, '#')
60
+ cache.set(schema, result)
61
+ return result
62
62
  }
package/src/klona.js CHANGED
@@ -9,30 +9,30 @@
9
9
  * @returns {any}
10
10
  */
11
11
  export function klona (val) {
12
- let index, out, tmp
12
+ let index, out, tmp
13
13
 
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
- }
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
+ }
19
19
 
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
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
+ }
35
33
  }
34
+ return out
35
+ }
36
36
 
37
- return val
37
+ return val
38
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
- if (!cache.has(schema)) {
16
- cache.set(schema, new Map())
17
- }
18
- const schemaCache = cache.get(schema)
15
+ if (!cache.has(schema)) {
16
+ cache.set(schema, new Map())
17
+ }
18
+ const schemaCache = cache.get(schema)
19
19
 
20
- if (schemaCache.has(ref)) {
21
- return schemaCache.get(ref)
22
- }
20
+ if (schemaCache.has(ref)) {
21
+ return schemaCache.get(ref)
22
+ }
23
23
 
24
- const path = ref.split('/').slice(1)
24
+ const path = ref.split('/').slice(1)
25
25
 
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
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
33
31
  }
32
+ current = current[segment] ?? null
33
+ }
34
34
 
35
- schemaCache.set(ref, current)
36
- return current
35
+ schemaCache.set(ref, current)
36
+ return current
37
37
  }