1 | import { AxiosAdapter, AxiosInstance, AxiosRequestConfig } from 'axios';
|
2 |
|
3 | interface AxiosHeaders {
|
4 | [key: string]: string | number | boolean | null | undefined;
|
5 | }
|
6 |
|
7 | type MockArrayResponse = [
|
8 | status: number,
|
9 | data?: any,
|
10 | headers?: AxiosHeaders
|
11 | ];
|
12 |
|
13 | type MockObjectResponse = {
|
14 | status: number;
|
15 | data: any;
|
16 | headers?: AxiosHeaders,
|
17 | config?: AxiosRequestConfig
|
18 | };
|
19 |
|
20 | type MockResponse = MockArrayResponse | MockObjectResponse;
|
21 |
|
22 | type CallbackResponseSpecFunc = (
|
23 | config: AxiosRequestConfig
|
24 | ) => MockResponse | Promise<MockResponse>;
|
25 |
|
26 | type ResponseSpecFunc = <T = any>(
|
27 | statusOrCallback: number | CallbackResponseSpecFunc,
|
28 | data?: T,
|
29 | headers?: AxiosHeaders
|
30 | ) => MockAdapter;
|
31 |
|
32 | declare namespace MockAdapter {
|
33 | export interface RequestHandler {
|
34 | withDelayInMs(delay: number): RequestHandler;
|
35 | reply: ResponseSpecFunc;
|
36 | replyOnce: ResponseSpecFunc;
|
37 | passThrough(): MockAdapter;
|
38 | abortRequest(): MockAdapter;
|
39 | abortRequestOnce(): MockAdapter;
|
40 | networkError(): MockAdapter;
|
41 | networkErrorOnce(): MockAdapter;
|
42 | timeout(): MockAdapter;
|
43 | timeoutOnce(): MockAdapter;
|
44 | }
|
45 | }
|
46 |
|
47 | interface MockAdapterOptions {
|
48 | delayResponse?: number;
|
49 | onNoMatch?: 'passthrough' | 'throwException';
|
50 | }
|
51 |
|
52 | interface AsymmetricMatcher {
|
53 | asymmetricMatch: Function;
|
54 | }
|
55 |
|
56 | interface ParamsMatcher {
|
57 | [param: string]: any;
|
58 | }
|
59 |
|
60 | interface HeadersMatcher {
|
61 | [header: string]: string;
|
62 | }
|
63 |
|
64 | type UrlMatcher = string | RegExp;
|
65 | type AsymmetricParamsMatcher = AsymmetricMatcher | ParamsMatcher;
|
66 | type AsymmetricHeadersMatcher = AsymmetricMatcher | HeadersMatcher;
|
67 | type AsymmetricRequestDataMatcher = AsymmetricMatcher | any;
|
68 |
|
69 | interface ConfigMatcher {
|
70 | params?: AsymmetricParamsMatcher;
|
71 | headers?: AsymmetricHeadersMatcher;
|
72 | data?: AsymmetricRequestDataMatcher;
|
73 | }
|
74 |
|
75 | type RequestMatcherFunc = (
|
76 | matcher?: UrlMatcher,
|
77 | body?: AsymmetricRequestDataMatcher,
|
78 | config?: ConfigMatcher
|
79 | ) => MockAdapter.RequestHandler;
|
80 |
|
81 | type NoBodyRequestMatcherFunc = (
|
82 | matcher?: UrlMatcher,
|
83 | config?: ConfigMatcher
|
84 | ) => MockAdapter.RequestHandler;
|
85 |
|
86 | type verb =
|
87 | | 'get'
|
88 | | 'post'
|
89 | | 'put'
|
90 | | 'delete'
|
91 | | 'patch'
|
92 | | 'options'
|
93 | | 'head'
|
94 | | 'list'
|
95 | | 'link'
|
96 | | 'unlink';
|
97 |
|
98 | type HistoryArray = AxiosRequestConfig[] & Record<verb, AxiosRequestConfig[]>
|
99 |
|
100 | declare class MockAdapter {
|
101 | static default: typeof MockAdapter;
|
102 |
|
103 | constructor(axiosInstance: AxiosInstance, options?: MockAdapterOptions);
|
104 |
|
105 | adapter(): AxiosAdapter;
|
106 | reset(): void;
|
107 | resetHandlers(): void;
|
108 | resetHistory(): void;
|
109 | restore(): void;
|
110 |
|
111 | history: HistoryArray;
|
112 |
|
113 | onAny: NoBodyRequestMatcherFunc;
|
114 | onGet: NoBodyRequestMatcherFunc;
|
115 | onDelete: NoBodyRequestMatcherFunc;
|
116 | onHead: NoBodyRequestMatcherFunc;
|
117 | onOptions: NoBodyRequestMatcherFunc;
|
118 | onPost: RequestMatcherFunc;
|
119 | onPut: RequestMatcherFunc;
|
120 | onPatch: RequestMatcherFunc;
|
121 | onList: RequestMatcherFunc;
|
122 | onLink: RequestMatcherFunc;
|
123 | onUnlink: RequestMatcherFunc;
|
124 | }
|
125 |
|
126 | export = MockAdapter;
|