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

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

View File

@ -10,6 +10,7 @@ export interface RequestData {
httpHeaders: HttpHeaders;
data: any;
silenceMode: boolean;
withCredentials: boolean;
}
export class RequestBuilder {
@ -18,6 +19,7 @@ export class RequestBuilder {
private httpHeaders: HttpHeaders = new HttpHeaders();
private data: any = null;
private silenceMode: boolean = false;
private withCredentials: boolean = false;
private readonly object: any;
constructor(obj: any) {
@ -46,18 +48,24 @@ export class RequestBuilder {
return this;
}
public setSilenceMode(silence: boolean): RequestBuilder {
public setSilenceMode(silence: boolean = true): RequestBuilder {
this.silenceMode = silence;
return this;
}
public setWithCredentials(credentials: boolean = true): RequestBuilder {
this.withCredentials = credentials;
return this;
}
public build<Type>(): Type {
(this.object as SetRequestBuilderAfterBuild).setRequestBuilder({
endpoint: this.endpoint,
queryParams: this.queryParams,
httpHeaders: this.httpHeaders,
data: this.data,
silenceMode: this.silenceMode
silenceMode: this.silenceMode,
withCredentials: this.withCredentials
});
return this.object as Type;
}
@ -88,7 +96,8 @@ export class RequestBuilder {
queryParams: null,
httpHeaders: new HttpHeaders(),
data: null,
silenceMode: false
silenceMode: false,
withCredentials: false
}
}
@ -98,5 +107,6 @@ export class RequestBuilder {
this.httpHeaders = new HttpHeaders();
this.data = null;
this.silenceMode = false;
this.withCredentials = false;
}
}

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;
})
);
})
);
}

View File

@ -25,6 +25,7 @@ export default class SetupService extends ApiService {
return this.createRequestBuilder()
.setEndpoint('SetPsql')
.setData(data)
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
@ -33,6 +34,7 @@ export default class SetupService extends ApiService {
return this.createRequestBuilder()
.setEndpoint('SetMysql')
.setData(data)
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
@ -41,6 +43,7 @@ export default class SetupService extends ApiService {
return this.createRequestBuilder()
.setEndpoint('SetSqlite')
.setQueryParams({path: path})
.setWithCredentials()
.build<ApiService>()
.get<boolean>();
}
@ -49,18 +52,24 @@ export default class SetupService extends ApiService {
return this.createRequestBuilder()
.setEndpoint('SetRedis')
.setData(data)
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
public setMemcached() {
return this.post<boolean>('SetMemcached');
return this.createRequestBuilder()
.setEndpoint('SetMemcached')
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
public createAdmin(data: CreateUserRequest) {
return this.createRequestBuilder()
.setEndpoint('CreateAdmin')
.setData(data)
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
@ -69,6 +78,7 @@ export default class SetupService extends ApiService {
return this.createRequestBuilder()
.setEndpoint('SetLogging')
.setData(data)
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
@ -77,6 +87,7 @@ export default class SetupService extends ApiService {
return this.createRequestBuilder()
.setEndpoint('SetEmail')
.setData(data)
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
@ -87,11 +98,20 @@ export default class SetupService extends ApiService {
return this.createRequestBuilder()
.setEndpoint('SetSchedule')
.setData(data)
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
public submit() {
return this.post<boolean>('Submit');
return this.createRequestBuilder()
.setEndpoint('Submit')
.setWithCredentials()
.build<ApiService>()
.post<boolean>();
}
public isConfigured() {
return this.get<boolean>('IsConfigured');
}
}