-
Notifications
You must be signed in to change notification settings - Fork 835
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 #806
Open
ab3MN
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
ab3MN:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #806
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class ErrorCase { | ||
caseTypes = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER']; | ||
|
||
convertErrorMessage = '/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>'; | ||
|
||
originalTextError = `Text to convert is required. Correct request is: "${this.convertErrorMessage}".`; | ||
|
||
originalCaseError = `"toCase" query param is required. Correct request is: "${this.convertErrorMessage}".`; | ||
|
||
caseTypesError = `This case is not supported. Available cases: ${this.caseTypes.join(', ')}.`; | ||
|
||
hasError(originalText, originalCase) { | ||
return ( | ||
!originalText || !originalCase || !this.caseTypes.includes(originalCase) | ||
); | ||
} | ||
|
||
getError(originalText, originalCase) { | ||
const errors = []; | ||
|
||
if (!originalText) { | ||
errors.push(this.originalTextError); | ||
} | ||
|
||
if (!originalCase) { | ||
errors.push(this.originalCaseError); | ||
} else if (!this.caseTypes.includes(originalCase)) { | ||
errors.push(this.caseTypesError); | ||
} | ||
|
||
return errors.map((error) => ({ message: error })); | ||
} | ||
} | ||
|
||
const errorCase = new ErrorCase(); | ||
|
||
module.exports = { | ||
errorCase, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,47 @@ | ||
// 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/convertToCase'); | ||
const { errorCase } = require('./Error'); | ||
const { sendJSONResponse } = require('./senResponse'); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the import path for |
||
const createServer = () => { | ||
const server = http.createServer((req, res) => { | ||
const url = new URL(req.url || '', `http://${req.headers.host}`); | ||
|
||
const originalText = url.pathname.slice(1); | ||
const targetCase = url.searchParams.get('toCase'); | ||
|
||
const hasError = errorCase.hasError(originalText, targetCase); | ||
|
||
if (hasError) { | ||
const errors = errorCase.getError(originalText, targetCase); | ||
|
||
return sendJSONResponse(res, 400, { errors }, 'Bad request'); | ||
} | ||
|
||
try { | ||
const { originalCase, convertedText } = convertToCase( | ||
originalText, | ||
targetCase, | ||
); | ||
|
||
return sendJSONResponse(res, 200, { | ||
originalCase, | ||
targetCase, | ||
originalText, | ||
convertedText, | ||
}); | ||
} catch (error) { | ||
return sendJSONResponse( | ||
res, | ||
500, | ||
{ errors: { message: error.message } }, | ||
'Internal Server Error', | ||
); | ||
} | ||
}); | ||
|
||
return server; | ||
}; | ||
|
||
module.exports = { createServer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
const { createServer } = require('./createServer'); | ||
|
||
createServer().listen(5700, () => { | ||
const PORT = process.env.PORT || 5701; | ||
|
||
createServer().listen(PORT, () => { | ||
// eslint-disable-next-line no-console | ||
console.log('Server started! 🚀'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const sendJSONResponse = (res, code, content, message) => { | ||
res.statusCode = code; | ||
|
||
if (message) { | ||
res.statusMessage = message; | ||
} | ||
|
||
res.setHeader('Content-Type', 'application/json'); | ||
|
||
res.end(JSON.stringify(content)); | ||
}; | ||
|
||
module.exports = { sendJSONResponse }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
convertErrorMessage
string uses a placeholder format that might not be correctly interpreted if used directly in a URL. Consider using a more explicit format or providing an example of how the URL should be structured.