0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-21 06:02:34 -05:00

chore: refactor delete api

This commit is contained in:
Juanfran 2024-03-04 08:43:53 +01:00 committed by María Valderrama
parent a33e185146
commit dd7b7757f4

View file

@ -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);