This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
forked from rawrmaan/restyped-axios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
95 lines (79 loc) · 3.15 KB
/
index.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import axios, {
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
CancelStatic,
CancelTokenStatic
} from 'axios'
import {RestypedBase, RestypedIndexedBase, RestypedRoute} from 'restyped'
export interface TypedAxiosRequestConfig<
API extends RestypedIndexedBase,
Path extends Extract<keyof API, string>,
Method extends keyof API[Path],
RouteDef extends RestypedRoute = API[Path][Method]
> extends AxiosRequestConfig {
url?: Path
method?: Extract<Method, string>
params?: RouteDef['query']
data?: RouteDef['body']
}
export interface TypedAxiosResponse<
API extends RestypedIndexedBase,
Path extends Extract<keyof API, string>,
Method extends keyof API[Path],
RouteDef extends RestypedRoute = API[Path][Method]
> extends AxiosResponse {
data: RouteDef['response']
config: TypedAxiosRequestConfig<API, Path, Method>
}
export interface TypedAxiosInstance<API extends RestypedBase = any>
extends AxiosInstance {
request<Path extends Extract<keyof API, string>, Method extends keyof API[Path] = 'GET'>(
config: TypedAxiosRequestConfig<API, Path, Method>
): Promise<TypedAxiosResponse<API, Path, Method>>
get<Path extends Extract<keyof API, string>>(
url: Path | string,
config?: TypedAxiosRequestConfig<API, Path, 'GET'>
): Promise<TypedAxiosResponse<API, Path, 'GET'>>
delete<Path extends Extract<keyof API, string>>(
url: Path | string,
config?: TypedAxiosRequestConfig<API, Path, 'DELETE'>
): Promise<TypedAxiosResponse<API, Path, 'DELETE'>>
head<Path extends Extract<keyof API, string>>(
url: Path | string,
config?: TypedAxiosRequestConfig<API, Path, 'HEAD'>
): Promise<TypedAxiosResponse<API, Path, 'HEAD'>>
post<Path extends Extract<keyof API, string>>(
url: Path | string,
data?: API[Path]['POST']['body'],
config?: TypedAxiosRequestConfig<API, Path, 'POST'>
): Promise<TypedAxiosResponse<API, Path, 'POST'>>
put<Path extends Extract<keyof API, string>>(
url: Path | string,
data?: API[Path]['PUT']['body'],
config?: TypedAxiosRequestConfig<API, Path, 'PUT'>
): Promise<TypedAxiosResponse<API, Path, 'PUT'>>
patch<Path extends Extract<keyof API, string>>(
url: Path | string,
data?: API[Path]['PATCH']['body'],
config?: TypedAxiosRequestConfig<API, Path, 'PATCH'>
): Promise<TypedAxiosResponse<API, Path, 'PATCH'>>
}
export interface TypedAxiosStatic<API extends RestypedBase = any>
extends TypedAxiosInstance<API> {
<Path extends Extract<keyof API, string>, Method extends keyof API[Path] = 'GET'>(
config: TypedAxiosRequestConfig<API, Path, Method>
): Promise<TypedAxiosResponse<API, Path, Method>>
<Path extends Extract<keyof API, string>, Method extends keyof API[Path]>(
url: Path | string,
config?: TypedAxiosRequestConfig<API, Path, Method>
): Promise<TypedAxiosResponse<API, Path, Method>>
create<T extends API>(config?: AxiosRequestConfig): TypedAxiosInstance<T>
Cancel: CancelStatic
CancelToken: CancelTokenStatic
isCancel(value: any): boolean
all<T>(values: (T | Promise<T>)[]): Promise<T[]>
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R
}
const TypedAxios: TypedAxiosStatic = axios as any
export default TypedAxios