refactor: use object RequestData
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user