-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Solution #1086
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
base: master
Are you sure you want to change the base?
feat: Solution #1086
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ master ] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - run: npm install | ||
| - run: npm test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const supportedCases = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER']; | ||
|
|
||
| module.exports = { | ||
| supportedCases, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,48 @@ | ||
| // Write code here | ||
| // Also, you can create additional files in the src folder | ||
| // and import (require) them here | ||
| const http = require('node:http'); | ||
|
|
||
| const { prepareUrl } = require('./servises/prepareUrl'); | ||
| const { convertToCase } = require('./convertToCase/convertToCase'); | ||
| const { prepareErrors } = require('./servises/prepareErrors'); | ||
|
|
||
| function createServer() { | ||
| const server = http.createServer((req, res) => { | ||
| try { | ||
| const params = prepareUrl(req.url, req.headers.host); | ||
|
|
||
| if (params.errorArr && params.errorArr.length > 0) { | ||
| const errorResponse = prepareErrors(params.errorArr); | ||
|
|
||
| res.statusCode = 400; | ||
| res.statusMessage = 'Bad request'; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
|
|
||
| return res.end(JSON.stringify({ errors: errorResponse })); | ||
| } | ||
|
|
||
| const data = convertToCase(params.textToConvert, params.caseToConvert); | ||
|
|
||
| const response = { | ||
| originalCase: data.originalCase, | ||
| targetCase: params.caseToConvert, | ||
| originalText: params.textToConvert, | ||
| convertedText: data.convertedText, | ||
| }; | ||
|
|
||
| res.statusCode = 200; | ||
|
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. The requirements state that for a successful request, the server should respond with an |
||
| res.statusMessage = 'OK'; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(response)); | ||
| } catch (err) { | ||
| res.statusCode = 500; | ||
| res.statusMessage = 'Internal Server Error'; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify({ error: 'Internal Server Error' })); | ||
| } | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "missedTextToConvert": "Text to convert is required. Correct request is: \"/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>\".", | ||
| "missedCaseToConvert": "\"toCase\" query param is required. Correct request is: \"/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>\".", | ||
| "notSupportedCase": "This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| function prepareErrors(errorsArray) { | ||
| return errorsArray.map((msg) => ({ message: msg })); | ||
| } | ||
|
|
||
| module.exports = { | ||
| prepareErrors, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| const errors = require('../errors.json'); | ||
| const { supportedCases } = require('../constants/supportedCases'); | ||
|
|
||
| function prepareUrl(urlFromWeb, baseUrl) { | ||
| const dataFromUrl = new URL(urlFromWeb, `http://${baseUrl}`); | ||
| const textFromUrl = dataFromUrl.pathname.slice(1); | ||
| const caseFromUrl = dataFromUrl.searchParams.get('toCase'); | ||
| const appearedErrors = []; | ||
|
|
||
| if (!textFromUrl) { | ||
| appearedErrors.push(errors.missedTextToConvert); | ||
| } | ||
|
|
||
| if (!caseFromUrl) { | ||
| appearedErrors.push(errors.missedCaseToConvert); | ||
| } else if (!supportedCases.includes(caseFromUrl)) { | ||
| appearedErrors.push(errors.notSupportedCase); | ||
| } | ||
|
|
||
| return { | ||
| textToConvert: textFromUrl, | ||
| caseToConvert: caseFromUrl, | ||
| errorArr: appearedErrors, | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| prepareUrl, | ||
| }; |
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 task requires responding with a
Bad requeststatusText for validation failures. While Node.js often sets a default message, it's important to set it explicitly as required. You can set it usingres.statusMessage.