From 226bc5903144cc208f85f5315b57bf8c7fcf6ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Tue, 2 Jun 2020 20:13:16 +0200 Subject: [PATCH] fix: urijs type (#208) --- package.json | 10 ++++----- src/__tests__/resolver.spec.ts | 34 ++++++++++++++--------------- src/resolver.ts | 3 ++- src/runner.ts | 12 +++++----- src/types.ts | 29 ++++++++++++------------ src/utils.ts | 6 +++-- yarn.lock | 40 ++++++++++++++++++++++++---------- 7 files changed, 77 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index 3139b3d..933dc2d 100644 --- a/package.json +++ b/package.json @@ -45,15 +45,15 @@ }, "dependencies": { "@stoplight/json": "^3.4.0", - "@stoplight/path": "^1.3.0", - "@stoplight/types": "^11.4.0", - "@types/urijs": "^1.19.5", + "@stoplight/path": "^1.3.1", + "@stoplight/types": "^11.6.0", + "@types/urijs": "^1.19.9", "dependency-graph": "~0.8.0", "fast-memoize": "^2.5.1", "immer": "^5.3.2", "lodash": "^4.17.15", - "tslib": "^1.10.0", - "urijs": "~1.19.2" + "tslib": "^1.13.0", + "urijs": "^1.19.2" }, "devDependencies": { "@stoplight/scripts": "^5", diff --git a/src/__tests__/resolver.spec.ts b/src/__tests__/resolver.spec.ts index a7e6903..c7ca190 100644 --- a/src/__tests__/resolver.spec.ts +++ b/src/__tests__/resolver.spec.ts @@ -12,7 +12,7 @@ import httpMocks from './fixtures/http-mocks'; import resolvedResults from './fixtures/resolved'; export class FileReader implements Types.IResolver { - public async resolve(uri: uri.URI) { + public async resolve(uri: URI) { const path = uri.path(); return new Promise((resolve, reject) => { try { @@ -26,7 +26,7 @@ export class FileReader implements Types.IResolver { } export class HttpReader implements Types.IResolver { - public async resolve(uri: uri.URI) { + public async resolve(uri: URI) { const mock = httpMocks[uri.toString()]; if (mock) return mock; @@ -150,7 +150,7 @@ describe('resolver', () => { const uris: string[] = []; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { const uri = ref.toString(); uris.push(uri); return remotes[uri]; @@ -686,7 +686,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(uri: uri.URI): Promise { + async resolve(uri: URI): Promise { return data[uri.authority()]; }, }; @@ -767,7 +767,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(uri: uri.URI): Promise { + async resolve(uri: URI): Promise { return data[uri.authority()]; }, }; @@ -944,7 +944,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { return data[ref.authority()]; }, }; @@ -1074,7 +1074,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { return data[ref.authority()]; }, }; @@ -1334,7 +1334,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { if (data[ref.authority()]) { return data[ref.authority()]; } @@ -1410,7 +1410,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { if (data[ref.authority()]) { return data[ref.authority()]; } @@ -1549,7 +1549,7 @@ describe('resolver', () => { }); const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { if (ref.path() === '/b') { return { definitions: { @@ -1726,7 +1726,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { if (ref.path().split('.')[1] === 'md') { return data.markdown; } @@ -1792,7 +1792,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { return data[ref.path().slice(1)]; }, }; @@ -1896,7 +1896,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { if (ref.path().split('.')[1] === 'md') { return data; } @@ -1967,7 +1967,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { return data[ref.path().slice(1)]; }, }; @@ -2123,7 +2123,7 @@ describe('resolver', () => { const uris: string[] = []; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { const uri = ref.toString(); uris.push(uri); return remotes[uri]; @@ -2338,7 +2338,7 @@ describe('resolver', () => { }; const reader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { return data[ref.authority()]; }, }; @@ -2370,7 +2370,7 @@ describe('resolver', () => { }; const httpReader: Types.IResolver = { - async resolve(ref: uri.URI): Promise { + async resolve(ref: URI): Promise { return data[ref.toString()]; }, }; diff --git a/src/resolver.ts b/src/resolver.ts index fb8eb14..915203b 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1,4 +1,5 @@ import { DepGraph } from 'dependency-graph'; +import * as URI from 'urijs'; import { Cache } from './cache'; import { ResolveRunner } from './runner'; @@ -20,7 +21,7 @@ export class Resolver { }; protected getRef?: (key: string, val: any) => string | void; - protected transformRef?: (opts: Types.IRefTransformer, ctx: any) => uri.URI | any; + protected transformRef?: (opts: Types.IRefTransformer, ctx: any) => URI | any; protected parseResolveResult?: (opts: Types.IUriParser) => Promise; protected transformDereferenceResult?: (opts: Types.IDereferenceTransformer) => Promise; diff --git a/src/runner.ts b/src/runner.ts index ebcb7f5..2b0d636 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -22,7 +22,7 @@ export const defaultGetRef = (key: string, val: any) => { /** @hidden */ export class ResolveRunner implements Types.IResolveRunner { public readonly id: number; - public readonly baseUri: uri.URI; + public readonly baseUri: URI; public readonly uriCache: Types.ICache; public readonly graph: Types.IResolveRunner['graph']; public readonly root: string; @@ -38,7 +38,7 @@ export class ResolveRunner implements Types.IResolveRunner { }; public readonly getRef: (key: string, val: any) => string | void; - public readonly transformRef?: (opts: Types.IRefTransformer, ctx: any) => uri.URI | any; + public readonly transformRef?: (opts: Types.IRefTransformer, ctx: any) => URI | any; public readonly parseResolveResult?: (opts: Types.IUriParser) => Promise; public readonly transformDereferenceResult?: ( opts: Types.IDereferenceTransformer, @@ -315,7 +315,7 @@ export class ResolveRunner implements Types.IResolveRunner { * * If so, return the appropriate URI object. */ - public computeRef = (opts: Types.IComputeRefOpts): uri.URI | void => { + public computeRef = (opts: Types.IComputeRefOpts): URI | void => { const refStr = this.getRef(opts.key, opts.val); if (!refStr) return; @@ -367,7 +367,7 @@ export class ResolveRunner implements Types.IResolveRunner { public lookupUri = async (opts: { fragment: string; - ref: uri.URI; + ref: URI; cacheKey: string; parentPath: string[]; }): Promise => { @@ -522,7 +522,7 @@ export class ResolveRunner implements Types.IResolveRunner { return sOpts && typeof sOpts === 'object' && sOpts.cacheKey ? sOpts.cacheKey : JSON.stringify(arguments); } - private computeUriCacheKey(ref: uri.URI) { + private computeUriCacheKey(ref: URI) { // don't include the fragment on uri cache key return ref .clone() @@ -530,7 +530,7 @@ export class ResolveRunner implements Types.IResolveRunner { .toString(); } - private isFile(ref: uri.URI): boolean { + private isFile(ref: URI): boolean { const scheme = ref.scheme(); if (scheme === 'file') return true; diff --git a/src/types.ts b/src/types.ts index 3d1ba46..1e2d567 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,6 @@ import { Dictionary, Segment } from '@stoplight/types'; import { DepGraph } from 'dependency-graph'; +import * as URI from 'urijs'; /** * The following interfaces are the primary interaction points for json-ref-resolver. @@ -42,7 +43,7 @@ export interface IResolverOpts { * * It should return a URI object to change the value being resolved, or void to make no changes. */ - transformRef?: (opts: IRefTransformer, ctx: any) => uri.URI | void; + transformRef?: (opts: IRefTransformer, ctx: any) => URI | void; /** * Hook to customize how the result of an uri look is parsed. @@ -127,15 +128,15 @@ export interface IResolveResult { */ export interface IResolver { - resolve(ref: uri.URI, ctx: any): Promise; + resolve(ref: URI, ctx: any): Promise; } export interface IUriParser { result: any; fragment: string; uriResult: IUriResult; - targetAuthority: uri.URI; - parentAuthority: uri.URI; + targetAuthority: URI; + parentAuthority: URI; parentPath: string[]; } @@ -148,8 +149,8 @@ export interface IDereferenceTransformer { result: any; source: any; fragment: string; - targetAuthority: uri.URI; - parentAuthority: uri.URI; + targetAuthority: URI; + parentAuthority: URI; parentPath: string[]; } @@ -161,7 +162,7 @@ export interface ITransformerResult { export interface IUriResult { pointerStack: string[]; targetPath: string[]; - uri: uri.URI; + uri: URI; resolved?: IResolveResult; error?: IResolveError; } @@ -175,8 +176,8 @@ export interface IComputeRefOpts { } export interface IRefTransformer extends IComputeRefOpts { - ref?: uri.URI; - uri: uri.URI; + ref?: URI; + uri: URI; } export type ResolverErrorCode = @@ -191,7 +192,7 @@ export interface IResolveError { code: ResolverErrorCode; message: string; path: Segment[]; - uri: uri.URI; + uri: URI; uriStack: string[]; pointerStack: string[]; } @@ -215,7 +216,7 @@ export interface ICacheOpts { /** @hidden */ export interface IRefHandlerOpts { - ref: uri.URI; + ref: URI; val: any; pointerStack: string[]; cacheKey: string; @@ -248,20 +249,20 @@ export interface IResolveRunner { uriCache: ICache; depth: number; uriStack: string[]; - baseUri: uri.URI; + baseUri: URI; graph: DepGraph; root: string; atMaxUriDepth: () => boolean; resolve: (opts?: IResolveOpts) => Promise; - computeRef: (opts: IComputeRefOpts) => uri.URI | void | undefined; + computeRef: (opts: IComputeRefOpts) => URI | void | undefined; lookupAndResolveUri: (opts: IRefHandlerOpts) => Promise; } /** @hidden */ export interface IResolveRunnerOpts extends IResolveOpts { - root?: uri.URI; + root?: URI; depth?: number; uriStack?: string[]; diff --git a/src/utils.ts b/src/utils.ts index 2b3a4f4..b327083 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import * as URI from 'urijs'; + const replace = (str: string, find: string, repl: string): string => { // modified from http://jsperf.com/javascript-replace-all/10 const orig = str.toString(); @@ -31,11 +33,11 @@ export const addToJSONPointer = (pointer: string, part: string): string => { }; /** @hidden */ -export const uriToJSONPointer = (uri: uri.URI): string => { +export const uriToJSONPointer = (uri: URI): string => { return uri.fragment() !== '' ? `#${uri.fragment()}` : uri.href() === '' ? '#' : ''; }; /** @hidden */ -export const uriIsJSONPointer = (ref: uri.URI): boolean => { +export const uriIsJSONPointer = (ref: URI): boolean => { return ref.path() === ''; }; diff --git a/yarn.lock b/yarn.lock index 7e91d3e..d5f6f15 100644 --- a/yarn.lock +++ b/yarn.lock @@ -788,7 +788,7 @@ lodash "^4.17.15" safe-stable-stringify "^1.1" -"@stoplight/path@^1.3.0": +"@stoplight/path@^1.3.1": version "1.3.1" resolved "https://registry.yarnpkg.com/@stoplight/path/-/path-1.3.1.tgz#1246c983279af20dcfb806d138add9f81cb1efd2" integrity sha512-I6YEfxspGglxt7MbgNbKThHdqh8CJWnDC1x1JUk2rka2D7mCpMSEu5I8IiAp997Dp4YIXDY6Did6gge8OY8KnA== @@ -827,12 +827,13 @@ dependencies: "@types/json-schema" "^7.0.3" -"@stoplight/types@^11.4.0": - version "11.4.0" - resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-11.4.0.tgz#a2bedbbec37700d3c5a8c7d36c763a61d7a8bce7" - integrity sha512-kMh1Sv7bA8BdbUaRXsRyi2K8Y5PzPOUWNSjB4qKOi0P6+dLczjlKggEIw9Xzuu1tCgBFdEvNwjnYDey0iqgeZQ== +"@stoplight/types@^11.6.0": + version "11.6.0" + resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-11.6.0.tgz#c4507f564ea4be719f66ae2fd2bb83f4719c2210" + integrity sha512-J2wOl6FlN4IeY99MZTbgLVbIqrE9eVcHIvWmSEFzxfnbHCh4reXcGkvxlQ7I/pTKScd5/F/HJKSYnNXRjCnM2A== dependencies: - "@types/json-schema" "^7.0.3" + "@types/json-schema" "^7.0.4" + utility-types "^3.10.0" "@types/babel__core@^7.1.0": version "7.1.3" @@ -932,6 +933,11 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636" integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A== +"@types/json-schema@^7.0.4": + version "7.0.4" + resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339" + integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA== + "@types/lodash@^4.14.110": version "4.14.144" resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.144.tgz#12e57fc99064bce45e5ab3c8bc4783feb75eab8e" @@ -980,10 +986,10 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e" integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw== -"@types/urijs@^1.19.5": - version "1.19.5" - resolved "https://registry.yarnpkg.com/@types/urijs/-/urijs-1.19.5.tgz#f2083392f5859be59cbba0b1d463b922c8aef842" - integrity sha512-LAyzQkr9rZDoHrv8xRcHStLo8Z4PbP3ZHMqw8WRr1T7Jn4O1z13Qkv+ed9e12pBXWaaqsBj1l4ADU/FlA/jn3w== +"@types/urijs@^1.19.9": + version "1.19.9" + resolved "https://registry.yarnpkg.com/@types/urijs/-/urijs-1.19.9.tgz#cedccc8e69677080cc8bb2e9ec50c6dd6093429e" + integrity sha512-/tiJyrc0GPcsReHzgC0SXwOmoPjLqYe01W7dLYB0yasQXMbcRee+ZIk+g8MIQhoBS8fPoBQO3Y93+aeBrI93Ug== "@types/yargs-parser@*": version "13.1.0" @@ -8241,7 +8247,12 @@ tslib@1.9.3: resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286" integrity sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ== -tslib@^1.10.0, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3: +tslib@^1.13.0: + version "1.13.0" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043" + integrity sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q== + +tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3: version "1.10.0" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a" integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ== @@ -8470,7 +8481,7 @@ uri-js@^4.2.2: dependencies: punycode "^2.1.0" -urijs@~1.19.2: +urijs@^1.19.2: version "1.19.2" resolved "https://registry.yarnpkg.com/urijs/-/urijs-1.19.2.tgz#f9be09f00c4c5134b7cb3cf475c1dd394526265a" integrity sha512-s/UIq9ap4JPZ7H1EB5ULo/aOUbWqfDi7FKzMC2Nz+0Si8GiT1rIEaprt8hy3Vy2Ex2aJPpOQv4P4DuOZ+K1c6w== @@ -8522,6 +8533,11 @@ util.promisify@^1.0.0: define-properties "^1.1.2" object.getownpropertydescriptors "^2.0.3" +utility-types@^3.10.0: + version "3.10.0" + resolved "https://registry.yarnpkg.com/utility-types/-/utility-types-3.10.0.tgz#ea4148f9a741015f05ed74fd615e1d20e6bed82b" + integrity sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg== + uuid@^3.3.2: version "3.3.3" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"