# vue-api-eventbus

> global event bus and axios instances

Latest version **1.0.2** (published 2019-11-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-api-eventbus
pnpm add vue-api-eventbus
yarn add vue-api-eventbus
bun add vue-api-eventbus
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-11-26 |
| First published | 2019-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 20.4 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | d.smirnov |
| Maintainers | d.smirnov |
| Keywords | vue, event, eventbus, axios, interceptor |

## Links

- npm: https://www.npmjs.com/package/vue-api-eventbus
- Repository: https://github.com/Dimas555/vue-api-eventbus
- Homepage: https://github.com/Dimas555/vue-api-eventbus#readme
- Issues: https://github.com/Dimas555/vue-api-eventbus/issues
- npm.io page: https://npm.io/package/vue-api-eventbus

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.19.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-11-26
- 1.0.1 — 2019-11-26
- 1.0.0 — 2019-11-26

## README

# vue-api-eventbus

### Add global axios instance `$api` and event bus `$eventBus` 

#### Installation
````
npm install vue-api-eventbus
````

##### main.js
````
import VueApiEventbus from 'vue-api-eventbus';

Vue.use(VueApiEventbus[, options]);
````

##### options (default)
`xhr` axios configuration  
`events` event list for axios interceptors

````
options = {
  xhr: {
    headers: {
      'X-Requested-With': 'XMLHttpRequest',
    },
    baseURL: '/',
  },
  events: {
    beforeRequest: 'before-request',
    requestError: 'request-error',
    afterResponse: 'after-response',
    responseError: 'response-error',
    poolCreate: 'pool-create',
    poolRemove: 'pool-remove',
    poolPush: 'pool-push',
    poolPop: 'pool-pop',
  }
}
````
Eventa are triggered:

`beforeRequest`: before request create  
`requestError`: on request error  
`afterResponse`: after response  
`responseError`: on response error  
`poolCreate`: generated request pool  
`poolRemove`: request pool is empty  
`poolPush`: add new request to the pool  
`poolPop`: complete request and remove from the pool  

Used as
`this.$eventBus.$on('event-name', handler);`

#### Usage

##### Create request
````
this.$api.get('URL').then((resp) => {})
````

##### Listen request lifecycle
````
this.$eventBus.$on('before-request', handler);
this.$eventBus.$off('before-request', handler);
````

##### Custom events
````
this.$eventBus.$emit('event-name'[, payload]);
this.$eventBus.$on('event-name', handler);
this.$eventBus.$off('event-name', handler);
````

---
_Source: https://npm.io/package/vue-api-eventbus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
