@stoplight/json-ref-resolver
Advanced tools
Comparing version 1.5.1 to 2.0.0
@@ -0,0 +0,0 @@ import * as Types from './types'; |
import { DepGraph } from 'dependency-graph'; | ||
import * as Types from './types'; | ||
export declare class ResolveCrawler implements Types.ICrawler { | ||
readonly authorityResolvers: Array<Promise<Types.IAuthorityLookupResult>>; | ||
readonly resolvers: Array<Promise<Types.IUriResult>>; | ||
jsonPointer?: string; | ||
@@ -6,0 +6,0 @@ readonly pointerGraph: DepGraph<string>; |
export * from './resolver'; | ||
import { defaultGetRef } from './runner'; | ||
export { defaultGetRef }; | ||
export { Cache } from './cache'; | ||
export { defaultGetRef } from './runner'; |
{ | ||
"name": "@stoplight/json-ref-resolver", | ||
"version": "1.5.1", | ||
"version": "2.0.0", | ||
"description": "Recursively resolve JSON pointers and remote authorities.", | ||
@@ -18,2 +18,3 @@ "keywords": [ | ||
], | ||
"main": "index.js", | ||
"sideEffects": false, | ||
@@ -33,4 +34,4 @@ "files": [ | ||
"dependencies": { | ||
"@stoplight/json": "2.1.x", | ||
"@types/urijs": "1.19.1", | ||
"@stoplight/json": "2.x.x", | ||
"@types/urijs": "1.x.x", | ||
"dependency-graph": "0.8.x", | ||
@@ -40,7 +41,6 @@ "fast-memoize": "2.x.x", | ||
"lodash": "4.x.x", | ||
"urijs": "1.x.x" | ||
"urijs": "1.x.x", | ||
"vscode-uri": "2.x.x" | ||
}, | ||
"main": "index.cjs.js", | ||
"module": "index.es.js", | ||
"typings": "src/index.d.ts" | ||
"typings": "index.d.ts" | ||
} |
@@ -5,3 +5,3 @@ # JSON Ref Resolver | ||
Recursively resolves JSON pointers and remote authorities. | ||
Dereference $ref values in JSON Schema, OpenAPI (Swagger), and any other objects with $ref values inside of them. | ||
@@ -12,7 +12,8 @@ - View the changelog: [Releases](https://github.com/stoplightio/json-ref-resolver/releases) | ||
- **Performant**: Hot paths are memoized, remote authorities are resolved concurrently, and the minimum surface area is crawled and resolved. | ||
- **Caching**: Results from remote authorities are cached. | ||
- **Immutable**: The original object is not changed, and structural sharing is used to only change relevant bits. [example test](src/__tests__/resolver.spec.ts#L139-L143) | ||
- **Reference equality:** Pointers to the same location will resolve to the same object in memory. [example test](src/__tests__/resolver.spec.ts#L145) | ||
- **Flexible:** Bring your own readers for `http://`, `file://`, `mongo://`, `custom://`... etc. | ||
- **Performant**: Hot paths are memoized, remote URIs are resolved concurrently, and the minimum surface area is crawled and resolved. | ||
- **Caching**: Results from remote URIs are cached. | ||
- **Immutable**: The original object is not changed, and structural sharing is used to only change relevant bits. [example test](src/__tests__/resolver.spec.ts#L182) | ||
- **Reference equality:** $refs to the same location will resolve to the same object in memory. [example test](src/__tests__/resolver.spec.ts#L329) | ||
- **Flexible:** Bring your own resolvers for `http://`, `file://`, `mongo://`, `custom://`... etc. | ||
- **Cross Platform:** Supports POSIX and Windows style file paths. | ||
- **Reliable:** Well tested to handle all sorts of circular reference edge cases. | ||
@@ -66,4 +67,6 @@ | ||
#### Example: Basic Local Resolution | ||
#### Example: Basic Inline Dereferencing | ||
By default, only inline references will be dereferenced. | ||
```ts | ||
@@ -97,9 +100,9 @@ import { Resolver } from "@stoplight/json-ref-resolver"; | ||
#### Example: Resolve a Subset of the Source | ||
#### Example: Dereference a Subset of the Source | ||
This will resolve the minimal number of references needed for the given target, and return the target. | ||
This will dereference the minimal number of references needed for the given target, and return the target. | ||
In the example below, the address reference (`https://slow-website.com/definitions#/address`) will NOT be resolved, since | ||
it is not needed to resolve the `#/user` jsonPointer target we have specified. However, `#/models/user/card` IS resolved since | ||
it is needed in order to full resolve the `#/user` property. | ||
In the example below, the address reference (`https://slow-website.com/definitions#/address`) will NOT be dereferenced, since | ||
it is not needed to resolve the `#/user` jsonPointer target we have specified. However, `#/models/user/card` IS dereferenced since | ||
it is needed in order to full dereference the `#/user` property. | ||
@@ -144,9 +147,9 @@ ```ts | ||
#### Example: Resolving Remote References with Readers | ||
#### Example: Dereferencing Remote References with Resolvers | ||
By default only local references (those that point to values inside of the original source) are resolved. | ||
By default only inline references (those that point to values inside of the original object) are dereferenced. | ||
In order to resolve remote authorities (file, http, etc) you must provide readers for each authority scheme. | ||
In order to dereference remote URIs (file, http, etc) you must provide resolvers for each URI scheme. | ||
Readers are keyed by scheme, receive the URI to fetch, and must return the fetched data. | ||
Resolvers are keyed by scheme, receive the URI to fetch, and must return the fetched data. | ||
@@ -159,3 +162,3 @@ ```ts | ||
// if we're in node, we create a file reader with fs | ||
// if we're in node, we create a file resolver with fs | ||
import * as fs from "fs"; | ||
@@ -165,7 +168,7 @@ | ||
const resolver = new Resolver({ | ||
// readers can do anything, so long as they define an async read function that resolves to a value | ||
readers: { | ||
// this reader will be invoked for refs with the https protocol | ||
// resolvers can do anything, so long as they define an async read function that resolves to a value | ||
resolvers: { | ||
// this resolver will be invoked for refs with the https protocol | ||
https: { | ||
async read(ref: uri.URI) { | ||
async resolve(ref: uri.URI) { | ||
return axios({ | ||
@@ -178,5 +181,5 @@ method: "get", | ||
// this reader will be invoked for refs with the file protocol | ||
// this resolver will be invoked for refs with the file protocol | ||
file: { | ||
async read(ref: uri.URI) { | ||
async resolve(ref: uri.URI) { | ||
return fs.read(String(ref)); | ||
@@ -212,6 +215,6 @@ } | ||
#### Example: Resolving Relative Remote References with the Authority Option | ||
#### Example: Dereferencing Relative Remote References with the baseUri Option | ||
If there are relative remote references (for example, a relative file path `../model.json`), then the location of the source | ||
data must be specified via the `authority` resolve option. Relative references will be resolved against this authority. | ||
data must be specified via the `baseUri` option. Relative references will be dereferenced against this baseUri. | ||
@@ -243,3 +246,3 @@ ```ts | ||
// Indicate where the `sourceData` being resolved lives, so that relative remote references can be fetched and resolved. | ||
authority: new URI(sourcePath) | ||
baseUri: new URI(sourcePath) | ||
}); | ||
@@ -254,3 +257,3 @@ | ||
In the above example, the user \$ref will resolve to `/models/user.json`, because `../models/user.json` is resolved against the authority of the current document (which was indicated at `/specs/api.json`). Relative references will not work if the source document has no authority set. | ||
In the above example, the user \$ref will resolve to `/models/user.json`, because `../models/user.json` is resolved against the baseUri of the current document (which was indicated at `/specs/api.json`). Relative references will not work if the source document has no baseUri set. | ||
@@ -257,0 +260,0 @@ ### Contributing |
/// <reference types="urijs" /> | ||
import * as Types from './types'; | ||
export declare class Resolver { | ||
readonly authorityCache: Types.ICache; | ||
protected resolvePointers: boolean; | ||
protected resolveAuthorities: boolean; | ||
readonly uriCache: Types.ICache; | ||
protected dereferenceInline: boolean; | ||
protected dereferenceRemote: boolean; | ||
protected ctx: any; | ||
protected readers: { | ||
[scheme: string]: Types.IReader; | ||
protected resolvers: { | ||
[scheme: string]: Types.IResolver; | ||
}; | ||
protected getRef?: (key: string, val: any) => string | void; | ||
protected transformRef?: (opts: Types.IRefTransformer, ctx: any) => uri.URI | any; | ||
protected parseAuthorityResult?: (opts: Types.IAuthorityParser) => Promise<Types.IAuthorityParserResult>; | ||
protected parseResolveResult?: (opts: Types.IUriParser) => Promise<Types.IUriParserResult>; | ||
constructor(opts?: Types.IResolverOpts); | ||
resolve(source: any, opts?: Types.IResolveOpts): Promise<Types.IResolveResult>; | ||
} |
@@ -6,15 +6,15 @@ /// <reference types="urijs" /> | ||
readonly id: number; | ||
readonly authority: uri.URI; | ||
readonly authorityCache: Types.ICache; | ||
readonly baseUri: uri.URI; | ||
readonly uriCache: Types.ICache; | ||
depth: number; | ||
authorityStack: string[]; | ||
readonly resolvePointers: boolean; | ||
readonly resolveAuthorities: boolean; | ||
uriStack: string[]; | ||
readonly dereferenceInline: boolean; | ||
readonly dereferenceRemote: boolean; | ||
ctx: any; | ||
readonly readers: { | ||
[scheme: string]: Types.IReader; | ||
readonly resolvers: { | ||
[scheme: string]: Types.IResolver; | ||
}; | ||
readonly getRef: (key: string, val: any) => string | void; | ||
readonly transformRef?: (opts: Types.IRefTransformer, ctx: any) => uri.URI | any; | ||
readonly parseAuthorityResult?: (opts: Types.IAuthorityParser) => Promise<Types.IAuthorityParserResult>; | ||
readonly parseResolveResult?: (opts: Types.IUriParser) => Promise<Types.IUriParserResult>; | ||
private _source; | ||
@@ -25,10 +25,11 @@ constructor(source: any, opts?: Types.IResolveRunnerOpts); | ||
computeRef: (opts: Types.IComputeRefOpts) => void | uri.URI; | ||
atMaxAuthorityDepth: () => boolean; | ||
lookupAuthority: (opts: { | ||
atMaxUriDepth: () => boolean; | ||
lookupUri: (opts: { | ||
ref: uri.URI; | ||
cacheKey: string; | ||
}) => Promise<ResolveRunner>; | ||
lookupAndResolveAuthority: (opts: Types.IRefHandlerOpts) => Promise<Types.IAuthorityLookupResult>; | ||
lookupAndResolveUri: (opts: Types.IRefHandlerOpts) => Promise<Types.IUriResult>; | ||
_cacheKeySerializer(sOpts: any): any; | ||
private computeAuthorityCacheKey; | ||
private computeUriCacheKey; | ||
private isFile; | ||
} |
@@ -5,11 +5,11 @@ /// <reference types="urijs" /> | ||
export interface IResolverOpts { | ||
authorityCache?: ICache; | ||
readers?: { | ||
[scheme: string]: IReader; | ||
uriCache?: ICache; | ||
resolvers?: { | ||
[scheme: string]: IResolver; | ||
}; | ||
getRef?: (key: string, val: any) => string | void; | ||
transformRef?: (opts: IRefTransformer, ctx: any) => uri.URI | void; | ||
parseAuthorityResult?: (opts: IAuthorityParser) => Promise<IAuthorityParserResult>; | ||
resolvePointers?: boolean; | ||
resolveAuthorities?: boolean; | ||
parseResolveResult?: (opts: IUriParser) => Promise<IUriParserResult>; | ||
dereferenceInline?: boolean; | ||
dereferenceRemote?: boolean; | ||
ctx?: any; | ||
@@ -19,3 +19,3 @@ } | ||
jsonPointer?: string; | ||
authority?: uri.URI; | ||
baseUri?: string; | ||
} | ||
@@ -30,8 +30,8 @@ export interface IResolveResult { | ||
} | ||
export interface IReader { | ||
read(ref: uri.URI, ctx: any): Promise<any>; | ||
export interface IResolver { | ||
resolve(ref: uri.URI, ctx: any): Promise<any>; | ||
} | ||
export interface IAuthorityParser { | ||
export interface IUriParser { | ||
result: any; | ||
authorityResult: IAuthorityLookupResult; | ||
uriResult: IUriResult; | ||
targetAuthority: uri.URI; | ||
@@ -41,7 +41,7 @@ parentAuthority: uri.URI; | ||
} | ||
export interface IAuthorityParserResult { | ||
export interface IUriParserResult { | ||
result?: any; | ||
error?: Error; | ||
} | ||
export interface IAuthorityLookupResult { | ||
export interface IUriResult { | ||
pointerStack: string[]; | ||
@@ -61,5 +61,5 @@ targetPath: string[]; | ||
ref?: uri.URI; | ||
authority: uri.URI; | ||
uri: uri.URI; | ||
} | ||
export declare type ResolverErrorCode = 'POINTER_MISSING' | 'RESOLVE_AUTHORITY' | 'PARSE_AUTHORITY' | 'RESOLVE_POINTER'; | ||
export declare type ResolverErrorCode = 'POINTER_MISSING' | 'RESOLVE_URI' | 'PARSE_URI' | 'RESOLVE_POINTER'; | ||
export interface IResolveError { | ||
@@ -69,4 +69,4 @@ code: ResolverErrorCode; | ||
path: Segment[]; | ||
authority: uri.URI; | ||
authorityStack: string[]; | ||
uri: uri.URI; | ||
uriStack: string[]; | ||
pointerStack: string[]; | ||
@@ -102,14 +102,14 @@ } | ||
source: any; | ||
resolvePointers: boolean; | ||
resolveAuthorities: boolean; | ||
authorityCache: ICache; | ||
dereferenceInline: boolean; | ||
dereferenceRemote: boolean; | ||
uriCache: ICache; | ||
depth: number; | ||
atMaxAuthorityDepth: () => boolean; | ||
atMaxUriDepth: () => boolean; | ||
resolve: (source: any, opts?: IResolveOpts) => Promise<IResolveResult>; | ||
computeRef: (opts: IComputeRefOpts) => uri.URI | void | undefined; | ||
lookupAndResolveAuthority: (opts: IRefHandlerOpts) => Promise<IAuthorityLookupResult>; | ||
lookupAndResolveUri: (opts: IRefHandlerOpts) => Promise<IUriResult>; | ||
} | ||
export interface IResolveRunnerOpts extends IResolveOpts { | ||
depth?: number; | ||
authorityStack?: string[]; | ||
uriStack?: string[]; | ||
} | ||
@@ -116,0 +116,0 @@ export interface ICrawler { |
@@ -0,0 +0,0 @@ /// <reference types="urijs" /> |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
24
1
260
69137
8
762
+ Addedvscode-uri@2.x.x
+ Added@stoplight/json@2.3.3(transitive)
+ Added@stoplight/types@9.1.2(transitive)
+ Added@types/json-schema@7.0.15(transitive)
+ Added@types/urijs@1.19.25(transitive)
+ Addedjsonc-parser@2.1.1(transitive)
+ Addedvscode-uri@2.1.2(transitive)
- Removed@stoplight/json@2.1.0(transitive)
- Removed@stoplight/types@5.1.2(transitive)
- Removed@types/urijs@1.19.1(transitive)
- Removedjsonc-parser@2.1.0(transitive)
Updated@stoplight/json@2.x.x
Updated@types/urijs@1.x.x