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

Finished #805

Open
wants to merge 3 commits 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 .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=5700
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
extends: '@mate-academy/eslint-config',
env: {
jest: true
jest: true,
},
rules: {
'no-proto': 0
'no-proto': 0,
},
plugins: ['jest']
plugins: ['jest'],
};
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
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
3,966 changes: 1,597 additions & 2,369 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand All @@ -26,5 +26,9 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"dotenv": "^16.4.5",
"nodemon": "^3.1.7"
}
}
42 changes: 39 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
// 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 { errorHandler } = require('./errorHandler');

require('dotenv').config();

const BASE = 'http://localhost' + ':' + process.env.PORT;

function createServer() {
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');

const params = new URL(req.url, BASE);
const textToConvert = req.url.slice(1).split('?')[0];
const toCase = params.searchParams.get('toCase');
const isError = errorHandler(res, textToConvert, toCase);

if (isError) {
return;
}

const convertedMsg = convertToCase(textToConvert, toCase);

res.statusCode = 200;

const JSONResponse = {
originalCase: convertedMsg.originalCase,
targetCase: toCase,
originalText: textToConvert,
convertedText: convertedMsg.convertedText,
};

res.end(JSON.stringify(JSONResponse));
});

return server;
}

module.exports = { createServer, BASE };
54 changes: 54 additions & 0 deletions src/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function errorHandler(res, textToConvert, toCase) {
const availableCases = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER'];
const isTextApsent = textToConvert.length === 0;
const isToCaseParamApsent = toCase === null;
const isToCaseUnaccepted =
!availableCases.includes(toCase) && !isToCaseParamApsent;
const isTextApsentErrorMsg =
'Text to convert is required. Correct request' +
' is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".';
const isToCaseParamApsentErrorMsg =
'"toCase" query param is required.' +
' Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".';
const isToCaseUnacceptedErrorMsg =
'This case is not supported. Available' +
' cases: SNAKE, KEBAB, CAMEL, PASCAL, UPPER.';

const errorMsgsArray = [];

if (isTextApsent) {
const errorMessage = {
message: isTextApsentErrorMsg,
};

errorMsgsArray.push(errorMessage);
}

if (isToCaseParamApsent) {
const errorMessage = {
message: isToCaseParamApsentErrorMsg,
};

errorMsgsArray.push(errorMessage);
}

if (isToCaseUnaccepted) {
const errorMessage = {
message: isToCaseUnacceptedErrorMsg,
};

errorMsgsArray.push(errorMessage);
}

if (errorMsgsArray.length > 0) {
res.end(
JSON.stringify({
errors: errorMsgsArray,
}),
);

return true;
}
}

module.exports = { errorHandler };
6 changes: 3 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { createServer } = require('./createServer');
const { createServer, BASE } = require('./createServer');

createServer().listen(5700, () => {
createServer().listen(process.env.PORT, () => {
// eslint-disable-next-line no-console
console.log('Server started! 🚀');
console.log('Server started! at: 🚀', BASE);
});
Loading