FetchREST(ful)
🚀 A wrapper around fetch for REST API dependent projects.
Installation
yarn add fetch-restful
Usage
Constructor
;
GlobalRequestOptions
- request options that will be used as the defaults for every outgoing request.GlobalRequestOptionsGetter
- a method that returns aGlobalRequestOptions
object.
Request methods.
await request.get'/users', QueryObject, RequestOptions;await request.post'/users', Payload, RequestOptions;await request.patch'/users', Payload, RequestOptions;await request.put'/users', Payload, RequestOptions;await request.delete'/users', Payload, RequestOptions;
QueryObject
- object with query parameters to use.Payload
- your request payload.RequestOptions
- request options that will be merged with your global request options.
Middleware
Use the middleware method to define a function that will be added to the promise chain for all outgoing requests.
request.middlewareMiddleware;
Middleware
- method that will be added to the promise chain.
Examples
GET
.
Basic ; await request.get'/users/kvendrik';
GET
with query.
Basic ; await fetchRest.get'/users', ;
Using an options getter.
; await fetchRest.get'/users/kvendrik';
Setting global headers.
; await request.get'/users/kvendrik';
Locally adding and/or overriding headers.
; await request.get '/users/kvendrik', , ,;
Adding data to all responses.
; fetchRest.middlewarerequest.then,; await fetchRest.get'/users/kvendrik';
Global and local error handling (resolved).
; fetchRest.middlewarerequest.catch,; await fetchRest.get'/users/kvendrik';
Global and local error handling (unresolved).
; fetchRest.middlewarerequest.catch,; fetchRest .get'/users/kvendrik' .then .catch;
Cancelling a request.
; ;fetchRest.get'/users', , ;fetchRest.abortabortToken;
Request timeout.
; fetchRest.get'/users', , ;
Working with multiple APIs.
; ; await githubApi.get'/users';await appApi.get'/users';
🏗 Contributing
- Make your changes.
- Add/Alter the appropriate tests.
- Make sure all tests pass (
yarn lint && yarn test
). - Create a PR.