Comparing version 12.5.0 to 13.0.0
269
lib/index.js
@@ -6,9 +6,11 @@ 'use strict'; | ||
const Events = require('events'); | ||
const Url = require('url'); | ||
const Http = require('http'); | ||
const Https = require('https'); | ||
const Stream = require('stream'); | ||
const Url = require('url'); | ||
const Zlib = require('zlib'); | ||
const Boom = require('boom'); | ||
const Hoek = require('hoek'); | ||
const Boom = require('boom'); | ||
const Payload = require('./payload'); | ||
@@ -23,19 +25,14 @@ const Recorder = require('./recorder'); | ||
jsonRegex: /^application\/([a-z0-9.]*[+-]json|json)$/, | ||
shallowOptions: ['agent', 'agents', 'beforeRedirect', 'downstreamRes', 'payload', 'redirected'], | ||
emitSymbol: Symbol.for('wreck') | ||
shallowOptions: ['agent', 'agents', 'beforeRedirect', 'downstreamRes', 'payload', 'redirected'] | ||
}; | ||
process[internals.emitSymbol] = process[internals.emitSymbol] || new Events.EventEmitter(); | ||
// New instance is exported as module.exports | ||
// new instance is exported as module.exports | ||
internals.Client = function (options = {}) { | ||
internals.Client = function (defaults) { | ||
Hoek.assert(!options.agents || (options.agents.https && options.agents.http && options.agents.httpsAllowUnauthorized), 'Option agents must include "http", "https", and "httpsAllowUnauthorized"'); | ||
defaults = defaults || {}; | ||
Hoek.assert(!defaults.agents || (defaults.agents.https && defaults.agents.http && defaults.agents.httpsAllowUnauthorized), | ||
'defaults.agents must include "http", "https", and "httpsAllowUnauthorized"'); | ||
this._defaults = Hoek.cloneWithShallow(options, internals.shallowOptions); | ||
this._defaults = Hoek.cloneWithShallow(defaults, internals.shallowOptions); | ||
this.agents = this._defaults.agents || { | ||
@@ -47,18 +44,14 @@ https: new Https.Agent({ maxSockets: Infinity }), | ||
Events.EventEmitter.call(this); | ||
if (!options.events) { | ||
return; | ||
} | ||
// replay request/response events to process[Symbol.for('wreck')] | ||
const self = this; | ||
const selfEmit = this.emit; | ||
this.emit = function () { | ||
this.events = new Events.EventEmitter(); | ||
this._emit = function (...args) { | ||
const processEmitter = process[internals.emitSymbol]; | ||
selfEmit.apply(self, arguments); | ||
processEmitter.emit.apply(processEmitter, arguments); | ||
this.events.emit(...args); | ||
}; | ||
}; | ||
Hoek.inherits(internals.Client, Events.EventEmitter); | ||
internals.Client.prototype.defaults = function (options) { | ||
@@ -80,3 +73,5 @@ | ||
const parsedPath = Url.parse(path); | ||
if (parsedPath.host && parsedPath.protocol) { | ||
if (parsedPath.host && | ||
parsedPath.protocol) { | ||
return Url.format(parsedPath); | ||
@@ -94,24 +89,17 @@ } | ||
internals.Client.prototype.request = function (method, url, options, callback, _trace) { | ||
internals.Client.prototype.request = function (method, url, options = {}) { | ||
options = Hoek.applyToDefaultsWithShallow(this._defaults, options || {}, internals.shallowOptions); | ||
try { | ||
options = Hoek.applyToDefaultsWithShallow(this._defaults, options, internals.shallowOptions); | ||
Hoek.assert(options.payload === undefined || typeof options.payload === 'string' || typeof options.payload === 'object', | ||
'options.payload must be a string, a Buffer, a Stream, or an Object'); | ||
Hoek.assert(options.payload === undefined || typeof options.payload === 'string' || typeof options.payload === 'object', 'options.payload must be a string, a Buffer, a Stream, or an Object'); | ||
Hoek.assert((options.agent === undefined || options.agent === null) || (typeof options.rejectUnauthorized !== 'boolean'), 'options.agent cannot be set to an Agent at the same time as options.rejectUnauthorized is set'); | ||
Hoek.assert(options.beforeRedirect === undefined || options.beforeRedirect === null || typeof options.beforeRedirect === 'function', 'options.beforeRedirect must be a function'); | ||
Hoek.assert(options.redirected === undefined || options.redirected === null || typeof options.redirected === 'function', 'options.redirected must be a function'); | ||
Hoek.assert(options.gunzip === undefined || typeof options.gunzip === 'boolean' || options.gunzip === 'force', 'options.gunzip must be a boolean or "force"'); | ||
} | ||
catch (err) { | ||
return Promise.reject(err); | ||
} | ||
Hoek.assert((options.agent === undefined || options.agent === null) || (typeof options.rejectUnauthorized !== 'boolean'), | ||
'options.agent cannot be set to an Agent at the same time as options.rejectUnauthorized is set'); | ||
Hoek.assert(options.beforeRedirect === undefined || options.beforeRedirect === null || typeof options.beforeRedirect === 'function', | ||
'options.beforeRedirect must be a function'); | ||
Hoek.assert(options.redirected === undefined || options.redirected === null || typeof options.redirected === 'function', | ||
'options.redirected must be a function'); | ||
Hoek.assert(options.gunzip === undefined || typeof options.gunzip === 'boolean' || options.gunzip === 'force', | ||
'options.gunzip must be a boolean or "force"'); | ||
Hoek.assert(options.onRequest === undefined || typeof options.onRequest === 'function', | ||
'options.onRequest must be a function'); | ||
options.beforeRedirect = options.beforeRedirect || ((redirectMethod, statusCode, location, resHeaders, redirectOptions, next) => next()); | ||
@@ -124,2 +112,25 @@ | ||
const relay = {}; | ||
const req = this._request(method, url, options, relay); | ||
const promise = new Promise((resolve, reject) => { | ||
relay.callback = (err, res) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(res); | ||
return; | ||
}; | ||
}); | ||
promise.req = req; | ||
return promise; | ||
}; | ||
internals.Client.prototype._request = function (method, url, options, relay, _trace) { | ||
const uri = Url.parse(url); | ||
@@ -181,3 +192,5 @@ | ||
this.emit('request', uri, options); | ||
if (this._emit) { | ||
this._emit('request', uri, options); | ||
} | ||
@@ -195,2 +208,3 @@ const start = Date.now(); | ||
}; | ||
req.once('error', onError); | ||
@@ -234,3 +248,3 @@ | ||
const redirectReq = this.request(redirectMethod, location, redirectOptions, finishOnce, _trace); | ||
const redirectReq = this._request(redirectMethod, location, redirectOptions, { callback: finishOnce }, _trace); | ||
@@ -255,7 +269,8 @@ if (options.redirected) { | ||
clearTimeout(timeoutId); | ||
this.emit('response', err, { req, res, start, uri }); | ||
if (callback) { | ||
return callback(err, res); | ||
if (this._emit) { | ||
this._emit('response', err, { req, res, start, uri }); | ||
} | ||
return relay.callback(err, res); | ||
}; | ||
@@ -313,7 +328,2 @@ | ||
stream.pipe(req); | ||
if (options.onRequest) { | ||
options.onRequest(req); | ||
} | ||
return req; | ||
@@ -328,7 +338,2 @@ } | ||
req.end(); | ||
if (options.onRequest) { | ||
options.onRequest(req); | ||
} | ||
return req; | ||
@@ -360,15 +365,28 @@ }; | ||
// read() | ||
internals.Client.prototype.read = function (res, options = {}) { | ||
internals.Client.prototype.read = function (res, options, callback) { | ||
return new Promise((resolve, reject) => { | ||
options = Hoek.applyToDefaultsWithShallow(this._defaults, options || {}, internals.shallowOptions); | ||
this._read(res, options, (err, payload) => { | ||
// Set stream timeout | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
const clientTimeout = options.timeout; | ||
let clientTimeoutId = null; | ||
resolve(payload); | ||
return; | ||
}); | ||
}); | ||
}; | ||
internals.Client.prototype._read = function (res, options, callback) { | ||
options = Hoek.applyToDefaultsWithShallow(this._defaults, options, internals.shallowOptions); | ||
// Finish once | ||
let clientTimeoutId = null; | ||
const finish = (err, buffer) => { | ||
@@ -383,6 +401,8 @@ | ||
if (err || | ||
!options.json) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return callback(err, buffer); | ||
if (!options.json) { | ||
return callback(null, buffer); | ||
} | ||
@@ -392,3 +412,2 @@ | ||
let result; | ||
if (buffer.length === 0) { | ||
@@ -399,4 +418,3 @@ return callback(null, null); | ||
if (options.json === 'force') { | ||
result = internals.tryParseBuffer(buffer); | ||
return callback(result.err, result.json); | ||
return internals.tryParseBuffer(buffer, callback); | ||
} | ||
@@ -413,7 +431,7 @@ | ||
} | ||
return callback(null, buffer); | ||
} | ||
result = internals.tryParseBuffer(buffer); | ||
return callback(result.err, result.json); | ||
return internals.tryParseBuffer(buffer, callback); | ||
}; | ||
@@ -423,2 +441,3 @@ | ||
const clientTimeout = options.timeout; | ||
if (clientTimeout && | ||
@@ -489,4 +508,2 @@ clientTimeout > 0) { | ||
// toReadableStream() | ||
internals.Client.prototype.toReadableStream = function (payload, encoding) { | ||
@@ -498,4 +515,2 @@ | ||
// parseCacheControl() | ||
internals.Client.prototype.parseCacheControl = function (field) { | ||
@@ -539,104 +554,78 @@ | ||
internals.Client.prototype.get = function (uri, options, callback) { | ||
internals.Client.prototype.get = function (uri, options) { | ||
return this._shortcutWrap('GET', uri, options, callback); | ||
return this._shortcut('GET', uri, options); | ||
}; | ||
internals.Client.prototype.post = function (uri, options, callback) { | ||
internals.Client.prototype.post = function (uri, options) { | ||
return this._shortcutWrap('POST', uri, options, callback); | ||
return this._shortcut('POST', uri, options); | ||
}; | ||
internals.Client.prototype.patch = function (uri, options, callback) { | ||
internals.Client.prototype.patch = function (uri, options) { | ||
return this._shortcutWrap('PATCH', uri, options, callback); | ||
return this._shortcut('PATCH', uri, options); | ||
}; | ||
internals.Client.prototype.put = function (uri, options, callback) { | ||
internals.Client.prototype.put = function (uri, options) { | ||
return this._shortcutWrap('PUT', uri, options, callback); | ||
return this._shortcut('PUT', uri, options); | ||
}; | ||
internals.Client.prototype.delete = function (uri, options, callback) { | ||
internals.Client.prototype.delete = function (uri, options) { | ||
return this._shortcutWrap('DELETE', uri, options, callback); | ||
return this._shortcut('DELETE', uri, options); | ||
}; | ||
// Wrapper so that shortcut can be optimized with required params | ||
internals.Client.prototype._shortcut = async function (method, uri, options = {}) { | ||
internals.Client.prototype._shortcutWrap = function (method, uri /* [options], callback */) { | ||
const res = await this.request(method, uri, options); | ||
const options = (typeof arguments[2] === 'function' ? {} : arguments[2]); | ||
const callback = (typeof arguments[2] === 'function' ? arguments[2] : arguments[3]); | ||
let payload; | ||
try { | ||
payload = await this.read(res, options); | ||
} | ||
catch (err) { | ||
err.data = err.data || {}; | ||
err.data.res = res; | ||
throw err; | ||
} | ||
return this._shortcut(method, uri, options, callback); | ||
}; | ||
internals.Client.prototype._shortcut = function (method, uri, options, callback) { | ||
let req = null; | ||
let promise = null; | ||
if (typeof callback !== 'function') { | ||
promise = new Promise((resolve, reject) => { | ||
callback = (err, res, payload) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve({ res, req, payload }); | ||
}; | ||
}); | ||
if (res.statusCode < 400) { | ||
return { res, payload }; | ||
} | ||
req = this.request(method, uri, options, (err, res) => { | ||
// Response error | ||
if (err) { | ||
return callback(err); | ||
} | ||
const err = new Error(`Response Error: ${res.statusCode} ${res.statusMessage}`); | ||
const data = { | ||
isResponseError: true, | ||
headers: res.headers, | ||
res, | ||
payload | ||
}; | ||
this.read(res, options, (err, payload) => { | ||
if (!err && res.statusCode >= 400) { | ||
return callback(Boom.create(res.statusCode, new Error(`Response Error: ${res.statusCode} ${res.statusMessage}`), { | ||
isResponseError: true, | ||
headers: res.headers, | ||
response: res, | ||
payload | ||
})); | ||
} | ||
return callback(err, res, payload); | ||
}); | ||
}); | ||
return promise || req; | ||
throw Boom.create(res.statusCode, err, data); | ||
}; | ||
internals.tryParseBuffer = function (buffer) { | ||
internals.tryParseBuffer = function (buffer, next) { | ||
const result = { | ||
json: null, | ||
err: null | ||
}; | ||
let payload; | ||
try { | ||
const json = JSON.parse(buffer.toString()); | ||
result.json = json; | ||
payload = JSON.parse(buffer.toString()); | ||
} | ||
catch (err) { | ||
result.err = err; | ||
result.json = buffer; | ||
return next(Boom.badGateway(err.message, { payload: buffer })); | ||
} | ||
return result; | ||
return next(null, payload); | ||
}; | ||
internals.findHeader = function (headerName, headers) { | ||
@@ -643,0 +632,0 @@ |
{ | ||
"name": "wreck", | ||
"description": "HTTP Client Utilities", | ||
"version": "12.5.0", | ||
"version": "13.0.0", | ||
"repository": "git://github.com/hapijs/wreck", | ||
@@ -13,3 +13,3 @@ "main": "lib/index", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=8.0.0" | ||
}, | ||
@@ -21,4 +21,4 @@ "dependencies": { | ||
"devDependencies": { | ||
"lab": "14.x.x", | ||
"require-reload": "0.2.x" | ||
"code": "5.x.x", | ||
"lab": "14.x.x" | ||
}, | ||
@@ -25,0 +25,0 @@ "scripts": { |
190
README.md
@@ -13,26 +13,7 @@ ![wreck Logo](https://raw.github.com/hapijs/wreck/master/images/wreck.png) | ||
### Basic | ||
```javascript | ||
const Wreck = require('wreck'); | ||
Wreck.get('https://google.com/', (err, res, payload) => { | ||
/* do stuff */ | ||
}); | ||
``` | ||
```javascript | ||
const Wreck = require('wreck'); | ||
Wreck.post('https://posttestserver.com/post.php', { payload: { hello: 'post' } }, (err, res, payload) => { | ||
/* do stuff */ | ||
}); | ||
``` | ||
### With Async/Await | ||
```javascript | ||
const Wreck = require('wreck'); | ||
async function example () { | ||
const { req, res, payload } = await Wreck.get('http://example.com'); | ||
const { res, payload } = await Wreck.get('http://example.com'); | ||
console.log(payload.toString()); | ||
@@ -88,15 +69,16 @@ } | ||
const optionalCallback = (err, res) => { | ||
const promise = wreck.request(method, uri, options); | ||
try { | ||
const res = await promise; | ||
const body = await Wreck.read(res); | ||
} | ||
catch (err) { | ||
// Handle errors | ||
} | ||
``` | ||
/* handle err if it exists, in which case res will be undefined */ | ||
Use `promise.req.abort()` to terminate the request early. Note that this is limited to the initial request only. | ||
If the request was already redirected, aborting the original request will not abort execution of pending redirections. | ||
// buffer the response stream | ||
Wreck.read(res, null, (err, body) => { | ||
/* do stuff */ | ||
}); | ||
}; | ||
const req = wreck.request(method, uri, options, optionalCallback); | ||
``` | ||
### `defaults(options)` | ||
@@ -107,3 +89,3 @@ | ||
### `request(method, uri, [options, [callback]])` | ||
### `request(method, uri, [options])` | ||
@@ -122,3 +104,2 @@ Initiate an HTTP request. | ||
- `headers` - An object containing request headers. | ||
- `onRequest` - A function that is called when a request is available using the signature `function(req)` where `req` is a [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest). | ||
- `redirects` - The maximum number of redirects to follow. | ||
@@ -153,11 +134,7 @@ - `redirect303` - if `true`, a HTTP 303 status code will redirect using a GET method. Defaults to no redirection on 303. | ||
for possible [TLS_CIPHERS](https://www.openssl.org/docs/man1.0.2/apps/ciphers.html#CIPHER-LIST-FORMAT). | ||
- `callback` - The optional callback function using the signature `function (err, response)` where: | ||
- `err` - Any error that may have occurred during the handling of the request. | ||
- `response` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
object, which is also a readable stream. | ||
Returns an instance of the node.js [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object. | ||
Returns a promise that resolves into a node response object. The promise has a `req` property which is the instance of the node.js | ||
[ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object. | ||
### `read(response, options, callback)` | ||
### `read(response, options)` | ||
- `response` - An HTTP Incoming Message object. | ||
@@ -176,6 +153,5 @@ - `options` - `null` or a configuration object with the following optional keys: | ||
- `maxBytes` - The maximum allowed response payload size. Defaults to unlimited. | ||
- `callback` - The callback function using the signature `function (err, payload)` where: | ||
- `err` - Any error that may have occurred while reading the response. | ||
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON). | ||
Returns a promise that resolves into the payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON). | ||
#### Notes about gunzip | ||
@@ -185,6 +161,4 @@ | ||
Node v4 does not detect premature ending of gzipped content, if the payload is partial, you will not get an error on this specific version of node.js. | ||
### `get(uri, [options])` | ||
### `get(uri, [options, [callback]])` | ||
Convenience method for GET operations. | ||
@@ -194,18 +168,18 @@ - `uri` - The URI of the requested resource. | ||
`read` operations. | ||
- `callback` - Optional callback function using the signature `function (err, response, payload)` where: | ||
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom properties. | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.response` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
- `response` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
Returns a promise that resolves into an object with the following properties: | ||
- `res` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
object, which is a readable stream that has "ended" and contains no more data to read. | ||
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON). | ||
If a callback function is provided then an instance of the node.js [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object is returned. | ||
If no callback function is provided then a Promise is returned that resolves an object with the following structure: `{ req, res, payload }`; | ||
Throws any error that may have occurred during handling of the request or a Boom error object if the response has an error status | ||
code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom | ||
properties: | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.res` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
### `post(uri, [options])` | ||
### `post(uri, [options, [callback]])` | ||
Convenience method for POST operations. | ||
@@ -215,18 +189,18 @@ - `uri` - The URI of the requested resource. | ||
`read` operations. | ||
- `callback` - Optional callback function using the signature `function (err, response, payload)` where: | ||
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom properties. | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.response` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
- `response` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
Returns a promise that resolves into an object with the following properties: | ||
- `res` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
object, which is a readable stream that has "ended" and contains no more data to read. | ||
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON). | ||
If a callback function is provided then an instance of the node.js [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object is returned. | ||
If no callback function is provided then a Promise is returned that resolves an object with the following structure: `{ req, res, payload }`; | ||
Throws any error that may have occurred during handling of the request or a Boom error object if the response has an error status | ||
code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom | ||
properties: | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.res` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
### `patch(uri, [options])` | ||
### `patch(uri, [options, [callback]])` | ||
Convenience method for PATCH operations. | ||
@@ -236,18 +210,18 @@ - `uri` - The URI of the requested resource. | ||
`read` operations. | ||
- `callback` - Optional callback function using the signature `function (err, response, payload)` where: | ||
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom properties. | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.response` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
- `response` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
Returns a promise that resolves into an object with the following properties: | ||
- `res` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
object, which is a readable stream that has "ended" and contains no more data to read. | ||
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON). | ||
If a callback function is provided then an instance of the node.js [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object is returned. | ||
If no callback function is provided then a Promise is returned that resolves an object with the following structure: `{ req, res, payload }`; | ||
Throws any error that may have occurred during handling of the request or a Boom error object if the response has an error status | ||
code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom | ||
properties: | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.res` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
### `put(uri, [options])` | ||
### `put(uri, [options, [callback]])` | ||
Convenience method for PUT operations. | ||
@@ -257,18 +231,18 @@ - `uri` - The URI of the requested resource. | ||
`read` operations. | ||
- `callback` - Optional callback function using the signature `function (err, response, payload)` where: | ||
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom properties. | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.response` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
- `response` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
Returns a promise that resolves into an object with the following properties: | ||
- `res` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
object, which is a readable stream that has "ended" and contains no more data to read. | ||
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON). | ||
If a callback function is provided then an instance of the node.js [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object is returned. | ||
If no callback function is provided then a Promise is returned that resolves an object with the following structure: `{ req, res, payload }`; | ||
Throws any error that may have occurred during handling of the request or a Boom error object if the response has an error status | ||
code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom | ||
properties: | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.res` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
### `delete(uri, [options])` | ||
### `delete(uri, [options, [callback]])` | ||
Convenience method for DELETE operations. | ||
@@ -278,16 +252,16 @@ - `uri` - The URI of the requested resource. | ||
`read` operations. | ||
- `callback` - Optional callback function using the signature `function (err, response, payload)` where: | ||
- `err` - Any error that may have occurred during handling of the request or a Boom error object if the response has an error status code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom properties. | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.response` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
- `response` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
Returns a promise that resolves into an object with the following properties: | ||
- `res` - The [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
object, which is a readable stream that has "ended" and contains no more data to read. | ||
- `payload` - The payload in the form of a Buffer or (optionally) parsed JavaScript object (JSON). | ||
If a callback function is provided then an instance of the node.js [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object is returned. | ||
If no callback function is provided then a Promise is returned that resolves an object with the following structure: `{ req, res, payload }`; | ||
Throws any error that may have occurred during handling of the request or a Boom error object if the response has an error status | ||
code (i.e. 4xx or 5xx). If the error is a boom error object it will have the following properties in addition to the standard boom | ||
properties: | ||
- `data.isResponseError` - boolean, indicates if the error is a result of an error response status code | ||
- `data.headers` - object containing the response headers | ||
- `data.payload` - the payload in the form of a Buffer or as a parsed object | ||
- `data.res` - the [HTTP Incoming Message](https://nodejs.org/api/http.html#http_class_http_incomingmessage) object | ||
### `toReadableStream(payload, [encoding])` | ||
@@ -353,2 +327,5 @@ | ||
To enable events, use `Wreck.defaults({ events: true })`. Events are available via the | ||
`events` emitter attached to the client returned by `Wreck.defaults()`. | ||
#### `request` | ||
@@ -388,16 +365,1 @@ | ||
processed. | ||
The `EventEmitter` is attached to the `process` object under a `Symbol` with the | ||
value of `'wreck'`. Therefore, if you want to capture a wreck event, after | ||
wreck has been loaded, but in a module that doesn't require wreck, you can | ||
handle events in the following way: | ||
```js | ||
const symbol = Symbol.for('wreck'); | ||
process[symbol].on('response', (err, details) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
}); | ||
``` |
Sorry, the diff of this file is not supported yet
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
39960
496
352