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;
|
2024-06-28 21:32:10 +03:00
|
|
|
withCredentials: boolean;
|
2024-08-24 04:24:44 +03:00
|
|
|
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;
|
2024-06-28 21:32:10 +03:00
|
|
|
private withCredentials: boolean = false;
|
2024-06-27 01:46:32 +03:00
|
|
|
|
2024-08-24 04:24:44 +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;
|
|
|
|
}
|
|
|
|
|
2024-06-28 21:32:10 +03:00
|
|
|
public setSilenceMode(silence: boolean = true): RequestBuilder {
|
2024-06-27 01:46:32 +03:00
|
|
|
this.silenceMode = silence;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-06-28 21:32:10 +03:00
|
|
|
public setWithCredentials(credentials: boolean = true): RequestBuilder {
|
|
|
|
this.withCredentials = credentials;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-08-24 04:24:44 +03:00
|
|
|
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,
|
2024-06-28 21:32:10 +03:00
|
|
|
silenceMode: this.silenceMode,
|
2024-08-24 04:24:44 +03:00
|
|
|
withCredentials: this.withCredentials,
|
|
|
|
needAuth: false
|
|
|
|
};
|
2024-06-27 01:46:32 +03:00
|
|
|
}
|
|
|
|
}
|