Get support for luin/superfetch
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
luin/superfetch
SuperFetch
SuperFetch is a promise-based HTTP client for Node.js. It's based on the request library and has an elegant interface.
Install
$ npm install superfetch
Super easy to use
var fetch = require('superfetch');
fetch.get('http://example.com').then(function (body) {
// when the status code begins with "2", the response body will be returned.
}).catch(function (response) {
// otherwise, the whole response(including headers) is returned.
});
// It's easy to pass the option to the request library
fetch.get.option({ encoding: 'utf8' })('http://example.com').then(//...
// Chain!
var base = fetch.post.option({ baseSetting: 'foo' }).option({ otherSetting: 'bar' });
base('http://example1.com', { postbody: {} }).then(//...
base.option({ thirdSetting: 'zoo' })('http://example2.com');
API
-
fetch[http verb]
will return a VerbInstance. -
VerbInstance.option(settingObj)
orVerbInstance.option(key, value)
will return a new VerbInstance with the specified settings setted. All options will be passed to the request library except thetransform
which will be introduced later. -
VerbInstance.header(headerObj)
orVerbInstance.header(key, value)
is an alias forVerbInstance.option('headers', headerObj)
. -
VerbInstance.auth(user, pass)
is an alias forVerbInstance.option('auth', { user: user, pass: pass })
. -
VerbInstance(url[, body])
will send a request, and the optionallybody
sets thejson
property of the request library. -
fetch.defaults(defaultOptions)
will create a new fetch instance with a different default options:
var d = fetch.defaults({ encoding: 'utf8' });
d.post('http://example.com').then(//
Transform
Both request and response can be transformed.
fetch.option('transform', {
request: function (options) {
options.method = 'post';
return options;
},
response: function (err, resp, body) {
if (err) {
throw err;
}
return resp;
}
}).then(function (resp) {
});
Run tests
$ npm test
More examples can refer to the test
directory.
Why build another HTTP client?
I'm aware that there are many Node.js HTTP client libraries such as request, superagent and axios. However, I find out none of these libraries exactly suit my needs. What I want is a library that is powerful, supports promise and has an elegant API, while the fact is request lib is powerful but doesn't support promise, superagent has an elegant API but doesn't as powerful as request lib, and axios lib supports promise but lacks many features I want. So here is SuperFetch.
Articles
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2