Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #818

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 104 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -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: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',
NO_CASE:
'"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',
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 };
Loading