UNPKG

x-http-client

Version:

An http client to simplify sending requests (HTTP & JSONP) in the browser.

139 lines (131 loc) 4.22 kB
var encodeQueryString = require('x-query-string/encode'); var constants = require('../shared/constants'); var template = require('./template'); var uuid = require('./uuid'); var HTTP_REQUEST = constants.HTTP_REQUEST; /** * Create a new default request options. * * @returns {RequestOptions} Returns a new default request opitons. */ function createDefaultOptions() { /*eslint no-unused-vars: ["error", { "args": "none" }]*/ /** * @type {RequestOptions} */ var options = { method: 'GET', baseURL: '', url: '', model: null, query: null, headers: null, body: null, timeout: 0, jsonpDefaultTimeout: 60000, cors: false, noCache: false, noCacheHeaders: { 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, must-revalidate' }, jsonp: 'callback', settings: {}, controller: null, requestFunctionName: null, requestType: null, xhrProps: null, username: null, password: null, httpRequestBodyProcessor: { raw: { priority: 0, headers: null, processor: null, }, form: { priority: 1, headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, processor: function (data, options) { return encodeQueryString(data); } }, json: { priority: 2, headers: { 'Content-Type': 'application/json; charset=UTF-8' }, processor: function (data, options) { return JSON.stringify(data); } } }, httpResponseMixin: { json: function () { // `this` is point to the current instance of `HttpResponse`. var responseText = this.request.xhr.responseText; return responseText ? JSON.parse(responseText) : null; }, text: function () { return this.request.xhr.responseText; }, status: function () { return this.request.xhr.status; } }, jsonpResponseMixin: { json: function () { return this.request.responseJSON; } }, httpResponseErrorMixin: null, jsonpResponseErrorMixin: null, handleOptions: null, createXHR: function (options) { return new XMLHttpRequest(); }, createScript: function (options) { var script = document.createElement('script'); script.setAttribute('type', 'text/javascript'); script.setAttribute('charset', 'utf-8'); return script; }, jsonpContainerNode: function (options) { return document.head || document.getElementsByName('head')[0]; }, jsonpCallbackName: function (options) { return 'jsonp_' + uuid() + '_' + (new Date().getTime()); }, compileURL: function (url, model, options) { return template(url, model); }, encodeQueryString: function (query, options) { return encodeQueryString(query); }, onXhrCreated: null, onXhrOpened: null, onXhrSent: null, onRequestCreated: null, isResponseOk: function (requestType, response) { var isOk; var status; // Http request if (requestType === HTTP_REQUEST) { status = +response.request.xhr.status; isOk = (status >= 200 && status < 300) || status === 304; // JSONP request } else { isOk = true; } return isOk; }, transformError: null, transformResponse: null, shouldCallErrorCallback: null, shouldCallSuccessCallback: null }; return options; } module.exports = createDefaultOptions;