javascript wrapper function for http.request
and https.request
that returns parsed, transformed data in callback.
This probject is archived - the features here can be achieved easily enough with something like node-fetch and parsing the response.
-
url
The URL to access
-
options
Optional. An object specifying the options for the request, parsing and transformation.
-
accept
: Accept request header (string
). Defaults totext/plain
- "text" or "plain" are changed to
text/plain
- "json" is changed to
application/json
- "xml" is changed to
application/xml
- "text" or "plain" are changed to
-
parse
:function
used to parse the data returned from http(s) request. Default returns the dataBuffer
as-is in the callbackGiven 2 parameters:
buffer
: TheBuffer
that holds the data returned from the http(s) requestcallback
: Callbackfunction
taking two paramers (err
,data
)
If
parse
is a string, one of the internal parsers are returned:- "json": wraps
JSON.parse
- "raw": returns the data data as-is
-
transform
:function
used to transform the parsed data. Default returns the data as-is is the callback. Given 2 parameters:data
: The data returned from the parse functioncallback
: Callbackfunction
taking two paramers (err
,data
)
-
protocol
:string
to specify if http or https should be used. Default depends on protocol returned byurl.parse
-
http
:function
used forhttp.request
. Used for mocking -
https
:function
used forhttps.request
. Used for mocking
-
-
callback
The callback function called when an error has occured or when the data was received, parsed and transformed
Parameters:-
err
:null
when no error has occured or an error details object with the keys:error
: The actual error that was caughtaction
: The action where the error occured (request, response, parse, transform)length
: The value of thecontent-length
headerreceived
: The number of bytes received so farbuffer
: TheBuffer
object that is used to hold the data returned from the http(s) responseparsed
: The parsed data
If the
content-length
response header is not present,length
will be-1
(while transfering) or the number of bytesreceived
when theend
event is fired -
data
: The transformed data.undefined
if an error has occured.
-
Class to build http/https mocks
-
options:
- statusCode:
number
to use as the status code for the request. Defaults to200
- data:
text
to return for the request. Defaults to empty string
- statusCode:
Wrapper for http.request
and https.request
. Returns a new mock.RequestMock
setup to call callback
on end
with a new mock.ResponseMock
- requestCallback: The callback that will get the response object
- response: The response object /
mock.ResponseMock
to return onend
- dataToReturn: The data to return
- rules:
object
where keys are the urls, and values are functions (taking a singlecallback
function parameter) that is used to match incomming requests to the mock
-
defaultify
Handles options setup for get defaultify on npm