Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Sunny_yiyi
Swagger Ui
提交
62354568
S
Swagger Ui
项目概览
Sunny_yiyi
/
Swagger Ui
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Swagger Ui
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
62354568
编写于
4月 26, 2018
作者:
K
kyle
提交者:
GitHub
4月 26, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: request/response interceptors for remote config fetch (#4484)
上级
d981f0f2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
132 addition
and
38 deletion
+132
-38
src/core/plugins/configs/helpers.js
src/core/plugins/configs/helpers.js
+12
-0
src/core/plugins/configs/index.js
src/core/plugins/configs/index.js
+2
-38
src/core/plugins/configs/spec-actions.js
src/core/plugins/configs/spec-actions.js
+34
-0
test/core/plugins/configs/actions.js
test/core/plugins/configs/actions.js
+84
-0
未找到文件。
src/core/plugins/configs/helpers.js
0 → 100644
浏览文件 @
62354568
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
{}
}
}
src/core/plugins/configs/index.js
浏览文件 @
62354568
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
)
...
...
src/core/plugins/configs/spec-actions.js
0 → 100644
浏览文件 @
62354568
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
))
}
}
}
test/core/plugins/configs/actions.js
0 → 100644
浏览文件 @
62354568
/* 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
})
})
})
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录