Vue Chimera
VueJS RESTful client with reactive features. Vue-Chimera is based on axios http client library.
Overview of features:
- Reactive endpoints and requests based on vue instance data
- Loading flags
- Get time when the resource was latestly loaded
- Set interval time to refresh data on an endpoint
- Simple robust resource definition
- Nuxtjs compatible with Server side prefetching
- Simple cancelation on requests
- Cancel all pending requests on vue instance destroy
- Events and handlings
- Lightweight
Installing
Using npm:
Using bower:
Using cdn:
Getting started
To add vue-chimera to your Vue you must use it as a plugin:
ECMAScript 6
If you use old browser style just add vue.min.js
and dist/vue-chimera.js
script tag to your HTML file and everything would be fine to go on.
Defining Resources
To use Vue Chimera in your vue instance you should add chimera
property to root of your vue component instance:
Vue Chimera automatically converts your resources
to Resource Object
Your resources can be:
- A simple string for simple GET requests
- An Object for complex resources like: POST, PATCH, with Parameters, with Headers, Response/Error transformer, Event listeners
- An instance of Resource
- A Function for reactive resources Reactive-Resources
Now it's time to use our resources in our vue template or inside vue methods.
Use Resources in template
chimera
instance can be accessed in templates with $chimera
(Dollar sign prepended) or you can get resources by simply type it's name, It had been injected in computed properties by the plugin.
data
is the final json result of our restful resource
loading
is a boolean flag, identifies the resource is loading.
You can read other resource property and methods here.
Use Resources in script
You can simply access chimera
instance with $chimera
Resource properties and methods
Property | Type | Default Value | Description |
---|---|---|---|
data | Object/ Array | null | The resource response object or string returned from server when request is successfull |
loading | Boolean | false | Indentifies the resource is in loading state |
error | Object/string | null | Error json object or string returned from server when request failed |
lastLoaded | Date | null | The date/time from last time resource successfully loaded (null if not loaded yet) |
status | number | null | Resource response status |
headers | Object | null | Resource response/error headers |
Method | Return type | Description |
---|---|---|
reload(force) | Promise | Fetches the resource from server. force : True for cache busting |
execute(force) | Promise | Same as Reload |
on(event, handler) | Sets an event listener. Events | |
cancel() | void | Interupts request |
Reactive Resources
You can also set a function to a resource that will return String, Object, instance of Resource same as before,
to let your resources be reactive and change.
Note that prefetch
evaluates to false, your data will be reactive and change but won't be fetched until you call the reload
function of the resource
Chimera instance properties
Transformers
Transformers is used to change the response to another format. It would be called before the request response (error or success) is mapped to the data
attribute of Resource object.
Events
Using with Nuxt.js
You can use Vue-Chimera with nuxtjs to use it's SSR features so you can easily prefetch the data.
You can also disable SSR for some heavy resources