Skip to content

Commit d77130d

Browse files
fix: formatting, common api-handler function
1 parent 9d26684 commit d77130d

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

src/util/api-request-handler.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,42 @@ import { formatErrors } from "./error-helper";
33

44
interface RequestParams {
55
orgUid: string;
6+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
67
queryParams?: Record<string, any>;
78
payload?: any;
89
url: string;
910
}
1011

1112
export async function apiRequestHandler(params: RequestParams): Promise<any> {
12-
const { orgUid, queryParams, payload, url } = params;
13+
const { orgUid, method, queryParams, payload, url } = params;
1314
const authtoken = configHandler.get('authtoken');
14-
15+
1516
const headers = {
1617
organization_uid: orgUid,
1718
authtoken
18-
}
19-
const httpClient = new HttpClient()
20-
httpClient.headers(headers)
21-
if (queryParams)
19+
};
20+
21+
const httpClient = new HttpClient();
22+
httpClient.headers(headers);
23+
24+
if (queryParams) {
2225
httpClient.queryParams(queryParams);
26+
}
2327

2428
try {
25-
const response = await httpClient.put(url, payload)
29+
let response;
30+
if (method === 'GET') {
31+
response = await httpClient.get(url);
32+
} else if (method === 'POST') {
33+
response = await httpClient.post(url, payload);
34+
} else if (method === 'PUT') {
35+
response = await httpClient.put(url, payload);
36+
} else if (method === 'DELETE') {
37+
response = await httpClient.delete(url);
38+
} else {
39+
throw new Error(`Unsupported HTTP method: ${method}`);
40+
}
41+
2642
const { status, data } = response;
2743
if (status >= 200 && status < 300) {
2844
return data;

src/util/common-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ async function reinstallApp(params: {
157157
queryParams: params,
158158
payload,
159159
url,
160-
})
160+
method: "PUT"
161+
})
161162
return result
162163
} catch (err) {
163164
throw err

src/util/error-helper.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
export function formatErrors(errors: any): string {
2-
const errorMessages: string[] = [];
3-
for (const errorKey in errors) {
4-
const errorValue = errors[errorKey];
5-
if (Array.isArray(errorValue)) {
6-
errorMessages.push(...errorValue.map((error: any) => formatError(error)));
7-
} else {
8-
errorMessages.push(formatError(errorValue));
9-
}
2+
const errorMessages: string[] = [];
3+
for (const errorKey in errors) {
4+
const errorValue = errors[errorKey];
5+
if (Array.isArray(errorValue)) {
6+
errorMessages.push(...errorValue.map((error: any) => formatError(error)));
7+
} else {
8+
errorMessages.push(formatError(errorValue));
109
}
11-
return errorMessages.join(' ');
1210
}
13-
function formatError(error: any): string {
14-
if (typeof error === 'object') {
15-
return Object.values(error).join(' ');
16-
}
17-
return String(error);
18-
}
11+
return errorMessages.join(" ");
12+
}
13+
function formatError(error: any): string {
14+
if (typeof error === "object") {
15+
return Object.values(error).join(" ");
16+
}
17+
return String(error);
18+
}

0 commit comments

Comments
 (0)