refactor: Improve code formatting and readability in various service and utility files

This commit is contained in:
Nawaz Dhandala
2025-09-19 20:58:32 +01:00
parent 5e7ed2be73
commit af7835fc8a
6 changed files with 196 additions and 166 deletions

View File

@@ -24,7 +24,11 @@ export class StatusPageCertificateService extends BaseService {
const httpProtocol: Protocol = await DatabaseConfig.getHttpProtocol();
return await API.post<EmptyResponseData>({
url: new URL(httpProtocol, AppApiHostname, new Route("/api/workers/cert")),
url: new URL(
httpProtocol,
AppApiHostname,
new Route("/api/workers/cert"),
),
data: body,
});
}
@@ -40,7 +44,11 @@ export class StatusPageCertificateService extends BaseService {
};
return await API.delete<EmptyResponseData>({
url: new URL(httpProtocol, AppApiHostname, new Route("/api/workers/cert")),
url: new URL(
httpProtocol,
AppApiHostname,
new Route("/api/workers/cert"),
),
data: body,
});
}
@@ -54,7 +62,11 @@ export class StatusPageCertificateService extends BaseService {
const httpProtocol: Protocol = await DatabaseConfig.getHttpProtocol();
return await API.get<JSONObject>({
url: new URL(httpProtocol, AppApiHostname, new Route("/api/workers/cert")),
url: new URL(
httpProtocol,
AppApiHostname,
new Route("/api/workers/cert"),
),
data: body,
});
}

View File

