init
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var axios = require('axios');
|
||||
|
||||
const getResponse = (res, config) => {
|
||||
const { statusCode, errMsg } = res;
|
||||
const response = {
|
||||
...res,
|
||||
status: statusCode,
|
||||
statusText: errMsg,
|
||||
config,
|
||||
request: null,
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const uniAdapter = (config) => {
|
||||
if (!uni) {
|
||||
throw new Error("please use this in uni-app project!");
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const { baseURL, url, headers, data, params } = config;
|
||||
const uniConfig = {
|
||||
...config,
|
||||
url: baseURL + url,
|
||||
header: headers,
|
||||
};
|
||||
|
||||
if (data || params) {
|
||||
try {
|
||||
uniConfig.data = JSON.parse(data || params);
|
||||
} catch (e) {
|
||||
uniConfig.data = data || params;
|
||||
}
|
||||
}
|
||||
uni.request({
|
||||
...uniConfig,
|
||||
success(res) {
|
||||
const response = getResponse(res, config);
|
||||
resolve(response);
|
||||
},
|
||||
fail(res) {
|
||||
const response = getResponse(res, config);
|
||||
reject(response);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const UniAdapter = uniAdapter;
|
||||
|
||||
axios.defaults.adapter = uniAdapter;
|
||||
|
||||
exports.default = axios;
|
||||
exports.UniAdapter = UniAdapter;
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
import axios from 'axios';
|
||||
export { default } from 'axios';
|
||||
|
||||
const getResponse = (res, config) => {
|
||||
const { statusCode, errMsg } = res;
|
||||
const response = {
|
||||
...res,
|
||||
status: statusCode,
|
||||
statusText: errMsg,
|
||||
config,
|
||||
request: null,
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const uniAdapter = (config) => {
|
||||
if (!uni) {
|
||||
throw new Error("please use this in uni-app project!");
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const { baseURL, url, headers, data, params } = config;
|
||||
const uniConfig = {
|
||||
...config,
|
||||
url: baseURL + url,
|
||||
header: headers,
|
||||
};
|
||||
|
||||
if (data || params) {
|
||||
try {
|
||||
uniConfig.data = JSON.parse(data || params);
|
||||
} catch (e) {
|
||||
uniConfig.data = data || params;
|
||||
}
|
||||
}
|
||||
uni.request({
|
||||
...uniConfig,
|
||||
success(res) {
|
||||
const response = getResponse(res, config);
|
||||
resolve(response);
|
||||
},
|
||||
fail(res) {
|
||||
const response = getResponse(res, config);
|
||||
reject(response);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const UniAdapter = uniAdapter;
|
||||
|
||||
axios.defaults.adapter = uniAdapter;
|
||||
|
||||
export { UniAdapter };
|
||||
Reference in New Issue
Block a user