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; httpHeaders: HttpHeaders;
data: any; data: any;
silenceMode: boolean; silenceMode: boolean;
withCredentials: boolean;
} }
export class RequestBuilder { export class RequestBuilder {
@ -18,6 +19,7 @@ export class RequestBuilder {
private httpHeaders: HttpHeaders = new HttpHeaders(); private httpHeaders: HttpHeaders = new HttpHeaders();
private data: any = null; private data: any = null;
private silenceMode: boolean = false; private silenceMode: boolean = false;
private withCredentials: boolean = false;
private readonly object: any; private readonly object: any;
constructor(obj: any) { constructor(obj: any) {
@ -46,18 +48,24 @@ export class RequestBuilder {
return this; return this;
} }
public setSilenceMode(silence: boolean): RequestBuilder { public setSilenceMode(silence: boolean = true): RequestBuilder {
this.silenceMode = silence; this.silenceMode = silence;
return this; return this;
} }
public setWithCredentials(credentials: boolean = true): RequestBuilder {
this.withCredentials = credentials;
return this;
}
public build<Type>(): Type { public build<Type>(): Type {
(this.object as SetRequestBuilderAfterBuild).setRequestBuilder({ (this.object as SetRequestBuilderAfterBuild).setRequestBuilder({
endpoint: this.endpoint, endpoint: this.endpoint,
queryParams: this.queryParams, queryParams: this.queryParams,
httpHeaders: this.httpHeaders, httpHeaders: this.httpHeaders,
data: this.data, data: this.data,
silenceMode: this.silenceMode silenceMode: this.silenceMode,
withCredentials: this.withCredentials
}); });
return this.object as Type; return this.object as Type;
} }
@ -88,7 +96,8 @@ export class RequestBuilder {
queryParams: null, queryParams: null,
httpHeaders: new HttpHeaders(), httpHeaders: new HttpHeaders(),
data: null, data: null,
silenceMode: false silenceMode: false,
withCredentials: false
} }
} }
@ -98,5 +107,6 @@ export class RequestBuilder {
this.httpHeaders = new HttpHeaders(); this.httpHeaders = new HttpHeaders();
this.data = null; this.data = null;
this.silenceMode = false; 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) { constructor(private http: HttpClient, private notify: OpenNotifyService, private router: Router) {
} }
private urlApi = environment.apiUrl; private apiUrl = environment.apiUrl;
protected abstract basePath: string; protected abstract basePath: string;
protected abstract version: AvailableVersion; protected abstract version: AvailableVersion;
private request: RequestData = RequestBuilder.getStandardRequestData(); private request: RequestData = RequestBuilder.getStandardRequestData();
@ -66,16 +66,22 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
} }
private static combineUrls(...parts: string[]): string { private static combineUrls(...parts: string[]): string {
let test = parts.map(part => part.replace(/(^\/+|\/+$)/g, '')).join('/'); return parts.map(part => part.replace(/(^\/+|\/+$)/g, '')).join('/');
console.log(test); }
return test;
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> { 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.tokenRefreshService.getTokenRefreshing$().pipe(
filter(refreshing => !refreshing),
take(1),
switchMap(_ => {
return this.http.request<Type>(method, doneEndpoint, { return this.http.request<Type>(method, doneEndpoint, {
withCredentials: true, withCredentials: this.request.withCredentials,
headers: this.request.httpHeaders, headers: this.request.httpHeaders,
body: this.request.data body: this.request.data
}).pipe( }).pipe(
@ -89,6 +95,8 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
throw error; throw error;
}) })
); );
})
);
} }
public createRequestBuilder() { public createRequestBuilder() {

View File

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