@trojs/openapi-dereference 0.2.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/LICENSE +21 -0
- package/README.md +54 -0
- package/package.json +66 -0
- package/src/dereference.js +58 -0
- package/src/index.js +6 -0
- package/src/klona.js +37 -0
- package/src/resolveRef.js +37 -0
- package/src/types.d.ts +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 TroJS
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# OpenAPI dereference
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm-image]][npm-url] [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference)
|
|
4
|
+
[](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference)
|
|
5
|
+
[](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference) [](https://sonarcloud.io/summary/new_code?id=hckrnews_openapi-dereference)
|
|
6
|
+
|
|
7
|
+
Dereference $ref pointers in JSONSchema or OpenAPI documents.
|
|
8
|
+
|
|
9
|
+
Zero dependencies. Synchronous core. Handles circular refs.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
`npm install @trojs/openapi-dereference`
|
|
14
|
+
or
|
|
15
|
+
`yarn add @trojs/openapi-dereference`
|
|
16
|
+
|
|
17
|
+
## Test the package
|
|
18
|
+
|
|
19
|
+
`npm run test`
|
|
20
|
+
or
|
|
21
|
+
`yarn test`
|
|
22
|
+
|
|
23
|
+
## How to use
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
npm i @trojs/openapi-dereference
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import { dereferenceSync } from '@trojs/openapi-dereference';
|
|
33
|
+
|
|
34
|
+
const schemaWithRefs = {
|
|
35
|
+
schemas: {
|
|
36
|
+
Person: {
|
|
37
|
+
type: 'object',
|
|
38
|
+
properties: {
|
|
39
|
+
name: {
|
|
40
|
+
$ref: '#/schemas/Name',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
Name: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const schemaWithNoRefs = dereferenceSync(schemaWithRefs);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
[npm-url]: https://www.npmjs.com/package/@trojs/openapi-dereference
|
|
54
|
+
[npm-image]: https://img.shields.io/npm/v/@trojs/openapi-dereference.svg
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trojs/openapi-dereference",
|
|
3
|
+
"description": "OpenAPI dereference",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Pieter Wigboldus",
|
|
7
|
+
"url": "https://trojs.org/"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"lint": "eslint src/*.js --config .eslintrc",
|
|
12
|
+
"lint:report": "eslint src/*.js --config .eslintrc -f json -o report.json",
|
|
13
|
+
"lint:fix": "eslint src/*.js --config .eslintrc --fix",
|
|
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",
|
|
17
|
+
"cpd": "node_modules/jscpd/bin/jscpd src",
|
|
18
|
+
"vulnerabilities": "npm audit --omit=dev"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"files": [
|
|
22
|
+
"src/dereference.js",
|
|
23
|
+
"src/index.js",
|
|
24
|
+
"src/klona.js",
|
|
25
|
+
"src/resolveRef.js",
|
|
26
|
+
"src/types.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"main": "src/index.js",
|
|
29
|
+
"types": "src/types.d.ts",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@hckrnews/eslint-config": "^3.0.0",
|
|
32
|
+
"c8": "^9.0.0",
|
|
33
|
+
"eslint": "^8.23.0",
|
|
34
|
+
"eslint-config-standard": "^17.1.0",
|
|
35
|
+
"eslint-plugin-import": "^2.26.0",
|
|
36
|
+
"eslint-plugin-jsdoc": "^48.0.0",
|
|
37
|
+
"eslint-plugin-n": "^16.0.0",
|
|
38
|
+
"eslint-plugin-promise": "^6.0.1",
|
|
39
|
+
"eslint-plugin-sonarjs": "^0.24.0",
|
|
40
|
+
"jscpd": "^3.2.1",
|
|
41
|
+
"json-schema": "^0.4.0"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/hckrnews/openapi-dereference"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">= 18.13"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"openapi",
|
|
52
|
+
"model",
|
|
53
|
+
"entity",
|
|
54
|
+
"schema",
|
|
55
|
+
"specification",
|
|
56
|
+
"api",
|
|
57
|
+
"ajv",
|
|
58
|
+
"dereference",
|
|
59
|
+
"reference",
|
|
60
|
+
"ref"
|
|
61
|
+
],
|
|
62
|
+
"funding": {
|
|
63
|
+
"type": "github",
|
|
64
|
+
"url": "https://github.com/sponsors/w3nl"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/* eslint-disable sonarjs/cognitive-complexity */
|
|
2
|
+
/* eslint-disable no-param-reassign */
|
|
3
|
+
/* eslint-disable no-restricted-syntax */
|
|
4
|
+
import { klona } from './klona.js'
|
|
5
|
+
import { resolveRefSync } from './resolveRef.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('./types').JSONSchema} JSONSchema
|
|
9
|
+
* @typedef {import('./types').DereferencedJSONSchema} DereferencedJSONSchema
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const cache = new Map()
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Resolves all $ref pointers in a schema and returns a new schema without any $ref pointers.
|
|
16
|
+
* @param {JSONSchema} schema
|
|
17
|
+
* @returns {DereferencedJSONSchema}
|
|
18
|
+
*/
|
|
19
|
+
export const dereferenceSync = (schema) => {
|
|
20
|
+
if (cache.has(schema)) {
|
|
21
|
+
return cache.get(schema)
|
|
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)
|
|
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
|
+
return resolveRefSync(cloned, current.$ref)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (const key in current) {
|
|
47
|
+
current[key] = resolve(current[key], `${path}/${key}`)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return current
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const result = resolve(cloned, '#')
|
|
56
|
+
cache.set(schema, result)
|
|
57
|
+
return result
|
|
58
|
+
}
|
package/src/index.js
ADDED
package/src/klona.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/* eslint-disable sonarjs/cognitive-complexity */
|
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
|
3
|
+
/**
|
|
4
|
+
* klona/json - MIT License
|
|
5
|
+
*
|
|
6
|
+
* https://github.com/lukeed/klona/blob/master/license
|
|
7
|
+
* @param {any} val
|
|
8
|
+
* @returns {any}
|
|
9
|
+
*/
|
|
10
|
+
export function klona (val) {
|
|
11
|
+
let index, out, tmp
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(val)) {
|
|
14
|
+
out = Array((index = val.length))
|
|
15
|
+
while (index--) out[index] = (tmp = val[index]) && typeof tmp === 'object' ? klona(tmp) : tmp
|
|
16
|
+
return out
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (Object.prototype.toString.call(val) === '[object Object]') {
|
|
20
|
+
out = {} // null
|
|
21
|
+
for (index in val) {
|
|
22
|
+
if (index === '__proto__') {
|
|
23
|
+
Object.defineProperty(out, index, {
|
|
24
|
+
value: klona(val[index]),
|
|
25
|
+
configurable: true,
|
|
26
|
+
enumerable: true,
|
|
27
|
+
writable: true
|
|
28
|
+
})
|
|
29
|
+
} else {
|
|
30
|
+
out[index] = (tmp = val[index]) && typeof tmp === 'object' ? klona(tmp) : tmp
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return val
|
|
37
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./types').JSONSchema} JSONSchema
|
|
3
|
+
* @typedef {import('./types').DereferencedJSONSchema} DereferencedJSONSchema
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const cache = new Map()
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Resolves a $ref pointer in a schema and returns the referenced value.
|
|
10
|
+
* @param {JSONSchema} schema
|
|
11
|
+
* @param {string} ref
|
|
12
|
+
* @returns {unknown}
|
|
13
|
+
*/
|
|
14
|
+
export const resolveRefSync = (schema, ref) => {
|
|
15
|
+
if (!cache.has(schema)) {
|
|
16
|
+
cache.set(schema, new Map())
|
|
17
|
+
}
|
|
18
|
+
const schemaCache = cache.get(schema)
|
|
19
|
+
|
|
20
|
+
if (schemaCache.has(ref)) {
|
|
21
|
+
return schemaCache.get(ref)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const path = ref.split('/').slice(1)
|
|
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
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
schemaCache.set(ref, current)
|
|
36
|
+
return current
|
|
37
|
+
}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { JSONSchema4, JSONSchema4Type, JSONSchema6, JSONSchema6Type } from 'json-schema';
|
|
2
|
+
|
|
3
|
+
export type JSONSchema = Prettify<JSONSchema4 | JSONSchema6>;
|
|
4
|
+
export type JSONSchemaType = Prettify<JSONSchema4Type | JSONSchema6Type>;
|
|
5
|
+
export type DereferencedJSONSchema = Prettify<DeepOmit<JSONSchema, '$ref'>>;
|
|
6
|
+
|
|
7
|
+
type Prettify<T> = {
|
|
8
|
+
[K in keyof T]: T[K];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type Primitive = string | Function | number | boolean | Symbol | undefined | null;
|
|
12
|
+
|
|
13
|
+
type DeepOmitArray<T extends any[], K> = {
|
|
14
|
+
[P in keyof T]: DeepOmit<T[P], K>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type DeepOmit<T, K> = T extends Primitive
|
|
18
|
+
? T
|
|
19
|
+
: {
|
|
20
|
+
[P in Exclude<keyof T, K>]: T[P] extends infer TP
|
|
21
|
+
? TP extends Primitive
|
|
22
|
+
? TP // leave primitives and functions alone
|
|
23
|
+
: TP extends any[]
|
|
24
|
+
? DeepOmitArray<TP, K> // Array special handling
|
|
25
|
+
: DeepOmit<TP, K>
|
|
26
|
+
: never;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const dereferenceSync: (schema: JSONSchema) => DereferencedJSONSchema
|