feat: add withCredentials that doesn't send cookie if not needed

This commit is contained in:
2024-06-28 21:32:10 +03:00
parent b498b0204c
commit 7eebe4632c
3 changed files with 61 additions and 23 deletions

View File

@ -38,7 +38,7 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
constructor(private http: HttpClient, private notify: OpenNotifyService, private router: Router) {
}
private urlApi = environment.apiUrl;
private apiUrl = environment.apiUrl;
protected abstract basePath: string;
protected abstract version: AvailableVersion;
private request: RequestData = RequestBuilder.getStandardRequestData();
@ -66,28 +66,36 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
}
private static combineUrls(...parts: string[]): string {
let test = parts.map(part => part.replace(/(^\/+|\/+$)/g, '')).join('/');
console.log(test);
return test;
return parts.map(part => part.replace(/(^\/+|\/+$)/g, '')).join('/');
}
protected get combinedUrl() {
return ApiService.addQuery(ApiService.combineUrls(this.apiUrl, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams)
}
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put'): Observable<Type> {
const doneEndpoint = ApiService.addQuery(ApiService.combineUrls(this.urlApi, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams);
const doneEndpoint = this.combinedUrl;
return this.http.request<Type>(method, doneEndpoint, {
withCredentials: true,
headers: this.request.httpHeaders,
body: this.request.data
}).pipe(
tap(_ => this.request = RequestBuilder.getStandardRequestData()),
retryWithInterval<Type>(),
catchError(error => {
if (!this.request.silenceMode)
this.handleError(error);
return this.tokenRefreshService.getTokenRefreshing$().pipe(
filter(refreshing => !refreshing),
take(1),
switchMap(_ => {
return this.http.request<Type>(method, doneEndpoint, {
withCredentials: this.request.withCredentials,
headers: this.request.httpHeaders,
body: this.request.data
}).pipe(
tap(_ => this.request = RequestBuilder.getStandardRequestData()),
retryWithInterval<Type>(),
catchError(error => {
if (!this.request.silenceMode)
this.handleError(error);
this.request = RequestBuilder.getStandardRequestData();
throw error;
})
this.request = RequestBuilder.getStandardRequestData();
throw error;
})
);
})
);
}