-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #904 from nestjs/feature/846-axios
feature(@nestjs/common) add configurable dynamic module (HttpModule)
- Loading branch information
Showing
3 changed files
with
37 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
import axios, { AxiosRequestConfig } from 'axios'; | ||
import { Module } from '../decorators/modules/module.decorator'; | ||
import { DynamicModule } from '../interfaces'; | ||
import { AXIOS_INSTANCE_TOKEN } from './http.constants'; | ||
import { HttpService } from './http.service'; | ||
|
||
@Module({ | ||
providers: [HttpService], | ||
providers: [HttpService, { | ||
provide: AXIOS_INSTANCE_TOKEN, | ||
useValue: axios, | ||
}], | ||
exports: [HttpService], | ||
}) | ||
export class HttpModule {} | ||
export class HttpModule { | ||
static register(config: AxiosRequestConfig): DynamicModule { | ||
return { | ||
module: HttpModule, | ||
providers: [{ | ||
provide: AXIOS_INSTANCE_TOKEN, | ||
useValue: axios.create(config), | ||
}], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,64 @@ | ||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { from as fromPromise, Observable } from 'rxjs'; | ||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { defer, Observable } from 'rxjs'; | ||
import { Inject } from '../decorators'; | ||
import { AXIOS_INSTANCE_TOKEN } from './http.constants'; | ||
|
||
export class HttpService { | ||
constructor( | ||
@Inject(AXIOS_INSTANCE_TOKEN) | ||
private readonly instance: AxiosInstance = axios, | ||
) {} | ||
|
||
request<T = any>(config: AxiosRequestConfig): Observable<AxiosResponse<T>> { | ||
return fromPromise(axios.request<T>(config)); | ||
return defer(() => this.instance.request<T>(config)); | ||
} | ||
|
||
get<T = any>( | ||
url: string, | ||
config?: AxiosRequestConfig, | ||
): Observable<AxiosResponse<T>> { | ||
return fromPromise(axios.get<T>(url, config)); | ||
return defer(() => this.instance.get<T>(url, config)); | ||
} | ||
|
||
delete<T = any>( | ||
url: string, | ||
config?: AxiosRequestConfig, | ||
): Observable<AxiosResponse<T>> { | ||
return fromPromise(axios.delete(url, config)); | ||
return defer(() => this.instance.delete(url, config)); | ||
} | ||
|
||
head<T = any>( | ||
url: string, | ||
config?: AxiosRequestConfig, | ||
): Observable<AxiosResponse<T>> { | ||
return fromPromise(axios.head(url, config)); | ||
return defer(() => this.instance.head(url, config)); | ||
} | ||
|
||
post<T = any>( | ||
url: string, | ||
data?, | ||
config?: AxiosRequestConfig, | ||
): Observable<AxiosResponse<T>> { | ||
return fromPromise(axios.post(url, data, config)); | ||
return defer(() => this.instance.post(url, data, config)); | ||
} | ||
|
||
put<T = any>( | ||
url: string, | ||
data?, | ||
config?: AxiosRequestConfig, | ||
): Observable<AxiosResponse<T>> { | ||
return fromPromise(axios.put(url, data, config)); | ||
return defer(() => this.instance.put(url, data, config)); | ||
} | ||
|
||
patch<T = any>( | ||
url: string, | ||
data?, | ||
config?: AxiosRequestConfig, | ||
): Observable<AxiosResponse<T>> { | ||
return fromPromise(axios.patch(url, data, config)); | ||
return defer(() => this.instance.patch(url, data, config)); | ||
} | ||
|
||
get axiosRef() { | ||
return axios as any; | ||
get axiosRef(): AxiosInstance { | ||
return this.instance; | ||
} | ||
} |