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