UNPKG

2.91 kBTypeScriptView Raw
1import { AxiosAdapter, AxiosInstance, AxiosRequestConfig } from 'axios';
2
3interface AxiosHeaders {
4 [key: string]: string | number | boolean | null | undefined;
5}
6
7type MockArrayResponse = [
8 status: number,
9 data?: any,
10 headers?: AxiosHeaders
11];
12
13type MockObjectResponse = {
14 status: number;
15 data: any;
16 headers?: AxiosHeaders,
17 config?: AxiosRequestConfig
18};
19
20type MockResponse = MockArrayResponse | MockObjectResponse;
21
22type CallbackResponseSpecFunc = (
23 config: AxiosRequestConfig
24) => MockResponse | Promise<MockResponse>;
25
26type ResponseSpecFunc = <T = any>(
27 statusOrCallback: number | CallbackResponseSpecFunc,
28 data?: T,
29 headers?: AxiosHeaders
30) => MockAdapter;
31
32declare 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
47interface MockAdapterOptions {
48 delayResponse?: number;
49 onNoMatch?: 'passthrough' | 'throwException';
50}
51
52interface AsymmetricMatcher {
53 asymmetricMatch: Function;
54}
55
56interface ParamsMatcher {
57 [param: string]: any;
58}
59
60interface HeadersMatcher {
61 [header: string]: string;
62}
63
64type UrlMatcher = string | RegExp;
65type AsymmetricParamsMatcher = AsymmetricMatcher | ParamsMatcher;
66type AsymmetricHeadersMatcher = AsymmetricMatcher | HeadersMatcher;
67type AsymmetricRequestDataMatcher = AsymmetricMatcher | any;
68
69interface ConfigMatcher {
70 params?: AsymmetricParamsMatcher;
71 headers?: AsymmetricHeadersMatcher;
72 data?: AsymmetricRequestDataMatcher;
73}
74
75type RequestMatcherFunc = (
76 matcher?: UrlMatcher,
77 body?: AsymmetricRequestDataMatcher,
78 config?: ConfigMatcher
79) => MockAdapter.RequestHandler;
80
81type NoBodyRequestMatcherFunc = (
82 matcher?: UrlMatcher,
83 config?: ConfigMatcher
84) => MockAdapter.RequestHandler;
85
86type verb =
87 | 'get'
88 | 'post'
89 | 'put'
90 | 'delete'
91 | 'patch'
92 | 'options'
93 | 'head'
94 | 'list'
95 | 'link'
96 | 'unlink';
97
98type HistoryArray = AxiosRequestConfig[] & Record<verb, AxiosRequestConfig[]>
99
100declare 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
126export = MockAdapter;