methods.ts
1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { RequestConfig } from "./types";
/**
* 为Request类添加HTTP方法别名
* @param {Object} instance Request实例
*/
export function addMethodAliases(instance: any): void {
// 添加别名方法
(["get", "post", "put", "delete", "patch"] as const).forEach((method) => {
if (method === "get" || method === "delete") {
// 对于GET、DELETE方法
instance[method] = function (this: any,
url: string,
config: RequestConfig = { sentryDsn: this.defaults.sentryDsn }
) {
return this.request({
url,
method: method.toUpperCase(),
...config,
});
};
} else {
// 对于POST、PUT、PATCH方法
instance[method] = function (this: any,
url: string,
data?: any,
config: RequestConfig = { sentryDsn: this.defaults.sentryDsn }
) {
return this.request({
url,
method: method.toUpperCase(),
data,
...config,
});
};
}
});
}