Skip to content

Commit be4050a

Browse files
committed
refactor: simplify function signatures and improve default values in HttpClient and helpers
1 parent c6e99ef commit be4050a

File tree

3 files changed

+60
-68
lines changed

3 files changed

+60
-68
lines changed

src/client/http-client.ts

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,76 @@ import {
2020
export class HttpClient {
2121
private config: HttpClientConfig;
2222
private interceptors: Interceptors;
23-
24-
constructor(config: HttpClientConfig = {}) {
23+
private initValues: HttpClientConfig = {
24+
baseURL: '',
25+
headers: {},
26+
params: {},
27+
timeout: 30000,
28+
interceptors: {
29+
request: [],
30+
response: [],
31+
},
32+
};
33+
34+
constructor(config: HttpClientConfig = this.initValues) {
2535
this.config = config;
2636
this.interceptors = {
2737
request: config.interceptors?.request || [],
2838
response: config.interceptors?.response || [],
2939
};
3040
}
3141

32-
setBaseURL(baseURL: string): HttpClient {
42+
setBaseURL(baseURL: string) {
3343
this.config.baseURL = baseURL;
3444
return this;
3545
}
3646

37-
setHeaders(headers: HttpHeaders): HttpClient {
47+
setHeaders(headers: HttpHeaders) {
3848
this.config.headers = mergeHeaders(this.config.headers, headers);
3949
return this;
4050
}
4151

42-
setHeader(key: keyof HttpHeaders, value: string): HttpClient {
52+
setHeader(key: keyof HttpHeaders, value: string) {
4353
if (!this.config.headers) {
4454
this.config.headers = {};
4555
}
4656
this.config.headers[key] = value;
4757
return this;
4858
}
4959

50-
setParams(params: QueryParams): HttpClient {
60+
setParams(params: QueryParams) {
5161
this.config.params = mergeParams(this.config.params, params);
5262
return this;
5363
}
5464

55-
setParam(key: string, value: string | number | boolean): HttpClient {
65+
setParam(key: string, value: string | number | boolean) {
5666
if (!this.config.params) {
5767
this.config.params = {};
5868
}
5969
this.config.params[key] = value;
6070
return this;
6171
}
6272

63-
setTimeout(timeout: number): HttpClient {
73+
setTimeout(timeout: number) {
6474
this.config.timeout = timeout;
6575
return this;
6676
}
6777

68-
addRequestInterceptor(interceptor: RequestInterceptor): HttpClient {
78+
get getConfig() {
79+
return this.config;
80+
}
81+
82+
addRequestInterceptor(interceptor: RequestInterceptor) {
6983
this.interceptors.request.push(interceptor);
7084
return this;
7185
}
7286

73-
addResponseInterceptor(interceptor: ResponseInterceptor): HttpClient {
87+
addResponseInterceptor(interceptor: ResponseInterceptor) {
7488
this.interceptors.response.push(interceptor);
7589
return this;
7690
}
7791

78-
private async applyRequestInterceptors(config: RequestConfig): Promise<RequestConfig> {
92+
private async applyRequestInterceptors(config: RequestConfig) {
7993
let processedConfig = config;
8094

8195
for (const interceptor of this.interceptors.request) {
@@ -85,7 +99,7 @@ export class HttpClient {
8599
return processedConfig;
86100
}
87101

88-
private async applyResponseInterceptors<T>(response: HttpResponse<T>): Promise<HttpResponse<T>> {
102+
private async applyResponseInterceptors<T>(response: HttpResponse<T>) {
89103
let processedResponse = response;
90104

91105
for (const interceptor of this.interceptors.response) {
@@ -95,13 +109,13 @@ export class HttpClient {
95109
return processedResponse;
96110
}
97111

98-
private async makeRequest<T = any>(config: RequestConfig): Promise<HttpResponse<T>> {
112+
private async makeRequest<T = any>(config: RequestConfig) {
99113
try {
100114
const mergedConfig: RequestConfig = {
101115
...config,
102116
headers: mergeHeaders(this.config.headers, config.headers),
103117
params: mergeParams(this.config.params, config.params),
104-
timeout: config.timeout || this.config.timeout || 30000,
118+
timeout: config.timeout || this.config.timeout || this.initValues.timeout,
105119
};
106120

107121
const processedConfig = await this.applyRequestInterceptors(mergedConfig);
@@ -167,22 +181,15 @@ export class HttpClient {
167181
}
168182
}
169183

170-
async get<T = any>(
171-
url: string,
172-
config?: Partial<Omit<RequestConfig, 'method'>>,
173-
): Promise<HttpResponse<T>> {
184+
async get<T = any>(url: string, config?: Partial<Omit<RequestConfig, 'method'>>) {
174185
return this.makeRequest<T>({
175186
url,
176187
method: 'GET',
177188
...config,
178189
});
179190
}
180191

181-
async post<T = any>(
182-
url: string,
183-
body?: any,
184-
config?: Partial<Omit<RequestConfig, 'method'>>,
185-
): Promise<HttpResponse<T>> {
192+
async post<T = any>(url: string, body?: any, config?: Partial<Omit<RequestConfig, 'method'>>) {
186193
return this.makeRequest<T>({
187194
url,
188195
method: 'POST',
@@ -191,11 +198,7 @@ export class HttpClient {
191198
});
192199
}
193200

194-
async put<T = any>(
195-
url: string,
196-
body?: any,
197-
config?: Partial<Omit<RequestConfig, 'method'>>,
198-
): Promise<HttpResponse<T>> {
201+
async put<T = any>(url: string, body?: any, config?: Partial<Omit<RequestConfig, 'method'>>) {
199202
return this.makeRequest<T>({
200203
url,
201204
method: 'PUT',
@@ -204,22 +207,15 @@ export class HttpClient {
204207
});
205208
}
206209

207-
async delete<T = any>(
208-
url: string,
209-
config?: Partial<Omit<RequestConfig, 'method'>>,
210-
): Promise<HttpResponse<T>> {
210+
async delete<T = any>(url: string, config?: Partial<Omit<RequestConfig, 'method'>>) {
211211
return this.makeRequest<T>({
212212
url,
213213
method: 'DELETE',
214214
...config,
215215
});
216216
}
217217

218-
async patch<T = any>(
219-
url: string,
220-
body?: any,
221-
config?: Partial<Omit<RequestConfig, 'method'>>,
222-
): Promise<HttpResponse<T>> {
218+
async patch<T = any>(url: string, body?: any, config?: Partial<Omit<RequestConfig, 'method'>>) {
223219
return this.makeRequest<T>({
224220
url,
225221
method: 'PATCH',
@@ -228,21 +224,15 @@ export class HttpClient {
228224
});
229225
}
230226

231-
async head<T = any>(
232-
url: string,
233-
config?: Partial<Omit<RequestConfig, 'method'>>,
234-
): Promise<HttpResponse<T>> {
227+
async head<T = any>(url: string, config?: Partial<Omit<RequestConfig, 'method'>>) {
235228
return this.makeRequest<T>({
236229
url,
237230
method: 'HEAD',
238231
...config,
239232
});
240233
}
241234

242-
async options<T = any>(
243-
url: string,
244-
config?: Partial<Omit<RequestConfig, 'method'>>,
245-
): Promise<HttpResponse<T>> {
235+
async options<T = any>(url: string, config?: Partial<Omit<RequestConfig, 'method'>>) {
246236
return this.makeRequest<T>({
247237
url,
248238
method: 'OPTIONS',
@@ -253,8 +243,4 @@ export class HttpClient {
253243
async request<T = any>(config: RequestConfig): Promise<HttpResponse<T>> {
254244
return this.makeRequest<T>(config);
255245
}
256-
257-
getConfig(): HttpClientConfig {
258-
return this.config;
259-
}
260246
}

src/helpers/get-fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function isNodeEnvironment(): boolean {
1+
function isNodeEnvironment() {
22
return (
33
typeof process !== 'undefined' && process.versions != null && process.versions.node != null
44
);

src/helpers/utils.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HttpCode } from '../constants';
22
import { HttpError, HttpHeaders, QueryParams } from '../types';
33

4-
export function buildURL(baseURL: string = '', endpoint: string, params?: QueryParams): string {
4+
export function buildURL(baseURL: string = '', endpoint: string, params?: QueryParams) {
55
const cleanBaseURL = baseURL.replace(/\/$/, '');
66
const cleanEndpoint = endpoint.replace(/^\//, '');
77
let fullURL = cleanBaseURL ? `${cleanBaseURL}/${cleanEndpoint}` : cleanEndpoint;
@@ -20,25 +20,29 @@ export function buildURL(baseURL: string = '', endpoint: string, params?: QueryP
2020
return fullURL;
2121
}
2222

23-
export function mergeHeaders(...headerObjects: (HttpHeaders | undefined)[]): HttpHeaders {
24-
return headerObjects.reduce<HttpHeaders>((merged, headers) => {
25-
if (headers) {
26-
return { ...merged, ...headers };
27-
}
28-
return merged;
29-
}, {} as HttpHeaders);
23+
export function mergeHeaders(...headerObjects: (HttpHeaders | undefined)[]) {
24+
return (
25+
headerObjects.reduce<HttpHeaders>((merged, headers) => {
26+
if (headers) {
27+
return { ...merged, ...headers };
28+
}
29+
return merged;
30+
}, {} as HttpHeaders) || {}
31+
);
3032
}
3133

32-
export function mergeParams(...paramObjects: (QueryParams | undefined)[]): QueryParams {
33-
return paramObjects.reduce<QueryParams>((merged, params) => {
34-
if (params) {
35-
return { ...merged, ...params };
36-
}
37-
return merged;
38-
}, {} as QueryParams);
34+
export function mergeParams(...paramObjects: (QueryParams | undefined)[]) {
35+
return (
36+
paramObjects.reduce<QueryParams>((merged, params) => {
37+
if (params) {
38+
return { ...merged, ...params };
39+
}
40+
return merged;
41+
}, {} as QueryParams) || {}
42+
);
3943
}
4044

41-
export function createTimeoutSignal(timeoutMs: number): AbortSignal {
45+
export function createTimeoutSignal(timeoutMs: number) {
4246
if (typeof AbortSignal !== 'undefined' && AbortSignal.timeout) {
4347
return AbortSignal.timeout(timeoutMs);
4448
}
@@ -48,13 +52,15 @@ export function createTimeoutSignal(timeoutMs: number): AbortSignal {
4852
return controller.signal;
4953
}
5054

51-
export function createHttpError(error: any, status: number = 0): HttpError {
55+
export function createHttpError(error: any, status: number = 0) {
5256
const message = error?.message || error?.toString() || 'Unknown error';
5357
const code = HttpCode[status] || 'UNKNOWN_ERROR';
5458

55-
return {
59+
const payload: HttpError = {
5660
message,
5761
status,
5862
code,
5963
};
64+
65+
return payload;
6066
}

0 commit comments

Comments
 (0)