rail Plugins
Buffers the response body and exports it as response.buffer
.
When the body is empty its value is null
, otherwise a Buffer
.
options
{boolean} default
Enable buffering for all requests, defaults tofalse
{number} max
The maximum buffer size, defaults to134217728
(128 MiB)
Note: When the maximum buffer size is reached, a bailout is performed putting all buffered data back into the response stream and emitting the response. Accordingly, the response object will receive the property bailout
set to true
as a state indicator.
request options
{boolean} buffer
En-/disable buffering
Manually intercept (buffer the response body). To be used by other plugins.
Get/Set cookies. Received cookies are exported as response.cookies
.
options
{Object} jar
The cookie jar to use, defaults to{}
request options
{boolean} cookies
Whenfalse
, the plugin is disabled for this call
Parse response body. Parsed body is exported as response.json
.
Uses the buffer
plugin.
options
{boolean} auto
Enable auto-parsing whenContent-Type: application/json
{number} max
The maximum JSON size, defaults to1048576
(1 MiB)
Note: When the JSON size exceeds the maximum size, it's not parsed. The response.buffer
is still available for manual parsing, though.
request options
{boolean} json
Enable JSON parsing
Manually intercept (buffer the response body & try to parse). To be used by other plugins.
A configurable redirect mechanism.
options
{Array} codes
HTTP status codes to redirect, defaults to[301, 302, 308]
{number} limit
The maximum number of redirects, defaults to1
{boolean} sameHost
Only allow redirects to the current host, defaults tofalse
{boolean} allowUpgrade
Allow switch fromhttp
tohttps/2
, defaults totrue
{boolean} allowDowngrade
Allow switch fromhttps/2
tohttp
, defaults tofalse
request options
{Object|boolean} redirect
Set tofalse
to disable the plugin{number} limit
Seeoptions
{boolean} sameHost
Seeoptions
{boolean} allowUpgrade
Seeoptions
{boolean} allowDowngrade
Seeoptions
Note: Undefined request options are set to plugin defaults.
Emitted when request.end()
of a redirected request has been called.
function({Object} options, {Object} response)
Where options
is the Configuration
object for the redirected request and response
is the original response object containing the Location
header.
Conditional request retry.
Retry requests on connect errors, on specific http status codes and failed response validation.
options
{Array|boolean} codes
HTTP status codes to retry, set tofalse
to disable. Defaults to[500, 501, 502, 503, 504]
{number} interval
Retry interval in ms, defaults to2000
{number} limit
Retry limit, defaults to0
{boolean} validate
Retry whenresponse.validate
is set aka. the validate plugin is not satisfied
request options
{Object|boolean} retry
Set tofalse
to disable the plugin{Array|boolean} codes
HTTP status codes to retry, set tofalse
to disable. Plugin defaults will be added when an array is passed.{number} interval
Seeoptions
{number} limit
Seeoptions
{boolean} validate
Seeoptions
Note: Undefined request options are set to plugin defaults.
Emitted when a retry has been scheduled.
function({Object} options, {?Object} response, {string} reason, {?string} code)
Possible reasons are connect
, codes
or validate
and for reason connect
the response
is null
.
Example: modify request options
var hosts = [
'ww1.example.com',
'ww2.example.com',
'ww3.example.com'
];
call.on('retry', function(options, response, reason) {
if (hosts.length > 0) {
options.request.host = hosts.shift();
} else {
call.abort();
}
});
Response & socket timeout detection.
options
{number} response
The response timeout in ms, defaults to60000
(1 min){number} socket
The socket idle timeout in ms, defaults to120000
(2 min)
Note: The socket idle timeout is only supported for https & http.
request options
{Object} timeout
{number} response
Set to0
to disable the timeout, also seeoptions
{number} socket
Set to0
to disable the timeout, also seeoptions
Note: Undefined request options are set to plugin defaults.
Emitted on the call
object when a timeout occurs. It is up to the user to abort the call.
function({string} type, {Object} options)
Where type
is either 'response'
or 'socket'
, and options
is the request configuration.
Response header & JSON body validation.
Schema definitions & validation are provided by mgl-validate.
- Body validation only supports JSON responses
- Every schema requires a unique
id
- Consider setting
allowUnknownProperties = true
when validating headers
Uses the buffer
& json
plugin.
options
{Array.<Object>} schemas
An array of schema definitions{boolean} breakOnError
Whether to return on first validation error or not, defaults totrue
request options
{Object} validate
{Object|string} headers
An existing schema id or a schema definition{Object|string} body
An existing schema id or a schema definition
Validation results are exported as response.validate = null
when all validations passed, and an object when a validation failed;
response.validate = {
headers: null,
body: [
['hello', 'number', 'type', 'world']
]
};
Headers Schema Definition Example
This example shows how-to validate that the Content-Type
header equals 'application/json'
.
{
type: 'object',
properties: {
'content-type': 'application/json'
},
allowUnknownProperties: true
}
The mgl-validate schema registry.