toget
Simple Node.js RESTful API builder for request.
Install
$ npm install toget --save
$ npm install request --save
Note: request 2+ required.
Overview
toget makes available through its API the most used properties of request.
const toget = ; // All request made with `request` instance will have// `http://domain.com` as base URL.let request = ; ;
Request API
Request
is the class in charge to build and execute requests.
const Request = ;
constructor(base)
Creates Request
instance.
base
(String) - protocol+auth+hostname+port to be taken as base for request.
let request = 'http://localhost:3000'; // Set request module for API to work.request;
.get(path, params)
Set request method
, path
and optional params
.
path
(String) - The pathname property consists of the entire path section of the URL.params
(Object) - Possible object to replace values in path.
Note: The same for .post()
, .put()
and .delete()
methods.
request
.query(qs)
Used to specify query string with an Object.
qs
(Object) - Object to be parsed into a URL query string.
request
.headers(values)
Set HTTP request headers.
values
(Object) - HTTP Headers as Object to be append in request.
requestheaders 'Accept-Encoding': 'gzip, deflate'
.json()
Adds Content-type: application/json
header and parses the response body as JSON.
request
.gzip()
Add an Accept-Encoding header to request compressed content encodings from the server and decode supported content encodings in the response.
request
.jar(value)
Define your custom cookie jar.
const url = 'http://localhost';const req = url;const j = reqrequest;j; request
.timeout(value)
Integer containing the number of milliseconds to wait for a server to send response before aborting the request.
request
.body(data)
Entity body for POST and PUT requests.
data
(Buffer|ReadStream|String|Object) - Entity body.
Note: If json is true, then body must be a JSON-serializable object.
requestbody firstName: 'Foo' lastName: 'Bar' age: 25
.encoding(value)
Set encoding code to the response body.
value
(Object) - Represent the enconding code.
Note: If value is undefined
means this is 'utf-8' by default. In the case for null
is used as encoding code,
the body is returned as a Buffer.
requestencoding'utf-8'
.toPromise()
Make request and returns Promise instance to handle response and possible errors.
request;
Note: response
resolved in promise is an instance of Response
.
.toOptions()
Returns request options Object ready to be used in request(options).
request; // { method: 'GET', url: 'http://localhost:3000/user' }
.exec(cb)
Shortcut for request(req.get('/user').toOptions(), cb)
to use as a normal
request call. This method returns request
instance.
cb
(Function) - Callback as specified in request docs; request(options, callback).
request;
Note: The callback here is handled by request so this means response
is
not the response
you would expect returned by a promise but the
response.rawResponse
instead.
Response API
Response
is the class in charge to handle response from request made.
const Response = ;
constructor(rawResponse)
Creates Response
instance.
rawResponse
(Object) - Response Object received in request callback.
let request = rawResponse;
Buffer|ReadStream|String
.body Response body extracted from rawResponse.body
.
responsebody
Object
.headers Response headers extracted from rawResponse.headers
.
responseheaders
Number
.statusCode Response status code extracted from rawResponse.statusCode
.
responsestatusCode
String
.url URL used in request.
responseurl
Method used in request (GET, POST, PUT, DELETE).
String
.method responsemethod
Object
.status Response status code dictionary available as string-boolean as defined in statusCodes.json
responsestatus
Number
.type Response status range type code (1, 2, 3, 4, 5, etc).
responsetype
Boolean
.info Response status code range from 100 to 199.
responseinfo
Boolean
.ok Response status code range from 200 to 299.
responseok
Boolean
.clientError Response status code range from 400 to 499.
responseclientError
Boolean
.serverError Response status code range from 500 to 599.
responseserverError
Error|Object|Boolean
.error Response error, false if no error or Error|Body instance otherwise.
if responseerror throw responseerror;
Test
$ npm test
License
MIT