diff --git a/electron-app/package-lock.json b/electron-app/package-lock.json index 05bc694..372a70e 100644 --- a/electron-app/package-lock.json +++ b/electron-app/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT", "dependencies": { "compression": "^1.7.4", + "cors": "^2.8.5", "electron-squirrel-startup": "^1.0.0", "express": "^4.18.2", "rxjs": "^7.8.1", @@ -26,6 +27,7 @@ "@electron-forge/plugin-auto-unpack-natives": "^6.4.2", "@electron-forge/publisher-github": "^6.4.2", "@types/compression": "^1.7.5", + "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/uuid": "^9.0.7", "@typescript-eslint/eslint-plugin": "^6.12.0", @@ -1561,6 +1563,15 @@ "@types/node": "*" } }, + "node_modules/@types/cors": { + "version": "2.8.17", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.17.tgz", + "integrity": "sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/express": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", @@ -2973,6 +2984,18 @@ "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", "dev": true }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -6262,6 +6285,14 @@ "node": "^12.13.0 || ^14.15.0 || >=16.0.0" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz", diff --git a/electron-app/package.json b/electron-app/package.json index b2a4824..4f94191 100644 --- a/electron-app/package.json +++ b/electron-app/package.json @@ -25,6 +25,7 @@ "license": "MIT", "dependencies": { "compression": "^1.7.4", + "cors": "^2.8.5", "electron-squirrel-startup": "^1.0.0", "express": "^4.18.2", "rxjs": "^7.8.1", @@ -41,6 +42,7 @@ "@electron-forge/plugin-auto-unpack-natives": "^6.4.2", "@electron-forge/publisher-github": "^6.4.2", "@types/compression": "^1.7.5", + "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/uuid": "^9.0.7", "@typescript-eslint/eslint-plugin": "^6.12.0", diff --git a/electron-app/src/logic/connector/infrastructure/connectors/folder.connector.ts b/electron-app/src/logic/connector/infrastructure/connectors/folder.connector.ts index b6d2c64..8ae1d2d 100644 --- a/electron-app/src/logic/connector/infrastructure/connectors/folder.connector.ts +++ b/electron-app/src/logic/connector/infrastructure/connectors/folder.connector.ts @@ -2,14 +2,7 @@ import { Blob as FSBlob } from 'buffer'; import * as fs from 'fs'; import path from 'path'; import { Observable, forkJoin, map, of, switchMap } from 'rxjs'; -import { - ConnectorParameters, - FileStatus, - IConnector, - Link, - SearchResults, - SyncItem, -} from 'src/logic/connector/domain/connector'; +import { ConnectorParameters, FileStatus, IConnector, Link, SearchResults, SyncItem } from '../../domain/connector'; import { SourceConnectorDefinition } from '../factory'; const FILES_TO_IGNORE = ['.DS_Store', 'Thumbs.db']; diff --git a/electron-app/src/logic/errors.ts b/electron-app/src/logic/errors.ts index 956afe9..f4df3b0 100644 --- a/electron-app/src/logic/errors.ts +++ b/electron-app/src/logic/errors.ts @@ -1,7 +1,7 @@ export class CustomError extends Error { constructor( public readonly message: string, - public readonly statusCode: number = 400, + public readonly statusCode: number = 500, ) { super(message); } diff --git a/electron-app/src/logic/sync/domain/sync.entity.ts b/electron-app/src/logic/sync/domain/sync.entity.ts index 0e8679e..31c8cc3 100644 --- a/electron-app/src/logic/sync/domain/sync.entity.ts +++ b/electron-app/src/logic/sync/domain/sync.entity.ts @@ -5,7 +5,7 @@ import { getConnector } from '../../connector/infrastructure/factory'; export type Connector = { name: 'gdrive' | 'folder'; - logo: string; + // logo: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any parameters: { [key: string]: any }; }; diff --git a/electron-app/src/logic/sync/presentation/routes.ts b/electron-app/src/logic/sync/presentation/routes.ts index 48735a2..ac15c6d 100644 --- a/electron-app/src/logic/sync/presentation/routes.ts +++ b/electron-app/src/logic/sync/presentation/routes.ts @@ -52,7 +52,7 @@ export class SyncFileSystemRoutes { router.post('/', async (req, res) => { const [error, createSyncDto] = CreateSyncDto.create(req.body); - if (error) return res.status(400).json({ error }); + if (error) return res.status(500).json({ error }); try { await new CreateSync(syncRepository).execute(createSyncDto!); res.status(201).send({ @@ -86,7 +86,7 @@ export class SyncFileSystemRoutes { router.patch('/:id', async (req, res) => { const { id } = req.params; const [error, updateSyncDto] = UpdateSyncDto.create({ ...req.body, id }); - if (error) return res.status(400).json({ error }); + if (error) return res.status(500).json({ error }); try { await new UpdateSync(syncRepository).execute(updateSyncDto!); diff --git a/electron-app/src/server.ts b/electron-app/src/server.ts index 712be61..368095c 100644 --- a/electron-app/src/server.ts +++ b/electron-app/src/server.ts @@ -1,5 +1,6 @@ import compression from 'compression'; import express, { Router } from 'express'; +import cors from 'cors'; import http from 'http'; import { EVENTS, EventEmitter } from './events/events'; @@ -46,10 +47,20 @@ export class Server { } async start() { + //* CORS + this.app.use( + cors({ + origin: '*', + }), + ); //* Middlewares this.app.use(express.json()); // raw this.app.use(express.urlencoded({ extended: true })); // x-www-form-urlencoded this.app.use(compression()); + this.app.use((req, res, next) => { + res.setHeader('Content-Type', 'application/json'); + next(); + }); //* Routes this.app.use(this.routes); @@ -59,7 +70,7 @@ export class Server { }); this.app.get('/status', async (_req, res) => { - res.status(200).send('Server is running'); + res.status(200).send(JSON.stringify({ running: true })); }); this.app.post('/stop', async (_req, res) => {