oneuptime/ProbeAPI/API/IncomingRequest.ts

91 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-07-29 14:11:10 +00:00
import Express, {
ExpressRequest,
ExpressResponse,
ExpressRouter,
NextFunction,
} from 'CommonServer/Utils/Express';
import Response from 'CommonServer/Utils/Response';
import ProbeApiIngestResponse from 'Common/Types/Probe/ProbeApiIngestResponse';
import BadDataException from 'Common/Types/Exception/BadDataException';
2023-07-30 15:44:08 +00:00
import ProbeMonitorResponseService from 'CommonServer/Utils/Probe/ProbeMonitorResponse';
2023-07-29 14:11:10 +00:00
import Dictionary from 'Common/Types/Dictionary';
import { JSONObject } from 'Common/Types/JSON';
2023-07-29 15:21:47 +00:00
import ObjectID from 'Common/Types/ObjectID';
import IncomingMonitorRequest from 'Common/Types/Monitor/IncomingMonitor/IncomingMonitorRequest';
2023-07-30 15:44:08 +00:00
import OneUptimeDate from 'Common/Types/Date';
2023-07-29 14:11:10 +00:00
const router: ExpressRouter = Express.getRouter();
2023-07-30 15:51:21 +00:00
const processIncomingRequest = async (req: ExpressRequest,
res: ExpressResponse,
next: NextFunction) => {
try {
const requestHeaders: Dictionary<string> =
req.headers as Dictionary<string>;
const requestBody: string | JSONObject = req.body as
| string
| JSONObject;
const monitorIdAsString: string | undefined =
req.params['monitor-id'];
if (!monitorIdAsString) {
throw new BadDataException('Monitor Id is required');
}
const monitorId: ObjectID = ObjectID.fromString(monitorIdAsString);
const incomingRequest: IncomingMonitorRequest = {
monitorId: monitorId,
requestHeaders: requestHeaders,
requestBody: requestBody,
incomingRequestReceivedAt: OneUptimeDate.getCurrentDate(),
onlyCheckForIncomingRequestReceivedAt: false,
};
// process probe response here.
const probeApiIngestResponse: ProbeApiIngestResponse =
await ProbeMonitorResponseService.processProbeResponse(
incomingRequest
);
return Response.sendJsonObjectResponse(req, res, {
monitorId: probeApiIngestResponse.monitorId.toString(),
rootCause: probeApiIngestResponse.rootCause,
criteriaMetId: probeApiIngestResponse.criteriaMetId?.toString(),
} as any);
} catch (err) {
return next(err);
}
}
2023-07-29 14:11:10 +00:00
router.post(
'/incoming-request/:monitor-id',
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
2023-07-30 15:51:21 +00:00
await processIncomingRequest(req, res, next);
2023-07-29 14:11:10 +00:00
}
);
2023-07-30 15:51:21 +00:00
router.get(
'/incoming-request/:monitor-id',
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
await processIncomingRequest(req, res, next);
}
);
2023-07-29 14:11:10 +00:00
export default router;