Files
parcer/server/src/api/record.ts

58 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-09-26 22:39:39 +05:30
import { deleteFile, readFile, readFiles, saveFile } from "../workflow-management/storage";
import { Router, Request, Response } from 'express';
2024-09-26 22:41:46 +05:30
import { requireAPIKey } from "../middlewares/api";
2024-09-26 22:39:39 +05:30
export const router = Router();
const formatRecording = (recordingData: any) => {
const recordingMeta = recordingData.recording_meta;
const workflow = recordingData.recording.workflow || [];
const firstWorkflowStep = workflow[0]?.where?.url || '';
2024-09-26 22:39:57 +05:30
2024-09-26 22:39:39 +05:30
const inputParameters = [
2024-09-26 22:39:57 +05:30
{
type: "string",
name: "originUrl",
label: "Origin URL",
required: true,
defaultValue: firstWorkflowStep,
},
2024-09-26 22:39:39 +05:30
];
2024-09-26 22:39:57 +05:30
2024-09-26 22:39:39 +05:30
return {
2024-09-26 22:39:57 +05:30
id: recordingMeta.id,
name: recordingMeta.name,
createdAt: new Date(recordingMeta.create_date).getTime(),
inputParameters,
2024-09-26 22:39:39 +05:30
};
2024-09-26 22:39:57 +05:30
};
2024-09-26 22:41:46 +05:30
router.get("/api/recordings", requireAPIKey, async (req: Request, res: Response) => {
2024-09-26 22:39:39 +05:30
try {
2024-09-26 22:39:57 +05:30
const fileContents = await readFiles('./../storage/recordings/');
const formattedRecordings = fileContents.map((fileContent: string) => {
const recordingData = JSON.parse(fileContent);
return formatRecording(recordingData);
});
const response = {
statusCode: 200,
messageCode: "success",
robots: {
totalCount: formattedRecordings.length,
items: formattedRecordings,
},
};
res.status(200).json(response);
2024-09-26 22:39:39 +05:30
} catch (error) {
2024-09-26 22:39:57 +05:30
console.error("Error fetching recordings:", error);
res.status(500).json({
statusCode: 500,
messageCode: "error",
message: "Failed to retrieve recordings",
});
2024-09-26 22:39:39 +05:30
}
2024-09-26 22:39:57 +05:30
});