0.4.15 • Published 4 years ago

axios-add-transforms v0.4.15

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

Axios add Transforms

LICENSE IMAGE npm codecov Build Status

How to use

to add an interceptor

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

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
        }
      }
    }
  ]
})
0.4.15

4 years ago

0.4.14

4 years ago

0.4.13

4 years ago

0.4.12

4 years ago

0.4.11

4 years ago

0.4.10

4 years ago

0.4.8

4 years ago

0.4.6

4 years ago

0.4.3

4 years ago

0.4.0

4 years ago

0.3.17

4 years ago

0.3.16

4 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4-alpha-2

5 years ago

0.3.4-alpha-1

5 years ago

0.3.4-alpha

5 years ago

0.3.3

5 years ago

0.3.3-alpha-1

5 years ago

0.3.3-alpha

5 years ago

0.3.2-alpha4

5 years ago

0.3.2-alpha3

5 years ago

0.3.2-alpha2

5 years ago

0.3.2-alpha1

5 years ago

0.3.2-alpha

5 years ago

0.3.1

5 years ago

0.3.1-alpha

5 years ago

0.3.0-alpha2

5 years ago

0.3.0-alpha1

5 years ago

0.3.0-alpha

5 years ago

0.2.4-alpha1

5 years ago

0.2.4-alpha

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.9

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago