@cb-public/fetch-utils
v1.2.0
Published
uniapp-请求工具
Readme
使用示例
1、pnpm i fetch-utils
import fetchUtils from 'fetch-utils'
//直接调用(直接调用,因为没用baseUrl和md5key,将不会进行接口签名)
fetchUtils({
url: "https://xxx/getById",
data: {
storeId: 41,
},
})
type fetchFunction = <T>(
data: UniApp.RequestOptions
) => Promise<{ code: number; data: T }>
// 或创建新实例
const fetchUtilsObj = fetchUtils.createInstance({
baseUrl: "https://xxx.com",
// signData,不填则不需要签名
signData: {
type: "shop", // 好生活用户 user 好生活商家 shop 好生活管理boss
md5PrivateKey: "",
},
// 这里如果要签名需要填租户,因为签名接口需要租户
headers: {
"cg-ac": "shop-user",
},
})
// 接口请求前拦截
fetchUtilsObj.interceptors.request.use((data) => {
data.header = {
alias: "shop",
"cg-ac": "shop-user",
"cg-av": "1.0.0",
"cg-am": "app",
"cg-os": "ios",
"cg-ap": "ios",
}
console.log(data.header, "header")
return data
}, (error: any) => {
return Promise.reject(error)
})
// 接口请求后拦截
fetchUtilsObj.interceptors.response.use((res) => {
return res
}, (error: any) => {
return Promise.reject(error)
})
export const get: fetchFunction = (data) => {
return fetchUtilsObj.get(data)
}
//获取签名
const data: {'cg-sign': string;'cg-ts': number} = fetchUtilsObj.sign({
data: {
a: 1
}
})
export default fetchUtilsObj
