From 3111f5657189a2824643da2428ad29c59a543b4c Mon Sep 17 00:00:00 2001 From: DarrenT~ Date: Thu, 8 May 2025 14:13:21 +0200 Subject: [PATCH] fix typing issue --- frontend/src/components/CanvasEndpoints.tsx | 44 +++++++++++++++++-- .../CanvasApiEndpointController.ts | 18 +++++++- src/apps/canvas-api/routes/EndpointsRoutes.ts | 4 ++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/frontend/src/components/CanvasEndpoints.tsx b/frontend/src/components/CanvasEndpoints.tsx index 785022c..5d94326 100644 --- a/frontend/src/components/CanvasEndpoints.tsx +++ b/frontend/src/components/CanvasEndpoints.tsx @@ -41,6 +41,7 @@ export default function CanvasEndpoints() { const [chatLoading, setChatLoading] = useState(false); const [chatError, setChatError] = useState(null); const toast = useRef(null); + const [editEndpointId, setEditEndpointId] = useState(null); useEffect(() => { initFilters(); @@ -203,6 +204,40 @@ export default function CanvasEndpoints() { } }; + const openEditModal = (endpoint: any) => { + setEditEndpointId(endpoint.id); + setNewEndpoint({ + name: endpoint.name, + method: endpoint.method, + path: endpoint.path, + description: endpoint.description || '', + }); + setShowCreateModal(true); + }; + + const handleSaveEndpoint = async () => { + try { + const sanitizedEndpoint = { ...newEndpoint, path: sanitizeCanvasPath(newEndpoint.path) }; + if (editEndpointId) { + await api('put', `/api/v1/canvas-api/endpoints/${editEndpointId}`, sanitizedEndpoint); + } else { + await api('post', '/api/v1/canvas-api/endpoints', sanitizedEndpoint); + } + setShowCreateModal(false); + setNewEndpoint({ name: '', method: '', path: '', description: '' }); + setEditEndpointId(null); + fetchEndpoints(); + } catch (error) { + console.error('Failed to save endpoint:', error); + } + }; + + const handleModalHide = () => { + setShowCreateModal(false); + setNewEndpoint({ name: '', method: '', path: '', description: '' }); + setEditEndpointId(null); + }; + const renderHeader = () => ( (
); @@ -274,12 +310,12 @@ export default function CanvasEndpoints() { setShowCreateModal(false)} - header="Create New Endpoint" + onHide={handleModalHide} + header={editEndpointId ? "Edit Endpoint" : "Create New Endpoint"} footer={
-
} > diff --git a/src/apps/canvas-api/http/controllers/CanvasApiEndpointController.ts b/src/apps/canvas-api/http/controllers/CanvasApiEndpointController.ts index 22bc825..4cec97f 100644 --- a/src/apps/canvas-api/http/controllers/CanvasApiEndpointController.ts +++ b/src/apps/canvas-api/http/controllers/CanvasApiEndpointController.ts @@ -22,12 +22,26 @@ export class CanvasApiEndpointController { return reply.send(endpoint); } - async delete(request: FastifyRequest, reply: FastifyReply) { - const id = Number(request.params['id']); + async delete(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) { + const id = Number(request.params.id); if (!id) return reply.status(400).send({ error: 'Missing id' }); const endpoint = await request.em.findOne(CanvasApiEndpoint, { id }); if (!endpoint) return reply.status(404).send({ error: 'Not found' }); await request.em.removeAndFlush(endpoint); return reply.send({ success: true }); } + + async update(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) { + const id = Number(request.params.id); + if (!id) return reply.status(400).send({ error: 'Missing id' }); + const endpoint = await request.em.findOne(CanvasApiEndpoint, { id }); + if (!endpoint) return reply.status(404).send({ error: 'Not found' }); + const { name, method, path, description } = request.body as any; + if (name) endpoint.name = name; + if (method) endpoint.method = method; + if (path) endpoint.path = (path as string).replace(/^\/api\/v1/, ''); + if (description !== undefined) endpoint.description = description; + await request.em.persistAndFlush(endpoint); + return reply.send(endpoint); + } } \ No newline at end of file diff --git a/src/apps/canvas-api/routes/EndpointsRoutes.ts b/src/apps/canvas-api/routes/EndpointsRoutes.ts index 76c2cba..572180f 100644 --- a/src/apps/canvas-api/routes/EndpointsRoutes.ts +++ b/src/apps/canvas-api/routes/EndpointsRoutes.ts @@ -26,6 +26,10 @@ const endpointsRoutes: FastifyPluginAsync = async (app) => { return controller.delete(request, reply); }); + app.put('/endpoints/:id', async (request: FastifyRequest, reply: FastifyReply) => { + return controller.update(request, reply); + }); + // Dynamic proxy route for external Canvas REST API app.post('/proxy-external', async (request: FastifyRequest, reply: FastifyReply) => { try {