lei-mu / luch-request Goto Github PK
View Code? Open in Web Editor NEWluch-request 是一个基于Promise 开发的uni-app跨平台、项目级别的请求库,它有更小的体积,易用的api,方便简单的自定义能力。
Home Page: https://www.quanzhan.co/luch-request/
License: MIT License
luch-request 是一个基于Promise 开发的uni-app跨平台、项目级别的请求库,它有更小的体积,易用的api,方便简单的自定义能力。
Home Page: https://www.quanzhan.co/luch-request/
License: MIT License
用支付宝的MP-ALIPAY对钉钉支持不好,日后可有计划支持?
全局响应拦截的error回调信息也太少了吧
比如我的接口报了401 我想取消所有正在异步的接口 防止都在401的时候 重复跳转登录页面
https://ext.dcloud.net.cn/plugin?id=392
预期结果:Vue 版本是否会影响
一样的请求地址,一样的代码,但是开发环境接口就能返回数据,但是一到体验版关闭调试或者线上版本,接口就不同了。这是为啥?用的是你文档中的 请求前拦截器的代码。我是在请求前给Header中添加token。
http.interceptors.request.use((config)=>{
//... //业务代码,添加token
return config;
}, config=>{
// 经不断的调试后发现,就是这段代码有问题,把这段去掉,正式版里接口就能访问成功,
// 但是如果是开发环境下,这段代码却不影响接口,是可以调通的。
// 我看你的文档,use后面是两个handle,一个fulfilled,一个rejected,为什么在正式环境下,就不同了呢?
return Promise.reject(config);
})
请问有全局的错误监听吗?想监听请求出错,该怎么搞呢?大佬~
dataType 参数值只能是 json 吗? 还可以有其他哪些类型?
我主要是想解决: 后端接口返回订单号出现订单号精度丢失的情况。
dataType 默认是 json,执行了 JSON.parse()。导致 长数字类型精度丢失。
在 Axios 中会有个 transformResponse,可以解决这个问题,在 luch-request 中有没有这个东西?
参考文档: https://blog.51cto.com/u_15585699/5192081
后续版本可以增加像axios.all( )和axios.spread( )这样的并发请求吗?
例如车联网的车辆位置轨迹查询,数据量比较大,单次请求不太合适,用分页多次并发请求,成功后合并结果绘制轨迹。PC端我们采用这种方式实现,效果挺好。
钉钉小程序 图片上传 ios手机 直接奔溃退出小程序 真机调试都无法捕获到错误信息 有人遇到过吗
请问支持Vue3,或者近期有支持Vue3的计划吗?
在这里 没有判断config.dataType 直接parse了
建议在这里加入对BigInt的支持
如题, 希望添加 typescript
支持
xxx.vue
evaluateUploadPhoto({
filePath: this.imagesData[i],
name: 'photo',
header: {
'token': uni.getStorageSync('token')
}
})
index.js
export const evaluateUploadPhoto = params => (
http.upload('/evaluate/upload-photo', params)
)
mergeConfig.js插件里有去除content-type
// eslint-disable-next-line no-empty
if (method === 'DOWNLOAD') {
} else if (method === 'UPLOAD') {
config.method = 'POST'
delete config.header['content-type']
delete config.header['Content-Type']
const uploadKeys = [
// #ifdef APP-PLUS || H5
'files',
// #endif
// #ifdef MP-ALIPAY
'fileType',
// #endif
// #ifdef H5
'file',
// #endif
'filePath',
'name',
'formData',
]
uploadKeys.forEach(prop => {
if (typeof config2[prop] !== 'undefined') {
config[prop] = config2[prop]
}
})
}
但是请求头里还是包含了Content-Type
Request Headers
Connection: close
Content-Type: application/json
Date: Wed, 16 Dec 2020 06:34:12 GMT
Server: nginx/1.15.4
Transfer-Encoding: chunked
希望执行完后,可以再次修改一下全局的baseURL 地址
http.interceptors.response.use((response) => { /* 请求之后拦截器。可以使用async await 做异步操作 */
// if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
// return Promise.reject(response)
// }
console.log(response.data.code)
if (response.data.code == 401) { // 服务端返回的状态码不等于200,则reject()
uni.clearStorage()
return Promise.reject(response)
}
return response
}, (response) => { // 请求错误做点什么。可以使用async await 做异步操作
console.log(response)
return Promise.reject(response)
})
export {
http
}
版本3.0.7,401的时候没走进来
目前luch-requrest 是支持创建多个实例和多个拦截器,导致现在的问题,当我在连续请求一个接口多次或者多个接口的时候,多个拦截器同触发,会造成性能内存的浪费,
http://localhost:8080/api/sanya/vehicle/1
http://localhost:8080/ + api/sanya/vehicle/ + 1
get请求这样的一个接口,参数怎么拼接在后面
/* 请求之后拦截器。可以使用async await 做异步操作 */
http.interceptors.response.use(async (response) => {
console.log(1)
uni.showToast({
icon: 'none',
title: '测试'
})
console.log(2)
剩下都是原来的代码
1和2都能打印,如果放alert也能弹窗,但是放showToast就没任何效果,也不报错,请问如何解决
有一个场景是频繁请求同一个接口,假如前一次请求还没有完成响应,就取消前一次请求,因为假如前一次请求比当前这一次请求响应慢些的话,前一次的响应数据会覆盖当前请求的响应数据
upload,formData含参,files:[]为空时报错,
检测到 lei-mu/luch-request 一共引入了886个开源组件,存在24个漏洞
漏洞标题:serialize-javascript 代码问题漏洞
缺陷组件:[email protected]
漏洞编号:CVE-2020-7660
漏洞描述:Verizon serialize-javascript是美国威瑞森电信(Verizon)公司的一款支持将JavaScript序列化为 JSON超集的软件包。
serialize-javascript 3.1.0之前版本中存在代码问题漏洞。远程攻击者可借助index.js文件中的‘deleteFunctions’函数利用该漏洞注入任意代码。
国家漏洞库信息:https://www.cnvd.org.cn/flaw/show/CNVD-2020-53801
影响范围:(∞, 3.1.0)
最小修复版本:3.1.0
缺陷组件引入路径:[email protected]>[email protected]>[email protected]
[email protected]>[email protected]>@vuepress/[email protected]>[email protected]>[email protected]
[email protected]>[email protected]>@vuepress/[email protected]>[email protected]>[email protected]>[email protected]
[email protected]>[email protected]>@vuepress/[email protected]>[email protected]>[email protected]
另外还有24个漏洞,详细报告:https://mofeisec.com/jr?p=i84d49
全局配置配置在哪?文档没有说明
模拟器上没有问题 在真机调试就会出现这种错误 请问是因为是什么原因呢 用到了请求拦截和响应拦截
{errMsg: "request:fail -200:net::ERR_CERT_COMMON_NAME_INVALID", config: {…}}
errMsg: "request:fail -200:net::ERR_CERT_COMMON_NAME_INVALID"
config:
baseURL: "https://test.xxxx"
url: "/auth/token/wechat"
params: {}
custom: {}
header: {}
validateStatus: ƒ validateStatus(status)
data: {ob: Observer}
timeout: 30000
dataType: "json"
responseType: "text"
当前在同时使用 TypeScript 和拦截器时,会遇到类似于 axios/axios#1510 的问题,官方添加了额外的范型解决,参考 axios/axios#1510 (comment) 和 axios/index.d.ts,是否可以参考着调整一下定义?
如果有需要,我可以抽空提个 PR。:D
这是官方说明连接: https://iot.weixin.qq.com/wiki/new/index.html?page=4-1-1
调用wx.invoke('configWXDeviceWiFi', {}, function(res) {
var err_msg = res.err_msg;
if (err_msg == 'configWXDeviceWiFi:ok') {
} else {
}
});
会跳转到官方提供的微信配网界面,现在是跳转不过去。这种情况怎么处理?
问题:在“为什么会请求两次?”
的回答中,关于跨域的描述和介绍不够准确。
建议:详细阅读关于跨域的文章,最后修改“有两种可能,一种是‘post三次握手’”
内容
请求的时候的时不时会提示“cant read property 'custom' of undefined”。这是什么原因导致的?代码如下:
`
//请求拦截器
http.interceptors.request.use((config) => { // 可使用async await 做异步操作
console.log('request interceptors')
console.log(config)
const token = getTokenStorage();
config.header = {
...config.header,
token: ''
}
if (token) {
config.header.token = 'Bearer ' + token;
}
if (config.method === 'POST') {
config.data = JSON.stringify(config.data);
}
if (config.custom.loading) {
uni.showLoading({title:'加载中...'})
}
return config
}, error => {
if (error.config.custom.loading) {
uni.hideLoading()
}
console.log('request interceptors error')
console.log(error)
console.log(error)
ret.code = (error.data==undefined||error.data.status==undefined)?405:error.data.status;
ret.message = (error.data==undefined||error.data.error==undefined)?error.errMsg:error.data.error;
return Promise.reject(error)
})
`
请问支持微信小程序调用吗
`// request.js 拦截器封装
import Request from 'luch-request'
import {baseURL} from "@/config";
const service = new Request({
baseURL,
method: 'POST',
timeout: 12000 // request timeout
});
service.interceptors.request.use(
config => {
// 拦截器header加参
config.header = {
...config.header,
}
return config
},
error => {
return Promise.reject(error)
}
)
service.interceptors.response.use(
response => {
const res = response.data
if (res.returnCode !== 1) {
return Promise.reject(new Error(res.returnMsg || 'Error'))
}
return res
},
error => {
uni.showToast({
title: error.msg || 'Server Error',
icon: "none",
duration: 2 * 1000
})
return Promise.reject(error)
}
)
export default service`
// api.js import http from '../common/js/request' export const loginWeixin = ({code,encryptedData,iv})=>{ return http.request({ url: '/user/loginWeixin', data:{code,encryptedData,iv} }) } export const uploadFile = ({filePath})=>{ return http.upload('/file/uploadFile',{ name:'file', filePath, }) }
普通的request请求 loginWeixin没问题,但是uploadFile请求则直接进入响应拦截器的catch里面error了。。。
搭建的时候,改动了源码,想卸载后重装,然后就装不上了
luch-request/src/lib/adapters/index.js
Line 32 in d8253ba
const axios = require('luch-request')
const ins = new axios({
baseUrl: config.baseURL
})
运行在小程序平台直接报错了, 请问大大这是为啥
Uncaught Error: Module parse failed: Unexpected token (18:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| export default class Request {
> config = {
| baseUrl: '',
| header: {},
at Object.<anonymous> (luch-request.js:2)
at __webpack_require__ (bootstrap:91)
at Module.<anonymous> (luch-request.js:1)
at __webpack_require__ (bootstrap:91)
at Object.<anonymous> (req.ts:2)
at __webpack_require__ (bootstrap:91)
at Object.<anonymous> (v1.ts:1)
at __webpack_require__ (bootstrap:91)
at Object.<anonymous> (index.vue?e69b:29)
at __webpack_require__ (bootstrap:91)
h5 运行时 提示 Cannot read property 'baseURL' of undefined
老哥 请问 如果发送一个请求A 此时A请求报错了 进入了响应错误拦截器回调 在这个回调中如何去重新再次发起前一次请求,并且成功后能在A请求的.then(success=>{})中进行触发。
现在遇到的问题是 只要请求报错进入拦截器 就会触发 A请求.then(success=>{},error=>{})中的error回调
就算我返回的是promise依然会进入error 无法进行多次请求重试的效果
响应错误拦截器回调中的代码
const backoff = new Promise(function(resolve){
setTimeout(()=>{
resolve()
},config.custom.retryDelay || 16)
})
return backoff.then(()=>{
return instance.request(config)
//此处instance.request(config).then(succes=>{})会触发数据回调 但是怎么把数据回调给到页面中的请求呢?
})
statusCode服务端必须返回这个属性吗?
目前luch-requrest 是支持创建多个实例和多个拦截器,导致现在的问题,当我在连续请求一个接口多次或者多个接口的时候,多个拦截器同触发,会造成性能内存的浪费,
设置了60s 没有效果 希望大佬指点一波!
upload 请求中获取不到response 的所有响应头信息
比如小程序里面,需要进行code换取token之后才能继续请求。
这个请求怎么实现请求队列功能。
已知文档处写明:data支持的文件类型只有Object/String/ArrayBuffer</code>这个真跟我没啥关系 0.0
上传多图片file[]后端这样子接收,一般都会这么操作哇,先说一般情况下~(且携带其他信息,一同formData提交)
尝试使用post请求,JSON对象作为data参数,设置header Content-type: ''application/x-www-form-urlencoded; charset=UTF-8"
成功上传格式 FormData,
小白问问put请求传header不成功?不能像post传吗
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.