@scalar/openapi-parser 0.8.10 → 0.9.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/CHANGELOG.md +6 -0
- package/README.md +14 -0
- package/dist/utils/dereference.d.ts +3 -2
- package/dist/utils/dereference.d.ts.map +1 -1
- package/dist/utils/resolveReferences.d.ts +7 -1
- package/dist/utils/resolveReferences.d.ts.map +1 -1
- package/dist/utils/resolveReferences.js +5 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -63,6 +63,20 @@ const specification = `{
|
|
|
63
63
|
const { schema, errors } = await dereference(specification)
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
+
### Track references
|
|
67
|
+
|
|
68
|
+
The `dereference` function accepts an `onDereference` callback option that gets called whenever a reference is resolved. This can be useful for tracking which schemas are being dereferenced:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { dereference } from '@scalar/openapi-parser'
|
|
72
|
+
|
|
73
|
+
const { schema, errors } = await dereference(specification, {
|
|
74
|
+
onDereference: ({ schema, ref }) => {
|
|
75
|
+
//
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
66
80
|
### Modify an OpenAPI document
|
|
67
81
|
|
|
68
82
|
```ts
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { AnyApiDefinitionFormat, DereferenceResult, Filesystem
|
|
2
|
-
|
|
1
|
+
import type { AnyApiDefinitionFormat, DereferenceResult, Filesystem } from '../types';
|
|
2
|
+
import { type ResolveReferencesOptions } from './resolveReferences.js';
|
|
3
|
+
export type DereferenceOptions = ResolveReferencesOptions;
|
|
3
4
|
/**
|
|
4
5
|
* Resolves all references in an OpenAPI document
|
|
5
6
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dereference.d.ts","sourceRoot":"","sources":["../../src/utils/dereference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,sBAAsB,EACtB,iBAAiB,EACjB,UAAU,
|
|
1
|
+
{"version":3,"file":"dereference.d.ts","sourceRoot":"","sources":["../../src/utils/dereference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,sBAAsB,EACtB,iBAAiB,EACjB,UAAU,EACX,MAAM,UAAU,CAAA;AAIjB,OAAO,EACL,KAAK,wBAAwB,EAE9B,MAAM,qBAAqB,CAAA;AAE5B,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,CAAA;AAEzD;;GAEG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,sBAAsB,GAAG,UAAU,EAC1C,OAAO,CAAC,EAAE,kBAAkB,GAC3B,OAAO,CAAC,iBAAiB,CAAC,CAY5B"}
|
|
@@ -5,8 +5,14 @@ export type ResolveReferencesResult = {
|
|
|
5
5
|
errors: ErrorObject[];
|
|
6
6
|
schema: OpenAPI.Document;
|
|
7
7
|
};
|
|
8
|
+
export type ResolveReferencesOptions = ThrowOnErrorOption & {
|
|
9
|
+
onDereference?: (data: {
|
|
10
|
+
schema: AnyObject;
|
|
11
|
+
ref: string;
|
|
12
|
+
}) => void;
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* Takes a specification and resolves all references.
|
|
10
16
|
*/
|
|
11
|
-
export declare function resolveReferences(input: AnyObject | Filesystem, options?:
|
|
17
|
+
export declare function resolveReferences(input: AnyObject | Filesystem, options?: ResolveReferencesOptions, file?: FilesystemEntry, errors?: ErrorObject[]): ResolveReferencesResult;
|
|
12
18
|
//# sourceMappingURL=resolveReferences.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveReferences.d.ts","sourceRoot":"","sources":["../../src/utils/resolveReferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAGpD,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,eAAe,EACf,kBAAkB,EACnB,MAAM,UAAU,CAAA;AAejB,MAAM,MAAM,uBAAuB,GAAG;IACpC,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,WAAW,EAAE,CAAA;IACrB,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAA;CACzB,CAAA;AAOD;;GAEG;AACH,wBAAgB,iBAAiB,CAE/B,KAAK,EAAE,SAAS,GAAG,UAAU,EAE7B,OAAO,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"resolveReferences.d.ts","sourceRoot":"","sources":["../../src/utils/resolveReferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAGpD,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,eAAe,EACf,kBAAkB,EACnB,MAAM,UAAU,CAAA;AAejB,MAAM,MAAM,uBAAuB,GAAG;IACpC,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,WAAW,EAAE,CAAA;IACrB,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG,kBAAkB,GAAG;IAC1D,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,MAAM,EAAE,SAAS,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAA;CACnE,CAAA;AAOD;;GAEG;AACH,wBAAgB,iBAAiB,CAE/B,KAAK,EAAE,SAAS,GAAG,UAAU,EAE7B,OAAO,CAAC,EAAE,wBAAwB,EAElC,IAAI,CAAC,EAAE,eAAe,EAEtB,MAAM,CAAC,EAAE,WAAW,EAAE,GACrB,uBAAuB,CAoGzB"}
|
|
@@ -26,10 +26,10 @@ errors) {
|
|
|
26
26
|
// Get the main file
|
|
27
27
|
const entrypoint = getEntrypoint(filesystem);
|
|
28
28
|
// Recursively resolve all references
|
|
29
|
-
resolve(file?.specification ?? entrypoint.specification, filesystem, file ?? entrypoint);
|
|
29
|
+
resolve(file?.specification ?? entrypoint.specification, filesystem, file ?? entrypoint, options?.onDereference);
|
|
30
30
|
// If we replace references with content, that includes a reference, we can’t deal with that right-away.
|
|
31
31
|
// That’s why we need a second run.
|
|
32
|
-
resolve(file?.specification ?? entrypoint.specification, filesystem, file ?? entrypoint);
|
|
32
|
+
resolve(file?.specification ?? entrypoint.specification, filesystem, file ?? entrypoint, options?.onDereference);
|
|
33
33
|
// Remove duplicats (according to message) from errors
|
|
34
34
|
errors = errors.filter((error, index, self) => index ===
|
|
35
35
|
self.findIndex((t) => t.message === error.message && t.code === error.code));
|
|
@@ -43,7 +43,7 @@ errors) {
|
|
|
43
43
|
/**
|
|
44
44
|
* Resolves the circular reference to an object and deletes the $ref properties.
|
|
45
45
|
*/
|
|
46
|
-
function resolve(schema, resolveFilesystem, resolveFile) {
|
|
46
|
+
function resolve(schema, resolveFilesystem, resolveFile, onResolve) {
|
|
47
47
|
let result;
|
|
48
48
|
// Iterate over the whole objecct
|
|
49
49
|
Object.entries(schema ?? {}).forEach(([_, value]) => {
|
|
@@ -54,6 +54,7 @@ errors) {
|
|
|
54
54
|
if (target === undefined) {
|
|
55
55
|
return undefined;
|
|
56
56
|
}
|
|
57
|
+
onResolve?.({ schema, ref: schema.$ref });
|
|
57
58
|
// Get rid of the reference
|
|
58
59
|
delete schema.$ref;
|
|
59
60
|
if (typeof target === 'object') {
|
|
@@ -65,7 +66,7 @@ errors) {
|
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
if (typeof value === 'object' && !isCircular(value)) {
|
|
68
|
-
result = resolve(value, resolveFilesystem, resolveFile);
|
|
69
|
+
result = resolve(value, resolveFilesystem, resolveFile, onResolve);
|
|
69
70
|
}
|
|
70
71
|
});
|
|
71
72
|
return {
|