Skip to content

Commit

Permalink
feature(@nestjs/common) add configurable dynamic module (HttpModule)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 24, 2018
1 parent e39c9d9 commit f385d5c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/common/http/http.constants.ts
@@ -0,0 +1 @@
export const AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN';
20 changes: 18 additions & 2 deletions packages/common/http/http.module.ts
@@ -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),
}],
};
}
}
29 changes: 18 additions & 11 deletions packages/common/http/http.service.ts
@@ -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;
}
}

0 comments on commit f385d5c

Please sign in to comment.