feat: lazy resolver (#4249)

* default to empty `ImmutableMap` when grabbing op metadata
* pass `errors` into JsonSchema components
* Account for Immutable data structure in JavaScriptonSchema...
    ...and create empty Lists instead of Maps by default.
* Pass ImmutableList through to JsonSchema child components
* Add lazy resolving spec state extensions
* TEMPORARY: disable conventional resolved spec
* WIP
* Use resolveSubtree in Operation display
* Freebie: short-circuit Markdown component if it is given plaintext
* NEW DEFAULT BEHAVIOR: `defaultModelsExpandDepth: 1` does not expand individual models
* Render faked Model expander to trigger resolution
* Baseline support for Editor lifecycles
* Display operation summaries before the operation is resolved
* Test migrations
* WIP
* Swagger2 TIO Body params
* a bit of cleanup
* Debounce string param inputs
* Reach into unresolved operation for deprecated flag, if available
* Fire subtree request outside of render
* Remove debugging flags
* Fix logical errors in spec statePlugins
* TODOs become TODONEs!
* Migrate deeplinking feature to non-resolved spec action
* ESLint fixes
This commit is contained in:
kyle
2018-02-23 01:12:53 -08:00
committed by GitHub
parent 54ed39d69f
commit ecf688171f
23 changed files with 359 additions and 97 deletions

View File

@@ -1,6 +1,7 @@
import YAML from "js-yaml"
import parseUrl from "url-parse"
import serializeError from "serialize-error"
import { Map } from "immutable"
import isString from "lodash/isString"
import { isJSONObject } from "core/utils"
@@ -21,6 +22,7 @@ export const CLEAR_REQUEST = "spec_clear_request"
export const CLEAR_VALIDATE_PARAMS = "spec_clear_validate_param"
export const UPDATE_OPERATION_META_VALUE = "spec_update_operation_meta_value"
export const UPDATE_RESOLVED = "spec_update_resolved"
export const UPDATE_RESOLVED_SUBTREE = "spec_update_resolved_subtree"
export const SET_SCHEME = "set_scheme"
const toStr = (str) => isString(str) ? str : ""
@@ -74,7 +76,14 @@ export const parseToJson = (str) => ({specActions, specSelectors, errActions}) =
return {}
}
let hasWarnedAboutResolveSpecDeprecation = false
export const resolveSpec = (json, url) => ({specActions, specSelectors, errActions, fn: { fetch, resolve, AST }, getConfigs}) => {
if(!hasWarnedAboutResolveSpecDeprecation) {
console.warn(`specActions.resolveSpec is deprecated since v3.10.0 and will be removed in v4.0.0; use resolveIn instead!`)
hasWarnedAboutResolveSpecDeprecation = true
}
const {
modelPropertyMacro,
parameterMacro,
@@ -124,6 +133,55 @@ export const resolveSpec = (json, url) => ({specActions, specSelectors, errActio
})
}
export const requestResolvedSubtree = path => system => {
const {
errActions,
fn: {
resolveSubtree,
AST: { getLineNumberForPath }
},
specSelectors,
specActions,
} = system
const specStr = specSelectors.specStr()
if(!resolveSubtree) {
console.error("Error: Swagger-Client did not provide a `resolveSubtree` method, doing nothing.")
return
}
const currentValue = specSelectors.specResolvedSubtree(path)
if(currentValue) {
return
}
return resolveSubtree(specSelectors.specJson().toJS(), path)
.then(({ spec, errors }) => {
errActions.clear({
type: "thrown"
})
if(Array.isArray(errors) && errors.length > 0) {
let preparedErrors = errors
.map(err => {
console.error(err)
err.line = err.fullPath ? getLineNumberForPath(specStr, err.fullPath) : null
err.path = err.fullPath ? err.fullPath.join(".") : null
err.level = "error"
err.type = "thrown"
err.source = "resolver"
Object.defineProperty(err, "message", { enumerable: true, value: err.message })
return err
})
errActions.newThrownErrBatch(preparedErrors)
}
return specActions.updateResolvedSubtree(path, spec)
})
.catch(e => console.error(e))
}
export function changeParam( path, paramName, paramIn, value, isXml ){
return {
type: UPDATE_PARAM,
@@ -131,6 +189,23 @@ export function changeParam( path, paramName, paramIn, value, isXml ){
}
}
export const updateResolvedSubtree = (path, value) => {
return {
type: UPDATE_RESOLVED_SUBTREE,
payload: { path, value }
}
}
export const invalidateResolvedSubtreeCache = () => {
return {
type: UPDATE_RESOLVED_SUBTREE,
payload: {
path: [],
value: Map()
}
}
}
export const validateParams = ( payload, isOAS3 ) =>{
return {
type: VALIDATE_PARAMS,
@@ -251,6 +326,7 @@ export const executeRequest = (req) =>
// track duration of request
const startTime = Date.now()
return fn.execute(req)
.then( res => {
res.duration = Date.now() - startTime
@@ -267,13 +343,22 @@ export const executeRequest = (req) =>
// I'm using extras as a way to inject properties into the final, `execute` method - It's not great. Anyone have a better idea? @ponelat
export const execute = ( { path, method, ...extras }={} ) => (system) => {
let { fn:{fetch}, specSelectors, specActions } = system
let spec = specSelectors.spec().toJS()
let spec = specSelectors.specJsonWithResolvedSubtrees().toJS()
let scheme = specSelectors.operationScheme(path, method)
let { requestContentType, responseContentType } = specSelectors.contentTypeValues([path, method]).toJS()
let isXml = /xml/i.test(requestContentType)
let parameters = specSelectors.parameterValues([path, method], isXml).toJS()
return specActions.executeRequest({fetch, spec, pathName: path, method, parameters, requestContentType, scheme, responseContentType, ...extras })
return specActions.executeRequest({
...extras,
fetch,
spec,
pathName: path,
method, parameters,
requestContentType,
scheme,
responseContentType
})
}
export function clearResponse (path, method) {