1- import type {
2- AxiosResponse ,
3- AxiosInstance ,
4- AxiosRequestConfig ,
5- InternalAxiosRequestConfig ,
6- } from 'axios' ;
7- import { isEqual } from 'ohash' ;
8-
9- type RouteConfig = (
10- config ?: AxiosRequestConfig ,
11- ) => [ number , unknown ] | Promise < [ number , unknown ] > ;
12-
13- type RequestConfigChanger = (
14- config : InternalAxiosRequestConfig ,
15- ) => InternalAxiosRequestConfig ;
16-
17- export default class Adapter {
1+ import type { AxiosResponse , AxiosInstance } from 'axios' ;
2+ import {
3+ clearAll ,
4+ ejectFromRequest ,
5+ ejectFromResponse ,
6+ matchRequest ,
7+ matchResponse ,
8+ } from './helpers' ;
9+
10+ export default class Proxy {
1811 axios : AxiosInstance ;
1912
2013 verb ! : string ;
@@ -30,48 +23,15 @@ export default class Adapter {
3023 this . once = false ;
3124 }
3225
33- private hasSameParams ( requestParams : object , proxyParams ?: object ) {
34- if ( ! proxyParams ) return true ;
35- return isEqual ( requestParams , proxyParams ) ;
36- }
37-
38- private matchRequest (
39- verb : string ,
40- path : string ,
41- config : AxiosRequestConfig ,
42- params ?: object ,
43- ) {
44- return (
45- config . method === verb &&
46- config . url === path &&
47- this . hasSameParams ( config . params , params )
48- ) ;
49- }
50-
51- private matchResponse (
52- verb : string ,
53- path : string ,
54- response : AxiosResponse ,
55- params ?: object ,
56- ) {
57- return (
58- response . config . method === verb &&
59- response . config . url === path &&
60- this . hasSameParams ( response . config . params , params )
61- ) ;
62- }
63-
6426 private setProxy ( statusCodeOrFunction : number | RouteConfig , mock ?: unknown ) {
6527 const verbConfig = this . verb ;
6628 const pathConfig = this . path ;
6729 const onceConfig = this . once ;
6830 const paramsConfig = this . params ;
6931 const interceptorId = this . axios . interceptors . request . use ( requestConfig => {
70- if (
71- this . matchRequest ( verbConfig , pathConfig , requestConfig , paramsConfig )
72- ) {
32+ if ( matchRequest ( verbConfig , pathConfig , requestConfig , paramsConfig ) ) {
7333 requestConfig . adapter = config => {
74- if ( onceConfig ) this . ejectFromRequest ( interceptorId ) ;
34+ if ( onceConfig ) ejectFromRequest ( this . axios , interceptorId ) ;
7535 return new Promise ( ( resolve , reject ) => {
7636 if ( typeof statusCodeOrFunction === 'function' ) {
7737 Promise . resolve ( statusCodeOrFunction ( requestConfig ) ) . then (
@@ -110,30 +70,30 @@ export default class Adapter {
11070 } ) ;
11171 }
11272
113- private setRequestConfigChanger ( configChanger : RequestConfigChanger ) {
73+ private setRequestConfigChanger (
74+ configChanger : RequestConfigChanger ,
75+ once = false ,
76+ ) {
11477 const verbConfig = this . verb ;
11578 const pathConfig = this . path ;
11679 const paramsConfig = this . params ;
11780 const interceptorId = this . axios . interceptors . request . use ( requestConfig => {
118- if (
119- this . matchRequest ( verbConfig , pathConfig , requestConfig , paramsConfig )
120- ) {
121- if ( this . once ) this . ejectFromRequest ( interceptorId ) ;
81+ if ( matchRequest ( verbConfig , pathConfig , requestConfig , paramsConfig ) ) {
82+ if ( once ) ejectFromRequest ( this . axios , interceptorId ) ;
12283 const result = configChanger ( requestConfig ) ;
12384 return result ;
12485 }
12586 return requestConfig ;
12687 } ) ;
12788 }
12889
129- private setPrintableResponse ( ) {
90+ private setPrintableResponse ( once = false ) {
13091 const verbConfig = this . verb ;
13192 const pathConfig = this . path ;
13293 const paramsConfig = this . params ;
133- const onceConfig = this . once ;
13494 const interceptorId = this . axios . interceptors . response . use ( response => {
135- if ( this . matchResponse ( verbConfig , pathConfig , response , paramsConfig ) ) {
136- if ( onceConfig ) this . ejectFromResponse ( interceptorId ) ;
95+ if ( matchResponse ( verbConfig , pathConfig , response , paramsConfig ) ) {
96+ if ( once ) ejectFromResponse ( this . axios , interceptorId ) ;
13797 console . log ( 'Response from:' , this . path ) ;
13898 console . log ( JSON . stringify ( response . data , null , 2 ) ) ;
13999 }
@@ -148,17 +108,8 @@ export default class Adapter {
148108 return this ;
149109 }
150110
151- private ejectFromRequest ( id : number ) {
152- this . axios . interceptors . request . eject ( id ) ;
153- }
154-
155- private ejectFromResponse ( id : number ) {
156- this . axios . interceptors . response . eject ( id ) ;
157- }
158-
159111 clear ( ) {
160- this . axios . interceptors . request . clear ( ) ;
161- this . axios . interceptors . response . clear ( ) ;
112+ clearAll ( this . axios ) ;
162113 }
163114
164115 reply ( statusCodeOrConfig : number | RouteConfig , mock ?: unknown ) {
@@ -174,26 +125,22 @@ export default class Adapter {
174125 }
175126
176127 changeRequest ( changer : RequestConfigChanger ) {
177- this . once = false ;
178128 this . setRequestConfigChanger ( changer ) ;
179129 return this ;
180130 }
181131
182132 changeRequestOnce ( changer : RequestConfigChanger ) {
183- this . once = true ;
184- this . setRequestConfigChanger ( changer ) ;
133+ this . setRequestConfigChanger ( changer , true ) ;
185134 return this ;
186135 }
187136
188137 printResponse ( ) {
189- this . once = false ;
190138 this . setPrintableResponse ( ) ;
191139 return this ;
192140 }
193141
194142 printResponseOnce ( ) {
195- this . once = true ;
196- this . setPrintableResponse ( ) ;
143+ this . setPrintableResponse ( true ) ;
197144 return this ;
198145 }
199146
0 commit comments