Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
fix: urijs type (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Jun 2, 2020
1 parent 11d1bd5 commit 226bc59
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 57 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 17 additions & 17 deletions src/__tests__/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('resolver', () => {

const uris: string[] = [];
const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
const uri = ref.toString();
uris.push(uri);
return remotes[uri];
Expand Down Expand Up @@ -686,7 +686,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(uri: uri.URI): Promise<any> {
async resolve(uri: URI): Promise<any> {
return data[uri.authority()];
},
};
Expand Down Expand Up @@ -767,7 +767,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(uri: uri.URI): Promise<any> {
async resolve(uri: URI): Promise<any> {
return data[uri.authority()];
},
};
Expand Down Expand Up @@ -944,7 +944,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
return data[ref.authority()];
},
};
Expand Down Expand Up @@ -1074,7 +1074,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
return data[ref.authority()];
},
};
Expand Down Expand Up @@ -1334,7 +1334,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
if (data[ref.authority()]) {
return data[ref.authority()];
}
Expand Down Expand Up @@ -1410,7 +1410,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
if (data[ref.authority()]) {
return data[ref.authority()];
}
Expand Down Expand Up @@ -1549,7 +1549,7 @@ describe('resolver', () => {
});

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
if (ref.path() === '/b') {
return {
definitions: {
Expand Down Expand Up @@ -1726,7 +1726,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
if (ref.path().split('.')[1] === 'md') {
return data.markdown;
}
Expand Down Expand Up @@ -1792,7 +1792,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
return data[ref.path().slice(1)];
},
};
Expand Down Expand Up @@ -1896,7 +1896,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
if (ref.path().split('.')[1] === 'md') {
return data;
}
Expand Down Expand Up @@ -1967,7 +1967,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
return data[ref.path().slice(1)];
},
};
Expand Down Expand Up @@ -2123,7 +2123,7 @@ describe('resolver', () => {

const uris: string[] = [];
const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
const uri = ref.toString();
uris.push(uri);
return remotes[uri];
Expand Down Expand Up @@ -2338,7 +2338,7 @@ describe('resolver', () => {
};

const reader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
return data[ref.authority()];
},
};
Expand Down Expand Up @@ -2370,7 +2370,7 @@ describe('resolver', () => {
};

const httpReader: Types.IResolver = {
async resolve(ref: uri.URI): Promise<any> {
async resolve(ref: URI): Promise<any> {
return data[ref.toString()];
},
};
Expand Down
3 changes: 2 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DepGraph } from 'dependency-graph';
import * as URI from 'urijs';

import { Cache } from './cache';
import { ResolveRunner } from './runner';
Expand All @@ -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<Types.IUriParserResult>;
protected transformDereferenceResult?: (opts: Types.IDereferenceTransformer) => Promise<Types.ITransformerResult>;

Expand Down
12 changes: 6 additions & 6 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Types.IUriParserResult>;
public readonly transformDereferenceResult?: (
opts: Types.IDereferenceTransformer,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ResolveRunner> => {
Expand Down Expand Up @@ -522,15 +522,15 @@ 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()
.fragment('')
.toString();
}

private isFile(ref: uri.URI): boolean {
private isFile(ref: URI): boolean {
const scheme = ref.scheme();

if (scheme === 'file') return true;
Expand Down
29 changes: 15 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -127,15 +128,15 @@ export interface IResolveResult {
*/

export interface IResolver {
resolve(ref: uri.URI, ctx: any): Promise<any>;
resolve(ref: URI, ctx: any): Promise<any>;
}

export interface IUriParser {
result: any;
fragment: string;
uriResult: IUriResult;
targetAuthority: uri.URI;
parentAuthority: uri.URI;
targetAuthority: URI;
parentAuthority: URI;
parentPath: string[];
}

Expand All @@ -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[];
}

Expand All @@ -161,7 +162,7 @@ export interface ITransformerResult {
export interface IUriResult {
pointerStack: string[];
targetPath: string[];
uri: uri.URI;
uri: URI;
resolved?: IResolveResult;
error?: IResolveError;
}
Expand All @@ -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 =
Expand All @@ -191,7 +192,7 @@ export interface IResolveError {
code: ResolverErrorCode;
message: string;
path: Segment[];
uri: uri.URI;
uri: URI;
uriStack: string[];
pointerStack: string[];
}
Expand All @@ -215,7 +216,7 @@ export interface ICacheOpts {

/** @hidden */
export interface IRefHandlerOpts {
ref: uri.URI;
ref: URI;
val: any;
pointerStack: string[];
cacheKey: string;
Expand Down Expand Up @@ -248,20 +249,20 @@ export interface IResolveRunner {
uriCache: ICache;
depth: number;
uriStack: string[];
baseUri: uri.URI;
baseUri: URI;

graph: DepGraph<IGraphNodeData>;
root: string;

atMaxUriDepth: () => boolean;
resolve: (opts?: IResolveOpts) => Promise<IResolveResult>;
computeRef: (opts: IComputeRefOpts) => uri.URI | void | undefined;
computeRef: (opts: IComputeRefOpts) => URI | void | undefined;
lookupAndResolveUri: (opts: IRefHandlerOpts) => Promise<IUriResult>;
}

/** @hidden */
export interface IResolveRunnerOpts extends IResolveOpts {
root?: uri.URI;
root?: URI;

depth?: number;
uriStack?: string[];
Expand Down
6 changes: 4 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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() === '';
};
Loading

0 comments on commit 226bc59

Please sign in to comment.