# galaxy-axios-adapter

> ```bash npm i axios-adapters -S ```

Latest version **0.0.1** (published 2023-03-29) · LSC license · 0 weekly downloads

## Install

```sh
npm install galaxy-axios-adapter
pnpm add galaxy-axios-adapter
yarn add galaxy-axios-adapter
bun add galaxy-axios-adapter
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2023-03-29 |
| First published | 2023-03-29 |
| Weekly downloads | 0 |
| License | LSC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 19.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | xngbb |

## Links

- npm: https://www.npmjs.com/package/galaxy-axios-adapter
- npm.io page: https://npm.io/package/galaxy-axios-adapter

## Dependencies (4)

- [tslib](https://npm.io/package/tslib.md) ^2.5.0
- [@types/node](https://npm.io/package/@types/node.md) ^18.15.11
- [await-to-js](https://npm.io/package/await-to-js.md) ^2.1.1
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^3.1.0

## Recent versions

- 0.0.1 (latest) — 2023-03-29

## README

# axios-adapters

## Install

```bash
npm i axios-adapters -S
```

or

```bash
yarn add axios-adapters
```

## Usage

```javascript
import axios from 'axios';
import { compose, defaultRequest, RESTful, pagination, preventDefault } from 'axios-adapters';

const http = axios.create({
    baseURL: '/',
    adapter: compose(preventDefault(), RESTful(), pagination(), defaultRequest())
});
```

## API

### compose

> 组装 axios middleware adapter

```typescript
compose(...adapters: AxiosMiddlewareAdapter[]): AxiosAdapter
```

#### Axios middleware adapter

```javascript
function customMiddleware(options) {
    return async function customMiddlewareAdapter(config, next) {
        // do something before downstream
        // ...
        const response = await next();
        // do something after upstream
        // ...
        return response;
    };
}
```

### defaultRequest

> 封装了 axios 默认的请求 adapter

发起真实请求的 axios middleware，正常情况下作为 compose 最后一个参数。

```javascript
axios.create({
    adapter: compose(defaultRequest())
});
```

### RESTful

> 支持 Express like style 请求

```javascript
const http = axios.create({
    adapter: compose(RESTful(), defaultRequest())
});

// GET /foo/baz
http.get('/foo/:bar', {
    params: {
        bar: 'baz'
    }
});

// POST /foo/baz
http.post('/foo/:bar', {
    bar: 'baz'
});
```

### pagination

> `X-Pagination` Header

若请求响应中存在 `X-Pagination` 头部，则解析并保存到 `response.pagination` 中。

#### Axios response

| Param      | Type         | Default Value | Description |
| ---------- | ------------ | ------------- | ----------- |
| pagination | `Pagination` |               | 分页类实例  |

```javascript
const http = axios.create({
    adapter: compose(pagination(), defaultRequest())
});

/**
 * ...
 * < HTTP/1.1 200 OK
 * < Content-Type: application/json; charset=utf-8
 * < X-Pagination: {"size":10,"num":1,"total":21}
 * < Content-Length: 15
 * < Date: Wed, 18 Sep 2019 03:53:38 GMT
 * < Connection: keep-alive
 * ...
 **/
http.get('/foo').then(response => {
    // do something with `response.pagination`
    // ...
});
```

### preventDefault

> `e.preventDefault()`

#### Options

| Param   | Type                          | Default value | Description  |
| ------- | ----------------------------- | ------------- | ------------ |
| onError | `(error: AxiosError) => void` |               | 请求错误处理 |

```javascript
cont onError = (e) => {
    console.log('error occured:', e);
};
const http = axios.create({
    adapter: compose(preventDefault({ onError }), defaultRequest())
});

http.get('/foo'); // 如果请求出错，则默认会调用 onError
```

#### Axios error

| Param          | Type         | Default value | Description                        |
| -------------- | ------------ | ------------- | ---------------------------------- |
| preventDefault | `() => void` |               | 若调用，则不会触发 options onError |

```javascript
cont onError = (e) => {
    console.log('error occured:', e);
};
const http = axios.create({
    adapter: compose(preventDefault({ onError }), defaultRequest())
});

http.get('/foo').catch(e => {
    e.preventDefault();
    // do something with error
    // ...
});
```

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