
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
fetch-http-wrapper
Advanced tools
A Simple, Intuitive and expressive http client built on top of fetch
A Simple, Intuitive and expressive http client built on top of fetch
✅ It's lightweight and depends on fetch 😍
👌 It has an intuitive and expressive api 🎉
👊 It depends on middleware pattern to extends functionality.
✅ It was built with and supports TypeScript 💪
😅 It still in beta
for me axios is a very powerful package to make different kinds of http requests. but, when it's used a lot the code turns to be messy because of the options object. So I build this package to provide an elegant, expressive and intuitive wrapper around fetch to keep code simple and orgnized when working with http apis.
all what you have to do is run the following command
npm i fetch-http-wrapper
You can instantiate new request by using the request
method or any of other related methods. All of the following methods will return an instance of the class FetchRequest
{method}
and {url}
const request = Fetch.request({method}, {url});
get
request with {url}
;const request = Fetch.get({url});
post
request with {url}
;const request = Fetch.post({url});
put
request with {url}
;const request = Fetch.put({url});
patch
request with {url}
;const request = Fetch.patch({url});
head
request with {url}
;const request = Fetch.head({url});
delete
request with {url}
;const request = Fetch.delete({url});
The FetchRequest
class has the following methods to help you build the request. All of the following methods will return the instance.
request.withParams({
key: 'value',
key2: 'value2'
})
request.withHeaders({
'Content-Type': 'application/json'
})
request.withBody({
key: 'value',
})
request.credentials('omit')
request.mode('cors')
request.cache('no-cache');
request.redirect('follow');
request.referrer('');
request.integrity('')
use call
method to send http request after being configured, It returns a promisy
request.call()
.then(response => {})
Fetch.get('http://mohammedmanssour.me')
.withHeaders({
'Authorization' : '{token}'
})
.withParams({
key: 'value'
})
.mode('no-cors')
.call()
.then(response => {});
before middlewares is used to manipulate request config before sending the http request.
to add a before middleware to Fetch.before()
method and make sure to return options.
use options.clone()
to get a new cloned object of the original options
Fetch.before(options => {
const newOptions = options.clone();
// do your thing here
return newOptions;
})
the option
paramaeter is an instance of FetchOptions
class that has the following attributes
url: string;
method: string;
headers: { [key: string]: string } = {};
mode?: string;
body?: any = {};
query?: QueryObject = {};
credentials?: string;
cache?: string;
redirect?: string;
referrer?: string;
integrity?: string;
in the following example we will use a middleware to add a base url to the provided path by the request.
Fetch.before(options => {
const newOptions = options.clone();
newOptions.url = `http://mohammedmanssour.me/api/${newOptions.url}`
return newOptions;
})
After middleware is used to manipulate the response when the call is done, use Fetch.after()
to add an after middleware. It doesn't matter what you return from your after middlewares but make sure to wrap it in a Promise
and keep it consistent.
After middlewares takes two arguments:
request
: the request that was sent.response
: the request response, an instance of Fetch Response
Fetch.after(
(request, response) =>
response.json()
.then(data => Promise.resolve(data))
You can reach the available middlewares by using the Fetch.middlewares
property
json()
it's a before
middleware used to add the right headers to the request and to strigify body.
jsonResponse()
it's an after
middleware used to convert the response to json response and returns a promise that has the data and the response.
return Promise.resolve({data, response});
query()
it's an before
middleware used to convert the options query params from an object to a stringFetch
.before(Fetch.middlewares.query)
.get('http://mohammedmanssour.me')
.withParams({
key: 'value',
key2: 'value2'
})
//will become
'http://mohammedmanssour.me?key=value&key2=value'
FAQs
A Simple, Intuitive and expressive http client built on top of fetch
We found that fetch-http-wrapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.