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 #810

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
55 changes: 52 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
// 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 { validateRequest } = require('./helpers/errorMessages');
const {
formatErrorResponse,
formatSuccessResponse,
} = require('./helpers/responseFormatter');

const createServer = () => {
return http.createServer((req, res) => {
const normalizedUrl = new URL(req.url, 'http://localhost:5700');
const params = new URLSearchParams(normalizedUrl.searchParams);
const text = normalizedUrl.pathname.slice(1);
const toCase = params.get('toCase');

res.setHeader('Content-Type', 'application/json');

const validationErrors = validateRequest(
text,
toCase ? toCase.toUpperCase() : null,
);

Choose a reason for hiding this comment

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

Ensure toCase is not null before calling toUpperCase(). You can add a check to handle cases where toCase is not provided, to prevent potential runtime errors.


if (validationErrors.length > 0) {
res.statusCode = 400;
res.statusMessage = 'Bad Request';
res.end(JSON.stringify(formatErrorResponse(validationErrors)));

return;
}

try {
const targetCase = toCase.toUpperCase();
const convertedData = convertToCase(text, targetCase);
const result = formatSuccessResponse(convertedData, targetCase, text);

res.statusCode = 200;
res.statusMessage = 'OK';
res.end(JSON.stringify(result));
} catch (error) {
res.statusCode = 500;
res.statusMessage = 'Internal Server Error';

res.end(
JSON.stringify({ errors: [{ message: 'Internal Server Error' }] }),
);
}
});
};

module.exports = {
createServer,
};
30 changes: 30 additions & 0 deletions src/helpers/errorMessages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const SUPPORTED_CASES = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER'];

const validateRequest = (text, toCase) => {
const errors = [];

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

Check failure on line 9 in src/helpers/errorMessages.js

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 100. Maximum allowed is 80
});
}

if (!toCase) {
errors.push({
message:
'"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".',

Check failure on line 16 in src/helpers/errorMessages.js

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 105. Maximum allowed is 80
});
} else if (!SUPPORTED_CASES.includes(toCase)) {
errors.push({
message:
'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.',

Check failure on line 21 in src/helpers/errorMessages.js

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 91. Maximum allowed is 80
});
}

return errors;
};

module.exports = {
validateRequest,
};
19 changes: 19 additions & 0 deletions src/helpers/responseFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const formatErrorResponse = (errors) => {
return {
errors,
};
};

const formatSuccessResponse = (conversionResult, targetCase, originalText) => {
return {
originalCase: conversionResult.originalCase,
targetCase,
originalText,
convertedText: conversionResult.convertedText,
};
};

module.exports = {
formatErrorResponse,
formatSuccessResponse,
};
Loading