Vue http plugin
Description
A lightweight http plugin for vue, defaults to rest, with promises.
Usage
Install in your VueJS project:
npm i -s tb-vue-http
Import in main.js:
Create a server config:
let apiConfig = // will be available in all components with this.$api.get( ... ), this.$api.post( ... ) name: "api" // the prefix for all requests url: "http://mydomain.com/api" headers: // a header for all request "api-key": "1x2y3zasd456fgh789" // sometimes not known on start - will be evaluated before every request localStorage /* optional - define as many as you want will be available with (for example) this.$google.get( ... ) */ name: "google" url: "http://google.com" headers:
And install the plugin with it:
Vue
And you are good to go!
Without headers:
- the response is already parsed (not the JSON string)
- the payload of post is serialized
- and has the 'Content-type': 'application/json' header
methods: { this$api } { this$api } { this$api } { this$api }
Or with them:
- ONLY the default headers are applied
- No parsing/serializing anywhere
methods: { let headers = 'Content-type': 'application/xml' 'My-custom-header-only-for-this-request': 'whatever' let payload = this$api }
You can use the low level api:
- Not even domain headers are added
- No parsing, serializing anywhere
- Do whatever you want with the request/response
methods: { let headers = 'Content-type': 'application/xml' 'My-custom-header-only-for-this-request': 'whatever' let payload = this$api }