-
Notifications
You must be signed in to change notification settings - Fork 761
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
Showing
3 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
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,181 @@ | ||
import { fetch } from 'cross-fetch'; | ||
|
||
import createError from './create-error'; | ||
import lib from '.'; | ||
|
||
const externalValuesCache = {}; | ||
|
||
/** | ||
* Fetches a document. | ||
* @param {String} docPath the absolute URL of the document. | ||
* @return {Promise} a promise of the document content. | ||
* @api public | ||
*/ | ||
const fetchRaw = (url) => fetch(url).then((res) => res.text); | ||
|
||
const shouldResolveTestFn = [ | ||
// OAS 3.0 Response Media Type Examples externalValue | ||
(path) => | ||
// ["paths", *, *, "responses", *, "content", *, "examples", *, "externalValue"] | ||
path[0] === 'paths' && | ||
path[3] === 'responses' && | ||
path[5] === 'content' && | ||
path[7] === 'examples' && | ||
path[9] === 'externalValue', | ||
|
||
// OAS 3.0 Request Body Media Type Examples externalValue | ||
(path) => | ||
// ["paths", *, *, "requestBody", "content", *, "examples", *, "externalValue"] | ||
path[0] === 'paths' && | ||
path[3] === 'requestBody' && | ||
path[4] === 'content' && | ||
path[6] === 'examples' && | ||
path[8] === 'externalValue', | ||
|
||
// OAS 3.0 Parameter Examples externalValue | ||
(path) => | ||
// ["paths", *, "parameters", *, "examples", *, "externalValue"] | ||
path[0] === 'paths' && | ||
path[2] === 'parameters' && | ||
path[4] === 'examples' && | ||
path[6] === 'externalValue', | ||
(path) => | ||
// ["paths", *, *, "parameters", *, "examples", *, "externalValue"] | ||
path[0] === 'paths' && | ||
path[3] === 'parameters' && | ||
path[5] === 'examples' && | ||
path[7] === 'externalValue', | ||
(path) => | ||
// ["paths", *, "parameters", *, "content", *, "examples", *, "externalValue"] | ||
path[0] === 'paths' && | ||
path[2] === 'parameters' && | ||
path[4] === 'content' && | ||
path[6] === 'examples' && | ||
path[8] === 'externalValue', | ||
(path) => | ||
// ["paths", *, *, "parameters", *, "content", *, "examples", *, "externalValue"] | ||
path[0] === 'paths' && | ||
path[3] === 'parameters' && | ||
path[5] === 'content' && | ||
path[7] === 'examples' && | ||
path[9] === 'externalValue', | ||
]; | ||
|
||
const shouldSkipResolution = (path) => !shouldResolveTestFn.some((fn) => fn(path)); | ||
|
||
const ExternalValueError = createError('ExternalValueError', function cb(message, extra, oriError) { | ||
this.originalError = oriError; | ||
Object.assign(this, extra || {}); | ||
}); | ||
|
||
/** | ||
* This plugin resolves externalValue keys. | ||
* In order to do so it will use a cache in case the url was already requested. | ||
* It will use the fetchRaw method in order get the raw content hosted on specified url. | ||
* If successful retrieved it will replace the url with the actual value | ||
*/ | ||
const plugin = { | ||
key: 'externalValue', | ||
plugin: (externalValue, _, fullPath) => { | ||
const parent = fullPath.slice(0, -1); | ||
|
||
if (shouldSkipResolution(fullPath)) { | ||
return undefined; | ||
} | ||
|
||
if (typeof externalValue !== 'string') { | ||
return new ExternalValueError('externalValue: must be a string', { | ||
externalValue, | ||
fullPath, | ||
}); | ||
} | ||
|
||
try { | ||
let externalValueOrPromise = getExternalValue(externalValue, fullPath); | ||
if (typeof externalValueOrPromise === 'undefined') { | ||
externalValueOrPromise = new ExternalValueError( | ||
`Could not resolve externalValue: ${externalValue}`, | ||
{ | ||
externalValue, | ||
fullPath, | ||
} | ||
); | ||
} | ||
// eslint-disable-next-line no-underscore-dangle | ||
if (externalValueOrPromise.__value != null) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
externalValueOrPromise = externalValueOrPromise.__value; | ||
} else { | ||
externalValueOrPromise = externalValueOrPromise.catch((e) => { | ||
throw wrapError(e, { | ||
externalValue, | ||
fullPath, | ||
}); | ||
}); | ||
} | ||
|
||
if (externalValueOrPromise instanceof Error) { | ||
return [lib.remove(fullPath), externalValueOrPromise]; | ||
} | ||
|
||
const patch = lib.replace([...parent, 'value'], externalValueOrPromise, { | ||
$$externalValue: externalValue, | ||
}); | ||
return [patch, lib.remove(fullPath)]; | ||
} catch (err) { | ||
return [ | ||
lib.remove(fullPath), | ||
wrapError(err, { | ||
externalValue, | ||
fullPath, | ||
}), | ||
]; | ||
} | ||
}, | ||
}; | ||
const mod = Object.assign(plugin, { | ||
ExternalValueError, | ||
fetchRaw, | ||
getExternalValue, | ||
}); | ||
export default mod; | ||
|
||
/** | ||
* Wraps an error as ExternalValueError. | ||
* @param {Error} e the error. | ||
* @param {Object} extra (optional) optional data. | ||
* @return {Error} an instance of ExternalValueError. | ||
* @api public | ||
*/ | ||
function wrapError(e, extra) { | ||
let message; | ||
|
||
if (e && e.response && e.response.body) { | ||
message = `${e.response.body.code} ${e.response.body.message}`; | ||
} else { | ||
message = e.message; | ||
} | ||
|
||
return new ExternalValueError(`Could not resolve externalValue: ${message}`, extra, e); | ||
} | ||
|
||
/** | ||
* Fetches and caches a ExternalValue. | ||
* @param {String} docPath the absolute URL of the document. | ||
* @return {Promise} a promise of the document content. | ||
* @api public | ||
*/ | ||
function getExternalValue(url) { | ||
const val = externalValuesCache[url]; | ||
if (val) { | ||
return lib.isPromise(val) ? val : Promise.resolve(val); | ||
} | ||
|
||
// NOTE: we need to use `mod.fetchRaw` in order to be able to overwrite it. | ||
// Any tips on how to make this cleaner, please ping! | ||
externalValuesCache[url] = mod.fetchRaw(url).then((raw) => { | ||
externalValuesCache[url] = raw; | ||
return raw; | ||
}); | ||
return externalValuesCache[url]; | ||
} |