@stoplight/json-ref-resolver
Advanced tools
Comparing version 2.0.3 to 2.1.0
{ | ||
"name": "@stoplight/json-ref-resolver", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Recursively resolve JSON pointers and remote authorities.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -14,4 +14,5 @@ /// <reference types="urijs" /> | ||
protected parseResolveResult?: (opts: Types.IUriParser) => Promise<Types.IUriParserResult>; | ||
protected transformDereferenceResult?: (opts: Types.IDereferenceTransformer) => Promise<Types.ITransformerResult>; | ||
constructor(opts?: Types.IResolverOpts); | ||
resolve(source: any, opts?: Types.IResolveOpts): Promise<Types.IResolveResult>; | ||
} |
@@ -15,6 +15,7 @@ "use strict"; | ||
this.parseResolveResult = opts.parseResolveResult; | ||
this.transformDereferenceResult = opts.transformDereferenceResult; | ||
this.ctx = opts.ctx; | ||
} | ||
resolve(source, opts = {}) { | ||
const runner = new runner_1.ResolveRunner(source, Object.assign({ uriCache: this.uriCache, resolvers: this.resolvers, getRef: this.getRef, transformRef: this.transformRef, dereferenceInline: this.dereferenceInline, dereferenceRemote: this.dereferenceRemote, parseResolveResult: this.parseResolveResult }, opts, { ctx: Object.assign({}, this.ctx || {}, opts.ctx || {}) })); | ||
const runner = new runner_1.ResolveRunner(source, Object.assign({ uriCache: this.uriCache, resolvers: this.resolvers, getRef: this.getRef, transformRef: this.transformRef, dereferenceInline: this.dereferenceInline, dereferenceRemote: this.dereferenceRemote, parseResolveResult: this.parseResolveResult, transformDereferenceResult: this.transformDereferenceResult }, opts, { ctx: Object.assign({}, this.ctx || {}, opts.ctx || {}) })); | ||
return runner.resolve(opts.jsonPointer); | ||
@@ -21,0 +22,0 @@ } |
@@ -19,2 +19,3 @@ /// <reference types="urijs" /> | ||
readonly parseResolveResult?: (opts: Types.IUriParser) => Promise<Types.IUriParserResult>; | ||
readonly transformDereferenceResult?: (opts: Types.IDereferenceTransformer) => Promise<Types.ITransformerResult>; | ||
private _source; | ||
@@ -21,0 +22,0 @@ constructor(source: any, opts?: Types.IResolveRunnerOpts); |
@@ -93,2 +93,3 @@ "use strict"; | ||
parseResolveResult: this.parseResolveResult, | ||
transformDereferenceResult: this.transformDereferenceResult, | ||
dereferenceRemote: this.dereferenceRemote, | ||
@@ -191,2 +192,3 @@ dereferenceInline: this.dereferenceInline, | ||
this.parseResolveResult = opts.parseResolveResult; | ||
this.transformDereferenceResult = opts.transformDereferenceResult; | ||
this.ctx = opts.ctx; | ||
@@ -318,2 +320,29 @@ this.lookupUri = memoize(this.lookupUri, { | ||
} | ||
if (this.transformDereferenceResult) { | ||
const ref = new URI(jsonPointer || ''); | ||
try { | ||
const { result, error } = yield this.transformDereferenceResult({ | ||
source: this.source, | ||
result: resolved.result, | ||
targetAuthority: ref, | ||
parentAuthority: this.baseUri, | ||
parentPath: targetPath, | ||
fragment: ref.fragment(), | ||
}); | ||
resolved.result = result; | ||
if (error) { | ||
throw new Error(`Could not transform dereferenced result for '${ref.toString()}' - ${String(error)}`); | ||
} | ||
} | ||
catch (e) { | ||
resolved.errors.push({ | ||
code: 'TRANSFORM_DEREFERENCED', | ||
message: `Error: Could not transform dereferenced result for '${this.baseUri.toString()}${ref.fragment() !== '' ? `#${ref.fragment()}` : ``}' - ${String(e)}`, | ||
uri: ref, | ||
uriStack: this.uriStack, | ||
pointerStack: [], | ||
path: targetPath, | ||
}); | ||
} | ||
} | ||
return resolved; | ||
@@ -320,0 +349,0 @@ }); |
@@ -12,2 +12,3 @@ /// <reference types="urijs" /> | ||
parseResolveResult?: (opts: IUriParser) => Promise<IUriParserResult>; | ||
transformDereferenceResult?: (opts: IDereferenceTransformer) => Promise<ITransformerResult>; | ||
dereferenceInline?: boolean; | ||
@@ -44,2 +45,14 @@ dereferenceRemote?: boolean; | ||
} | ||
export interface IDereferenceTransformer { | ||
result: any; | ||
source: any; | ||
fragment: string; | ||
targetAuthority: uri.URI; | ||
parentAuthority: uri.URI; | ||
parentPath: string[]; | ||
} | ||
export interface ITransformerResult { | ||
result?: any; | ||
error?: Error; | ||
} | ||
export interface IUriResult { | ||
@@ -62,3 +75,3 @@ pointerStack: string[]; | ||
} | ||
export declare type ResolverErrorCode = 'POINTER_MISSING' | 'RESOLVE_URI' | 'PARSE_URI' | 'RESOLVE_POINTER'; | ||
export declare type ResolverErrorCode = 'POINTER_MISSING' | 'RESOLVE_URI' | 'PARSE_URI' | 'RESOLVE_POINTER' | 'TRANSFORM_DEREFERENCED'; | ||
export interface IResolveError { | ||
@@ -65,0 +78,0 @@ code: ResolverErrorCode; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
72728
813