diff --git a/app/app.js b/app/app.js index 6f81d3f..9fcb4da 100644 --- a/app/app.js +++ b/app/app.js @@ -45,7 +45,7 @@ class App { } buildEndpoints() { - this.endpoints = []; + this.endpoints = this.endpoints.filter((endpoint) => endpoint.source !== 'file'); read(mocksDirectory) .filter(fileName => this.getEndpointFileExtension(fileName) !== undefined) @@ -62,7 +62,8 @@ class App { return { ...endpoint, - id: HttpMockServer.getNewEndpointId() + id: HttpMockServer.getNewEndpointId(), + source: 'file' }; }) ); diff --git a/app/http-api-server.js b/app/http-api-server.js index 47cc92f..67d8b3f 100644 --- a/app/http-api-server.js +++ b/app/http-api-server.js @@ -12,104 +12,46 @@ class HttpApiServer { this.api .route('/api/endpoints') .post((req, res) => { - if ( - !req.body.uri || - !req.body.status || - !req.body.body || - !req.body.headers - ) { - res.writeHead(400); + let newEndpoint = req.body; + + if (!newEndpoint.request) { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.write( + JSON.stringify({ + error: 'Endpoint request required', + }) + ); res.end(); return; } - let createNew = true; - let uriFounded = false; + if (!newEndpoint.response) { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.write( + JSON.stringify({ + error: 'Endpoint response required', + }) + ); + res.end(); + return; + } - this.app.endpoints.forEach((endpoint, index) => { - if (req.body.uri === endpoint.uri) { - if (req.body.method === endpoint.method) { - this.app.endpoints[index] = { - uri: req.body.uri, - method: req.body.method, - status: req.body.status, - time: req.body.time, - body: req.body.body, - headers: req.body.headers, - velocity: req.body.velocity - ? req.body.velocity - : { enabled: true }, - }; - createNew = false; - } else if (!endpoint.method && createNew) { - this.app.endpoints.splice(index, 0, { - uri: req.body.uri, - method: req.body.method, - status: req.body.status, - time: req.body.time, - body: req.body.body, - headers: req.body.headers, - velocity: req.body.velocity - ? req.body.velocity - : { enabled: true }, - }); - createNew = false; - } - uriFounded = true; - } else if (uriFounded && createNew) { - this.app.endpoints.splice(index, 0, { - uri: req.body.uri, - method: req.body.method, - status: req.body.status, - time: req.body.time, - body: req.body.body, - headers: req.body.headers, - velocity: req.body.velocity - ? req.body.velocity - : { enabled: true }, - }); - createNew = false; - } - }); + newEndpoint = { + ...newEndpoint, + id: httpMockServer.getNewEndpointId(), + source: 'user', + }; - if (createNew) { - this.app.endpoints.push({ - uri: req.body.uri, - method: req.body.method, - status: req.body.status, - time: req.body.time, - body: req.body.body, - headers: req.body.headers, - velocity: req.body.velocity ? req.body.velocity : { enabled: true }, - }); - } + this.app.endpoints.push(newEndpoint); - res.writeHead(204); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.write(JSON.stringify(newEndpoint)); res.end(); }) .get((req, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.write(JSON.stringify(this.app.endpoints)); res.end(); - }) - .delete((req, res) => { - if (!req.body.uri) { - res.writeHead(400); - res.end(); - return; - } - - this.app.endpoints.forEach((endpoint, index) => { - if ( - req.body.uri === endpoint.uri && - (!req.body.method || req.body.method === endpoint.method) - ) { - this.app.endpoints.splice(index, 1); - } - }); - - res.writeHead(204); - res.end(); }); this.api.use('/', express.static('.'));