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