refactor: introduced helper method to remove redundant code
This commit is contained in:
1 parent
d2c13fac5a
commit
f637b01631
3 files changed
+74
-84
No files matched your search
@@ -40,6 +40,62 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
return type === "http";
|
return type === "http";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private buildResponse<T>(
|
||||||
|
monitor: Monitor,
|
||||||
|
opts: {
|
||||||
|
body: string;
|
||||||
|
contentType: string;
|
||||||
|
statusCode: number;
|
||||||
|
statusUp: boolean;
|
||||||
|
message: string;
|
||||||
|
responseTime: number;
|
||||||
|
timings?: MonitorStatusResponse<T>["timings"];
|
||||||
|
}
|
||||||
|
): MonitorStatusResponse<T> {
|
||||||
|
const { body, contentType, statusCode, statusUp, message, responseTime, timings } = opts;
|
||||||
|
const isJson = contentType.includes("application/json");
|
||||||
|
|
||||||
|
if (monitor.jsonPath && !isJson) {
|
||||||
|
return {
|
||||||
|
monitorId: monitor.id,
|
||||||
|
teamId: monitor.teamId,
|
||||||
|
type: monitor.type,
|
||||||
|
status: false,
|
||||||
|
code: statusCode,
|
||||||
|
message: "Response is not JSON",
|
||||||
|
responseTime,
|
||||||
|
timings,
|
||||||
|
payload: body,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let payload: T | string;
|
||||||
|
if (isJson) {
|
||||||
|
try {
|
||||||
|
payload = JSON.parse(body) as T;
|
||||||
|
} catch {
|
||||||
|
payload = body;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
payload = body;
|
||||||
|
}
|
||||||
|
|
||||||
|
const matchResult = this.advancedMatcher.validate<T | string>(payload, monitor);
|
||||||
|
|
||||||
|
return {
|
||||||
|
monitorId: monitor.id,
|
||||||
|
teamId: monitor.teamId,
|
||||||
|
type: monitor.type,
|
||||||
|
status: statusUp && matchResult.ok,
|
||||||
|
code: statusCode,
|
||||||
|
message: matchResult.ok ? message : matchResult.message,
|
||||||
|
responseTime,
|
||||||
|
timings,
|
||||||
|
payload,
|
||||||
|
extracted: matchResult.extracted,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private handleHttpError<T>(error: unknown, monitor: Monitor): MonitorStatusResponse<T> {
|
private handleHttpError<T>(error: unknown, monitor: Monitor): MonitorStatusResponse<T> {
|
||||||
if (error instanceof HTTPError || error instanceof RequestError) {
|
if (error instanceof HTTPError || error instanceof RequestError) {
|
||||||
const statusCode = error.response?.statusCode;
|
const statusCode = error.response?.statusCode;
|
||||||
@@ -60,50 +116,15 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const { jsonPath } = monitor;
|
return this.buildResponse<T>(monitor, {
|
||||||
const body = error.response?.body ?? "";
|
body: (error.response?.body ?? "") as string,
|
||||||
const contentType = error.response?.headers?.["content-type"] || "";
|
contentType: error.response?.headers?.["content-type"] || "",
|
||||||
const isJson = contentType.includes("application/json");
|
statusCode: statusCode ?? NETWORK_ERROR,
|
||||||
|
statusUp,
|
||||||
if (jsonPath && !isJson) {
|
message: error.message,
|
||||||
return {
|
|
||||||
monitorId: monitor.id,
|
|
||||||
teamId: monitor.teamId,
|
|
||||||
type: monitor.type,
|
|
||||||
status: false,
|
|
||||||
code: statusCode ?? NETWORK_ERROR,
|
|
||||||
message: "Response is not JSON",
|
|
||||||
responseTime,
|
|
||||||
timings: error.timings,
|
|
||||||
payload: body as unknown as T,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let payload: T;
|
|
||||||
if (isJson) {
|
|
||||||
try {
|
|
||||||
payload = JSON.parse(body) as T;
|
|
||||||
} catch {
|
|
||||||
payload = body as unknown as T;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
payload = body as unknown as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
const matchResult = this.advancedMatcher.validate<T>(payload, monitor);
|
|
||||||
|
|
||||||
return {
|
|
||||||
monitorId: monitor.id,
|
|
||||||
teamId: monitor.teamId,
|
|
||||||
type: monitor.type,
|
|
||||||
status: statusUp && matchResult.ok,
|
|
||||||
code: statusCode ?? NETWORK_ERROR,
|
|
||||||
message: matchResult.ok ? error.message : matchResult.message,
|
|
||||||
responseTime,
|
responseTime,
|
||||||
timings: error.timings,
|
timings: error.timings,
|
||||||
payload,
|
});
|
||||||
extracted: matchResult.extracted,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -119,7 +140,7 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handle<T>(monitor: Monitor): Promise<MonitorStatusResponse<T>> {
|
async handle<T>(monitor: Monitor): Promise<MonitorStatusResponse<T>> {
|
||||||
const { url, secret, jsonPath, ignoreTlsErrors } = monitor;
|
const { url, secret, ignoreTlsErrors } = monitor;
|
||||||
|
|
||||||
if (!url) {
|
if (!url) {
|
||||||
throw new Error("URL is required for HTTP monitor");
|
throw new Error("URL is required for HTTP monitor");
|
||||||
@@ -136,48 +157,17 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await this.got<string>(url, options);
|
const response = await this.got<string>(url, options);
|
||||||
const contentType = response.headers["content-type"] || "";
|
|
||||||
const isJson = contentType.includes("application/json");
|
|
||||||
|
|
||||||
if (jsonPath && !isJson) {
|
|
||||||
return {
|
|
||||||
monitorId: monitor.id,
|
|
||||||
teamId: monitor.teamId,
|
|
||||||
type: monitor.type,
|
|
||||||
status: false,
|
|
||||||
code: response.statusCode,
|
|
||||||
message: "Response is not JSON",
|
|
||||||
responseTime: response.timings.phases.firstByte ?? 0,
|
|
||||||
timings: response.timings,
|
|
||||||
payload: response.body as unknown as T,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let payload: T;
|
|
||||||
if (isJson) {
|
|
||||||
try {
|
|
||||||
payload = JSON.parse(response.body) as T;
|
|
||||||
} catch {
|
|
||||||
payload = response.body as unknown as T;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
payload = response.body as unknown as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
const matchResult = this.advancedMatcher.validate<T>(payload, monitor);
|
|
||||||
const statusUp = isStatusUp(response.statusCode, monitor.customUpCodes);
|
const statusUp = isStatusUp(response.statusCode, monitor.customUpCodes);
|
||||||
return {
|
|
||||||
monitorId: monitor.id,
|
return this.buildResponse<T>(monitor, {
|
||||||
teamId: monitor.teamId,
|
body: response.body,
|
||||||
type: monitor.type,
|
contentType: response.headers["content-type"] || "",
|
||||||
status: statusUp && matchResult.ok,
|
statusCode: response.statusCode,
|
||||||
code: response.statusCode,
|
statusUp,
|
||||||
message: matchResult.ok ? (response.statusMessage ?? "OK") : matchResult.message,
|
message: response.statusMessage ?? "OK",
|
||||||
responseTime: response.timings.phases.firstByte ?? 0,
|
responseTime: response.timings.phases.firstByte ?? 0,
|
||||||
timings: response.timings,
|
timings: response.timings,
|
||||||
payload,
|
});
|
||||||
extracted: matchResult.extracted,
|
|
||||||
};
|
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
return this.handleHttpError(error, monitor);
|
return this.handleHttpError(error, monitor);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
|
|||||||
const breaches: ThresholdBreach[] = [];
|
const breaches: ThresholdBreach[] = [];
|
||||||
|
|
||||||
// Check if this is a hardware monitor with threshold data
|
// Check if this is a hardware monitor with threshold data
|
||||||
if (monitor.type !== "hardware" || !monitorStatusResponse.payload) {
|
if (monitor.type !== "hardware" || !monitorStatusResponse.payload || typeof monitorStatusResponse.payload === "string") {
|
||||||
return breaches;
|
return breaches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export interface MonitorStatusResponse<
|
|||||||
code: number;
|
code: number;
|
||||||
message: string;
|
message: string;
|
||||||
responseTime?: number;
|
responseTime?: number;
|
||||||
payload?: T | null;
|
payload?: T | string | null;
|
||||||
timings?: GotTimings;
|
timings?: GotTimings;
|
||||||
first_byte_took?: number;
|
first_byte_took?: number;
|
||||||
body_read_took?: number;
|
body_read_took?: number;
|
||||||
|
|||||||
Reference in new issue
Block a user