mixit/src/helpers/loadable/AxiosLoadable.ts

20 lines
529 B
TypeScript
Raw Normal View History

2019-05-01 15:07:08 +00:00
import { AxiosPromise, AxiosResponse } from 'axios'
import { unsafeAxiosMapper } from '../unsafeAxiosMapper'
import ErrorLoadable from './ErrorLoadable'
export default class AxiosLoadable<T, E> extends ErrorLoadable<T, E> {
load<U>(promise: AxiosPromise<U>, then: (res: AxiosResponse<U>) => T = res => unsafeAxiosMapper<T, U>(res), reset = true) {
if (reset) {
this.reset()
}
promise
.then(res => this.success(then(res)))
.catch(err => {
this.fail(err)
throw err
})
}
}