1- import axios , {
2- AxiosInstance ,
3- AxiosRequestConfig ,
4- AxiosResponse ,
5- AxiosDefaults ,
6- } from 'axios' ;
7-
8- export type MethodName =
9- | 'get'
10- | 'delete'
11- | 'head'
12- | 'options'
13- | 'post'
14- | 'put'
15- | 'patch' ;
1+ import axios , { AxiosInstance , AxiosRequestConfig , AxiosResponse , AxiosDefaults } from 'axios' ;
2+
3+ export type MethodName = 'get' | 'delete' | 'head' | 'options' | 'post' | 'put' | 'patch' ;
164
175export interface Next < R > {
186 ( config : AxiosRequestConfig ) : Promise < AxiosResponse < R > > ;
@@ -44,11 +32,7 @@ export interface AxiosInstanceWrapper {
4432const ArgsToConfig = {
4533 withBody : (
4634 method : MethodName ,
47- [ url , data , config ] : [
48- url ?: string ,
49- data ?: any ,
50- config ?: AxiosRequestConfig ,
51- ] ,
35+ [ url , data , config ] : [ url ?: string , data ?: any , config ?: AxiosRequestConfig ] ,
5236 ) : AxiosRequestConfig => ( {
5337 url,
5438 method,
@@ -65,9 +49,7 @@ const ArgsToConfig = {
6549 } ) ,
6650} ;
6751
68- export const create = (
69- instanceConfig : AxiosRequestConfig ,
70- ) : AxiosInstanceWrapper => {
52+ export function create ( instanceConfig : AxiosRequestConfig ) : AxiosInstanceWrapper {
7153 const instance = axios . create ( instanceConfig ) ;
7254
7355 let request = instance . request . bind ( instance ) ;
@@ -84,14 +66,10 @@ export const create = (
8466 patch : ( ...args ) => request ( ArgsToConfig . withBody ( 'patch' , args ) ) ,
8567 } ;
8668
87- const useMiddleware = < M > (
88- middleware : Middleware < M > ,
89- ) : AxiosInstanceWrapper => {
69+ const useMiddleware = < M > ( middleware : Middleware < M > ) : AxiosInstanceWrapper => {
9070 const wrapped = request ;
9171
92- request = async function < T = any , R = AxiosResponse < T > > (
93- requestConfig : AxiosRequestConfig ,
94- ) {
72+ request = async function < T = any , R = AxiosResponse < T > > ( requestConfig : AxiosRequestConfig ) {
9573 let promise : Promise < R > | undefined ;
9674
9775 await middleware (
@@ -106,9 +84,7 @@ export const create = (
10684 ) ;
10785
10886 if ( ! promise ) {
109- throw Error (
110- 'Looks like one of your middleware functions is not called "next"' ,
111- ) ;
87+ throw Error ( 'Looks like one of your middleware functions is not called "next"' ) ;
11288 }
11389
11490 // IMPORTANT: returns original promise here and don`t create another
@@ -125,4 +101,4 @@ export const create = (
125101 } ;
126102
127103 return wrapper ;
128- } ;
104+ }
0 commit comments