hailu
2023-05-25 3644568ac3b6db7e387d5ab72da717e51626f869
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
96
97
98
99
100
101
102
103
104
105
export interface AnyObject {
  [x: string]: any
}
 
export type Data = string | AnyObject | ArrayBuffer
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'HEAD' | 'OPTIONS' | 'TRACE'
export type DataType = 'json' | 'text' | 'html'
export type ResponseType = 'text' | 'arraybuffer'
export type Callback<T = any> = (result: T) => void
 
export interface Request<T> extends Promise<T>, AjaxRequestTask<Request<T>> {}
 
export interface RequestConstructor extends PromiseConstructor {
  readonly prototype: Request<any>
  new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Request<T>
  task: AjaxRequestTask | null
  aborted: boolean
  onHeadersReceivedCallback: Callback | null
  offHeadersReceivedCallback: Callback | null
  onHeadersReceived(callback: Callback): void
  offHeadersReceived(callback: Callback): void
}
 
export interface AjaxRequestTask<T = void> {
  abort(): T
  onHeadersReceived(callback: Callback): T
  offHeadersReceived(callback: Callback): T
}
 
export interface AjaxRequestConfig {
  baseURL?: string
  url?: string
  data?: Data
  query?: AnyObject
  params?: AnyObject
  header?: any
  method?: Method
  timeout?: number
  dataType?: DataType
  responseType?: ResponseType
  sslVerify?: boolean
  withCredentials?: boolean
  firstIpv4?: boolean
  xhr?: (task: AjaxRequestTask, config: AjaxRequestConfig) => void
  validateStatus?: ((statusCode?: number) => boolean) | null
  adapter?: (config: AjaxRequestConfig, Request: RequestConstructor) => Promise<any>
}
 
export type AjaxConfigType =
  | AjaxRequestConfig
  | (() => AjaxRequestConfig)
  | (() => Promise<AjaxRequestConfig>)
  | undefined
 
export interface AjaxCallbackConfig<T = any> extends AjaxRequestConfig {
  success?: Callback<T>
  fail?: Callback
  complete?: Callback
}
 
export interface AjaxResponse<T = any> {
  data: T
  statusCode: number
  header: any
  config: AjaxRequestConfig
  errMsg: string
  cookies: string[]
}
 
export interface AjaxInterceptorManager<T> {
  use(onFulfilled?: (value: T) => T | Promise<T>, onRejected?: (error: any) => any): number
  eject(id: number): void
}
 
export interface AjaxInvoke {
  <T = any, R = AjaxResponse<T>>(config?: AjaxRequestConfig): Request<R>
  <T = any, R = AjaxResponse<T>>(config?: AjaxCallbackConfig<R>): Request<void>
  <T = any, R = AjaxResponse<T>>(url?: string, data?: Data, config?: AjaxRequestConfig): Request<R>
}
 
export interface AjaxInstance<T extends AjaxConfigType> extends AjaxInvoke {
  get: AjaxInvoke
  post: AjaxInvoke
  put: AjaxInvoke
  delete: AjaxInvoke
  connect: AjaxInvoke
  head: AjaxInvoke
  options: AjaxInvoke
  trace: AjaxInvoke
  getURL(config?: AjaxConfigType): Promise<string>
  readonly defaults: AjaxRequestConfig
  readonly config: T
  interceptors: {
    request: AjaxInterceptorManager<AjaxRequestConfig>
    response: AjaxInterceptorManager<AjaxResponse>
  }
}
 
export interface AjaxStatic extends AjaxInstance<undefined> {
  create<T extends AjaxConfigType = undefined>(config?: T): AjaxInstance<T>
}
 
declare const Ajax: AjaxStatic
 
export default Ajax