refactor: use object RequestData

This commit is contained in:
2025-02-01 16:25:05 +03:00
parent 3d38b49839
commit 612da04cbb

View File

@ -11,57 +11,44 @@ export interface RequestData {
}
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;
private result: RequestData = Object.create({});
constructor() {
}
public setEndpoint(endpoint: string): this {
this.endpoint = endpoint;
this.result.endpoint = endpoint;
return this;
}
public setQueryParams(queryParams: Record<string, string | number | boolean | Array<any> | null>): RequestBuilder {
this.queryParams = queryParams;
this.result.queryParams = queryParams;
return this;
}
public addHeaders(headers: Record<string, string>): RequestBuilder {
Object.keys(headers).forEach(key => {
this.httpHeaders = this.httpHeaders.set(key, headers[key]);
this.result.httpHeaders = this.result.httpHeaders.set(key, headers[key]);
});
return this;
}
public setData(data: any): RequestBuilder {
this.data = data;
this.result.data = data;
return this;
}
public setSilenceMode(silence: boolean = true): RequestBuilder {
this.silenceMode = silence;
this.result.silenceMode = silence;
return this;
}
public setWithCredentials(credentials: boolean = true): RequestBuilder {
this.withCredentials = credentials;
this.result.withCredentials = credentials;
return this;
}
public get build(): RequestData {
return {
endpoint: this.endpoint,
queryParams: this.queryParams,
httpHeaders: this.httpHeaders,
data: this.data,
silenceMode: this.silenceMode,
withCredentials: this.withCredentials,
needAuth: false
};
return this.result;
}
}