Skip to content

Commit 780bd9c

Browse files
Merge pull request nestjs#904 from nestjs/feature/846-axios
feature(@nestjs/common) add configurable dynamic module (HttpModule)
2 parents a79041d + f385d5c commit 780bd9c

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN';
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
import axios, { AxiosRequestConfig } from 'axios';
12
import { Module } from '../decorators/modules/module.decorator';
3+
import { DynamicModule } from '../interfaces';
4+
import { AXIOS_INSTANCE_TOKEN } from './http.constants';
25
import { HttpService } from './http.service';
36

47
@Module({
5-
providers: [HttpService],
8+
providers: [HttpService, {
9+
provide: AXIOS_INSTANCE_TOKEN,
10+
useValue: axios,
11+
}],
612
exports: [HttpService],
713
})
8-
export class HttpModule {}
14+
export class HttpModule {
15+
static register(config: AxiosRequestConfig): DynamicModule {
16+
return {
17+
module: HttpModule,
18+
providers: [{
19+
provide: AXIOS_INSTANCE_TOKEN,
20+
useValue: axios.create(config),
21+
}],
22+
};
23+
}
24+
}
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,64 @@
1-
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
2-
import { from as fromPromise, Observable } from 'rxjs';
1+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
2+
import { defer, Observable } from 'rxjs';
3+
import { Inject } from '../decorators';
4+
import { AXIOS_INSTANCE_TOKEN } from './http.constants';
35

46
export class HttpService {
7+
constructor(
8+
@Inject(AXIOS_INSTANCE_TOKEN)
9+
private readonly instance: AxiosInstance = axios,
10+
) {}
11+
512
request<T = any>(config: AxiosRequestConfig): Observable<AxiosResponse<T>> {
6-
return fromPromise(axios.request<T>(config));
13+
return defer(() => this.instance.request<T>(config));
714
}
815

916
get<T = any>(
1017
url: string,
1118
config?: AxiosRequestConfig,
1219
): Observable<AxiosResponse<T>> {
13-
return fromPromise(axios.get<T>(url, config));
20+
return defer(() => this.instance.get<T>(url, config));
1421
}
1522

1623
delete<T = any>(
1724
url: string,
1825
config?: AxiosRequestConfig,
1926
): Observable<AxiosResponse<T>> {
20-
return fromPromise(axios.delete(url, config));
27+
return defer(() => this.instance.delete(url, config));
2128
}
2229

2330
head<T = any>(
2431
url: string,
2532
config?: AxiosRequestConfig,
2633
): Observable<AxiosResponse<T>> {
27-
return fromPromise(axios.head(url, config));
34+
return defer(() => this.instance.head(url, config));
2835
}
2936

3037
post<T = any>(
3138
url: string,
3239
data?,
3340
config?: AxiosRequestConfig,
3441
): Observable<AxiosResponse<T>> {
35-
return fromPromise(axios.post(url, data, config));
42+
return defer(() => this.instance.post(url, data, config));
3643
}
3744

3845
put<T = any>(
3946
url: string,
4047
data?,
4148
config?: AxiosRequestConfig,
4249
): Observable<AxiosResponse<T>> {
43-
return fromPromise(axios.put(url, data, config));
50+
return defer(() => this.instance.put(url, data, config));
4451
}
4552

4653
patch<T = any>(
4754
url: string,
4855
data?,
4956
config?: AxiosRequestConfig,
5057
): Observable<AxiosResponse<T>> {
51-
return fromPromise(axios.patch(url, data, config));
58+
return defer(() => this.instance.patch(url, data, config));
5259
}
5360

54-
get axiosRef() {
55-
return axios as any;
61+
get axiosRef(): AxiosInstance {
62+
return this.instance;
5663
}
5764
}

0 commit comments

Comments
 (0)