1- import type { AxiosResponse , AxiosInstance } from 'axios' ;
2- import {
3- clearAll ,
4- ejectFromRequest ,
5- ejectFromResponse ,
6- matchRequest ,
7- matchResponse ,
8- } from './helpers' ;
1+ import type { AxiosInstance } from 'axios' ;
2+ import Handler from './handler' ;
3+ import { clearAll } from './helpers' ;
94
105export default class Proxy {
116 axios : AxiosInstance ;
@@ -16,134 +11,19 @@ export default class Proxy {
1611
1712 params ?: object ;
1813
19- once : boolean ;
20-
2114 constructor ( axios : AxiosInstance ) {
2215 this . axios = axios ;
23- this . once = false ;
24- }
25-
26- private setProxy ( statusCodeOrFunction : number | RouteConfig , mock ?: unknown ) {
27- const verbConfig = this . verb ;
28- const pathConfig = this . path ;
29- const onceConfig = this . once ;
30- const paramsConfig = this . params ;
31- const interceptorId = this . axios . interceptors . request . use ( requestConfig => {
32- if ( matchRequest ( verbConfig , pathConfig , requestConfig , paramsConfig ) ) {
33- requestConfig . adapter = config => {
34- if ( onceConfig ) ejectFromRequest ( this . axios , interceptorId ) ;
35- return new Promise ( ( resolve , reject ) => {
36- if ( typeof statusCodeOrFunction === 'function' ) {
37- Promise . resolve ( statusCodeOrFunction ( requestConfig ) ) . then (
38- result => {
39- const [ status , data ] = result ;
40- const response = {
41- data,
42- status,
43- headers : { } ,
44- statusText : '' ,
45- config,
46- request : requestConfig ,
47- } ;
48- if ( status < 400 ) resolve ( response ) ;
49- else reject ( response ) ;
50- } ,
51- ) ;
52- return ;
53- }
54- const status = statusCodeOrFunction ;
55- const response : AxiosResponse = {
56- data : mock ,
57- status,
58- headers : { } ,
59- statusText : '' ,
60- config,
61- request : requestConfig ,
62- } ;
63-
64- if ( status < 400 ) resolve ( response ) ;
65- else reject ( response ) ;
66- } ) ;
67- } ;
68- }
69- return requestConfig ;
70- } ) ;
71- }
72-
73- private setRequestConfigChanger (
74- configChanger : RequestConfigChanger ,
75- once = false ,
76- ) {
77- const verbConfig = this . verb ;
78- const pathConfig = this . path ;
79- const paramsConfig = this . params ;
80- const interceptorId = this . axios . interceptors . request . use ( requestConfig => {
81- if ( matchRequest ( verbConfig , pathConfig , requestConfig , paramsConfig ) ) {
82- if ( once ) ejectFromRequest ( this . axios , interceptorId ) ;
83- const result = configChanger ( requestConfig ) ;
84- return result ;
85- }
86- return requestConfig ;
87- } ) ;
88- }
89-
90- private setPrintableResponse ( once = false ) {
91- const verbConfig = this . verb ;
92- const pathConfig = this . path ;
93- const paramsConfig = this . params ;
94- const interceptorId = this . axios . interceptors . response . use ( response => {
95- if ( matchResponse ( verbConfig , pathConfig , response , paramsConfig ) ) {
96- if ( once ) ejectFromResponse ( this . axios , interceptorId ) ;
97- console . log ( 'Response from:' , this . path ) ;
98- console . log ( JSON . stringify ( response . data , null , 2 ) ) ;
99- }
100- return response ;
101- } ) ;
10216 }
10317
10418 private setup ( verb : string , path : string , params ?: object ) {
105- this . verb = verb ;
106- this . path = path ;
107- this . params = params ;
108- return this ;
19+ const handler = new Handler ( this , verb , path , params ) ;
20+ return handler ;
10921 }
11022
11123 clear ( ) {
11224 clearAll ( this . axios ) ;
11325 }
11426
115- reply ( statusCodeOrConfig : number | RouteConfig , mock ?: unknown ) {
116- this . once = false ;
117- this . setProxy ( statusCodeOrConfig , mock ) ;
118- return this ;
119- }
120-
121- replyOnce ( statusCodeOrConfig : number | RouteConfig , mock ?: unknown ) {
122- this . once = true ;
123- this . setProxy ( statusCodeOrConfig , mock ) ;
124- return this ;
125- }
126-
127- changeRequest ( changer : RequestConfigChanger ) {
128- this . setRequestConfigChanger ( changer ) ;
129- return this ;
130- }
131-
132- changeRequestOnce ( changer : RequestConfigChanger ) {
133- this . setRequestConfigChanger ( changer , true ) ;
134- return this ;
135- }
136-
137- printResponse ( ) {
138- this . setPrintableResponse ( ) ;
139- return this ;
140- }
141-
142- printResponseOnce ( ) {
143- this . setPrintableResponse ( true ) ;
144- return this ;
145- }
146-
14727 onGet ( path : string , params ?: object ) {
14828 return this . setup ( 'get' , path , params ) ;
14929 }
0 commit comments