custom-request
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -14,2 +14,8 @@ 'use strict'; | ||
exports.noop = noop; | ||
var _utils = require('./utils'); | ||
var _utils2 = _interopRequireDefault(_utils); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -21,10 +27,14 @@ | ||
}, | ||
json: { | ||
'Content-Type': 'application/json;charset=utf-8' | ||
}, | ||
html: { | ||
'Content-Type': 'text/html;charset=utf-8' | ||
}, | ||
json: { | ||
'Content-Type': 'application/json;charset=utf-8' | ||
} | ||
}; | ||
var defaultContentType = { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}; | ||
function noop() {} | ||
@@ -36,3 +46,5 @@ | ||
headers: { | ||
'Accept': 'application/json' | ||
'Accept': 'application/json, text/plain, */*', | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' | ||
}, | ||
@@ -46,4 +58,4 @@ | ||
timeout: 3000 | ||
}, (0, _defineProperty3.default)(_defaults, 'cache', 'default'), (0, _defineProperty3.default)(_defaults, 'store', false), (0, _defineProperty3.default)(_defaults, 'beforeRequest', noop), (0, _defineProperty3.default)(_defaults, 'beforeResponse', noop), (0, _defineProperty3.default)(_defaults, 'requestError', noop), _defaults); | ||
}, (0, _defineProperty3.default)(_defaults, 'cache', 'default'), (0, _defineProperty3.default)(_defaults, 'store', false), (0, _defineProperty3.default)(_defaults, 'transformRequest', noop), (0, _defineProperty3.default)(_defaults, 'transformResponse', noop), (0, _defineProperty3.default)(_defaults, 'requestError', noop), _defaults); | ||
exports.default = defaults; |
@@ -11,6 +11,2 @@ 'use strict'; | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); | ||
@@ -28,2 +24,6 @@ | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
var _fetch = require('./adapters/fetch'); | ||
@@ -37,6 +37,6 @@ | ||
var _lodash = require('lodash'); | ||
var _utils = require('./utils'); | ||
var _utils2 = _interopRequireDefault(_utils); | ||
var _defaults = require('./defaults'); | ||
@@ -53,2 +53,29 @@ | ||
function transformConfig(data, config) { | ||
if (_utils2.default.isFormData(data) || _utils2.default.isArrayBuffer(data) || _utils2.default.isBuffer(data) || _utils2.default.isStream(data) || _utils2.default.isFile(data) || _utils2.default.isBlob(data)) { | ||
(0, _utils.merge)({ | ||
body: data | ||
}); | ||
} else if (_utils2.default.isArrayBufferView(data)) { | ||
(0, _utils.merge)({ | ||
body: data.buffer | ||
}); | ||
} else if (_utils2.default.isURLSearchParams(data)) { | ||
(0, _utils.merge)(config, { | ||
body: data.toString(), | ||
headers: _defaults.contentTypes.default | ||
}); | ||
} else if (_utils2.default.isObject(data)) { | ||
(0, _utils.merge)(config, { | ||
body: (0, _stringify2.default)(data), | ||
headers: _defaults.contentTypes.json | ||
}); | ||
} else { | ||
(0, _utils.merge)({ | ||
body: data | ||
}); | ||
} | ||
return config.body; | ||
} | ||
var Request = function () { | ||
@@ -60,3 +87,3 @@ function Request(config) { | ||
this.defaults = (0, _lodash.merge)({}, _defaults2.default, config); | ||
this.defaults = (0, _utils.merge)({}, _defaults2.default, config); | ||
} | ||
@@ -67,3 +94,3 @@ | ||
value: function config(opts) { | ||
(0, _lodash.merge)(this.defaults, opts); | ||
(0, _utils.merge)(this.defaults, opts); | ||
} | ||
@@ -76,3 +103,3 @@ }, { | ||
var config, _config$method, method, _config$data, data, beforeRequest, beforeResponse, requestError, response, res; | ||
var config, _config$method, method, _config$data, data, transformRequest, transformResponse, requestError, response, res; | ||
@@ -83,31 +110,20 @@ return _regenerator2.default.wrap(function _callee$(_context) { | ||
case 0: | ||
config = (0, _lodash.merge)({}, this.defaults, opts); | ||
_config$method = config.method, method = _config$method === undefined ? 'GET' : _config$method, _config$data = config.data, data = _config$data === undefined ? {} : _config$data, beforeRequest = config.beforeRequest, beforeResponse = config.beforeResponse, requestError = config.requestError; | ||
config = (0, _utils.merge)({}, this.defaults, opts); | ||
_config$method = config.method, method = _config$method === undefined ? 'GET' : _config$method, _config$data = config.data, data = _config$data === undefined ? {} : _config$data, transformRequest = config.transformRequest, transformResponse = config.transformResponse, requestError = config.requestError; | ||
url = this.buildUrl(url, data); | ||
_context.t0 = method; | ||
_context.next = _context.t0 === 'POST' ? 6 : _context.t0 === 'PATCH' ? 6 : _context.t0 === 'GET' ? 9 : _context.t0 === 'DELETE' ? 9 : 9; | ||
break; | ||
if (method === 'GET') { | ||
(0, _utils.merge)(config, { | ||
url: (0, _utils.urlfix)(url, _queryString2.default.stringify(data)) | ||
}); | ||
} | ||
transformConfig(data, config); | ||
case 6: | ||
(0, _lodash.merge)(config, { | ||
body: (0, _stringify2.default)(data) | ||
}); | ||
delete config.data; | ||
return _context.abrupt('break', 10); | ||
(0, _utils.isFunction)(transformRequest) && transformRequest(config); | ||
case 9: | ||
url = (0, _utils.urlfix)(url, _queryString2.default.stringify(data)); | ||
case 10: | ||
(0, _lodash.merge)(config, { | ||
url: url | ||
}); | ||
(0, _lodash.isFunction)(beforeRequest) && beforeRequest(config); | ||
_context.next = 14; | ||
_context.next = 8; | ||
return (0, _fetch2.default)(url, config); | ||
case 14: | ||
case 8: | ||
response = _context.sent; | ||
@@ -118,13 +134,16 @@ | ||
_context.next = 18; | ||
_context.next = 12; | ||
return response.json(); | ||
case 18: | ||
case 12: | ||
res = _context.sent; | ||
(0, _lodash.isFunction)(beforeResponse) && beforeResponse(res); | ||
(0, _utils.isFunction)(transformResponse) && transformResponse(res); | ||
if (response instanceof Error && (0, _utils.isFunction)(requestError)) { | ||
requestError(res); | ||
} | ||
return _context.abrupt('return', res); | ||
case 21: | ||
case 16: | ||
case 'end': | ||
@@ -155,2 +174,7 @@ return _context.stop(); | ||
}, { | ||
key: 'validateStatus', | ||
value: function validateStatus(status) { | ||
return status >= 200 && status < 300; | ||
} | ||
}, { | ||
key: 'checkStatus', | ||
@@ -157,0 +181,0 @@ value: function checkStatus(response) { |
102
lib/utils.js
@@ -11,2 +11,6 @@ 'use strict'; | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _lodash = require('lodash'); | ||
@@ -20,2 +24,75 @@ | ||
var isBuffer = require('is-buffer'); | ||
var toString = Object.prototype.toString; | ||
function isArray(val) { | ||
return toString.call(val) === '[object Array]'; | ||
} | ||
function isArrayBuffer(val) { | ||
return toString.call(val) === '[object ArrayBuffer]'; | ||
} | ||
function isFormData(val) { | ||
return typeof FormData !== 'undefined' && val instanceof FormData; | ||
} | ||
function isArrayBufferView(val) { | ||
var result; | ||
if (typeof ArrayBuffer !== 'undefined' && ArrayBuffer.isView) { | ||
result = ArrayBuffer.isView(val); | ||
} else { | ||
result = val && val.buffer && val.buffer instanceof ArrayBuffer; | ||
} | ||
return result; | ||
} | ||
function isString(val) { | ||
return typeof val === 'string'; | ||
} | ||
function isNumber(val) { | ||
return typeof val === 'number'; | ||
} | ||
function isUndefined(val) { | ||
return typeof val === 'undefined'; | ||
} | ||
function isObject(val) { | ||
return val !== null && (typeof val === 'undefined' ? 'undefined' : (0, _typeof3.default)(val)) === 'object'; | ||
} | ||
function isDate(val) { | ||
return toString.call(val) === '[object Date]'; | ||
} | ||
function isFile(val) { | ||
return toString.call(val) === '[object File]'; | ||
} | ||
function isBlob(val) { | ||
return toString.call(val) === '[object Blob]'; | ||
} | ||
function isFunction(val) { | ||
return toString.call(val) === '[object Function]'; | ||
} | ||
function isStream(val) { | ||
return isObject(val) && isFunction(val.pipe); | ||
} | ||
function isURLSearchParams(val) { | ||
return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams; | ||
} | ||
function isStandardBrowserEnv() { | ||
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') { | ||
return false; | ||
} | ||
return typeof window !== 'undefined' && typeof document !== 'undefined'; | ||
} | ||
function setTypes() { | ||
@@ -47,8 +124,4 @@ var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var urlfix = '?'; | ||
if (url.indexOf('?') !== -1) { | ||
urlfix = '&'; | ||
} | ||
if (paramsUrl) { | ||
url += '' + urlfix + paramsUrl; | ||
url = url + (url.indexOf('?') === -1 ? '?' : '&') + paramsUrl; | ||
} | ||
@@ -66,4 +139,19 @@ return url; | ||
exports.default = { | ||
isArray: _lodash.isArray, | ||
isFunction: _lodash.isFunction, | ||
merge: _lodash.merge, | ||
isArray: isArray, | ||
isArrayBuffer: isArrayBuffer, | ||
isBuffer: isBuffer, | ||
isFormData: isFormData, | ||
isArrayBufferView: isArrayBufferView, | ||
isString: isString, | ||
isNumber: isNumber, | ||
isObject: isObject, | ||
isUndefined: isUndefined, | ||
isDate: isDate, | ||
isFile: isFile, | ||
isBlob: isBlob, | ||
isFunction: isFunction, | ||
isStream: isStream, | ||
isURLSearchParams: isURLSearchParams, | ||
isStandardBrowserEnv: isStandardBrowserEnv, | ||
isPlainObject: _lodash.isPlainObject, | ||
@@ -70,0 +158,0 @@ urlfix: urlfix, |
{ | ||
"name": "custom-request", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "custom restful api use axios or fetch", | ||
@@ -23,2 +23,3 @@ "main": "lib/index.js", | ||
"es6-promise": "^4.1.1", | ||
"is-buffer": "^1.1.5", | ||
"isomorphic-fetch": "^2.2.1", | ||
@@ -25,0 +26,0 @@ "lodash": "^4.17.4", |
import utils from './utils' | ||
export const contentTypes = { | ||
@@ -6,13 +8,19 @@ default: { | ||
}, | ||
json: { | ||
'Content-Type': 'application/json;charset=utf-8', | ||
}, | ||
html: { | ||
'Content-Type': 'text/html;charset=utf-8', | ||
}, | ||
json: { | ||
'Content-Type': 'application/json;charset=utf-8', | ||
}, | ||
} | ||
// URLSearchParams: 'application/x-www-form-urlencoded;charset=utf-8', | ||
// String: 'text/plain;charset=utf-8', | ||
// FormData: 'multipart/form-data', | ||
// Blob: Blob inherited from the blob.type property | ||
const defaultContentType = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
} | ||
// Empty function, used as default callback | ||
function noop() {} | ||
export function noop() {} | ||
@@ -23,5 +31,5 @@ const defaults = { | ||
headers: { | ||
'Accept': 'application/json', | ||
'Accept': 'application/json, text/plain, */*', | ||
// Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response. | ||
// 'Content-Type': 'application/json', | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
}, | ||
@@ -39,4 +47,4 @@ // The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://127.0.0.1:8090' is therefore not allowed access. | ||
store: false, | ||
beforeRequest: noop, | ||
beforeResponse: noop, | ||
transformRequest: noop, | ||
transformResponse: noop, | ||
requestError: noop, | ||
@@ -43,0 +51,0 @@ } |
@@ -5,8 +5,50 @@ | ||
import queryString from 'query-string' | ||
import { merge, isFunction } from 'lodash' | ||
import { urlfix, getUrl } from './utils' | ||
import defaults from './defaults' | ||
import utils, { merge, isFunction } from './utils' | ||
import { | ||
urlfix, | ||
getUrl, | ||
isFormData, | ||
isArrayBuffer, | ||
isBuffer, | ||
isStream, | ||
isBlob, | ||
} from './utils' | ||
import defaults, { contentTypes } from './defaults' | ||
const httpReg = /^https?:/ | ||
// 处理数据格式以及Content-Type | ||
function transformConfig(data, config) { | ||
if (utils.isFormData(data) || | ||
utils.isArrayBuffer(data) || | ||
utils.isBuffer(data) || | ||
utils.isStream(data) || | ||
utils.isFile(data) || | ||
utils.isBlob(data) | ||
) { | ||
merge({ | ||
body: data, | ||
}) | ||
} else if (utils.isArrayBufferView(data)) { | ||
merge({ | ||
body: data.buffer, | ||
}) | ||
} else if (utils.isURLSearchParams(data)) { | ||
merge(config, { | ||
body: data.toString(), | ||
headers: contentTypes.default, | ||
}) | ||
} else if (utils.isObject(data)) { | ||
merge(config, { | ||
body: JSON.stringify(data), | ||
headers: contentTypes.json, | ||
}) | ||
} else { | ||
merge({ | ||
body: data, | ||
}) | ||
} | ||
return config.body | ||
} | ||
// error 不能使用 message 字段赋值,属于只读属性,参见以下 Error对象 | ||
@@ -68,4 +110,5 @@ // Property: | ||
* - store {Boolean | Object} 是否使用 store,以及使用时间限制 | ||
* - beforeReq {Function} 请求前处理函数 | ||
* - beforeRes {Function} 返回数据前处理函数 | ||
* - transformRequest {Function} 请求前处理函数 | ||
* - transformResponse {Function} 返回数据前处理函数 | ||
* - requestError {Function} 请求错误处理函数 | ||
* | ||
@@ -84,30 +127,17 @@ * Response | ||
data = {}, | ||
beforeRequest, | ||
beforeResponse, | ||
transformRequest, | ||
transformResponse, | ||
requestError, | ||
} = config | ||
url = this.buildUrl(url, data) | ||
switch (method) { | ||
case 'POST': | ||
case 'PATCH': | ||
if (method === 'GET') { | ||
merge(config, { | ||
// headers: { | ||
// "Content-Type": "application/json;", | ||
// }, | ||
body: JSON.stringify(data), | ||
url: urlfix(url, queryString.stringify(data)), | ||
}) | ||
delete config.data | ||
break | ||
case 'GET': | ||
case 'DELETE': | ||
default: | ||
url = urlfix(url, queryString.stringify(data)) | ||
} | ||
merge(config, { | ||
url, | ||
}) | ||
isFunction(beforeRequest) && beforeRequest(config) | ||
transformConfig(data, config) | ||
isFunction(transformRequest) && transformRequest(config) | ||
// 如需开启代理或切换 API 域,应在项目中配置 | ||
@@ -124,3 +154,6 @@ const response = await fetch(url, config) | ||
const res = await response.json() | ||
isFunction(beforeResponse) && beforeResponse(res) | ||
isFunction(transformResponse) && transformResponse(res) | ||
if( response instanceof Error && isFunction(requestError) ) { | ||
requestError(res) | ||
} | ||
@@ -137,2 +170,6 @@ return res | ||
validateStatus(status) { | ||
return status >= 200 && status < 300 | ||
} | ||
checkStatus(response) { | ||
@@ -139,0 +176,0 @@ const { status, statusText } = response |
197
src/utils.js
import { | ||
isArray, | ||
isFunction, | ||
merge, | ||
// isArray, | ||
// isFunction, | ||
isPlainObject, | ||
} from 'lodash' | ||
import queryString from 'query-string' | ||
var isBuffer = require('is-buffer') | ||
var toString = Object.prototype.toString | ||
/** | ||
* Determine if a value is an Array | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is an Array, otherwise false | ||
*/ | ||
function isArray(val) { | ||
return toString.call(val) === '[object Array]' | ||
} | ||
/** | ||
* Determine if a value is an ArrayBuffer | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is an ArrayBuffer, otherwise false | ||
*/ | ||
function isArrayBuffer(val) { | ||
return toString.call(val) === '[object ArrayBuffer]' | ||
} | ||
/** | ||
* Determine if a value is a FormData | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is an FormData, otherwise false | ||
*/ | ||
function isFormData(val) { | ||
return (typeof FormData !== 'undefined') && (val instanceof FormData) | ||
} | ||
/** | ||
* Determine if a value is a view on an ArrayBuffer | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false | ||
*/ | ||
function isArrayBufferView(val) { | ||
var result | ||
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) { | ||
result = ArrayBuffer.isView(val) | ||
} else { | ||
result = (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer) | ||
} | ||
return result | ||
} | ||
/** | ||
* Determine if a value is a String | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a String, otherwise false | ||
*/ | ||
function isString(val) { | ||
return typeof val === 'string' | ||
} | ||
/** | ||
* Determine if a value is a Number | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a Number, otherwise false | ||
*/ | ||
function isNumber(val) { | ||
return typeof val === 'number' | ||
} | ||
/** | ||
* Determine if a value is undefined | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if the value is undefined, otherwise false | ||
*/ | ||
function isUndefined(val) { | ||
return typeof val === 'undefined' | ||
} | ||
/** | ||
* Determine if a value is an Object | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is an Object, otherwise false | ||
*/ | ||
function isObject(val) { | ||
return val !== null && typeof val === 'object' | ||
} | ||
/** | ||
* Determine if a value is a Date | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a Date, otherwise false | ||
*/ | ||
function isDate(val) { | ||
return toString.call(val) === '[object Date]' | ||
} | ||
/** | ||
* Determine if a value is a File | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a File, otherwise false | ||
*/ | ||
function isFile(val) { | ||
return toString.call(val) === '[object File]' | ||
} | ||
/** | ||
* Determine if a value is a Blob | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a Blob, otherwise false | ||
*/ | ||
function isBlob(val) { | ||
return toString.call(val) === '[object Blob]' | ||
} | ||
/** | ||
* Determine if a value is a Function | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a Function, otherwise false | ||
*/ | ||
function isFunction(val) { | ||
return toString.call(val) === '[object Function]' | ||
} | ||
/** | ||
* Determine if a value is a Stream | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a Stream, otherwise false | ||
*/ | ||
function isStream(val) { | ||
return isObject(val) && isFunction(val.pipe) | ||
} | ||
/** | ||
* Determine if a value is a URLSearchParams object | ||
* | ||
* @param {Object} val The value to test | ||
* @returns {boolean} True if value is a URLSearchParams object, otherwise false | ||
*/ | ||
function isURLSearchParams(val) { | ||
return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams | ||
} | ||
/** | ||
* Determine if we're running in a standard browser environment | ||
* | ||
* This allows axios to run in a web worker, and react-native. | ||
* Both environments support XMLHttpRequest, but not fully standard globals. | ||
* | ||
* web workers: | ||
* typeof window -> undefined | ||
* typeof document -> undefined | ||
* | ||
* react-native: | ||
* navigator.product -> 'ReactNative' | ||
*/ | ||
function isStandardBrowserEnv() { | ||
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') { | ||
return false | ||
} | ||
return ( | ||
typeof window !== 'undefined' && | ||
typeof document !== 'undefined' | ||
) | ||
} | ||
/*! | ||
@@ -29,8 +201,4 @@ * 对提交参数一层封装,当POST JSON,并且结果也为JSON时使用 */ | ||
function urlfix(url, paramsUrl = '') { | ||
let urlfix = '?' | ||
if(url.indexOf('?') !== -1) { | ||
urlfix = '&' | ||
} | ||
if (paramsUrl) { | ||
url += `${urlfix}${paramsUrl}` | ||
url = url + (url.indexOf('?') === -1 ? '?' : '&') + paramsUrl | ||
} | ||
@@ -45,4 +213,19 @@ return url | ||
export default { | ||
merge, | ||
isArray, | ||
isArrayBuffer, | ||
isBuffer, | ||
isFormData, | ||
isArrayBufferView, | ||
isString, | ||
isNumber, | ||
isObject, | ||
isUndefined, | ||
isDate, | ||
isFile, | ||
isBlob, | ||
isFunction, | ||
isStream, | ||
isURLSearchParams, | ||
isStandardBrowserEnv, | ||
isPlainObject, | ||
@@ -49,0 +232,0 @@ urlfix, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
37410
920
4
6
+ Addedis-buffer@^1.1.5