
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
microvideo-request
Advanced tools
研发人: 朱琦
研发日期: 2022/1/10
microvideo-request 插件是基于 axios,qs 开发的请求封装,可以支持多服务域名请求
npm i microvideo-request
import { setRequestConfig, setInterceptor } from 'microvideo-request'
/**
* 初始化 配置
* @param {Object} config 初始配置
* @param {Object} services 多个服务域名配置
* */
setRequestConfig(
// config 初始配置 可设置baseUrl timeout headers
{
baseURL: process.env.VUE_APP_BASE_API,
timeout: 60000, // 非必传 接口超时时间设置, 默认 60000ms
headers: {}, // 非必传 请求头
successStatus: [301, 302], // 非必传 请求成功状态码, 默认 2xx 都为成功状态码,可额外配置
},
// services 多个服务域名配置, 可不传 不传时config.baseURL 为必传
[
{
// 默认服务域名 第一个
name: 'BASE_SERVER',
url: process.env.VUE_APP_BASE_API
},
{
// 其他服务域名
name: 'JTQG_SERVER',
url: process.env.VUE_APP_JTQG_SERVER_BASE_API
},
]
)
若后续需要设置 headers 可再次调用 setRequestConfig(config)
import { createRequest } from 'microvideo-request'
/**
* 实例1
* */
getList (params) {
return createRequest().setParameters(params).get('/event/list')
}
/**
* 实例2
* 用setAxios请求
* 传参方法遵循 axios
* */
getList (params) {
return createRequest().setAxios({
url: '/event/listForJtqg',
method: 'get',
params
})
}
createRequest()
创建请求类,传参为空时 使用默认服务域名
createRequest()
// 等同于
createRequest('BASE_SERVER')
设置当前 请求超时 时间
createRequest('BASE_SERVER',timeout)
setParameters(object)
传参
createRequest().setParameters(object)
add()
添加参数
createRequest().add('key','value')
get()
GET 请求
createRequest().add('key','value').get(url)
<!-- 修改单个请求的headers 其余请求类型同理 -->
createRequest().add('key','value').get(url, {
headers:{
userId: xxx
}
})
post()
POST 请求
默认 application/json
createRequest().setParameters(object).post(url)
post() POST 请求 setParameters(object,'formdata')
请求类型为 multipart/form-data 传参一定要是个对象,不能是 formData 类型
createRequest().setParameters(object,'formdata').post(url)
post() POST 请求 setParameters(object,'urlencoded')
请求类型为 application/x-www-form-urlencoded
createRequest().setParameters(object,'urlencoded').post(url)
delete()
DELETE 请求
createRequest().setParameters(object).delete(url)
delete() DELETE 请求 setParameters(object,'urlencoded')
请求类型为 application/x-www-form-urlencoded
createRequest().setParameters(object,'urlencoded').delete(url)
put() PUT 请求
createRequest().setParameters(object).put(url)
url 拼接参数
createRequest().setParameters({id:123, type:0}).get(url/{id}/{type})
file()
下载文件流
createRequest().setParameters(object).file(url, config)
config [Object] 配置
config | 类型 | 说明 |
---|---|---|
fileType | String | 下载文件类型 |
fileName | String | 下载文件名 |
关于 fileType 文件类型,可取值:
拓展名 | 文件类型 | fileType |
---|---|---|
.jpg 、.jpeg | JPEG 图片 | image/jpeg |
.png | PNG 图片 | image/png |
.json | JSON 格式 | application/json |
application/pdf | ||
.ppt | Microsoft PowerPoint | application/vnd.ms-powerpoint |
.xls | Microsoft Excel | application/vnd.ms-excel |
.zip | ZIP | application/zip |
.rar | RAR 存档 | application/x-rar-compressed |
fileUrl()
获取 url 链接createRequest().setParameters(object).fileUrl(url, openUrl)
openUrl [Boolean] 是否打开 url 默认为 true
为 true 时,会直接通过 window.open(href) 打开连接 为 false 时,可以通过 then 回调获取 url
config | 类型 | 说明 |
---|---|---|
baseURL | String | 默认服务域名 |
timeout | Number | 接口超时时间设置 非必传 |
headers | Object | 请求头 非必传 |
[{
name: "BASE_SERVER",
url: process.env.VUE_APP_BASE_API
},
{
name: "JTQG_SERVER",
url: process.env.VUE_APP_JTQG_SERVER_BASE_API
},
...
]
servicesName [String] 服务名
timeout [Number] 超时时间
fun(res)
type | 类型 | 说明 |
---|---|---|
SUCCESS | String | 请求成功时 |
ERROR | String | 请求失败时 |
BEFORE | String | 请求之前时 |
FAQs
> 研发人: 朱琦
The npm package microvideo-request receives a total of 0 weekly downloads. As such, microvideo-request popularity was classified as not popular.
We found that microvideo-request demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.