From efd02106a2d86bd8de3e842bec8b9711865e55e4 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 30 Nov 2017 15:57:37 -0800 Subject: [PATCH] Support namespaces in OAS3 server state --- src/core/plugins/oas3/actions.js | 8 +-- src/core/plugins/oas3/reducers.js | 10 +-- src/core/plugins/oas3/selectors.js | 68 ++++++++++++++++++--- test/core/plugins/oas3/state-integration.js | 6 +- 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/src/core/plugins/oas3/actions.js b/src/core/plugins/oas3/actions.js index c9abc855..ac6f244a 100644 --- a/src/core/plugins/oas3/actions.js +++ b/src/core/plugins/oas3/actions.js @@ -7,10 +7,10 @@ export const UPDATE_REQUEST_CONTENT_TYPE = "oas3_set_request_content_type" export const UPDATE_RESPONSE_CONTENT_TYPE = "oas3_set_response_content_type" export const UPDATE_SERVER_VARIABLE_VALUE = "oas3_set_server_variable_value" -export function setSelectedServer (selectedServerUrl) { +export function setSelectedServer (selectedServerUrl, namespace) { return { type: UPDATE_SELECTED_SERVER, - payload: selectedServerUrl + payload: {selectedServerUrl, namespace} } } @@ -35,9 +35,9 @@ export function setResponseContentType ({ value, path, method }) { } } -export function setServerVariableValue ({ server, key, val }) { +export function setServerVariableValue ({ server, namespace, key, val }) { return { type: UPDATE_SERVER_VARIABLE_VALUE, - payload: { server, key, val } + payload: { server, namespace, key, val } } } diff --git a/src/core/plugins/oas3/reducers.js b/src/core/plugins/oas3/reducers.js index 8590284d..871aff89 100644 --- a/src/core/plugins/oas3/reducers.js +++ b/src/core/plugins/oas3/reducers.js @@ -7,8 +7,9 @@ import { } from "./actions" export default { - [UPDATE_SELECTED_SERVER]: (state, { payload: selectedServerUrl } ) =>{ - return state.setIn( [ "selectedServer" ], selectedServerUrl) + [UPDATE_SELECTED_SERVER]: (state, { payload: { selectedServerUrl, namespace } } ) =>{ + const path = namespace ? [ namespace, "selectedServer"] : [ "selectedServer"] + return state.setIn( path, selectedServerUrl) }, [UPDATE_REQUEST_BODY_VALUE]: (state, { payload: { value, pathMethod } } ) =>{ let [path, method] = pathMethod @@ -21,7 +22,8 @@ export default { [UPDATE_RESPONSE_CONTENT_TYPE]: (state, { payload: { value, path, method } } ) =>{ return state.setIn( [ "requestData", path, method, "responseContentType" ], value) }, - [UPDATE_SERVER_VARIABLE_VALUE]: (state, { payload: { server, key, val } } ) =>{ - return state.setIn( [ "serverVariableValues", server, key ], val) + [UPDATE_SERVER_VARIABLE_VALUE]: (state, { payload: { server, namespace, key, val } } ) =>{ + const path = namespace ? [ namespace, "serverVariableValues", server, key ] : [ "serverVariableValues", server, key ] + return state.setIn(path, val) }, } diff --git a/src/core/plugins/oas3/selectors.js b/src/core/plugins/oas3/selectors.js index 86fc617f..c65a6882 100644 --- a/src/core/plugins/oas3/selectors.js +++ b/src/core/plugins/oas3/selectors.js @@ -15,8 +15,9 @@ function onlyOAS3(selector) { } } -export const selectedServer = onlyOAS3(state => { - return state.getIn(["selectedServer"]) || "" +export const selectedServer = onlyOAS3((state, namespace) => { + const path = namespace ? [namespace, "selectedServer"] : ["selectedServer"] + return state.getIn(path) || "" } ) @@ -35,19 +36,68 @@ export const responseContentType = onlyOAS3((state, path, method) => { } ) -export const serverVariableValue = onlyOAS3((state, server, key) => { - return state.getIn(["serverVariableValues", server, key]) || null +export const serverVariableValue = onlyOAS3((state, locationData, key) => { + let path + + // locationData may take one of two forms, for backwards compatibility + // Object: ({server, namespace?}) or String:(server) + if(typeof locationData !== "string") { + const { server, namespace } = locationData + if(namespace) { + path = [namespace, "serverVariableValues", server, key] + } else { + path = ["serverVariableValues", server, key] + } + } else { + const server = locationData + path = ["serverVariableValues", server, key] + } + + return state.getIn(path) || null } ) -export const serverVariables = onlyOAS3((state, server) => { - return state.getIn(["serverVariableValues", server]) || OrderedMap() +export const serverVariables = onlyOAS3((state, locationData) => { + let path + + // locationData may take one of two forms, for backwards compatibility + // Object: ({server, namespace?}) or String:(server) + if(typeof locationData !== "string") { + const { server, namespace } = locationData + if(namespace) { + path = [namespace, "serverVariableValues", server] + } else { + path = ["serverVariableValues", server] + } + } else { + const server = locationData + path = ["serverVariableValues", server] + } + + return state.getIn(path) || OrderedMap() } ) -export const serverEffectiveValue = onlyOAS3((state, server) => { - let varValues = state.getIn(["serverVariableValues", server]) || OrderedMap() - let str = server +export const serverEffectiveValue = onlyOAS3((state, locationData) => { + var varValues, serverValue + + // locationData may take one of two forms, for backwards compatibility + // Object: ({server, namespace?}) or String:(server) + if(typeof locationData !== "string") { + const { server, namespace } = locationData + serverValue = server + if(namespace) { + varValues = state.getIn([namespace, "serverVariableValues", serverValue]) + } else { + varValues = state.getIn(["serverVariableValues", serverValue]) + } + } else { + serverValue = locationData + varValues = state.getIn(["serverVariableValues", serverValue]) + } + + varValues = varValues || OrderedMap() + let str = serverValue varValues.map((val, key) => { str = str.replace(new RegExp(`{${key}}`, "g"), val) diff --git a/test/core/plugins/oas3/state-integration.js b/test/core/plugins/oas3/state-integration.js index 3f2a7b0a..04f70907 100644 --- a/test/core/plugins/oas3/state-integration.js +++ b/test/core/plugins/oas3/state-integration.js @@ -15,9 +15,9 @@ import { setServerVariableValue, } from "corePlugins/oas3/actions" -describe("OAS3 plugin - state", function() { +describe.only("OAS3 plugin - state", function() { describe("action + reducer + selector integration", function() { - describe.only("selectedServer", function() { + describe("selectedServer", function() { it("should set and get a global selectedServer", function() { const state = new OrderedMap() const system = { @@ -241,7 +241,7 @@ describe("OAS3 plugin - state", function() { }) expect(globalRes.toJS()).toEqual({ - foo: "bar" + foo: "123" }) }) })