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

server #820

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/convertToCase/convertToCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { wordsToCase } = require('./wordsToCase');
/**
* @typedef {'SNAKE' | 'KEBAB' | 'CAMEL' | 'PASCAL' | 'UPPER'} CaseName
*
// eslint-disable-next-line prettier/prettier
* @param {string} text
* @param {CaseName} caseName
*
Expand Down
1 change: 1 addition & 0 deletions src/convertToCase/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line prettier/prettier
const { convertToCase } = require('./convertToCase');

module.exports = {
Expand Down
64 changes: 61 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
// Write code here
// Also, you can create additional files in the src folder
// and import (require) them here
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 = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming the PATH variable to path to follow JavaScript naming conventions, which typically use camelCase for variable names.


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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that toCase is not null or undefined before calling toUpperCase() to avoid potential runtime errors. This is already checked earlier, but it's good practice to ensure the variable is valid before using it.

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