Quasar App Extension
API Wrapper
===
An easy to use out of the box API wrapper that use axios in its core
Note: The extension can be used as an npm
package, using: api-client-wrapper
This extension was designed to wrap up the most common features of an api client implementation, in an effort to have a quick and stable tool for each project.
The present features in the extension are:
-
Easy global access: The extension came with a Quasar boot script that put an $api object available at global scope for any Vue component as: this.$api
. You can access the $api
object from inside any Vue component.
-
Base API url: The url that will be used as a root for any call that contains a relative path this.$api.baseURL
. Note: If a call contains a full path (one with http or https protocol) it will be called as is without baseURL concatenation.
-
Homogeneous response: Each request is a promise that resolve with an homogeneous response object: {success:Boolean, attempts:int, data:Object, info:string, error:Error}
. For more details refer to the Response Schema section on this readme.
-
Concurrent requests: All the requests could be made simultaneously. By default the limit is configured to 5 concurrent requests, but it could be changed at this.$api.simultaneousCalls
-
Timeout and retry: In the case of a timeout response a request could be configured to try again with: this.$api.maxAttemptsPerCall
the default value is 1. Each attempt will be executed transparently and it will be just one response so the code for a request with a single attempt will be the same as for a request with multiple attempts.
-
Masive requests: There is out of the box support for bulk calls that allows multiple requests in a single call. for more information refer to the How To use it section on this readme.
-
Vuex integration: The extension came with Vuex store integration for state monitoring. During Quasar boot phase a module is registered to Vuex with the next information:
- Working: Indicating that is at least one request executing
this.$store.state.APIwrapper.working
- Uploading: Indicating that is at least one request executing with a method different from
GET
this.$store.state.APIwrapper.uploading
- Downloading: Indicating that is at least one request executing with a method equal to
GET
this.$store.state.APIwrapper.downloading
- Request Count: Indicates the amount of requests being managed, executing requests + waiting for execution requests
this.$store.state.APIwrapper.request_count
- Execute Count: Indicates the amount of requests being executed in a concurrent manner
this.$store.state.APIwrapper.executing_count
Note: It is possible to have the store configured out of the boot phase using this.$api.setStore(Vuex_instance)
. Refer to the How to use it section.
Install
quasar ext add api-wrapper
Quasar CLI will retrieve it from NPM and install the extension.
Uninstall
quasar ext remove api-wrapper
How to use it
Performing a GET
request
let result = await this.$api.get('path');
this.$api.get('path').then(result=>{});
Performing a POST
request
let result = await this.$api.post('path', {data});
this.$api.post('path', {data}).then(result=>{});
The supported methods are:
this.$api.get(path='', conf = {})
this.$api.post(path='', data = {}, conf = {})
this.$api.patch(path='', data = {}, conf = {})
this.$api.put(path='', data = {}, conf = {})
this.$api.delete(path='', conf = {})
this.$api.call({request configuration})
Note that each method has a final argument that is a custom configuration for the request, this configuration takes precedence over the global configuration. For the supported properties please refer to the Request Configuration section.
Use outside a Vue component
You can use APIWrapper from outside a Vue component, this is common if you want to use it in a store module or inside any other js file Eg. A Quasar bootfile.
import api from "api-client-wrapper"
api.get(path='', conf = {})
api.post(path='', data = {}, conf = {})
api.patch(path='', data = {}, conf = {})
api.put(path='', data = {}, conf = {})
api.delete(path='', conf = {})
Important Note: You are still referencing the same object that you use inside any Vue component, so if you make any configuration it will affect any other place where you were using it, and the same goes in viceversa, any previous configuration made will take effect.
Bulk calls
Each method has a bulk counterpart that allows for bulk calls
let result = await this.$api.bulkGet(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await this.$api.bulkPost(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await this.$api.bulkPatch(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await this.$api.bulkPut(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await this.$api.bulkDelete(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await this.$api.bulkCall([{configs}], continueWithFailure:Boolean, onProgress)
Params:
- [paths or configs]:[]-> An array containing the paths for each request or an array of request configuration objects (described next).
- continueWithFailure:Boolean-> Optional with the default to false.
false
The request will be considered failed when any of it subrequests fail and it will stop any further execution (there could be some sub-requests that never get executed if some other request failed before). true
All the requests are executed, it doesn´t matter if some of them failed. - onProgress:Function(progress:Number)-> Is an optional callback that receives progress between [0-1], the progress is computed with the next formula: (request-completed / total-amount-of-request-in-the-bulkCall)
Request Configuration
A configuration object for each request, which have precedence over any global configuration (so it is a way to override any global configuration for special scenarios) It has the next scheme:
{
url:String,
method:String,
params:{},
data:{},
alias:String,
attempts:1,
timeout:10000
...
}
It is possible to add any property supported by axios (like headers or encoding).
Response Scheme
For any individual request the response schema is:
{
alias:String,
success:Boolean,
attempts:0,
data:{}
info:"",
error:null,
...
}
In the case of a bulk call the response data is an Array containing the response for each request (in the same order that the requests where passed)
let result = await anyBulkCall([{configs}]
result.data.forEach(response=>{
console.log (response)
})
let result = await anyBulkCall([{alias:'a',...},{alias:'b',...}]
console.log(result.data.a)
console.log(result.data.b)
Global Configuration
Configuration of the overall behaviour for the extension.
Note: The configuration could be made outside of a Vue component: See the: Use outside a Vue component
this.$api.baseURL = '';
this.$api.maxAttemptsPerCall = 1;
this.$api.simultaneousCalls = 5;
this.$api.timeout = 10000;
this.$api.setContentType('application/json')
this.$api.setAuthorization('token', 'Bearer')
this.$api.setStore(vuex_instance)