-
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.
refactor(resolver): refactor code to allow using strategy pattern
Refs #2744
- Loading branch information
Showing
8 changed files
with
126 additions
and
102 deletions.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// eslint-disable-next-line camelcase | ||
import resolveOpenAPI2_30Strategy from './strategies/openapi-2--3-0.js'; | ||
import { makeFetchJSON } from './utils/index.js'; | ||
import * as optionsUtil from './utils/options.js'; | ||
|
||
const resolve = async (options) => { | ||
const { spec, requestInterceptor, responseInterceptor } = options; | ||
|
||
const retrievalURI = optionsUtil.retrievalURI(options); | ||
const httpClient = optionsUtil.httpClient(options); | ||
const retrievedSpec = | ||
spec || | ||
(await makeFetchJSON(httpClient, { requestInterceptor, responseInterceptor })(retrievalURI)); | ||
|
||
return resolveOpenAPI2_30Strategy({ ...options, spec: retrievedSpec }); | ||
}; | ||
|
||
export default resolve; |
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,67 @@ | ||
import mapSpec, { plugins } from '../../specmap/index.js'; | ||
// eslint-disable-next-line camelcase | ||
import normalizeOpenAPI2__30 from '../../helpers/normalize/openapi-2--3-0.js'; | ||
import { makeFetchJSON } from '../utils/index.js'; | ||
import * as optionsUtil from '../utils/options.js'; | ||
|
||
// Wipe out the http cache | ||
export function clearCache() { | ||
plugins.refs.clearCache(); | ||
} | ||
|
||
// eslint-disable-next-line camelcase | ||
export default function resolveOpenAPI2_30Strategy(obj) { | ||
const { | ||
spec, | ||
mode, | ||
allowMetaPatches = true, | ||
pathDiscriminator, | ||
modelPropertyMacro, | ||
parameterMacro, | ||
requestInterceptor, | ||
responseInterceptor, | ||
skipNormalization, | ||
useCircularStructures, | ||
} = obj; | ||
|
||
const retrievalURI = optionsUtil.retrievalURI(obj); | ||
const httpClient = optionsUtil.httpClient(obj); | ||
|
||
return doResolve(spec); | ||
|
||
function doResolve(_spec) { | ||
if (retrievalURI) { | ||
plugins.refs.docCache[retrievalURI] = _spec; | ||
} | ||
|
||
// Build a json-fetcher ( ie: give it a URL and get json out ) | ||
plugins.refs.fetchJSON = makeFetchJSON(httpClient, { requestInterceptor, responseInterceptor }); | ||
|
||
const plugs = [plugins.refs]; | ||
|
||
if (typeof parameterMacro === 'function') { | ||
plugs.push(plugins.parameters); | ||
} | ||
|
||
if (typeof modelPropertyMacro === 'function') { | ||
plugs.push(plugins.properties); | ||
} | ||
|
||
if (mode !== 'strict') { | ||
plugs.push(plugins.allOf); | ||
} | ||
|
||
// mapSpec is where the hard work happens | ||
return mapSpec({ | ||
spec: _spec, | ||
context: { baseDoc: retrievalURI }, | ||
plugins: plugs, | ||
allowMetaPatches, // allows adding .meta patches, which include adding `$$ref`s to the spec | ||
pathDiscriminator, // for lazy resolution | ||
parameterMacro, | ||
modelPropertyMacro, | ||
useCircularStructures, | ||
// eslint-disable-next-line camelcase | ||
}).then(skipNormalization ? async (a) => a : normalizeOpenAPI2__30); | ||
} | ||
} |
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,19 @@ | ||
import { ACCEPT_HEADER_VALUE_FOR_DOCUMENTS } from '../../constants.js'; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function makeFetchJSON(http, opts = {}) { | ||
const { requestInterceptor, responseInterceptor } = opts; | ||
// Set credentials with 'http.withCredentials' value | ||
const credentials = http.withCredentials ? 'include' : 'same-origin'; | ||
return (docPath) => | ||
http({ | ||
url: docPath, | ||
loadSpec: true, | ||
requestInterceptor, | ||
responseInterceptor, | ||
headers: { | ||
Accept: ACCEPT_HEADER_VALUE_FOR_DOCUMENTS, | ||
}, | ||
credentials, | ||
}).then((res) => res.body); | ||
} |
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,17 @@ | ||
import Http from '../../http/index.js'; | ||
|
||
export const retrievalURI = (options) => { | ||
const { baseDoc, url } = options; | ||
|
||
// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both | ||
// need to fix and pick one. | ||
return baseDoc || url; | ||
}; | ||
|
||
export const httpClient = (options) => { | ||
const { fetch, http } = options; | ||
|
||
// @TODO fetch should be removed, and http used instead | ||
// provide a default fetch implementation | ||
return fetch || http || Http; | ||
}; |
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