61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
|
|
import type { HttpDefine, HttpInjectInfo } from './type'
|
|
import { http } from './http'
|
|
|
|
export class HTTPClient {
|
|
base: HttpDefine
|
|
inject: HttpInjectInfo
|
|
|
|
constructor(base: HttpDefine, inject?: HttpInjectInfo) {
|
|
this.base = base
|
|
this.inject = inject || {}
|
|
}
|
|
|
|
request<R = any>(config: HttpDefine): Promise<R> {
|
|
return http({ ...this.base, ...config }, this.inject)
|
|
}
|
|
|
|
get<R = any>(url: string, query?: any, config?: HttpDefine): Promise<R> {
|
|
return this.request({ ...config, url, method: 'get', query })
|
|
}
|
|
|
|
delete<R = any>(url: string, query?: any, config?: HttpDefine): Promise<R> {
|
|
return this.request({ ...config, url, method: 'delete', query })
|
|
}
|
|
|
|
head<R = any>(url: string, config?: HttpDefine): Promise<R> {
|
|
return this.request({ ...config, url, method: 'head' })
|
|
}
|
|
|
|
options<R = any>(url: string, config?: HttpDefine): Promise<R> {
|
|
return this.request({ ...config, url, method: 'options' })
|
|
}
|
|
|
|
post<R = any>(url: string, body?: any, config?: HttpDefine): Promise<R> {
|
|
return this.request({
|
|
...config,
|
|
url,
|
|
method: 'post',
|
|
body,
|
|
})
|
|
}
|
|
|
|
put<R = any>(url: string, body?: any, config?: HttpDefine): Promise<R> {
|
|
return this.request({
|
|
...config,
|
|
url,
|
|
method: 'put',
|
|
body,
|
|
})
|
|
}
|
|
|
|
patch<R = any>(url: string, body?: any, config?: HttpDefine): Promise<R> {
|
|
return this.request({
|
|
...config,
|
|
url,
|
|
method: 'patch',
|
|
body,
|
|
})
|
|
}
|
|
}
|