Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
fe4e45a
init: start crud api task
Quiddlee Jan 29, 2024
5df9464
feat: add env
Quiddlee Jan 29, 2024
1ead72f
chore: adjust nodemon cofig
Quiddlee Jan 29, 2024
7041e91
chore: remove tsconfig comments
Quiddlee Jan 29, 2024
f2fdf3f
feat: add more env variables
Quiddlee Jan 29, 2024
6c74aba
chore: add more npm scripts
Quiddlee Jan 29, 2024
e536ebb
feat: add test endpoint
Quiddlee Jan 29, 2024
030cfb8
chore: change npm script
Quiddlee Jan 29, 2024
cc0fe46
feat: configure webpack
Quiddlee Jan 29, 2024
a52c414
chore: change app start script
Quiddlee Jan 29, 2024
82061eb
feat: add enums
Quiddlee Jan 29, 2024
6a1266d
chore: update env config
Quiddlee Jan 29, 2024
627c3e3
chore: add prettier eslint plugin
Quiddlee Jan 29, 2024
3b27b51
refactor: move all status code to enum
Quiddlee Jan 29, 2024
d21ff17
refactor: remove unused file
Quiddlee Jan 29, 2024
5e1981e
chore: add uuid package
Quiddlee Jan 29, 2024
552c7b9
feat: implement Get users
Quiddlee Jan 29, 2024
10d8030
refactor: abstract part of the logic in the api class
Quiddlee Jan 29, 2024
aaa0556
refactor: encapsulate routes logic in separate class
Quiddlee Jan 29, 2024
e2c7028
refactor: rename "ApiFeatures" class to "Api"
Quiddlee Jan 29, 2024
c734579
chore: change nodemon script
Quiddlee Jan 30, 2024
999918f
refactor: implement api routes
Quiddlee Jan 30, 2024
cb6be3a
refactor: change to utilize immutability
Quiddlee Jan 30, 2024
3ad8ee6
fix: url assign
Quiddlee Jan 30, 2024
5a35040
fix: router issue
Quiddlee Jan 30, 2024
a15d7b1
move all user related implementation to user controller
Quiddlee Jan 30, 2024
600c221
refactor: remove hardcoded types
Quiddlee Jan 30, 2024
fab65ea
feat: implement creating user
Quiddlee Jan 30, 2024
426be71
feat: implement response body valdiation & error handling
Quiddlee Jan 31, 2024
421b3ff
fix: remove unnecessary import
Quiddlee Jan 31, 2024
2cd1329
refactor: remove unused code
Quiddlee Jan 31, 2024
8c336f9
feat: implement updating user
Quiddlee Jan 31, 2024
09d038d
fix: public api to not be async
Quiddlee Jan 31, 2024
7c0edac
feat: implement deleting user
Quiddlee Jan 31, 2024
9ea7992
fix: change async/await to then/catch
Quiddlee Jan 31, 2024
1df50d4
feat: implement not found route
Quiddlee Jan 31, 2024
791e8c9
refactor: change api injections stage
Quiddlee Feb 2, 2024
b40808b
refactor: change api working logic
Quiddlee Feb 3, 2024
3493e4b
refactor: implement to run all routes as a middleware
Quiddlee Feb 3, 2024
7c9f64b
refactor: remove TODO
Quiddlee Feb 3, 2024
0eaab59
refactor: re-arrange server.ts code
Quiddlee Feb 3, 2024
20eda7b
refactor: change extendRes method api
Quiddlee Feb 3, 2024
4253e48
refactor: code cleanup & readability improve
Quiddlee Feb 3, 2024
6a402e0
refactor: change api private methods to use this
Quiddlee Feb 3, 2024
edca9a5
refactor: minor code style changes
Quiddlee Feb 3, 2024
20ffda3
refactor: add explicit public keyword
Quiddlee Feb 3, 2024
22874b4
refactor: abstract body validation into separate middleware
Quiddlee Feb 3, 2024
0b7217e
refactor: change to not mutate original request/response
Quiddlee Feb 3, 2024
3fbe6f4
fix: delete user to not left anything
Quiddlee Feb 3, 2024
e6bb8d4
refactor: change to not validate body in the middleware
Quiddlee Feb 3, 2024
d845105
feat: add vitest
Quiddlee Feb 4, 2024
1ba5d4f
fix: import issues
Quiddlee Feb 4, 2024
b4123f5
chore: add typescript import resolver
Quiddlee Feb 4, 2024
e2891b7
refactor: rename modules
Quiddlee Feb 4, 2024
4c9e273
feat: add vitest env config
Quiddlee Feb 4, 2024
c99c272
refactor: remove mock test
Quiddlee Feb 4, 2024
ab21cb6
chore: update eslint config
Quiddlee Feb 6, 2024
fff1859
refactor: update app api for testing
Quiddlee Feb 6, 2024
a2912af
chore: remove eslint resolver package
Quiddlee Feb 6, 2024
1ac639f
chore: add supertest package
Quiddlee Feb 6, 2024
797fa7d
chore: update tsconfig
Quiddlee Feb 6, 2024
34bfb50
fix: response to match one style
Quiddlee Feb 7, 2024
1c06491
feat: add TODO
Quiddlee Feb 7, 2024
5d2bffb
chore: change vitest config to .mts extension
Quiddlee Feb 7, 2024
79c767b
feat: add basic test scenario
Quiddlee Feb 7, 2024
8455a75
feat: encapsulate routes in enum
Quiddlee Feb 7, 2024
960097e
refactor: change response style
Quiddlee Feb 7, 2024
5dab7cc
fix: test to match new response style
Quiddlee Feb 7, 2024
457ef3a
feat: complete test scenario 1
Quiddlee Feb 7, 2024
867c268
refactor: rename test file from "api.test.ts" to "test-scenario-1.tes…
Quiddlee Feb 7, 2024
90669eb
refactor: rearrange files
Quiddlee Feb 7, 2024
79184ee
fix: to clear data after test complete
Quiddlee Feb 7, 2024
d3a5c1e
refactor: abstract data clearance in separate function
Quiddlee Feb 7, 2024
3a85c8d
feat: add test scenario 2
Quiddlee Feb 7, 2024
6ec8c58
fix: typo
Quiddlee Feb 7, 2024
31e1f9b
feat: add 3 test scenario
Quiddlee Feb 7, 2024
2d45d1d
refactor: change app logic to work with async handlers
Quiddlee Feb 10, 2024
3f17600
feat: add more types
Quiddlee Feb 10, 2024
496a0cf
refactor: remove data file
Quiddlee Feb 10, 2024
242c42b
feat: implement multi mode to run multiple instances of the app
Quiddlee Feb 10, 2024
9eceb69
refactor: modify app to share db state across all workers
Quiddlee Feb 10, 2024
dc20479
refactor: change CB type to include async behaviour
Quiddlee Feb 11, 2024
ae168ac
refactor: replace clear data with new DB API
Quiddlee Feb 11, 2024
99f21bc
refactor: rename function from "connectDB" to "connectMasterDB"
Quiddlee Feb 11, 2024
96fdabe
refactor: abstract workers initialization in separate function
Quiddlee Feb 11, 2024
7b5c80c
refactor: abstract which worker will handle next request logic in sep…
Quiddlee Feb 11, 2024
dcc29cb
refactor: clean up server file
Quiddlee Feb 11, 2024
be0b55e
refactor: encapsulate app initialization in separate fn
Quiddlee Feb 11, 2024
b28c7f8
refactor: rearrange files
Quiddlee Feb 11, 2024
0ef0765
refactor: replace port with const variable
Quiddlee Feb 11, 2024
3d3994d
fix: eslint rule
Quiddlee Feb 11, 2024
84c54d3
refactor: add user controllers JSDoc comments
Quiddlee Feb 11, 2024
c36a92b
refactor: handle internal server errors
Quiddlee Feb 11, 2024
c7391fa
refactor: handle json parse error
Quiddlee Feb 11, 2024
5184b58
refactor: change error message
Quiddlee Feb 11, 2024
8b5d467
refactor: remove comment
Quiddlee Feb 11, 2024
555946a
refactor: add JSDoc comment to validateId fn
Quiddlee Feb 11, 2024
848d624
fix: to ignore unnecessary fields from user request
Quiddlee Feb 11, 2024
86e2bf7
fix: body parsing
Quiddlee Feb 11, 2024
61914f1
feat: implement body type validation
Quiddlee Feb 11, 2024
717a257
feat: implement update user body type validation
Quiddlee Feb 11, 2024
15d7c8f
feat: add emoji errors
Quiddlee Feb 11, 2024
a08c49b
fix: test scenarios
Quiddlee Feb 11, 2024
e7118c3
docs: add API readme
Quiddlee Feb 11, 2024
6c83995
chore: add API postman collection
Quiddlee Feb 11, 2024
1b274f3
chore: add API postman collection
Quiddlee Feb 11, 2024
f757d54
fix: test describe typo
Quiddlee Feb 11, 2024
60329f2
docs: fix typo
Quiddlee Feb 11, 2024
08476ea
chore: update postman collection
Quiddlee Feb 12, 2024
6bf0793
docs: add postman collection description
Quiddlee Feb 12, 2024
a7eab0e
docs: fix typo
Quiddlee Feb 12, 2024
5643fe2
docs: mention .env file
Quiddlee Feb 14, 2024
3e387b6
docs: fix .env spelling
Quiddlee Feb 14, 2024
de636ea
docs: fix typo
Quiddlee Feb 14, 2024
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
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=YOUR_PORT
HOST=YOUR_HOST
80 changes: 80 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module.exports = {
root: true,
env: { es2020: true, es6: true },
extends: [
'airbnb',
'airbnb-typescript',
'airbnb/hooks',
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended',
'plugin:node/recommended',
'plugin:prettier/recommended',
],
ignorePatterns: [
'.eslintrc.cjs',
'prettier.config.js',
'node_modules',
'environment.d.ts',
],
plugins: ['import', 'prettier', '@typescript-eslint'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: ['tsconfig.json'],
tsconfigRootDir: __dirname,
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
'@typescript-eslint/no-var-requires': 'off',
'class-methods-use-this': 'off',
'no-process-exit': 'off',
'no-restricted-syntax': 'off',
'node/no-unpublished-import': 'off',
'node/no-missing-import': 'off',
'node/no-unsupported-features/es-syntax': [
'error',
{
ignores: ['modules'],
},
],
'sort-imports': [
'error',
{ ignoreCase: true, ignoreDeclarationSort: true },
],
'import/prefer-default-export': 'off',
'import/no-extraneous-dependencies': 'off',
'import/extensions': ['error', 'never'],
'import/order': [
'error',
{
groups: [
'builtin',
'external',
'internal',
['sibling', 'parent'],
'index',
],
pathGroups: [
{
pattern: 'node',
group: 'external',
position: 'before',
},
],
pathGroupsExcludedImportTypes: ['internal'],
'newlines-between': 'always',
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
},
],
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
},
},
};
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ dist
# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# IDE
.idea

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Build

