From 62354568a9ef7b42e459653479c4b936d077d722 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 26 Apr 2018 21:04:55 -0700 Subject: [PATCH] feat: request/response interceptors for remote config fetch (#4484) --- src/core/plugins/configs/helpers.js | 12 ++++ src/core/plugins/configs/index.js | 40 +---------- src/core/plugins/configs/spec-actions.js | 34 ++++++++++ test/core/plugins/configs/actions.js | 84 ++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 38 deletions(-) create mode 100644 src/core/plugins/configs/helpers.js create mode 100644 src/core/plugins/configs/spec-actions.js create mode 100644 test/core/plugins/configs/actions.js diff --git a/src/core/plugins/configs/helpers.js b/src/core/plugins/configs/helpers.js new file mode 100644 index 00000000..d36e4277 --- /dev/null +++ b/src/core/plugins/configs/helpers.js @@ -0,0 +1,12 @@ +import YAML from "js-yaml" + +export const parseYamlConfig = (yaml, system) => { + try { + return YAML.safeLoad(yaml) + } catch(e) { + if (system) { + system.errActions.newThrownErr( new Error(e) ) + } + return {} + } +} diff --git a/src/core/plugins/configs/index.js b/src/core/plugins/configs/index.js index 0ab33c96..5003e2e0 100644 --- a/src/core/plugins/configs/index.js +++ b/src/core/plugins/configs/index.js @@ -1,46 +1,10 @@ -import YAML from "js-yaml" import yamlConfig from "root/swagger-config.yaml" +import { parseYamlConfig } from "./helpers" import * as actions from "./actions" +import * as specActions from "./spec-actions" import * as selectors from "./selectors" import reducers from "./reducers" -const parseYamlConfig = (yaml, system) => { - try { - return YAML.safeLoad(yaml) - } catch(e) { - if (system) { - system.errActions.newThrownErr( new Error(e) ) - } - return {} - } -} - - -const specActions = { - downloadConfig: (url) => ({fn}) => { - let {fetch} = fn - return fetch(url) - }, - - getConfigByUrl: (configUrl, cb)=> ({ specActions }) => { - if (configUrl) { - return specActions.downloadConfig(configUrl).then(next, next) - } - - function next(res) { - if (res instanceof Error || res.status >= 400) { - specActions.updateLoadingStatus("failedConfig") - specActions.updateLoadingStatus("failedConfig") - specActions.updateUrl("") - console.error(res.statusText + " " + configUrl) - cb(null) - } else { - cb(parseYamlConfig(res.text)) - } - } - } -} - const specSelectors = { getLocalConfig: () => { return parseYamlConfig(yamlConfig) diff --git a/src/core/plugins/configs/spec-actions.js b/src/core/plugins/configs/spec-actions.js new file mode 100644 index 00000000..c0c857ce --- /dev/null +++ b/src/core/plugins/configs/spec-actions.js @@ -0,0 +1,34 @@ +import { parseYamlConfig } from "./helpers" + +export const downloadConfig = (url) => ({fn: { fetch }, getConfigs}) => { + const { requestInterceptor, responseInterceptor } = getConfigs() + let req = { url } + if(requestInterceptor) { + req = requestInterceptor(req) + } + return fetch(req) + .then(res => { + if(res) { + return responseInterceptor(res) + } + return res + }) +} + +export const getConfigByUrl = (configUrl, cb)=> ({ specActions }) => { + if (configUrl) { + return specActions.downloadConfig(configUrl).then(next, next) + } + + function next(res) { + if (res instanceof Error || res.status >= 400) { + specActions.updateLoadingStatus("failedConfig") + specActions.updateLoadingStatus("failedConfig") + specActions.updateUrl("") + console.error(res.statusText + " " + configUrl) + cb(null) + } else { + cb(parseYamlConfig(res.text)) + } + } +} diff --git a/test/core/plugins/configs/actions.js b/test/core/plugins/configs/actions.js new file mode 100644 index 00000000..d84cd6d0 --- /dev/null +++ b/test/core/plugins/configs/actions.js @@ -0,0 +1,84 @@ +/* eslint-env mocha */ +import expect, { createSpy } from "expect" +import { downloadConfig } from "corePlugins/configs/spec-actions" + +describe("configs plugin - actions", () => { + + describe("downloadConfig", () => { + it("should call the system fetch helper with a provided url", () => { + const fetchSpy = createSpy(async () => {}).andCallThrough() + const system = { + fn: { + fetch: fetchSpy + }, + getConfigs() { + return {} + } + } + + const url = "http://swagger.io/one" + + downloadConfig(url)(system) + + expect(fetchSpy).toHaveBeenCalledWith({ + url: url + }) + }) + it("should allow the globally configured requestInterceptor to modify the request", () => { + const fetchSpy = createSpy(async () => {}).andCallThrough() + const requestInterceptorSpy = createSpy((req) => { + req.url = "http://swagger.io/two" + return req + }).andCallThrough() + const system = { + fn: { + fetch: fetchSpy + }, + getConfigs() { + return { + requestInterceptor: requestInterceptorSpy + } + } + } + + const url = "http://swagger.io/one" + + downloadConfig(url)(system) + + expect(fetchSpy).toHaveBeenCalledWith({ + url: "http://swagger.io/two" + }) + }) + it("should allow the globally configured responseInterceptor to modify the response", async () => { + const fetchSpy = createSpy(async (req) => { + return { + url: req.url, + ok: true + } + }).andCallThrough() + const responseInterceptorSpy = createSpy((res) => { + res.url = "http://swagger.io/two" + return res + }).andCallThrough() + const system = { + fn: { + fetch: fetchSpy + }, + getConfigs() { + return { + responseInterceptor: responseInterceptorSpy + } + } + } + + const url = "http://swagger.io/one" + + const res = await downloadConfig(url)(system) + + expect(res).toEqual({ + url: "http://swagger.io/two", + ok: true + }) + }) + }) +})