From 0bb34d55f924de39581e49aae3463de61229ac97 Mon Sep 17 00:00:00 2001 From: Jhonatan Date: Wed, 1 Apr 2026 15:05:37 -0300 Subject: [PATCH 1/3] solution --- src/createServer.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/createServer.js b/src/createServer.js index 89724c920..ae23e7ab8 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,3 +1,27 @@ // Write code here // Also, you can create additional files in the src folder // and import (require) them here +import http from 'http'; +import { convertToCase } from './convertToCase/convertToCase'; +export function createServer() { + const server = http.createServer((r, s) => { + const url = r.url.split('?'); + if (url[1] === undefined) { + return; + } + const word = url[0].replace('/', ''); + if (url[1].includes('UPPER')) { + return convertToCase(word, 'UPPER'); + } else if (url[1].includes('KEBAB')) { + return convertToCase(word, 'KEBAB'); + } else if (url[1].includes('CAMEL')) { + return convertToCase(word, 'CAMEL'); + } else if (url[1].includes('PASCAL')) { + return convertToCase(word, 'PASCAL'); + } else if (url[1].includes('SNAKE')) { + return convertToCase(word, 'SNAKE'); + } + s.end('ok'); + }); + return server; +} From e313632cfbcc0fba34b5ca2fae01829816bcd371 Mon Sep 17 00:00:00 2001 From: Jhonatan Date: Wed, 1 Apr 2026 15:55:27 -0300 Subject: [PATCH 2/3] solution --- src/createServer.js | 72 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index ae23e7ab8..a5bc91b77 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -5,23 +5,63 @@ import http from 'http'; import { convertToCase } from './convertToCase/convertToCase'; export function createServer() { const server = http.createServer((r, s) => { - const url = r.url.split('?'); - if (url[1] === undefined) { - return; + s.setHeader('Content-Type', 'application/json'); + const word = r.url.split('?')[0].replace('/', ''); + const params = new URLSearchParams(r.url.split('?')[1]); + const toCase = params.get('toCase'); + if (!word) { + s.statusCode = 400; + return s.end( + JSON.stringify({ + errors: [ + { + message: + 'Text to convert is required. Correct request is: "/?toCase=".', + }, + ], + }), + ); + } else if (!toCase) { + s.statusCode = 400; + return s.end( + JSON.stringify({ + errors: [ + { + message: + '"toCase" query param is required. Correct request is: "/?toCase=".', + }, + ], + }), + ); + } else if ( + toCase !== 'SNAKE' && + toCase !== 'KEBAB' && + toCase !== 'CAMEL' && + toCase !== 'PASCAL' && + toCase !== 'UPPER' + ) { + s.statusCode = 400; + return s.end( + JSON.stringify({ + errors: [ + { + message: + 'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.', + }, + ], + }), + ); } - const word = url[0].replace('/', ''); - if (url[1].includes('UPPER')) { - return convertToCase(word, 'UPPER'); - } else if (url[1].includes('KEBAB')) { - return convertToCase(word, 'KEBAB'); - } else if (url[1].includes('CAMEL')) { - return convertToCase(word, 'CAMEL'); - } else if (url[1].includes('PASCAL')) { - return convertToCase(word, 'PASCAL'); - } else if (url[1].includes('SNAKE')) { - return convertToCase(word, 'SNAKE'); - } - s.end('ok'); + // conteudo principal + s.statusCode = 200; + s.end( + JSON.stringify({ + originalCase: convertToCase(word, toCase).originalCase, + targetCase: toCase, + originalText: word, + convertedText: convertToCase(word, toCase).convertedText, + }), + ); }); return server; } From 6457ebd5f502358bcbd4bce027e7ab74e991c478 Mon Sep 17 00:00:00 2001 From: Jhonatan Date: Wed, 1 Apr 2026 16:41:12 -0300 Subject: [PATCH 3/3] solution --- .github/workflows/test.yml-template | 23 ++++++++++ package-lock.json | 9 ++-- package.json | 2 +- src/createServer.js | 70 ++++++++++++++--------------- 4 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/test.yml-template diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 000000000..bb13dfc45 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -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 diff --git a/package-lock.json b/package-lock.json index fa582c7e6..1d9574a1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -1467,10 +1467,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", diff --git a/package.json b/package.json index a6c885a1d..b70296ae9 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", diff --git a/src/createServer.js b/src/createServer.js index a5bc91b77..e6c76a6cf 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,38 +1,32 @@ // Write code here // Also, you can create additional files in the src folder // and import (require) them here -import http from 'http'; -import { convertToCase } from './convertToCase/convertToCase'; -export function createServer() { +const http = require('http'); +const { convertToCase } = require('./convertToCase/convertToCase'); + +function createServer() { const server = http.createServer((r, s) => { s.setHeader('Content-Type', 'application/json'); + const word = r.url.split('?')[0].replace('/', ''); const params = new URLSearchParams(r.url.split('?')[1]); const toCase = params.get('toCase'); + const errors = []; + if (!word) { - s.statusCode = 400; - return s.end( - JSON.stringify({ - errors: [ - { - message: - 'Text to convert is required. Correct request is: "/?toCase=".', - }, - ], - }), - ); - } else if (!toCase) { - s.statusCode = 400; - return s.end( - JSON.stringify({ - errors: [ - { - message: - '"toCase" query param is required. Correct request is: "/?toCase=".', - }, - ], - }), - ); + errors.push({ + message: + 'Text to convert is required. Correct request' + + ' is: "/?toCase=".', + }); + } + + if (!toCase) { + errors.push({ + message: + '"toCase" query param is required. Correct ' + + 'request is: "/?toCase=".', + }); } else if ( toCase !== 'SNAKE' && toCase !== 'KEBAB' && @@ -40,20 +34,21 @@ export function createServer() { toCase !== 'PASCAL' && toCase !== 'UPPER' ) { + errors.push({ + message: + 'This case is not supported. Available ' + + 'cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.', + }); + } + + if (errors.length > 0) { s.statusCode = 400; - return s.end( - JSON.stringify({ - errors: [ - { - message: - 'This case is not supported. Available cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.', - }, - ], - }), - ); + + return s.end(JSON.stringify({ errors })); } // conteudo principal s.statusCode = 200; + s.end( JSON.stringify({ originalCase: convertToCase(word, toCase).originalCase, @@ -63,5 +58,8 @@ export function createServer() { }), ); }); + return server; } + +module.exports = { createServer };