@nathanfaucett/request
Version:
http requests for the browser and node.js
51 lines (41 loc) • 1.56 kB
JavaScript
var extend = require("@nathanfaucett/extend"),
isString = require("@nathanfaucett/is_string"),
isFunction = require("@nathanfaucett/is_function");
function defaults(options) {
options = extend({}, defaults.values, options);
options.url = isString(options.url || (options.url = options.src))
? options.url
: null;
options.method = isString(options.method)
? options.method.toUpperCase()
: "GET";
options.transformRequest = isFunction(options.transformRequest)
? options.transformRequest
: null;
options.transformResponse = isFunction(options.transformResponse)
? options.transformResponse
: null;
options.withCredentials =
options.withCredentials != null ? !!options.withCredentials : false;
options.headers = extend({}, defaults.values.headers, options.headers);
options.async = options.async != null ? !!options.async : true;
options.success = isFunction(options.success) ? options.success : null;
options.error = isFunction(options.error) ? options.error : null;
options.isPromise =
!isFunction(options.success) && !isFunction(options.error);
options.user = isString(options.user) ? options.user : undefined;
options.password = isString(options.password)
? options.password
: undefined;
return options;
}
defaults.values = {
url: "",
method: "GET",
data: null,
headers: {
Accept: "*/*",
"X-Requested-With": "XMLHttpRequest"
}
};
module.exports = defaults;