# @dmtkpv/api

> Composition-based HTTP client for Vue

Latest version **0.1.2** (published 2023-11-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dmtkpv/api
pnpm add @dmtkpv/api
yarn add @dmtkpv/api
bun add @dmtkpv/api
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2023-11-18 |
| First published | 2023-07-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 12 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dmitriy Karpov |
| Maintainers | dmtkpv |
| Keywords | vue, api, xhr, http |

## Links

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

## Dependencies (1)

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

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2023-11-18
- 0.1.1 — 2023-11-07
- 0.1.0 — 2023-11-05
- 0.0.7 — 2023-10-23
- 0.0.6 — 2023-10-19
- 0.0.5 — 2023-09-27
- 0.0.4 — 2023-09-27
- 0.0.3 — 2023-09-27
- 0.0.2 — 2023-09-26
- 0.0.1 — 2023-07-18

## README

# @dmtkpv/api
Composition-based HTTP client for Vue
```
npm install @dmtkpv/api
```

## Endpoints

A (deep) object of methods. Method must return a [request config](https://axios-http.com/docs/req_config).
Hooks serve as an additional option:
- onCancel
- onFetch
- onError
- onSuccess
- onComplete

`uncanceled: true` prevents request from aborting when `api.cancel()` called.

Example:

```js
const enpoints = {
    
    auth: {

        login (email, password) {
            return {
                uncanceled: true,
                method: 'POST',
                data: { email, password },
                onSuccess (data) {
                    console.log('login', data)
                }
            }
        }
        
    }    

}
```


## createAPI
Accepts the same options as [`axios.create()`](https://axios-http.com/docs/instance) and `endpoints`

```js
import { createApp } from 'vue'
import { createAPI } from '@dmtkpv/api'
import endpoints from './config/enpoints.js'
import App from './main.vue'

const app = createApp(App);

const api = createAPI({
    baseURL: 'http://example.com',
    endpoints
})

app.use(api);
app.mount('body');
```

## API properties
### `api.pending`
Computed property indicating whether there are pending requests

## API methods
### `api.cancel([key])`

`api.cancel()` - cancels all pending requests  
`api.cancel('login')` - cancels pending requests with key `login`

## Hooks

Hooks can be used on `api` instance:

```js
api.onFetch(() => {})
api.onFetch('login', () => {})
```

On `endpoint` instance:
```js
api('login').onFetch(() => {})
```

Inside `endpoint` configuration:
```js
{
    method: 'GET',
    url: '/',
    onFetch () {}
}
```

Hook callback can return a promise
```js
api.onFetch('login', async () => {
    console.log(endpoint.key) // login
})
```

Hook returns `off` function
```js
const off = onSuccess(() => {});
off();
```



### `api.onFetch([key], function (endpoint) {})`
Executes before the request is sent. 

Examples:
```js
api.onFetch(endpoint => {
    endpoint.config.params ??= {}
    endpoint.config.params.language = 'en'
})

api.onFetch('private', async ({ config }) => {
    const token = await api('token').fetch()
    config.headers ??= {}
    config.headers['Authorization'] = `Bearer ${token}`
})
```

### `api.onError([key], function (error, endpoint) {})`

Example:
```js
api.onError(error => {
    return { ok: true }
})

api.onError(error => {
    throw new Error('')
})

api.onError(error => {
    console.log('error')
})
```

### `api.onSuccess([key], function (data, endpoint) {})`
### `api.onComplete([key], function (endpoint) {})`

### Endpoint
```js
const { 
    data, 
    error,
    pending, 
    promise,
    quiet,
    fetch,
    cancel, 
    onFetch, 
    onError, 
    onSuccess, 
    onComplete,
    onCancel
} = api.endpoint('login');
```

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