Comparing version 1.1.1 to 1.2.0
{ | ||
"parserOptions": { | ||
"ecmaVersion": 8, | ||
"ecmaVersion": 2018, | ||
"sourceType": "script" | ||
}, | ||
"extends": "airbnb", | ||
"extends": "airbnb-base", | ||
"env": { | ||
@@ -25,3 +25,4 @@ "browser": true, | ||
"no-restricted-syntax": ["off"], | ||
"no-param-reassign": "off" | ||
"no-param-reassign": "off", | ||
"max-classes-per-file": [1, 5] | ||
}, | ||
@@ -28,0 +29,0 @@ "plugins": [ |
120
index.js
@@ -182,2 +182,3 @@ 'use strict'; | ||
mergeFrom(baseUrl) { | ||
// eslint-disable-next-line no-use-before-define | ||
if ((baseUrl instanceof Base) || (baseUrl instanceof Request)) { | ||
@@ -271,4 +272,4 @@ baseUrl = baseUrl.url; | ||
str += `${this.hostname}`; | ||
if (this.port && | ||
(!this.protocol || Url.protocolPortNumbers[this.protocol] !== this.port) | ||
if (this.port | ||
&& (!this.protocol || Url.protocolPortNumbers[this.protocol] !== this.port) | ||
) { | ||
@@ -370,6 +371,3 @@ str += `:${this.port}`; | ||
* @param {Object} [data] An object to send along with the request. | ||
* If the content-type header is set to 'application/json', | ||
* than this data will be stringified through JSON.stringify(). | ||
* Otherwise the data will be parsed as an url encoded form string | ||
* from the first-level key/value pairs. | ||
* This data argument is processed through Request.createSendBody(). | ||
* @returns {Promise.<Constr|null>} Returns a Promise with the constructed object on success, | ||
@@ -388,7 +386,7 @@ * or null if a 404 was returned. Other status codes reject the promise. | ||
toObjectArray(Constr, data) { | ||
return this.toFunctionArray(obj => new Constr(obj), data); | ||
return this.toFunctionArray((obj) => new Constr(obj), data); | ||
} | ||
toObjectMap(Constr, data) { | ||
return this.toFunctionMap(obj => new Constr(obj), data); | ||
return this.toFunctionMap((obj) => new Constr(obj), data); | ||
} | ||
@@ -398,9 +396,9 @@ | ||
return this.send(data) | ||
.then(res => fn(JSON.parse(res.data))) | ||
.catch((err) => { | ||
if (err.statusCode === 404) { | ||
return Promise.resolve(fn(null)); | ||
} | ||
return Promise.reject(err); | ||
}); | ||
.then((res) => fn(JSON.parse(res.data))) | ||
.catch((err) => { | ||
if (err.statusCode === 404) { | ||
return Promise.resolve(fn(null)); | ||
} | ||
return Promise.reject(err); | ||
}); | ||
} | ||
@@ -410,13 +408,13 @@ | ||
return this.send(data) | ||
.then((res) => { | ||
const json = JSON.parse(res.data); | ||
const arr = []; | ||
.then((res) => { | ||
const json = JSON.parse(res.data); | ||
const arr = []; | ||
let i; | ||
for (i = 0; i < json.length; i += 1) { | ||
arr.push(fn(json[i])); | ||
} | ||
let i; | ||
for (i = 0; i < json.length; i += 1) { | ||
arr.push(fn(json[i])); | ||
} | ||
return arr; | ||
}); | ||
return arr; | ||
}); | ||
} | ||
@@ -426,13 +424,13 @@ | ||
return this.send(data) | ||
.then((res) => { | ||
const json = JSON.parse(res.data); | ||
const map = {}; | ||
.then((res) => { | ||
const json = JSON.parse(res.data); | ||
const map = {}; | ||
let key; | ||
for (key in json) { | ||
map[key] = fn(json[key]); | ||
} | ||
let key; | ||
for (key in json) { | ||
map[key] = fn(json[key]); | ||
} | ||
return map; | ||
}); | ||
return map; | ||
}); | ||
} | ||
@@ -442,3 +440,3 @@ | ||
return this.send(data) | ||
.then(res => `${res.data}`); | ||
.then((res) => `${res.data}`); | ||
} | ||
@@ -448,3 +446,3 @@ | ||
return this.send(data) | ||
.then(res => JSON.parse(res.data)); | ||
.then((res) => JSON.parse(res.data)); | ||
} | ||
@@ -461,3 +459,3 @@ | ||
// set the headers | ||
const headers = Object.assign({}, Request.defaults.headers, this.headers); | ||
const headers = { ...Request.defaults.headers, ...this.headers }; | ||
Object.keys(headers).forEach((headerName) => { | ||
@@ -527,3 +525,3 @@ if (typeof headers[headerName] === 'string' || typeof headers[headerName] === 'number') { | ||
options.headers = Object.assign({}, Request.defaults.headers, this.headers); | ||
options.headers = { ...Request.defaults.headers, ...this.headers }; | ||
for (const key in options.headers) { | ||
@@ -608,3 +606,14 @@ if (typeof options.headers[key] !== 'string' && typeof options.headers[key] !== 'number') { | ||
send(data) { | ||
/** | ||
* Creates a stringified version of the input data, | ||
* so it can be sent to the server. | ||
* If the data is already a string, that string will be returned. | ||
* Otherwise, if the content-type header is set to 'application/json', | ||
* than this data will be stringified through JSON.stringify(). | ||
* Otherwise the data will be parsed as an url encoded form string | ||
* from the first-level key/value pairs. | ||
* @param {Object|String} data The data to be stringified. | ||
* @returns {String} | ||
*/ | ||
createSendBody(data) { | ||
if (data && typeof data !== 'string') { | ||
@@ -626,16 +635,22 @@ const contentType = this.headers['content-type'] || Request.defaults.headers['content-type']; | ||
return data; | ||
} | ||
send(data) { | ||
const sendBody = this.createSendBody(data); | ||
// auto setting of content-length header | ||
if (data && !this.headers['content-length'] && | ||
((typeof this.autoContentLength !== 'boolean' && Request.defaults.autoContentLength === true) || | ||
this.autoContentLength === true) | ||
if (sendBody && !this.headers['content-length'] | ||
&& ((typeof this.autoContentLength !== 'boolean' && Request.defaults.autoContentLength === true) | ||
|| this.autoContentLength === true) | ||
) { | ||
this.headers['content-length'] = utf8ByteLength(data); | ||
this.headers['content-length'] = utf8ByteLength(sendBody); | ||
} | ||
if (typeof window === 'undefined') { | ||
return this.sendNode(data); | ||
return this.sendNode(sendBody); | ||
} | ||
/* istanbul ignore next */ | ||
return this.sendBrowser(data); | ||
return this.sendBrowser(sendBody); | ||
} | ||
@@ -660,2 +675,3 @@ } | ||
this.autoContentLength = null; | ||
this.Request = Request; | ||
@@ -689,3 +705,3 @@ if (typeof obj === 'string' || (obj instanceof Url)) { | ||
reqUrl.mergeFrom(baseUrl); | ||
const req = new Request(method, reqUrl.toString()); | ||
const req = new this.Request(method, reqUrl.toString()); | ||
@@ -721,7 +737,9 @@ Object.assign(req.headers, this.headers); | ||
module.exports = { | ||
Response, | ||
Request, | ||
Base, | ||
Url | ||
}; | ||
if (typeof window === 'undefined') { | ||
module.exports = { | ||
Response, | ||
Request, | ||
Base, | ||
Url | ||
}; | ||
} |
{ | ||
"name": "oohttp", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "object-oriented http(s) request handler", | ||
@@ -29,5 +29,3 @@ "license": "MIT", | ||
"eslint-plugin-import": "*", | ||
"eslint-config-airbnb": "*", | ||
"eslint-plugin-jsx-a11y": "*", | ||
"eslint-plugin-react": "*", | ||
"eslint-config-airbnb-base": "*", | ||
"express": "*", | ||
@@ -34,0 +32,0 @@ "http-proxy": "*", |
@@ -7,6 +7,6 @@ # Description | ||
## Node.js | ||
<pre><code>npm install oohttp --only=prod</code></pre> | ||
<pre><code>npm install oohttp</code></pre> | ||
## Browser | ||
The provided index.js can be used directly in the browser if it supports ECMAscript 2015 (and higher). For older browser you will need to transpile. | ||
The provided index.js can be used directly in the browser if it supports ECMAscript 2018 (and higher). For older browser you will need to transpile. | ||
@@ -13,0 +13,0 @@ # Examples |
@@ -7,4 +7,4 @@ 'use strict'; | ||
const assert = require('assert'); | ||
const expressApp = require('express')(); | ||
const oohttp = require('../index.js'); | ||
const expressApp = require('express')(); | ||
@@ -29,3 +29,3 @@ const testObject = { | ||
describe('Base', function () { | ||
let base = new oohttp.Base('http://localhost'); | ||
const base = new oohttp.Base('http://localhost'); | ||
before(function () { | ||
@@ -39,3 +39,3 @@ expressServer = expressApp.listen(9800); | ||
describe('passing object as argument', function() { | ||
describe('passing object as argument', function () { | ||
const newBase = new oohttp.Base({ | ||
@@ -46,3 +46,3 @@ url: 'http://127.0.0.1/', | ||
it('should assign properties', function() { | ||
it('should assign properties', function () { | ||
assert.equal(newBase.autoContentLength, true); | ||
@@ -129,6 +129,6 @@ assert(newBase.url instanceof oohttp.Url); | ||
describe('base.url = str', function() { | ||
describe('base.url = str', function () { | ||
base.url = 'http://127.0.0.1'; | ||
it('should be parsed as Url when Request object is created', function() { | ||
it('should be parsed as Url when Request object is created', function () { | ||
const req = base.get('/something'); | ||
@@ -135,0 +135,0 @@ |
@@ -128,3 +128,3 @@ 'use strict'; | ||
describe('.parseQueryString(undefined)', function() { | ||
describe('.parseQueryString(undefined)', function () { | ||
it('should not parse no querystring', function () { | ||
@@ -131,0 +131,0 @@ assert.deepStrictEqual(oohttp.Url.parseQueryString(), {}); |
@@ -7,5 +7,6 @@ 'use strict'; | ||
const assert = require('assert'); | ||
const oohttp = require('../index.js'); | ||
const http = require('http'); | ||
const express = require('express'); | ||
const httpProxy = require('http-proxy'); | ||
const oohttp = require('../index.js'); | ||
@@ -80,3 +81,3 @@ const expressApp = express(); | ||
const proxyServer = httpProxy.createProxyServer(); | ||
const proxyWebserver = require('http').createServer((req, res) => { | ||
const proxyWebserver = http.createServer((req, res) => { | ||
res.setHeader('proxy-test', 'true'); | ||
@@ -142,3 +143,3 @@ | ||
it('should return a list of Obj instances', async function () { | ||
objs.forEach(obj => assert(obj instanceof Obj)); | ||
objs.forEach((obj) => assert(obj instanceof Obj)); | ||
}); | ||
@@ -187,3 +188,3 @@ | ||
it('should return a map of Obj instances', async function () { | ||
Object.keys(objs).forEach(key => assert(objs[key] instanceof Obj)); | ||
Object.keys(objs).forEach((key) => assert(objs[key] instanceof Obj)); | ||
}); | ||
@@ -390,3 +391,3 @@ | ||
}); | ||
it('should have proxy header', function () { | ||
@@ -393,0 +394,0 @@ assert.equal(res.headers['proxy-test'], 'true'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46267
7
1252