Skip to content

Commit 2933082

Browse files
committed
feat(common): Add request cancellation to http service
1 parent aa2e8cd commit 2933082

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import Axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
2-
import { defer, Observable } from 'rxjs';
1+
import Axios, {
2+
AxiosInstance,
3+
AxiosRequestConfig,
4+
AxiosResponse,
5+
AxiosPromise,
6+
} from 'axios';
7+
import { Observable } from 'rxjs';
38
import { Inject } from '../decorators';
49
import { AXIOS_INSTANCE_TOKEN } from './http.constants';
510

@@ -10,55 +15,81 @@ export class HttpService {
1015
) {}
1116

1217
request<T = any>(config: AxiosRequestConfig): Observable<AxiosResponse<T>> {
13-
return defer(() => this.instance.request<T>(config));
18+
return this.makeObservable<T>(this.instance.request, config);
1419
}
1520

1621
get<T = any>(
1722
url: string,
1823
config?: AxiosRequestConfig,
1924
): Observable<AxiosResponse<T>> {
20-
return defer(() => this.instance.get<T>(url, config));
25+
return this.makeObservable<T>(this.instance.get, url, config);
2126
}
2227

2328
delete<T = any>(
2429
url: string,
2530
config?: AxiosRequestConfig,
2631
): Observable<AxiosResponse<T>> {
27-
return defer(() => this.instance.delete(url, config));
32+
return this.makeObservable<T>(this.instance.delete, url, config);
2833
}
2934

3035
head<T = any>(
3136
url: string,
3237
config?: AxiosRequestConfig,
3338
): Observable<AxiosResponse<T>> {
34-
return defer(() => this.instance.head(url, config));
39+
return this.makeObservable<T>(this.instance.head, url, config);
3540
}
3641

3742
post<T = any>(
3843
url: string,
3944
data?: any,
4045
config?: AxiosRequestConfig,
4146
): Observable<AxiosResponse<T>> {
42-
return defer(() => this.instance.post(url, data, config));
47+
return this.makeObservable<T>(this.instance.post, url, data, config);
4348
}
4449

4550
put<T = any>(
4651
url: string,
4752
data?: any,
4853
config?: AxiosRequestConfig,
4954
): Observable<AxiosResponse<T>> {
50-
return defer(() => this.instance.put(url, data, config));
55+
return this.makeObservable<T>(this.instance.put, url, data, config);
5156
}
5257

5358
patch<T = any>(
5459
url: string,
5560
data?: any,
5661
config?: AxiosRequestConfig,
5762
): Observable<AxiosResponse<T>> {
58-
return defer(() => this.instance.patch(url, data, config));
63+
return this.makeObservable<T>(this.instance.patch, url, data, config);
5964
}
6065

6166
get axiosRef(): AxiosInstance {
6267
return this.instance;
6368
}
69+
70+
private makeObservable<T>(
71+
axios: (...args: any[]) => AxiosPromise<T>,
72+
...args: any[]
73+
) {
74+
return new Observable<AxiosResponse<T>>(subscriber => {
75+
let config = args[args.length - 1];
76+
if (!config) {
77+
config = {};
78+
args[args.length - 1] = config;
79+
}
80+
const cancelSource = Axios.CancelToken.source();
81+
config.cancelToken = cancelSource.token;
82+
axios(...args)
83+
.then(res => {
84+
subscriber.next(res);
85+
subscriber.complete();
86+
})
87+
.catch(err => {
88+
subscriber.error(err);
89+
});
90+
return () => {
91+
cancelSource.cancel();
92+
};
93+
});
94+
}
6495
}

0 commit comments

Comments
 (0)