UNPKG

x-http-client

Version:

An http client to make it easier to send requests (including JSONP requests) to the server.

138 lines (130 loc) 4.15 kB
var QS = require('x-query-string'); 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: null, url: null, model: null, query: null, headers: null, body: null, timeout: 0, 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 QS.encode(data); } }, json: { priority: 2, headers: { 'Content-Type': 'application/json; charset=UTF-8' }, processor: function (data, options) { return JSON.stringify(data); } } }, httpResponseParser: { 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; } }, jsonpResponseParser: { json: function () { return this.request.responseJSON; } }, httpResponseErrorParser: null, jsonpResponseErrorParser: 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 QS.encode(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;