| 13 | @Provide() |
| 14 | @Scope(ScopeEnum.Singleton) |
| 15 | export class HttpService { |
| 16 | private instance: AxiosInstance; |
| 17 | |
| 18 | @Inject() |
| 19 | private serviceFactory: HttpServiceFactory; |
| 20 | |
| 21 | @Init() |
| 22 | protected async init() { |
| 23 | const clientName = this.serviceFactory.getDefaultClientName() || 'default'; |
| 24 | |
| 25 | this.instance = this.serviceFactory.get(clientName); |
| 26 | if (!this.instance) { |
| 27 | throw new MidwayCommonError('axios default instance not found.'); |
| 28 | } |
| 29 | } |
| 30 | get defaults() { |
| 31 | return this.instance.defaults; |
| 32 | } |
| 33 | |
| 34 | get interceptors() { |
| 35 | return this.instance.interceptors; |
| 36 | } |
| 37 | |
| 38 | getUri(config?: AxiosRequestConfig): string { |
| 39 | return this.instance.getUri(config); |
| 40 | } |
| 41 | |
| 42 | request<T = any, R = AxiosResponse<T>, D = any>( |
| 43 | config: AxiosRequestConfig<D> |
| 44 | ): Promise<R> { |
| 45 | return this.instance.request(config); |
| 46 | } |
| 47 | |
| 48 | get<T = any, R = AxiosResponse<T>, D = any>( |
| 49 | url: string, |
| 50 | config?: AxiosRequestConfig<D> |
| 51 | ): Promise<R> { |
| 52 | return this.instance.get(url, config); |
| 53 | } |
| 54 | |
| 55 | delete<T = any, R = AxiosResponse<T>, D = any>( |
| 56 | url: string, |
| 57 | config?: AxiosRequestConfig<D> |
| 58 | ): Promise<R> { |
| 59 | return this.instance.delete(url, config); |
| 60 | } |
| 61 | |
| 62 | head<T = any, R = AxiosResponse<T>, D = any>( |
| 63 | url: string, |
| 64 | config?: AxiosRequestConfig<D> |
| 65 | ): Promise<R> { |
| 66 | return this.instance.head(url, config); |
| 67 | } |
| 68 | |
| 69 | options<T = any, R = AxiosResponse<T>, D = any>( |
| 70 | url: string, |
| 71 | config?: AxiosRequestConfig<D> |
| 72 | ): Promise<R> { |