From 7fb144bb22155cfe51c533477e8cae40e6a40888 Mon Sep 17 00:00:00 2001 From: Daria Nastas Date: Fri, 27 Dec 2024 14:27:09 +0200 Subject: [PATCH] add task solution --- src/createServer.js | 107 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 89724c92..54ce2004 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,3 +1,104 @@ -// Write code here -// Also, you can create additional files in the src folder -// and import (require) them here +/* eslint-disable max-len */ +const http = require('http'); +const { convertToCase } = require('./convertToCase'); + +const ERROR_MESSAGES = { + NO_TEXT: + 'Text to convert is required. Correct request is: "/?toCase=".', + NO_CASE: + '"toCase" query param is required. Correct request is: "/?toCase=".', + WRONG_CASE: + 'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.', +}; + +const SUPPORTED_CASES = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER']; + +function createServer() { + const server = http.createServer((req, res) => { + const url = new URL(req.url, `http://${req.headers.host}`); + const textToConvert = url.pathname.replace('/', ''); + const caseType = url.searchParams.get('toCase'); + + res.setHeader('Content-Type', 'application/json'); + + const validationErrors = validateRequest(textToConvert, caseType); + + if (validationErrors.length) { + return handleError(res, validationErrors); + } + + try { + const { originalCase, convertedText } = convertToCase( + textToConvert, + caseType, + ); + + handleSuccess(res, textToConvert, originalCase, caseType, convertedText); + } catch (error) { + handleInternalError(res); + } + }); + + return server; +} + +function validateRequest(textToConvert, caseType) { + const errors = []; + + if (!textToConvert) { + errors.push({ message: ERROR_MESSAGES.NO_TEXT }); + } + + if (!caseType) { + errors.push({ message: ERROR_MESSAGES.NO_CASE }); + } + + if (caseType && !SUPPORTED_CASES.includes(caseType.toUpperCase())) { + errors.push({ message: ERROR_MESSAGES.WRONG_CASE }); + } + + return errors; +} + +function handleError(res, errors) { + res.statusCode = 400; + res.statusMessage = 'Bad request'; + res.end(JSON.stringify({ errors })); +} + +function handleSuccess( + res, + originalText, + originalCase, + targetCase, + convertedText, +) { + res.statusCode = 200; + res.statusMessage = 'OK'; + + const result = { + originalCase, + targetCase, + originalText, + convertedText, + }; + + res.end(JSON.stringify(result)); +} + +function handleInternalError(res) { + res.statusCode = 500; + res.statusMessage = 'Internal Server Error'; + + res.end( + JSON.stringify({ + errors: [ + { + message: 'An unexpected error occurred. Please try again later.', + }, + ], + }), + ); +} + +module.exports = { createServer };