FETCH Rest Api Wrapper
Fetch rest api wrapper is a promise based API "abstraction" that allows you to easily interface with your backend services. It is build using the fetch library that is available via the Web Api's that can be found here: https://developer.mozilla.org/en-US/docs/Web/API .
FETCH API: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
IE Polyfill: https://github.com/github/fetch
Installation
To install the library you must use the following command npm install fetch-rest-api-wrapper --save
This will now install to your local node_modules folder and in order to require it you must import it like so.
;
If you don't want to have to write Api.GET() you can also destructure the object like so.
;const HttpService GET POST PUT DELETE Path Body DefaultHeaders Adapter } = Api;
Example App
API
-
GET - uses a HTTP Get to the Rest Api Services
@public {return null} -
POST - uses a HTTP Post to the Rest Api Services
@public {return null} -
PUT - uses a HTTP Put to the Rest Api Services
@public {return null} -
DELETE - uses a DELETE Put to the Rest Api Services
@public {return null} -
BaseUrl - this allows us to set base url of our code, this way we can simply write
@@public {return null}; -
DefaultHeaders - this allows us to set default headers on our code, this is set as a class decorator
@@public {return null}; -
Headers- this allows us to set headers for each request we send to the backend.
@ -
Adapter - this allows us to transform our response from the backend. See examples above
@@public {return null;} -
@Path - this allows us to write the following
@public return nullThis will allow us to pass an id into our function in order to replace the {id} with the value of the id parameter
-
@Body - this allows us to write the following
@public {return null}This will allow us to pass in an object that will be sent to the backend as the request body.
-
@Query - this allows us to write the following
@public return nullThis will allow us to set the query parameter of our URL
-
@Header - this allows us to write the following
@public return nullThis will set a custom header on our request
Example
To run the example application follow these steps (github only example is not included within the NPM package)
cd example
npm install
npm start
Coming 2018
- support for bearer token interception
- improved response / request interception
- Option to use observables instead of promises.
Copyright
Copyright 2018 Evan Burbidge
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.