# axios-add-transforms

> axios-add-transforms

Latest version **0.4.15** (published 2019-12-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install axios-add-transforms
pnpm add axios-add-transforms
yarn add axios-add-transforms
bun add axios-add-transforms
```

## 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.4.15 |
| Published | 2019-12-03 |
| First published | 2019-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 90.6 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | bichi |
| Maintainers | bichi |
| Keywords | axios |

## Links

- npm: https://www.npmjs.com/package/axios-add-transforms
- Repository: https://github.com/bichikim/axios-add-transforms
- Homepage: https://github.com/bichikim/axios-add-transforms#readme
- Issues: https://github.com/bichikim/axios-add-transforms/issues
- npm.io page: https://npm.io/package/axios-add-transforms

## Dependencies (2)

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

## Recent versions

- 0.4.15 (latest) — 2019-12-03
- 0.4.0 (alpha) — 2019-11-21
- 0.4.14 — 2019-11-27
- 0.4.13 — 2019-11-27
- 0.4.12 — 2019-11-27
- 0.4.11 — 2019-11-27
- 0.4.10 — 2019-11-23
- 0.4.8 — 2019-11-22
- 0.4.6 — 2019-11-22
- 0.4.3 — 2019-11-22
- 0.3.17 — 2019-11-14
- 0.3.16 — 2019-11-14
- 0.3.14 — 2019-11-14
- 0.3.13 — 2019-11-14
- 0.3.12 — 2019-11-14
- … 37 more at https://npm.io/package/axios-add-transforms/versions

## README

# Axios add Transforms
>

[![LICENSE IMAGE]](https://www.npmjs.org/package/axios-add-transforms)
![npm](https://img.shields.io/npm/v/axios-add-transforms.svg)
[![codecov](https://codecov.io/gh/bichikim/axios-add-transforms/branch/master/graph/badge.svg)](https://codecov.io/gh/winnter-love/axios-add-transforms)
[![Build Status](https://travis-ci.org/bichikim/axios-add-transforms.svg?branch=master)](https://travis-ci.org/winter-love/axios-add-transforms)

[LICENSE IMAGE]:https://img.shields.io/npm/l/axios-add-transforms.svg
[NPM LINK]:https://www.npmjs.org/package/axios-add-transforms

## How to use

### to add an interceptor


```typescript
import {AxiosRequestConfig} from 'axios'
import Transforms from './src'
import axios from 'axios'

const myAxios = axios.create({})
// refer to TransformsOptions
const transforms = new Transforms({
  context: () => ({axios: myAxios}),
  // first: TransformSet | TransformSet[]
  // final: TransformSet | TransformSet[]
  // margeResponse: 'front' | 'back' | undefined
  matchers: [
    {
      test: /^\/users\/?$/,
      // method: ...
      transform: {
        request: ({data: {foo, bar}, params, headers}) =>
         ({data: {'_foo': foo, '_bar': bar}, params, headers}),
        // response: Matcher | Matcher[]
        // error: Matcher | Matcher[]
      }
    }
  ]
})

const config: AxiosRequestConfig = {
  url: '/users/',
  data: {
    foo: 'foo',
    bar: 'bar',
  }
}

/**
 * @deprecated
 */
// transforms.addInterceptors(myAxios)
transforms.applyTransform(myAxios)


// request data will ba {_foo: 'foo', _bar: 'bar'}
myAxios(config).then(() => {
  
})

// remove transform
transForms.ejectTransform(myAxios)

```

### retry request

```typescript
import Transforms from './src'

const trsnsforms = new Transforms({
  matchers: [
    {
      test: /^\/users\/?$/,
      transform: {
        error: async (error, context, status) => {
          // sign-in again
          await context.axios({
            url: 'sign-in',
            method: 'put'
          })
          if(!status.retry) {
            status.retry = 0
          }
          status.retry += 1
          // retry until three times
          error.retry = status < 3
          return error
        }
      }
    }
  ]
})



```

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