Ladybug Fetch
A Promise based HTTP(S) client library for NodeJS
Features
- Lightweight (no dependencies)
- Easy to use
- Full Promises support
- Chainable
- Auto handle compressed responses (gzip, deflate)
- Auto parse response body (json)
- Auto detect and set content type
- Instances for reducing duplicate calls with options
- Easy plugin support
- Lot of function overloads making it possible to use your style
- Use a custom promise library
Install
Note It is still in early development, examples in this README may or may not work or is not ready, unstable or untested and the code design may change overtime if you would like to contribute Pull Requests and/or Issues are welcome.
npm install ladybug-fetch
Or the development version from the github repository
npm install freetnt5852/ladybug-fetch
You will need git
installed and added to PATH
for github installs
Examples
Basic Get (json)
const ladybug = require; ladybug .
Headers
ladybug .set .set .set .set // helper function .then .catch
Querystrings
ladybug .query .query .query .then((r) => console.log(r.body)) .catch((e) => console.error(e));
Post Requests
ladybug ;
Similar way is possible with other methods ladybug.[get|post|put|patch|delete]
with get being default if no method specified
Plugins
const ladybug = ; { // do anything with the request object req;} ;
If you want options for your plugin export a function that returns a function
const ladybug = ; { return { ifoptionssomething req; }}
Global plugins
Plugins are per request by req.use
however you can apply it globally for every request
const ladybug = ; { req;} ladybug; ;
Async/Await
async const res = await ; console; ;
Instances
Instances are powerful way to do many requests with lot of options instead of duplicating code
const ladybug = ; const api = ladybug; // POST -> https://baseurl.com/endpoint?limit=5 ;
Using a custom promise library
You can plug in a custom promise library easily for one or more requests
The first method is incase you are overriding the global Promise
then nothing else is needed
globalPromise = ;// continue as normal
But if you want to use it without changing the global promise there are ways
For per requests use .promise()
; ;
The second way is to use it on an instance this makes it apply for every request on that instance only
const api = ladybug ;// or also pass it in the options object. ;
The third way is overriding the promise library for the global instance
const ladybug = ;ladybug; ;// All requests using global instance now uses that promise library// This doesn't affect any other instances.
Status Codes
By default the library rejects the promise on 4xx
and 5xx
Errors but it is possible to override that behavior and validate status yourself using req.status()
status s < 500 // Anything below 500 is a success ;
As always all chainable methods are also available on instances to reduce duplications or even override the global instance
const ladybug = ; ladybugstatus s < 500; ; const api = ladybug status s < 500; ;
TODO
This library is still under early development and is not complete, Pull Requests are welcome if you want to contribute.
-
Add support for following redirect codes.(kind of experimental though) -
Form Data support - A good test suite.
-
TypeScript typings(Typings contributed by @IceeMC and fixed by me, may still have some issues.) - Streams support? e.g
req.pipe(fs.createWriteStream("./out.txt"));
See also comments in code with TODO