diff --git a/src/createServer.js b/src/createServer.js index 89724c920..f50812ef1 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,3 +1,61 @@ -// Write code here -// Also, you can create additional files in the src folder -// and import (require) them here +/* eslint-disable max-len */ +/* eslint-disable no-console */ +const { convertToCase } = require('./convertToCase'); +const http = require('http'); + +function createServer() { + return http.createServer((req, res) => { + const [path, query] = req.url.split('?'); + + const params = new URLSearchParams(query); + const text = path.slice(1); + const toCase = params.get('toCase'); + + const errors = []; + const supportedCases = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER']; + + if (!text) { + errors.push({ + message: + 'Text to convert is required. Correct request is: "/?toCase=".', + }); + } + + if (!toCase) { + errors.push({ + message: + '"toCase" query param is required. Correct request is: "/?toCase=".', + }); + } + + if (toCase && !supportedCases.includes(toCase)) { + errors.push({ + message: + 'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.', + }); + } + + if (errors.length > 0) { + res.writeHead(400, 'Bad request', { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ errors })); + + return; + } + + const result = convertToCase(text, toCase); + + const response = { + originalCase: result.originalCase, + targetCase: toCase, + originalText: text, + convertedText: result.convertedText, + }; + + res.writeHead(200, 'OK', { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(response)); + }); +} + +module.exports = { + createServer, +};