Comparing version 1.0.0 to 1.1.0
@@ -6,3 +6,2 @@ 'use strict'; | ||
const https = require('https'); | ||
const url = require('url'); | ||
const util = require('util'); | ||
@@ -15,5 +14,2 @@ | ||
/** | ||
* Client | ||
*/ | ||
class Client extends events.EventEmitter { | ||
@@ -25,3 +21,3 @@ constructor(opts) { | ||
if (typeof opts === 'string') { | ||
if (typeof opts === 'string' || opts instanceof URL) { | ||
opts = { baseUrl: opts }; | ||
@@ -36,3 +32,3 @@ } else { | ||
if (!(opts.baseUrl instanceof url.Url)) { | ||
if (!(opts.baseUrl instanceof URL)) { | ||
if (typeof opts.baseUrl !== 'string') { | ||
@@ -43,16 +39,23 @@ throw new errors.ValidationError('baseUrl must be a string: ' + | ||
opts.baseUrl = url.parse(opts.baseUrl); | ||
opts.baseUrl = new URL(opts.baseUrl); | ||
} | ||
const path = opts.baseUrl.pathname; | ||
opts.baseUrl = utils.pick(opts.baseUrl, | ||
'auth', 'hostname', 'port', 'protocol'); | ||
opts.baseUrl.path = path; | ||
if (opts.baseUrl.path === '/') { | ||
opts.baseUrl.path = ''; | ||
} else if (opts.baseUrl.path[opts.baseUrl.path.length - 1] === '/') { | ||
const baseUrl = {}; | ||
baseUrl.protocol = opts.baseUrl.protocol; | ||
baseUrl.hostname = opts.baseUrl.hostname; | ||
if (opts.baseUrl.username) baseUrl.auth = opts.baseUrl.username; | ||
if (opts.baseUrl.password) { | ||
baseUrl.auth = (baseUrl.auth || '') + ':' + opts.baseUrl.password; | ||
} | ||
if (opts.baseUrl.port) baseUrl.port = opts.baseUrl.port; | ||
if (!opts.baseUrl.pathname || opts.baseUrl.pathname === '/') { | ||
baseUrl.path = ''; | ||
} else if ( | ||
opts.baseUrl.pathname[opts.baseUrl.pathname.length - 1] === '/') { | ||
throw new errors.ValidationError( | ||
'baseUrl must not end with a forward slash'); | ||
'baseUrl must not end with a forward slash'); | ||
} else { | ||
baseUrl.path = opts.baseUrl.pathname; | ||
} | ||
opts.baseUrl = baseUrl; | ||
@@ -286,3 +289,3 @@ opts.headers = utils.mergeHeaders(opts.headers); | ||
// middlware can call next(false, value) to stop middleware | ||
// middleware can call next(false, value) to stop middleware | ||
if (err === false) { | ||
@@ -428,3 +431,5 @@ // basic backwards compatibility for old interface | ||
utils.pick(this._opts, constants.CLIENT_OPTIONS), | ||
utils.pick(this._opts.baseUrl, 'auth', 'hostname', 'port', 'path'), | ||
this._opts.socketPath ? | ||
utils.pick(this._opts.baseUrl, 'auth', 'path') : | ||
utils.pick(this._opts.baseUrl, 'auth', 'hostname', 'port', 'path'), | ||
utils.pick(opts, constants.REQUEST_OPTIONS), | ||
@@ -440,10 +445,12 @@ { headers: headers } | ||
request.transport = https; | ||
if (!request.req.port) request.req.port = 443; | ||
if (!request.req.port && !this._opts.socketPath) { | ||
request.req.port = 443; | ||
} | ||
} else { | ||
request.transport = http; | ||
if (!request.req.port) request.req.port = 80; | ||
if (!request.req.port && !this._opts.socketPath) { | ||
request.req.port = 80; | ||
} | ||
} | ||
if (request.req.auth === null) delete request.req.auth; | ||
next(); | ||
@@ -582,3 +589,3 @@ } | ||
if (Math.floor(res.statusCode / 100) !== 2) { | ||
var message; | ||
let message; | ||
@@ -585,0 +592,0 @@ if (res.body && mime === 'text/plain' && res.body.length < 80) { |
@@ -37,2 +37,11 @@ 'use strict'; | ||
'agent', | ||
'createConnection', | ||
'family', | ||
'hints', | ||
'localAddress', | ||
'localPort', | ||
'lookup', | ||
'maxHeaderSize', | ||
'setHost', | ||
'socketPath', | ||
// tls | ||
@@ -39,0 +48,0 @@ 'ca', |
'use strict'; | ||
const url = require('url'); | ||
const Client = require('./client').Client; | ||
@@ -12,4 +10,4 @@ const errors = require('./errors'); | ||
function request(opts) { | ||
if (typeof opts === 'string') { | ||
async function request(opts) { | ||
if (typeof opts === 'string' || opts instanceof URL) { | ||
arguments[0] = opts = { method: 'get', url: opts }; | ||
@@ -21,13 +19,16 @@ } else { | ||
if (!opts.url) { | ||
return Promise.reject(new errors.ValidationError('url required')); | ||
throw new errors.ValidationError('url required'); | ||
} | ||
if (typeof opts.url !== 'string') { | ||
return Promise.reject(new errors.ValidationError('url must be a string')); | ||
let baseUrl; | ||
if (typeof opts.url === 'string') { | ||
baseUrl = new URL(opts.url); | ||
} else if (opts.url instanceof URL) { | ||
baseUrl = opts.url; | ||
} else { | ||
throw new errors.ValidationError('url must be a string'); | ||
} | ||
const baseUrl = url.parse(opts.url); | ||
opts.path = baseUrl.pathname.replace('%7B', '{').replace('%7D', '}'); | ||
baseUrl.pathname = ''; | ||
baseUrl = new URL('/', baseUrl); | ||
@@ -38,3 +39,3 @@ const client = new Client({ baseUrl: baseUrl }); | ||
return client._request.apply(client, arguments); | ||
return await client._request.apply(client, arguments); | ||
} | ||
@@ -47,4 +48,4 @@ | ||
function method(name) { | ||
return function(opts) { | ||
if (typeof opts === 'string') { | ||
return async function(opts) { | ||
if (typeof opts === 'string' || opts instanceof URL) { | ||
arguments[0] = opts = { url: opts }; | ||
@@ -57,3 +58,3 @@ } else { | ||
return request.apply(null, arguments); | ||
return await request.apply(null, arguments); | ||
}; | ||
@@ -60,0 +61,0 @@ } |
@@ -50,3 +50,3 @@ 'use strict'; | ||
for (var i = 0; i < arguments.length; i++) { | ||
for (let i = 0; i < arguments.length; i++) { | ||
const arg = arguments[i]; | ||
@@ -73,3 +73,3 @@ | ||
for (var i = 0; i < arguments.length; i++) { | ||
for (let i = 0; i < arguments.length; i++) { | ||
const arg = arguments[i]; | ||
@@ -104,3 +104,3 @@ | ||
for (var i = start; i < args.length; i++) { | ||
for (let i = start; i < args.length; i++) { | ||
const key = args[i]; | ||
@@ -107,0 +107,0 @@ |
{ | ||
"name": "papi", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Build HTTP API clients", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
@@ -30,2 +30,3 @@ # Papi | ||
* tags (string[], optional): tags included in `_log` calls | ||
* socketPath (string, optional): unix socket path (baseUrl is still required) | ||
* timeout (number, optional): default number of milliseconds before request is aborted | ||
@@ -192,3 +193,3 @@ | ||
<a name="shortcuts"></a> | ||
### papi.request(request, [callback...], callback) | ||
### papi.request(request, [callback...]) | ||
@@ -195,0 +196,0 @@ Shortcuts for making one-off requests. |
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
36306
17
892
294