From 6820c809e473674f480e0762d09f6164288d7053 Mon Sep 17 00:00:00 2001 From: alanclarke Date: Thu, 10 Jun 2021 07:22:13 +0100 Subject: [PATCH] defaultsDeep --- lib/najax.js | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/najax.js b/lib/najax.js index 7cf9300..817d538 100644 --- a/lib/najax.js +++ b/lib/najax.js @@ -9,7 +9,7 @@ var querystring = require('qs') var url = require('url') var zlib = require('zlib') var $ = require('jquery-deferred') -var defaultsDeep = require('lodash/defaultsdeep') +var defaultsDeep = require('lodash/defaultsDeep') var parseOptions = require('./parse-options') var defaults = { method: 'GET', @@ -42,7 +42,12 @@ function najax (uri, options, callback) { // https://github.com/jquery/jquery/blob/master/src/ajax.js#L518 if (o.data && o.processData && o.method === 'GET') { o.data = querystring.stringify(o.data, { arrayFormat: 'brackets' }) - } else if (o.data && o.processData && typeof o.data !== 'string' && o.method !== 'GET') { + } else if ( + o.data && + o.processData && + typeof o.data !== 'string' && + o.method !== 'GET' + ) { switch (true) { case o.contentType.startsWith('application/json'): o.data = JSON.stringify(o.data) @@ -65,10 +70,13 @@ function najax (uri, options, callback) { } } else { /* set data content type */ - o.headers = Object.assign({ - 'Content-Type': o.contentType, - 'Content-Length': Buffer.byteLength(o.data) - }, o.headers) + o.headers = Object.assign( + { + 'Content-Type': o.contentType, + 'Content-Length': Buffer.byteLength(o.data) + }, + o.headers + ) } } @@ -97,7 +105,13 @@ function najax (uri, options, callback) { /* for debugging, method to get options and return */ if (o.getopts) { - var getopts = [ssl, options, o.data || false, o.success || false, o.error || false] + var getopts = [ + ssl, + options, + o.data || false, + o.success || false, + o.error || false + ] return getopts } @@ -121,7 +135,9 @@ function najax (uri, options, callback) { var req = (ssl ? https : http).request(options, function (res) { // Allow getting Response Headers from the XMLHTTPRequest object - dfd.getResponseHeader = jqXHR.getResponseHeader = function getResponseHeader (header) { + dfd.getResponseHeader = jqXHR.getResponseHeader = function getResponseHeader ( + header + ) { return res.headers[header.toLowerCase()] } dfd.getAllResponseHeaders = jqXHR.getAllResponseHeaders = function getAllResponseHeaders () { @@ -139,8 +155,9 @@ function najax (uri, options, callback) { // // Determine if successful // (per https://github.com/jquery/jquery/blob/master/src/ajax.js#L679) - var isSuccess = statusCode >= 200 && statusCode < 300 || statusCode === 304 - // Set readyState + var isSuccess = + (statusCode >= 200 && statusCode < 300) || statusCode === 304 + // Set readyState jqXHR.readyState = statusCode > 0 ? 4 : 0 jqXHR.status = statusCode @@ -174,7 +191,9 @@ function najax (uri, options, callback) { } } var chunks = [] - res.on('data', function (chunk) { chunks.push(chunk) }) + res.on('data', function (chunk) { + chunks.push(chunk) + }) res.on('end', function () { var buffer = Buffer.concat(chunks) var encoding = res.headers['content-encoding'] @@ -239,8 +258,14 @@ najax.defaults = function mergeDefaults (opts) { ;['GET', 'POST', 'PUT', 'DELETE'].forEach(handleMethod) function handleMethod (method) { - najax[method.toLowerCase()] = function methodHandler (uri, options, callback) { - return najax(defaultsDeep(parseOptions(uri, options, callback), { method: method })) + najax[method.toLowerCase()] = function methodHandler ( + uri, + options, + callback + ) { + return najax( + defaultsDeep(parseOptions(uri, options, callback), { method: method }) + ) } }