Skip to content

Commit

Permalink
defaultsDeep
Browse files Browse the repository at this point in the history
  • Loading branch information
alanclarke committed Jun 10, 2021
1 parent 772961a commit 6820c80
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions lib/najax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand All @@ -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
)
}
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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 () {
Expand All @@ -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

Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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 })
)
}
}

Expand Down

0 comments on commit 6820c80

Please sign in to comment.