From dd7b7757f41dd8b3b5d5fed1fce3b3a5715d0880 Mon Sep 17 00:00:00 2001 From: Juanfran Date: Mon, 4 Mar 2024 08:43:53 +0100 Subject: [PATCH] chore: refactor delete api --- apps/rpc-api/src/app/routes/root.ts | 104 ++++++++++++++++++---------- 1 file changed, 67 insertions(+), 37 deletions(-) diff --git a/apps/rpc-api/src/app/routes/root.ts b/apps/rpc-api/src/app/routes/root.ts index 1a09169..69ddc8c 100644 --- a/apps/rpc-api/src/app/routes/root.ts +++ b/apps/rpc-api/src/app/routes/root.ts @@ -4,9 +4,70 @@ import { v4 } from 'uuid'; const token = process.env.ACCESS_TOKEN; export default async function (fastify: FastifyInstance) { - const apiUrl = process.env.API_URL; + const apiUrl = process.env.API_URL + '/api/rpc/command'; const fakeSessionId = v4(); + // not working + // function uploadMediaObject(fileId: string) { + // const filePath = '/home/juanfran/tmpImages/example-bg-2.jpg'; + // const file = fs.readFileSync(filePath); + // const fileName = path.basename(filePath); + + // const formData = new FormData(); + // formData.append('content', new Blob([file]), fileName); + // formData.append('file-id', fileId); + // formData.append('name', fileName); + // formData.append('is-local', 'true'); + + // return fetch(`${apiUrl}/upload-file-media-object`, { + // method: 'POST', + // headers: { + // 'Content-Type': 'application/transit+json', + // Authorization: `Token ${token}`, + // }, + // body: formData, + // }); + // } + + function deleteObj( + fileId: string, + revn: number, + pageId: string, + objectId: string + ) { + const payload = { + '~:id': `~u${fileId}`, + '~:revn': revn, + '~:session-id': `~u${fakeSessionId}`, + '~:changes': [ + { + '~:type': '~:del-obj', + '~:page-id': `~u${pageId}`, + '~:ignore-touched': false, + '~:id': `~u${objectId}`, + }, + ], + '~:features': { + '~#set': [ + 'layout/grid', + 'styles/v2', + 'fdata/pointer-map', + 'fdata/objects-map', + 'fdata/shape-data-type', + ], + }, + }; + + return fetch(`${apiUrl}/update-file?id=${fileId}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/transit+json', + Authorization: `Token ${token}`, + }, + body: JSON.stringify(payload), + }).then((response) => response.json()); + } + fastify.get('/get-profile', function () { return fetch(`${apiUrl}/get-profile`, { method: 'GET', @@ -28,43 +89,12 @@ export default async function (fastify: FastifyInstance) { objectId: string; }; }>('/delete-object', function (request, reply) { - const payload = { - '~:id': `~u${request.body.fileId}`, - '~:revn': request.body.revn, - '~:session-id': `~u${fakeSessionId}`, - '~:changes': [ - { - '~:type': '~:del-obj', - '~:page-id': `~u${request.body.pageId}`, - '~:ignore-touched': false, - '~:id': `~u${request.body.objectId}`, - }, - ], - '~:features': { - '~#set': [ - 'layout/grid', - 'styles/v2', - 'fdata/pointer-map', - 'fdata/objects-map', - 'fdata/shape-data-type', - ], - }, - }; - - console.log('Payload:', payload); - - return fetch( - `http://localhost:3449/api/rpc/command/update-file?id=${request.body.fileId}`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/transit+json', - Authorization: `Token ${token}`, - }, - body: JSON.stringify(payload), - } + deleteObj( + request.body.fileId, + request.body.revn, + request.body.pageId, + request.body.objectId ) - .then((response) => response.json()) .then((data) => { console.log('Success:', data); reply.send(data);