reazi
v1.0.3-beta.2
Published
HTTP requests made easy using reazi.
Downloads
4
Maintainers
Readme
🚀 Reazi
Reazi - Requests made Easy, is a light-weight promise bases HTTP client library. reazi is made for javascript be it client or server side technologies (Isomorphic). It internally uses fetch API to make API calls over the network.
🌐 Features
Promise based API requests.
Uses Fetch API, which is modular and supported by native Javascript
Interceptors to handle requests and response
Global defaults to reduce bloat code
Timeouts (Global Timeout and request level Timeout options are available)
NOTE : Request level timeouts are given priority when both global timeout and request level timeout present
Automatic parsing of multiple response formats. (JSON, Text, Blob, ArrayBuffer). Special handling can be registered via hooks.
❗Methods Available
API Calls ✨ ✨
reazi.request instance can be used to make API calls to server/url using reazi.
Instance
- Instance() takes two parameters URL and headers.
- Instance can be create a request object and can be used to fire multiple API calls with same endpoint.
- Example
const instance = reazi.request.instance(URL, { "header": //method name, // other request headers }) instance.get() instance.post("", { "body" : //Data }) /* can fire multiple calls */
get()
- Get() takes two parameters URL and headers.
- Result will be Standard JSON Format, Developer will have full control over it.
- Example
reazi.request.get(URL, { // other request headers }, (true|false))
post()
- Post() takes three parameters URL, body and headers.
- body should be JSON object. Form Data isn't supported as of now. Will include support in near future.
- Result will be Standard JSON Format, Developer will have full control over it.
- Example
reazi.request.post(URL, { "data" : //data }, { // request headers }, (true|false))
put()
- Put() takes three parameters URL, body and headers.
- body should be JSON object. Form Data isn't supported as of now. Will include support in near future.
- Result will be Standard JSON Format, Developer will have full control over it.
- Example
reazi.request.put(URL, { "data" : //data }, { // request headers }, (true|false))
patch()
- Patch() takes three parameters URL, body and headers.
- body should be JSON object. Form Data isn't supported as of now. Will include support in near future.
- Result will be Standard JSON Format, Developer will have full control over it.
- Example
reazi.request.patch(URL, { "data" : //data }, { // request headers }, (true|false))
delete()
- Delete() takes two parameters URL and headers.
- Result will be Standard JSON Format, Developer will have full control over it.
- Example
reazi.request.delete(URL, { // other request headers }, (true|false))
all([])
all([], (true | false)) method is used to execute a series of API calls.
Unlike default Promise.all() where all the requests are executed even if any one of it fails, We will abort all the active requests which helps to improve the performance and reduce server overhead.
Example
reazi.request.all([ reazi.request.get(URL, { // request headers }, true), reazi.request.get(URL, { // request headers }, true) ])
Note1 : While making concurrent API calls it is mandatory to specify whether to execute remaining requests incase any one fails. By default it is set to false. You can override it passing second param.
Note2 : It is also mandatory to send isConcurrent param to let reazi know that current request is a concurrrent request. Because we store abort data in a Map, In any circumstances if it's misplaced it'll cause bloating unnecessary data. You can remove the same usingreazi.erase.abortMap()
.
File Upload ✨ ✨
upload() - POST
- upload() takes three parameters URL, body and headers.
- Result will be Standard JSON Format, Developer will have full control over it or can also add responseType in header list so the response will be transformed that way.
- Example
reazi.request.file(URL, body, { // request headers }, (true|false)).upload()
update() - PUT
- update() takes three parameters URL, body and headers.
- Result will be Standard JSON Format, Developer will have full control over it or can also add responseType in header list so the response will be transformed that way.
- Example
reazi.request.file(URL, body, { // request headers }, (true|false)).update()
download() - GET
- download() takes two parameters URL, and headers.
- Result will be Standard JSON Format, Developer will have full control over it or can also add responseType in header list so the response will be transformed that way.
- Body should be empty (2nd paramter) for download request.
- Example
reazi.request.file(URL, {}, { // request headers }, (true|false)).download()
Registering Globals ✨ ✨
headers()
- reazi.register.headers() method can be used to add defualt headers which will be passed to all the requests fired using reazi.
reazi.register.headers({ 'header name' : 'header value', // other headers })
- reazi.register.headers() method can be used to add defualt headers which will be passed to all the requests fired using reazi.
domain()
- reazi.register.domain() method can be used to add defualt domain which will be appended to all the requests fired using reazi.
reazi.register.domain(URL)
- reazi.register.domain() method can be used to add defualt domain which will be appended to all the requests fired using reazi.
registerAbortController()
- registerAbortController() method can be used to add global timeout for requests fired using reazi.
- This method requires
abortTime
param to be passed along.
reazi.register.abortController('{{Time in ms}}')
postRequest()
- reazi.interceptor.postRequest() method is an interceptor which can be used to override the default response handling of reazi as per user expects it to be.
This hook can modify only the response returned by the server. Unknown Error handling will not be modified such as aborting request.
reazi.interceptor.postRequest((response, success, failure) => { if (!response.ok) { // Failure Callback failure(response); //send data between parenthesis } else { // Success Callback success(response); //send data between parenthesis } })
preRequest()
- reazi.interceptor.preRequest() method is an interceptor which can be used to modify the request before making the call.
- We will be using native
Request
object. Refer the following article on how to modifyRequest
.
reazi.interceptor.preRequest((request) => { const newRequest = new Request( {modifiedChanges}, // Add changes need to be done before firing request. request ); return newRequest })
Removing Globals ✨ ✨
headers()
- reazi.erase.headers() method can be used to remove any defualt headers present in globalHeaders object.
reazi.erase.headers()
- reazi.erase.headers() method can be used to remove any defualt headers present in globalHeaders object.
domain()
- reazi.erase.domain() method can be used to remove defualt domain added previously.
reazi.erase.domain()
- reazi.erase.domain() method can be used to remove defualt domain added previously.
postRequest()
- reazi.erase.postRequest() method is used to remove the post response hook interceptor and sets back to default response handling.
reazi.erase.postRequest()
- reazi.erase.postRequest() method is used to remove the post response hook interceptor and sets back to default response handling.
preRequest()
- reazi.erase.preRequest() method is used to remove the pre request hook interceptor and sets back to default request handling.
reazi.erase.preRequest()
- reazi.erase.preRequest() method is used to remove the pre request hook interceptor and sets back to default request handling.
abortController()
- reazi.erase.removeAbortController() method will remove global timeout for requests fired using reazi. Timeout still can be used in request level.
reazi.erase.abortController()
- reazi.erase.removeAbortController() method will remove global timeout for requests fired using reazi. Timeout still can be used in request level.
For Vanilla JS, You can include the following script which contains the cdn to use reazi
<script src="https://cdn.jsdelivr.net/gh/jquery/[email protected]/dist/jquery.min.js"></script>