You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.appendRequestHeader = appendRequestHeader;
exports.fetchJSON = fetchJSON;
exports.fetchText = fetchText;
exports.postForm = postForm;
exports.postJSON = postJSON;
exports.postStream = postStream;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
// import { BUILD_VERSION } from '@/config'
var BUILD_VERSION = '';
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
var customHeaders = [];
// 添加 HTTP Reuqest 自定义头部
function appendRequestHeader(n, v) {
customHeaders.push({
name: n,
value: v
});
}
function getRequestHeader() {
return customHeaders.reduce(function (acc, item) {
acc[item.name] = item.value;
return acc;
}, {});
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
var message = 'Fetch error: ' + response.url + ' ' + response.status + ' (' + response.statusText + ')';
var error = new Error(message);
error.response = response;
throw error;
}
}
function checkBizCode(responseJson) {
if (responseJson.errcode === 0) {
return responseJson;
} else {
throw new Error(responseJson.errmsg + ': ' + responseJson.errcode);
}
}
function fetchText(url) {
var headerObj = getRequestHeader();
return fetch(url, {
method: 'GET',
headers: _objectSpread({
'X-Web-Version': BUILD_VERSION
}, headerObj)
}).then(checkStatus).then(function (response) {
return response.text();
})["catch"](function (error) {
throw error;
});
}
function fetchJSON(url, data) {
var params = data ? new URLSearchParams(data).toString() : '';
var ifp = url.includes('?') ? data ? '&' : '' : '?';
var headerObj = getRequestHeader();
return fetch("".concat(url).concat(ifp).concat(params), {
method: 'GET',
headers: _objectSpread({
'X-Web-Version': BUILD_VERSION
}, headerObj)
}).then(checkStatus).then(function (response) {
return response.json();
}).then(checkBizCode)["catch"](function (error) {
throw error;
});
}
function postForm(url, data) {
var headerObj = getRequestHeader();
return fetch(url, {
method: 'POST',
body: data,
headers: _objectSpread({
'X-Web-Version': BUILD_VERSION
}, headerObj)
}).then(checkStatus).then(function (response) {
return response.json();
}).then(checkBizCode)["catch"](function (error) {
throw error;
});
}
function postJSON(url, obj) {
var headerObj = getRequestHeader();
return fetch(url, {
method: 'POST',
body: JSON.stringify(obj),
headers: _objectSpread({
'Content-type': 'application/json; charset=UTF-8',
'X-Web-Version': BUILD_VERSION
}, headerObj)
}).then(checkStatus).then(function (response) {
return response.json();
}).then(checkBizCode)["catch"](function (error) {
throw error;
});
}
function postStream(url, obj) {
var headerObj = getRequestHeader();
return fetch(url, {
method: 'POST',
body: JSON.stringify(obj),
headers: _objectSpread({
'Content-type': 'application/octet-stream',
'X-Web-Version': BUILD_VERSION
}, headerObj)
}).then(checkStatus).then(function (response) {
return response.json();
}).then(checkBizCode)["catch"](function (error) {
throw error;
});
}