build
173 changes: 172 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,172 @@
# node-crud-api
# 💚 Node CRUD API

*🦥 RS-School task.*

# Getting Started 🚀
To run the project locally, you would have to download zip file with the repository or clone it to your computer. ✨

## Setup and Running ⚠️

What things do you need to do in order to run our project locally? 🤔

* Use node 20 LTS ⚡
* Installed [.git](https://git-scm.com/) on your computer. ✌️
* Code Editor of your choice. 📝
* Installed [npm](https://www.npmjs.com/). 📦

## Installation And Preparation 🔮

First make sure you have all the things listed in the previous section. Then clone our repository to your computer: 👌

```
git clone https://github.com/Quiddlee/node-crud-api.git
```

or download zip file manually with our repository.

Navigate into project folder and run 📦:

```
npm install
```

Create ```.env``` file in the root of the project and add all necessary variables 🔥.

You can find ```.env.example``` as an example file in the project root or follow the lines below 🐺:

```dotenv
PORT=YOUR_PORT
HOST=YOUR_HOST
```

Finally run a development server: 🤩
```
npm run start:dev
```
Aaaaand you're done! 🎉🥳

## Available Scripts 🥑

Here you can find all the scripts that are available in our project. 🦚

Start the app in `base` mode: ✅

```
npm run start:dev
```

Start the app in `multi` mode: 🪭

```
npm run start:multi
```

Start the app in `prod` mode: 🪶

```
npm run start:prod
```

Lint the app with `eslint`: 🦚

```
npm run lint
```

Lint adn fix the app errors with `eslint`: 🐨

```
npm run lint:fix
```

Format the App with **Prettier**: 🧹

```
npm run format
```

Format the App with **Prettier** fix: 🍃

```
npm run format:fix
```

Type check the App with **TypeScript**: 🦁

```
npm run type-check
```

Run unit-tests with **Vitest**: 🧪

```
npm run test
```
# Working with API 🐳
In the project root folder, you can find a Postman collection that will make your life easier while working with the API. 😉

![postman.jpg](./public/postman.jpg)

## API endpoints 🦉
The API has the following endpoints:

| Method | Endpoint | Description |
|---------|:--------------:|------------------------------------:|
| GET | /users | Get all the users from the database |
| GET | /users/:id | Get a single user by ID |
| POST | /users | Create a new user in the database |
| PUT | /users/:id | Update a user by ID |
| DELETE | /users/:id | Delete a user by ID |

## Request body 🥑

| Endpoint | Body | Example |
|----------|:----------:|----------------------------------------------------------------------------------------------------------------------------:|
| POST | /users | An object with the username age and hobbies ```{"username": "user", "age": 20, "hobbies": ["cooking", "sport"]}``` |
| PUT | /users/:id | An object with the updated username age and hobbies ```{"username": "updated user", "age": 30, "hobbies": ["updated hobbie"]}``` |

## Response format 🍇

| Field | Type | Description |
|---------|:-------------------:|-----------------------------------------------------------------------------:|
| status | "success" or "fail" | Indicates whether the request was successful or not |
| message | string | in case of failed result the response will contain message why it is failed |
| data | Object or Array | The data returned by the request |

## Response examples 🍋

**GET /users**

```json
{
"status": "success",
"data": {
"users": [
{
"username": "user",
"age": 20,
"hobbies": ["cooking", "sport", "programming"]
},
{
"username": "user2",
"age": 21,
"hobbies": ["sport", "programming"]
},
{
"username": "user3",
"age": 22,
"hobbies": ["hobbie"]
}
]
}
}
```

**GET /users/:id Error case**

```json
{
"status": "fail",
"message": "😯 User not found"
}
```
Loading