@trojs/openapi-dereference 1.2.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trojs/openapi-dereference",
3
3
  "description": "OpenAPI dereference",
4
- "version": "1.2.0",
4
+ "version": "2.0.0",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
@@ -12,6 +12,7 @@
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
+ "lint:types": "tsc --noEmit --project tsconfig.json --skipLibCheck",
15
16
  "test": "node --test --experimental-test-coverage --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=./coverage/lcov.info",
16
17
  "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
18
  "cpd": "node_modules/jscpd/bin/jscpd src",
@@ -26,13 +27,17 @@
26
27
  "src/types.d.ts"
27
28
  ],
28
29
  "main": "src/index.js",
30
+ "source": "src/index.js",
31
+ "module": "src/index.js",
29
32
  "types": "src/types.d.ts",
30
33
  "devDependencies": {
31
- "@trojs/lint": "^0.2.13",
34
+ "@trojs/lint": "^0.3.0",
35
+ "@types/node": "^24.9.2",
32
36
  "eslint": "^9.15.0",
33
37
  "globals": "^16.0.0",
34
38
  "jscpd": "^4.0.0",
35
- "json-schema": "^0.4.0"
39
+ "json-schema": "^0.4.0",
40
+ "typescript": "^5.9.3"
36
41
  },
37
42
  "repository": {
38
43
  "type": "git",
@@ -2,8 +2,9 @@ import { klona } from './klona.js'
2
2
  import { resolveRefSync } from './resolveRef.js'
3
3
 
4
4
  /**
5
- * @typedef {import('./types').JSONSchema} JSONSchema
6
- * @typedef {import('./types').DereferencedJSONSchema} DereferencedJSONSchema
5
+ * @typedef {import('./types.d.ts').JSONSchema} JSONSchema
6
+ * @typedef {import('./types.d.ts').DereferencedJSONSchema} DereferencedJSONSchema
7
+ * @typedef {WeakMap<{[key: string]: string}, any>} WeakMapRefAny
7
8
  */
8
9
 
9
10
  const PROHIBITED_KEYS = new Set(['__proto__', 'constructor', 'prototype'])
@@ -35,6 +36,7 @@ export const dereferenceSync = (schema) => {
35
36
  : schema
36
37
 
37
38
  const cloned = klona(filtered)
39
+ /** @type {WeakMapRefAny} */
38
40
  const seen = new WeakMap()
39
41
 
40
42
  /**
@@ -49,10 +51,14 @@ export const dereferenceSync = (schema) => {
49
51
  // Handle circular references
50
52
  if (seen.has(current)) return seen.get(current)
51
53
 
52
- // Handle arrays
54
+ // Handle arrays (register before descending)
53
55
  if (Array.isArray(current)) {
54
- const arr = current.map((item, i) => resolve(item, `${path}/${i}`))
56
+ const arr = new Array(current.length)
57
+ // @ts-expect-error WeakMap type
55
58
  seen.set(current, arr)
59
+ for (let i = 0; i < current.length; i++) {
60
+ arr[i] = resolve(current[i], `${path}/${i}`)
61
+ }
56
62
  return arr
57
63
  }
58
64
 
@@ -62,28 +68,22 @@ export const dereferenceSync = (schema) => {
62
68
  if (!ref) {
63
69
  return null
64
70
  }
65
- if (seen.has(ref)) {
71
+ // @ts-expect-error WeakMap type
72
+ if (typeof ref === 'object' && ref !== null && seen.has(ref)) {
73
+ // @ts-expect-error WeakMap type
66
74
  return seen.get(ref)
67
75
  }
68
- if (Array.isArray(ref)) {
69
- const resolvedArray = resolve(ref, current.$ref)
70
- seen.set(ref, resolvedArray)
71
- return resolvedArray
72
- }
73
- const placeholder = {}
74
- seen.set(current, placeholder)
75
- const resolved = resolve(ref, current.$ref)
76
- Object.assign(placeholder, resolved)
77
- return resolved
76
+ // Resolve the referenced value (object/array/primitive)
77
+ return resolve(ref, current.$ref)
78
78
  }
79
79
 
80
- // Handle objects
81
- const obj = Object.fromEntries(
82
- Object.entries(current)
83
- .filter(([key]) => !PROHIBITED_KEYS.has(key))
84
- .map(([key, value]) => [key, resolve(value, `${path}/${key}`)])
85
- )
80
+ // Handle objects (register before descending)
81
+ const obj = {}
86
82
  seen.set(current, obj)
83
+ for (const [key, value] of Object.entries(current)) {
84
+ if (PROHIBITED_KEYS.has(key)) continue
85
+ obj[key] = resolve(value, `${path}/${key}`)
86
+ }
87
87
  return obj
88
88
  }
89
89
 
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * @typedef {import('./types').JSONSchema} JSONSchema
3
- * @typedef {import('./types').DereferencedJSONSchema} DereferencedJSONSchema
2
+ * @typedef {import('./types.js').JSONSchema} JSONSchema
3
+ * @typedef {import('./types.js').DereferencedJSONSchema} DereferencedJSONSchema
4
4
  */
5
5
  export * from './dereference.js'
6
6
  export * from './resolveRef.js'
package/src/resolveRef.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * @typedef {import('./types').JSONSchema} JSONSchema
3
- * @typedef {import('./types').DereferencedJSONSchema} DereferencedJSONSchema
2
+ * @typedef {import('./types.d.ts').JSONSchema} JSONSchema
3
+ * @typedef {import('./types.d.ts').DereferencedJSONSchema} DereferencedJSONSchema
4
4
  */
5
5
 
6
6
  const cache = new Map()