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

Solution #813

Open
wants to merge 2 commits 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
59 changes: 56 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
// 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 createServer = () => {
return http.createServer((req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const toCase = url.searchParams.get('toCase');
const PATH = url.pathname.slice(1);
const errors = [];

if (!toCase) {
errors.push({
message:
'"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',
});
} else {
const supportedCases = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER'];

if (!supportedCases.includes(toCase.toUpperCase())) {
errors.push({
message:
'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.',
});
}
}

if (!PATH) {
errors.push({
message:
'Text to convert is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',
});
}

if (errors.length > 0) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ errors }));

return res.end();
}

const text = convertToCase(PATH, toCase.toUpperCase());

const objectToReturn = {
originalText: PATH,
targetCase: toCase,
originalCase: text.originalCase,
convertedText: text.convertedText,
};

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(objectToReturn));
});
};

module.exports = { createServer };
Loading