Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "route4me-nodejs-sdk",
"version": "1.0.25",
"version": "1.0.27",
"description": "Access Route4Me's logistics-as-a-service API using our Node.js SDK",
"main": "src/index.js",
"browser": "src/route4me.js",
Expand Down Expand Up @@ -57,12 +57,11 @@
},
"homepage": "http://route4me.com",
"dependencies": {
"cookiejar": "^2.1.3",
"debug": "^4.3.2",
"del": "^6.0.0",
"platform": "^1.3.6",
"qs": "^6.10.3",
"superagent": "^7.1.1"
"superagent": "^8.0.9"
},
"devDependencies": {
"ajv": "^6.12.6",
Expand Down
8 changes: 2 additions & 6 deletions src/request-manager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict"

const request = require("superagent")
const qstr = require("qs")
const errors = require("./errors")
const utils = require("./utils")

class ResponseHandler {
constructor(PromiseConstructor, logger, validate, validateContext, callback, returns) {
Expand Down Expand Up @@ -247,11 +247,7 @@ class RequestManager {
.timeout(timeouts)
.redirects(1000) // unlimited number of redirects
.accept("application/json")
.query(qs)

if (options.indices) {
utils.patchRequest.bind(req)(req)
}
.query(options.indices ? qstr.stringify(qs, { arrayFormat: "indices" }) : qs)

if (form) {
req.type("multipart/form-data")
Expand Down
227 changes: 0 additions & 227 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,233 +218,6 @@ function toIsoDateString(d) {
return d.toISOString().substring(0, 10)
}

// ( added by 0x000f, 20220910
// bc superagent has hardcoded "indices: false" it prevent to have array indexes in query string
// also added dependencies to project 'cookiejar' and 'qs'
// use in request-manager.js RequestManager._makeRequest()

/* eslint-disable func-names, yoda, no-new-object, no-param-reassign, no-var, vars-on-top,
global-require, prefer-destructuring, prefer-arrow-callback, no-plusplus, no-return-assign,
no-func-assign, eqeqeq, no-shadow, no-sequences, no-restricted-syntax */
function patchRequest(Request) {
const parse = require("url").parse
const qs = require("qs")
const https = require("https")
const http = require("http")
const CookieJar = require("cookiejar")

const exports = {
protocols: {
"http:": http,
"https:": https,
"http2:": null // TODO: add http2
}
}

const hasOwn = Object.hasOwn || function (object, property) {
if (object == null) {
throw new TypeError("Cannot convert undefined or null to object")
}
return Object.prototype.hasOwnProperty.call(new Object(object), property)
}

function _typeof(obj) {
"@babel/helpers - typeof"

return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator
? function (obj) {
return typeof obj
}
: function (obj) {
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
}, _typeof(obj)
}

Request.request = function () {
var _this4 = this

if (this.req) return this.req
var options = {}

try {
var query = qs.stringify(this.qs, {
// ( changed by 0x000f
// indices: false,
indices: true,
// changed by 0x000f )
strictNullHandling: true
})

if (query) {
this.qs = {}

this._query.push(query)
}

this._finalizeQueryString()
} catch (err) {
return this.emit("error", err)
}

var url = this.url
var retries = this._retries // Capture backticks as-is from the final query string built above.
// Note: this'll only find backticks entered in req.query(String)
// calls, because qs.stringify unconditionally encodes backticks.

var queryStringBackticks

if (url.includes("`")) {
var queryStartIndex = url.indexOf("?")

if (queryStartIndex !== -1) {
var queryString = url.slice(queryStartIndex + 1)
queryStringBackticks = queryString.match(/`|%60/g)
}
} // default to http://

if (url.indexOf("http") !== 0) url = "http://".concat(url)
url = parse(url) // See https://github.com/visionmedia/superagent/issues/1367

if (queryStringBackticks) {
var i = 0
url.query = url.query.replace(/%60/g, function () {
return queryStringBackticks[i++]
})
url.search = "?".concat(url.query)
url.path = url.pathname + url.search
} // support unix sockets

if (/^https?\+unix:/.test(url.protocol) === true) {
// get the protocol
url.protocol = "".concat(url.protocol.split("+")[0], ":") // get the socket, path

var unixParts = url.path.match(/^([^/]+)(.+)$/)
options.socketPath = unixParts[1].replace(/%2F/g, "/")
url.path = unixParts[2]
} // Override IP address of a hostname

if (this._connectOverride) {
var _url = url
var hostname = _url.hostname
var match = hostname in this._connectOverride ? this._connectOverride[hostname] : this._connectOverride["*"]

if (match) {
// backup the real host
if (!this._header.host) {
this.set("host", url.host)
}

var newHost
var newPort

if (_typeof(match) === "object") {
newHost = match.host
newPort = match.port
} else {
newHost = match
newPort = url.port
} // wrap [ipv6]

url.host = /:/.test(newHost) ? "[".concat(newHost, "]") : newHost

if (newPort) {
url.host += ":".concat(newPort)
url.port = newPort
}

url.hostname = newHost
}
} // options

options.method = this.method
options.port = url.port
options.path = url.path
options.host = url.hostname
options.ca = this._ca
options.key = this._key
options.pfx = this._pfx
options.cert = this._cert
options.passphrase = this._passphrase
options.agent = this._agent
options.lookup = this._lookup
options.rejectUnauthorized = typeof this._disableTLSCerts === "boolean" ? !this._disableTLSCerts : process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0" // Allows request.get('https://1.2.3.4/').set('Host', 'example.com')

if (this._header.host) {
options.servername = this._header.host.replace(/:\d+$/, "")
}

if (this._trustLocalhost && /^(?:localhost|127\.0\.0\.\d+|(0*:)+:0*1)$/.test(url.hostname)) {
options.rejectUnauthorized = false
} // initiate request

var module_ = this._enableHttp2 ? exports.protocols["http2:"].setProtocol(url.protocol) : exports.protocols[url.protocol] // request

this.req = module_.request(options)
var req = this.req // set tcp no delay

req.setNoDelay(true)

if (options.method !== "HEAD") {
req.setHeader("Accept-Encoding", "gzip, deflate")
}

this.protocol = url.protocol
this.host = url.host // expose events

req.once("drain", function () {
_this4.emit("drain")
})
req.on("error", function (error) {
// flag abortion here for out timeouts
// because node will emit a faux-error "socket hang up"
// when request is aborted before a connection is made
// if not the same, we are in the **old** (cancelled) request,
// so need to continue (same as for above)
if (_this4._aborted) return

// if we've received a response then we don't want to let
// an error in the request blow up the response
if (_this4._retries !== retries) return

if (_this4.response) return

_this4.callback(error)
}) // auth

if (url.auth) {
var auth = url.auth.split(":")
this.auth(auth[0], auth[1])
}

if (this.username && this.password) {
this.auth(this.username, this.password)
}

for (var key in this.header) {
if (hasOwn(this.header, key)) req.setHeader(key, this.header[key])
} // add cookies

if (this.cookies) {
if (hasOwn(this._header, "cookie")) {
// merge
var temporaryJar = new CookieJar.CookieJar()
temporaryJar.setCookies(this._header.cookie.split(""))
temporaryJar.setCookies(this.cookies.split(""))
req.setHeader("Cookie", temporaryJar.getCookies(CookieJar.CookieAccessInfo.All).toValueString())
} else {
req.setHeader("Cookie", this.cookies)
}
}
return req
}
}
/* eslint-disable func-names, yoda, no-new-object, no-param-reassign, no-var, vars-on-top,
global-require, prefer-destructuring, prefer-arrow-callback, no-plusplus, no-return-assign,
no-func-assign, eqeqeq, no-shadow, no-sequences, no-restricted-syntax */

exports.patchRequest = patchRequest
// added by 0x000f, 20220910 )

exports.ILogger = ILogger
exports.CustomInternalPostProcessing = CustomInternalPostProcessing

Expand Down
14 changes: 7 additions & 7 deletions test/resources/vehicles-v5.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe(helper.toSuiteName(__filename), () => {
"page": "1",
"per_page": "10",
"show": "all",
"search_query": "TST 15"
"search_query": "TST%2015"
},
null
)
Expand Down Expand Up @@ -494,7 +494,7 @@ describe(helper.toSuiteName(__filename), () => {
"GET",
route4meClient.baseUrl5() + "/api/v5.0/vehicles/location",
{
"ids": "004ADB6E1335BA6BCE4FB8A89E6311"
"ids%5B0%5D": "004ADB6E1335BA6BCE4FB8A89E6311"
},
null
)
Expand Down Expand Up @@ -546,7 +546,7 @@ describe(helper.toSuiteName(__filename), () => {
"POST",
route4meClient.baseUrl5() + "/api/v5.0/vehicles/bulk/delete",
{
"vehicle_ids": "004ADB6E1335BA6BCE4FB8A89E6311"
"vehicle_ids%5B0%5D": "004ADB6E1335BA6BCE4FB8A89E6311"
},
null
)
Expand All @@ -567,7 +567,7 @@ describe(helper.toSuiteName(__filename), () => {
"POST",
route4meClient.baseUrl5() + "/api/v5.0/vehicles/bulk/activate",
{
"vehicle_ids": "004ADB6E1335BA6BCE4FB8A89E6311"
"vehicle_ids%5B0%5D": "004ADB6E1335BA6BCE4FB8A89E6311"
},
null
)
Expand Down Expand Up @@ -603,7 +603,7 @@ describe(helper.toSuiteName(__filename), () => {
"POST",
route4meClient.baseUrl5() + "/api/v5.0/vehicles/bulk/deactivate",
{
"vehicle_ids": "004ADB6E1335BA6BCE4FB8A89E6311"
"vehicle_ids%5B0%5D": "004ADB6E1335BA6BCE4FB8A89E6311"
},
null
)
Expand Down Expand Up @@ -639,7 +639,7 @@ describe(helper.toSuiteName(__filename), () => {
"POST",
route4meClient.baseUrl5() + "/api/v5.0/vehicles/bulk/restore",
{
"vehicle_ids": "004ADB6E1335BA6BCE4FB8A89E6311"
"vehicle_ids%5B0%5D": "004ADB6E1335BA6BCE4FB8A89E6311"
},
null
)
Expand Down Expand Up @@ -746,7 +746,7 @@ describe(helper.toSuiteName(__filename), () => {
"mergePagesParam": "true",
"page": "1",
"per_page": "10",
"search_query": "TST 15"
"search_query": "TST%2015"
},
null
)
Expand Down