@@ -218,20 +218,22 @@ export default class GitHubUtil extends HostedCodeRepository {
`https://api.github.com/repos/${data.organizationName}/${data.repositoryName}/pulls`,
);
const result: HTTPErrorResponse | HTTPResponse<JSONObject> = await API.post({
url: url,
data: {
base: data.baseBranchName,
head: data.headBranchName,
title: data.title,
body: data.body,
const result: HTTPErrorResponse | HTTPResponse<JSONObject> = await API.post(
{
url: url,
data: {
base: data.baseBranchName,
head: data.headBranchName,
title: data.title,
body: data.body,
},
headers: {
Authorization: `Bearer ${gitHubToken}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
},
headers: {
Authorization: `Bearer ${gitHubToken}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
});
);
if (result instanceof HTTPErrorResponse) {
throw result;

View File

@@ -118,21 +118,23 @@ export default class SlackUtil extends WorkspaceBase {
logger.debug(JSON.stringify(modalJson, null, 2));
// use view.open API to show modal
const result: HTTPErrorResponse | HTTPResponse<JSONObject> = await API.post({
url: URL.fromString("https://slack.com/api/views.open"),
data: {
trigger_id: data.triggerId,
view: modalJson,
const result: HTTPErrorResponse | HTTPResponse<JSONObject> = await API.post(
{
url: URL.fromString("https://slack.com/api/views.open"),
data: {
trigger_id: data.triggerId,
view: modalJson,
},
headers: {
Authorization: `Bearer ${data.authToken}`,
["Content-Type"]: "application/json",
},
options: {
retries: 3,
exponentialBackoff: true,
},
},
headers: {
Authorization: `Bearer ${data.authToken}`,
["Content-Type"]: "application/json",
},
options: {
retries: 3,
exponentialBackoff: true,
},
});
);
if (result instanceof HTTPErrorResponse) {
logger.error("Error response from Slack API:");

View File

@@ -85,41 +85,31 @@ export default class API {
public async get<
T extends JSONObject | JSONArray | BaseModel | Array<BaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse> {
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse> {
return await API.get<T>(options);
}
public async delete<
T extends JSONObject | JSONArray | BaseModel | Array<BaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse> {
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse> {
return await API.delete<T>(options);
}
public async head<
T extends JSONObject | JSONArray | BaseModel | Array<BaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse> {
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse> {
return await API.head<T>(options);
}
public async put<
T extends JSONObject | JSONArray | BaseModel | Array<BaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse> {
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse> {
return await API.put<T>(options);
}
public async patch<
T extends JSONObject | JSONArray | BaseModel | Array<BaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse> {
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse> {
return await API.patch<T>(options);
}
@@ -180,9 +170,7 @@ export default class API {
| Array<BaseModel>
| AnalyticsBaseModel
| Array<AnalyticsBaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse>;
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse>;
public static async get<
T extends
| JSONObject
@@ -207,21 +195,26 @@ export default class API {
undefined,
options,
);
} else {
// New signature
const { url, data: newData, headers: newHeaders, params, options: newOptions } = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch<T>(
HTTPMethod.GET,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
// New signature
const {
url,
data: newData,
headers: newHeaders,
params,
options: newOptions,
} = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch<T>(
HTTPMethod.GET,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
public static async delete<
@@ -246,9 +239,7 @@ export default class API {
| Array<BaseModel>
| AnalyticsBaseModel
| Array<AnalyticsBaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse>;
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse>;
public static async delete<
T extends
| JSONObject
@@ -273,21 +264,26 @@ export default class API {
undefined,
options,
);
} else {
// New signature
const { url, data: newData, headers: newHeaders, params, options: newOptions } = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.DELETE,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
// New signature
const {
url,
data: newData,
headers: newHeaders,
params,
options: newOptions,
} = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.DELETE,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
public static async head<
@@ -312,9 +308,7 @@ export default class API {
| Array<BaseModel>
| AnalyticsBaseModel
| Array<AnalyticsBaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse>;
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse>;
public static async head<
T extends
| JSONObject
@@ -339,21 +333,26 @@ export default class API {
undefined,
options,
);
} else {
// New signature
const { url, data: newData, headers: newHeaders, params, options: newOptions } = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.HEAD,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
// New signature
const {
url,
data: newData,
headers: newHeaders,
params,
options: newOptions,
} = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.HEAD,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
public static async put<
@@ -378,9 +377,7 @@ export default class API {
| Array<BaseModel>
| AnalyticsBaseModel
| Array<AnalyticsBaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse>;
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse>;
public static async put<
T extends
| JSONObject
@@ -405,21 +402,26 @@ export default class API {
undefined,
options,
);
} else {
// New signature
const { url, data: newData, headers: newHeaders, params, options: newOptions } = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.PUT,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
// New signature
const {
url,
data: newData,
headers: newHeaders,
params,
options: newOptions,
} = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.PUT,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
public static async patch<
@@ -444,9 +446,7 @@ export default class API {
| Array<BaseModel>
| AnalyticsBaseModel
| Array<AnalyticsBaseModel>,
>(
options: APIRequestOptions,
): Promise<HTTPResponse<T> | HTTPErrorResponse>;
>(options: APIRequestOptions): Promise<HTTPResponse<T> | HTTPErrorResponse>;
public static async patch<
T extends
| JSONObject
@@ -471,21 +471,26 @@ export default class API {
undefined,
options,
);
} else {
// New signature
const { url, data: newData, headers: newHeaders, params, options: newOptions } = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.PATCH,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
// New signature
const {
url,
data: newData,
headers: newHeaders,
params,
options: newOptions,
} = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.PATCH,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
public static async post<
@@ -512,21 +517,26 @@ export default class API {
undefined,
options,
);
} else {
// New signature
const { url, data: newData, headers: newHeaders, params, options: newOptions } = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.POST,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
// New signature
const {
url,
data: newData,
headers: newHeaders,
params,
options: newOptions,
} = urlOrOptions;
if (!url) {
throw new APIException("URL is required for static method");
}
return await this.fetch(
HTTPMethod.POST,
url,
newData || undefined,
newHeaders,
params,
newOptions,
);
}
public static async fetch<
@@ -555,21 +565,27 @@ export default class API {
params,
options,
);
} else {
// New signature
const { method, url: newUrl, data: newData, headers: newHeaders, params: newParams, options: newOptions } = methodOrOptions;
if (!newUrl) {
throw new APIException("URL is required for static method");
}
return await this.fetchInternal(
method,
newUrl,
newData,
newHeaders,
newParams,
newOptions,
);
}
// New signature
const {
method,
url: newUrl,
data: newData,
headers: newHeaders,
params: newParams,
options: newOptions,
} = methodOrOptions;
if (!newUrl) {
throw new APIException("URL is required for static method");
}
return await this.fetchInternal(
method,
newUrl,
newData,
newHeaders,
newParams,
newOptions,
);
}
private static async fetchInternal<

View File

@@ -1,8 +1,4 @@
import React, {
FunctionComponent,
ReactElement,
useEffect,
} from "react";
import React, { FunctionComponent, ReactElement, useEffect } from "react";
import Modal from "Common/UI/Components/Modal/Modal";
import { ButtonStyleType } from "Common/UI/Components/Button/Button";
import Dictionary from "Common/UI/Components/Dictionary/Dictionary";
@@ -53,7 +49,7 @@ const SlackChannelCacheModal: FunctionComponent<ComponentProps> = (
const cacheObj: JSONObject = response.data || {};
const newChannelCache: { [channelName: string]: string } = {};
Object.keys(cacheObj).forEach((key: string) => {
const value: any = (cacheObj as any)[key];
// value may be either {id, name} or just {id}. Prefer id
@@ -61,7 +57,7 @@ const SlackChannelCacheModal: FunctionComponent<ComponentProps> = (
(value?.id as string) || (value as any)?.toString?.() || "";
newChannelCache[key] = id;
});
setChannelCache(newChannelCache);
} catch (e: unknown) {
setError(API.getFriendlyErrorMessage(e as Exception));

View File

@@ -986,7 +986,9 @@ const HomeFeatureSet: FeatureSet = {
if (!gitHubBasicInfo) {
const basicInfo: HTTPResponse<JSONObject> = await API.get({
url: URL.fromString("https://api.github.com/repos/oneuptime/oneuptime"),
url: URL.fromString(
"https://api.github.com/repos/oneuptime/oneuptime",
),
});
gitHubBasicInfo = basicInfo.data as JSONObject;