MireaFrontend/src/api/RequestBuilder.ts

68 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-06-27 01:46:32 +03:00
import {HttpHeaders} from "@angular/common/http";
export interface RequestData {
endpoint: string;
queryParams: Record<string, string | number | boolean | Array<any> | null> | null;
httpHeaders: HttpHeaders;
data: any;
silenceMode: boolean;
withCredentials: boolean;
needAuth: boolean;
2024-06-27 01:46:32 +03:00
}
export class RequestBuilder {
private endpoint: string = '';
private queryParams: Record<string, string | number | boolean | Array<any> | null> | null = null;
private httpHeaders: HttpHeaders = new HttpHeaders();
private data: any = null;
private silenceMode: boolean = false;
private withCredentials: boolean = false;
2024-06-27 01:46:32 +03:00
constructor() {
2024-06-27 01:46:32 +03:00
}
public setEndpoint(endpoint: string): this {
this.endpoint = endpoint;
return this;
}
public setQueryParams(queryParams: Record<string, string | number | boolean | Array<any> | null>): RequestBuilder {
this.queryParams = queryParams;
return this;
}
public addHeaders(headers: Record<string, string>): RequestBuilder {
Object.keys(headers).forEach(key => {
this.httpHeaders = this.httpHeaders.set(key, headers[key]);
});
return this;
}
public setData(data: any): RequestBuilder {
this.data = data;
return this;
}
public setSilenceMode(silence: boolean = true): RequestBuilder {
2024-06-27 01:46:32 +03:00
this.silenceMode = silence;
return this;
}
public setWithCredentials(credentials: boolean = true): RequestBuilder {
this.withCredentials = credentials;
return this;
}
public get build(): RequestData {
return {
2024-06-27 01:46:32 +03:00
endpoint: this.endpoint,
queryParams: this.queryParams,
httpHeaders: this.httpHeaders,
data: this.data,
silenceMode: this.silenceMode,
withCredentials: this.withCredentials,
needAuth: false
};
2024-06-27 01:46:32 +03:00
}
}