-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
376ef2e
commit 3062f85
Showing
10 changed files
with
280 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { newAsyncAPIDocument, AsyncAPIDocumentV2, AsyncAPIDocumentV3 } from './models'; | ||
import { unstringify } from './stringify'; | ||
import { createDetailedAsyncAPI } from './utils'; | ||
|
||
import { | ||
xParserSpecParsed, | ||
xParserSpecStringified, | ||
} from './constants'; | ||
|
||
import type { AsyncAPIDocumentInterface } from './models'; | ||
|
||
export function toAsyncAPIDocument(maybeDoc: unknown): AsyncAPIDocumentInterface | undefined { | ||
if (isAsyncAPIDocument(maybeDoc)) { | ||
return maybeDoc; | ||
} | ||
if (!isParsedDocument(maybeDoc)) { | ||
return; | ||
} | ||
return unstringify(maybeDoc) || newAsyncAPIDocument(createDetailedAsyncAPI(maybeDoc, maybeDoc as any)); | ||
} | ||
|
||
export function isAsyncAPIDocument(maybeDoc: unknown): maybeDoc is AsyncAPIDocumentInterface { | ||
return maybeDoc instanceof AsyncAPIDocumentV2 || maybeDoc instanceof AsyncAPIDocumentV3; | ||
} | ||
|
||
export function isParsedDocument(maybeDoc: unknown): maybeDoc is Record<string, unknown> { | ||
if (typeof maybeDoc !== 'object' || maybeDoc === null) { | ||
return false; | ||
} | ||
return Boolean((maybeDoc as Record<string, unknown>)[xParserSpecParsed]); | ||
} | ||
|
||
export function isStringifiedDocument(maybeDoc: unknown): maybeDoc is Record<string, unknown> { | ||
if (typeof maybeDoc !== 'object' || maybeDoc === null) { | ||
return false; | ||
} | ||
return ( | ||
Boolean((maybeDoc as Record<string, unknown>)[xParserSpecParsed]) && | ||
Boolean((maybeDoc as Record<string, unknown>)[xParserSpecStringified]) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,81 @@ | ||
import { Resolver as SpectralResolver } from '@stoplight/json-ref-resolver'; | ||
import { Resolver as SpectralResolver } from '@stoplight/spectral-ref-resolver'; | ||
import { resolveFile, resolveHttp } from '@stoplight/json-ref-readers'; | ||
|
||
import type Uri from 'urijs'; | ||
|
||
export interface Resolver { | ||
schema: 'file' | 'http' | 'https' | string; | ||
order?: number; | ||
canRead?: boolean | ((input: ResolverInput) => boolean); | ||
read: (input: ResolverInput) => string | Buffer | Promise<string | Buffer>; | ||
canRead?: boolean | ((uri: Uri, ctx?: any) => boolean); | ||
read: (uri: Uri, ctx?: any) => string | undefined | Promise<string | undefined>; | ||
} | ||
|
||
export interface ResolverInput { | ||
url: string; | ||
extension: string; | ||
export interface ResolverOptions { | ||
resolvers?: Array<Resolver>; | ||
} | ||
|
||
interface ResolverOptions { | ||
resolvers: Array<Resolver>; | ||
} | ||
export function createResolver(options: ResolverOptions = {}): SpectralResolver { | ||
const availableResolvers: Array<Resolver> = [ | ||
...createDefaultResolvers(), | ||
...(options?.resolvers || []) | ||
].map(r => ({ | ||
...r, | ||
order: r.order || Number.MAX_SAFE_INTEGER, | ||
canRead: typeof r.canRead === 'undefined' ? true: r.canRead, | ||
})); | ||
const availableSchemas = [...new Set(availableResolvers.map(r => r.schema))]; | ||
const resolvers = availableSchemas.reduce((acc, schema) => { | ||
acc[schema] = { resolve: createSchemaResolver(schema, availableResolvers) }; | ||
return acc; | ||
}, {} as Record<string, { resolve: (uri: Uri, ctx?: any) => string | Promise<string> }>); | ||
|
||
export function createResolver(options?: ResolverOptions): SpectralResolver { | ||
return new SpectralResolver({ | ||
resolvers: { | ||
https: { resolve: resolveHttp }, | ||
http: { resolve: resolveHttp }, | ||
file: { resolve: resolveFile }, | ||
}, | ||
resolvers: resolvers as any, | ||
}); | ||
} | ||
|
||
export function createFileResolver() { | ||
function createDefaultResolvers(): Array<Resolver> { | ||
return [ | ||
{ | ||
schema: 'file', | ||
read: resolveFile as (input: Uri, ctx?: any) => string | Promise<string>, | ||
}, | ||
{ | ||
schema: 'https', | ||
read: resolveHttp as (input: Uri, ctx?: any) => string | Promise<string>, | ||
}, | ||
{ | ||
schema: 'http', | ||
read: resolveHttp as (input: Uri, ctx?: any) => string | Promise<string>, | ||
}, | ||
] | ||
} | ||
|
||
function createSchemaResolver(schema: string, allResolvers: Array<Resolver>): (uri: Uri, ctx?: any) => string | Promise<string> { | ||
const resolvers = allResolvers.filter(r => r.schema === schema).sort((a, b) => { return (a.order as number) - (b.order as number); }); | ||
return async (uri, ctx) => { | ||
let result: string | undefined = undefined; | ||
let lastError: Error | undefined; | ||
for (const resolver of resolvers) { | ||
try { | ||
if (!canRead(resolver, uri, ctx)) continue; | ||
|
||
result = await resolver.read(uri, ctx); | ||
if (typeof result === 'string') { | ||
break; | ||
} | ||
} catch(e: any) { | ||
lastError = e; | ||
continue; | ||
} | ||
} | ||
if (typeof result !== 'string') { | ||
throw lastError || new Error(`None of the available resolvers for "${schema}" can resolve the given reference.`); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
export function createHttpResolver() { | ||
|
||
function canRead(resolver: Resolver, uri: Uri, ctx?: any) { | ||
return typeof resolver.canRead === 'function' ? resolver.canRead(uri, ctx) : resolver.canRead; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
payload: | ||
type: object | ||
properties: | ||
someProperty: | ||
type: string |
Oops, something went wrong.