fix: replace nested ternaries and remove type casts per review
- useNotificationForm: replace nested ternary with if/else if/else block - TelegramProvider: type error as unknown and access properties safely via instanceof checks instead of casting to HTTPError
This commit is contained in:
1 parent
d1f1d31ea2
commit
fe242b1977
2 files changed
+33
-26
No files matched your search
@@ -8,27 +8,30 @@ interface UseNotificationFormOptions {
|
||||
|
||||
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
|
||||
return useMemo(() => {
|
||||
const defaults =
|
||||
data?.type === "matrix"
|
||||
? {
|
||||
type: "matrix" as const,
|
||||
notificationName: data.notificationName || "",
|
||||
homeserverUrl: data.homeserverUrl || "",
|
||||
roomId: data.roomId || "",
|
||||
accessToken: data.accessToken || "",
|
||||
}
|
||||
: data?.type === "telegram"
|
||||
? {
|
||||
type: "telegram" as const,
|
||||
notificationName: data.notificationName || "",
|
||||
address: data.address || "",
|
||||
accessToken: data.accessToken || "",
|
||||
}
|
||||
: {
|
||||
type: (data?.type || "email") as Exclude<Notification["type"], "matrix" | "telegram">,
|
||||
notificationName: data?.notificationName || "",
|
||||
address: data?.address || "",
|
||||
};
|
||||
let defaults;
|
||||
|
||||
if (data?.type === "matrix") {
|
||||
defaults = {
|
||||
type: "matrix" as const,
|
||||
notificationName: data.notificationName || "",
|
||||
homeserverUrl: data.homeserverUrl || "",
|
||||
roomId: data.roomId || "",
|
||||
accessToken: data.accessToken || "",
|
||||
};
|
||||
} else if (data?.type === "telegram") {
|
||||
defaults = {
|
||||
type: "telegram" as const,
|
||||
notificationName: data.notificationName || "",
|
||||
address: data.address || "",
|
||||
accessToken: data.accessToken || "",
|
||||
};
|
||||
} else {
|
||||
defaults = {
|
||||
type: (data?.type || "email") as Exclude<Notification["type"], "matrix" | "telegram">,
|
||||
notificationName: data?.notificationName || "",
|
||||
address: data?.address || "",
|
||||
};
|
||||
}
|
||||
|
||||
return { schema: notificationSchema, defaults };
|
||||
}, [data]);
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { Notification } from "@/types/index.js";
|
||||
import { INotificationProvider } from "@/service/index.js";
|
||||
import type { NotificationMessage } from "@/types/notificationMessage.js";
|
||||
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
|
||||
import got, { HTTPError } from "got";
|
||||
import got from "got";
|
||||
import { ILogger } from "@/utils/logger.js";
|
||||
|
||||
export class TelegramProvider implements INotificationProvider {
|
||||
@@ -28,12 +28,14 @@ export class TelegramProvider implements INotificationProvider {
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
const err = error as HTTPError;
|
||||
const errMsg = error instanceof Error ? error.message : "unknown error";
|
||||
const errStack = error instanceof Error ? error.stack : undefined;
|
||||
this.logger.warn({
|
||||
message: "Telegram test alert failed",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendTestAlert",
|
||||
stack: err?.stack,
|
||||
stack: errStack,
|
||||
error: errMsg,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
@@ -61,12 +63,14 @@ export class TelegramProvider implements INotificationProvider {
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
const err = error as HTTPError;
|
||||
const errMsg = error instanceof Error ? error.message : "unknown error";
|
||||
const errStack = error instanceof Error ? error.stack : undefined;
|
||||
this.logger.warn({
|
||||
message: "Telegram alert failed",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendMessage",
|
||||
stack: err?.stack,
|
||||
stack: errStack,
|
||||
error: errMsg,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user