├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── package-lock.json ├── package.json ├── src ├── app.module.ts ├── main.ts └── recipe │ ├── dto │ ├── recipe.dto.ts │ └── update-description.dto.ts │ ├── entity │ └── recipe.ts │ ├── recipe.controller.spec.ts │ ├── recipe.controller.ts │ ├── recipe.module.ts │ ├── recipe.service.spec.ts │ └── recipe.service.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json └── tsconfig.json /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | tsconfigRootDir : __dirname, 6 | sourceType: 'module', 7 | }, 8 | plugins: ['@typescript-eslint/eslint-plugin'], 9 | extends: [ 10 | 'plugin:@typescript-eslint/recommended', 11 | 'plugin:prettier/recommended', 12 | ], 13 | root: true, 14 | env: { 15 | node: true, 16 | jest: true, 17 | }, 18 | ignorePatterns: ['.eslintrc.js'], 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/explicit-module-boundary-types': 'off', 23 | '@typescript-eslint/no-explicit-any': 'off', 24 | }, 25 | }; 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | pnpm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | lerna-debug.log* 13 | 14 | # OS 15 | .DS_Store 16 | 17 | # Tests 18 | /coverage 19 | /.nyc_output 20 | 21 | # IDEs and editors 22 | /.idea 23 | .project 24 | .classpath 25 | .c9/ 26 | *.launch 27 | .settings/ 28 | *.sublime-workspace 29 | 30 | # IDE - VSCode 31 | .vscode/* 32 | !.vscode/settings.json 33 | !.vscode/tasks.json 34 | !.vscode/launch.json 35 | !.vscode/extensions.json -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 6 | [circleci-url]: https://circleci.com/gh/nestjs/nest 7 | 8 |

A progressive Node.js framework for building efficient and scalable server-side applications.

9 |

10 | NPM Version 11 | Package License 12 | NPM Downloads 13 | CircleCI 14 | Coverage 15 | Discord 16 | Backers on Open Collective 17 | Sponsors on Open Collective 18 | 19 | Support us 20 | 21 |

22 | 24 | 25 | ## Description 26 | 27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 28 | 29 | ## Installation 30 | 31 | ```bash 32 | $ npm install 33 | ``` 34 | 35 | ## Running the app 36 | 37 | ```bash 38 | # development 39 | $ npm run start 40 | 41 | # watch mode 42 | $ npm run start:dev 43 | 44 | # production mode 45 | $ npm run start:prod 46 | ``` 47 | 48 | ## Test 49 | 50 | ```bash 51 | # unit tests 52 | $ npm run test 53 | 54 | # e2e tests 55 | $ npm run test:e2e 56 | 57 | # test coverage 58 | $ npm run test:cov 59 | ``` 60 | 61 | ## Support 62 | 63 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 64 | 65 | ## Stay in touch 66 | 67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 68 | - Website - [https://nestjs.com](https://nestjs.com/) 69 | - Twitter - [@nestframework](https://twitter.com/nestframework) 70 | 71 | ## License 72 | 73 | Nest is [MIT licensed](LICENSE). 74 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/nest-cli", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "recipe-api", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "prebuild": "rimraf dist", 10 | "build": "nest build", 11 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 12 | "start": "nest start", 13 | "start:dev": "nest start --watch", 14 | "start:debug": "nest start --debug --watch", 15 | "start:prod": "node dist/main", 16 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 17 | "test": "jest", 18 | "test:watch": "jest --watch", 19 | "test:cov": "jest --coverage", 20 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 21 | "test:e2e": "jest --config ./test/jest-e2e.json" 22 | }, 23 | "dependencies": { 24 | "@nestjs/common": "^9.0.0", 25 | "@nestjs/core": "^9.0.0", 26 | "@nestjs/platform-express": "^9.0.0", 27 | "reflect-metadata": "^0.1.13", 28 | "rimraf": "^3.0.2", 29 | "rxjs": "^7.2.0" 30 | }, 31 | "devDependencies": { 32 | "@nestjs/cli": "^9.0.0", 33 | "@nestjs/schematics": "^9.0.0", 34 | "@nestjs/testing": "^9.0.0", 35 | "@types/express": "^4.17.13", 36 | "@types/jest": "28.1.4", 37 | "@types/node": "^16.0.0", 38 | "@types/supertest": "^2.0.11", 39 | "@typescript-eslint/eslint-plugin": "^5.0.0", 40 | "@typescript-eslint/parser": "^5.0.0", 41 | "eslint": "^8.0.1", 42 | "eslint-config-prettier": "^8.3.0", 43 | "eslint-plugin-prettier": "^4.0.0", 44 | "jest": "28.1.2", 45 | "prettier": "^2.3.2", 46 | "source-map-support": "^0.5.20", 47 | "supertest": "^6.1.3", 48 | "ts-jest": "28.0.5", 49 | "ts-loader": "^9.2.3", 50 | "ts-node": "^10.0.0", 51 | "tsconfig-paths": "4.0.0", 52 | "typescript": "^4.3.5" 53 | }, 54 | "jest": { 55 | "moduleFileExtensions": [ 56 | "js", 57 | "json", 58 | "ts" 59 | ], 60 | "rootDir": "src", 61 | "testRegex": ".*\\.spec\\.ts$", 62 | "transform": { 63 | "^.+\\.(t|j)s$": "ts-jest" 64 | }, 65 | "collectCoverageFrom": [ 66 | "**/*.(t|j)s" 67 | ], 68 | "coverageDirectory": "../coverage", 69 | "testEnvironment": "node" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { RecipeModule } from './recipe/recipe.module'; 3 | 4 | @Module({ 5 | imports: [RecipeModule], 6 | controllers: [], 7 | providers: [], 8 | }) 9 | export class AppModule {} 10 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/recipe/dto/recipe.dto.ts: -------------------------------------------------------------------------------- 1 | export class RecipeDto { 2 | description: string; 3 | ingredients: IgredientDto[]; 4 | } 5 | 6 | export class IgredientDto { 7 | name: string; 8 | unit: Unit; 9 | quantity: number; 10 | } 11 | 12 | export enum Unit { 13 | MILILITERS = 'mililiters', 14 | LITERS = 'LITERS', 15 | GRAMS = 'grams', 16 | KILOGRAMS = 'kilograms', 17 | SPOONS = 'spoons', 18 | CUPS = 'cups', 19 | PIECES = 'pieces', 20 | } 21 | -------------------------------------------------------------------------------- /src/recipe/dto/update-description.dto.ts: -------------------------------------------------------------------------------- 1 | export class UpdatedescriptionDto { 2 | description: string; 3 | } 4 | -------------------------------------------------------------------------------- /src/recipe/entity/recipe.ts: -------------------------------------------------------------------------------- 1 | import { Unit } from '../dto/recipe.dto'; 2 | 3 | export class Recipe { 4 | id: string; 5 | description: string; 6 | ingredients: Igredient[]; 7 | } 8 | 9 | export class Igredient { 10 | name: string; 11 | unit: Unit; 12 | quantity: number; 13 | } 14 | -------------------------------------------------------------------------------- /src/recipe/recipe.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { RecipeController } from './recipe.controller'; 3 | 4 | describe('RecipeController', () => { 5 | let controller: RecipeController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [RecipeController], 10 | }).compile(); 11 | 12 | controller = module.get(RecipeController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/recipe/recipe.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Body, 3 | Controller, 4 | Get, 5 | Post, 6 | Param, 7 | Patch, 8 | Delete, 9 | } from '@nestjs/common'; 10 | import { RecipeDto } from './dto/recipe.dto'; 11 | import { UpdatedescriptionDto } from './dto/update-description.dto'; 12 | import { RecipeService } from './recipe.service'; 13 | 14 | @Controller('recipe') 15 | export class RecipeController { 16 | constructor(private recipeService: RecipeService) {} 17 | 18 | @Get() 19 | async getRecipes() { 20 | return await this.recipeService.getRecipes(); 21 | } 22 | 23 | @Get('/:id') 24 | async getRecipe(@Param('id') id: string) { 25 | return await this.recipeService.getRecipe(id); 26 | } 27 | 28 | @Post() 29 | async createRecipe(@Body() recipeDto: RecipeDto) { 30 | return await this.recipeService.createRecipe(recipeDto); 31 | } 32 | 33 | @Patch('/:id') 34 | async updateDescription( 35 | @Body() { description }: UpdatedescriptionDto, 36 | @Param('id') id: string, 37 | ) { 38 | return await this.recipeService.updateDescription(id, description); 39 | } 40 | 41 | @Delete('/:id') 42 | async deleteRecipe(@Param('id') id: string) { 43 | return await this.recipeService.deleteRecipe(id); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/recipe/recipe.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { RecipeController } from './recipe.controller'; 3 | import { RecipeService } from './recipe.service'; 4 | 5 | @Module({ 6 | controllers: [RecipeController], 7 | providers: [RecipeService], 8 | }) 9 | export class RecipeModule {} 10 | -------------------------------------------------------------------------------- /src/recipe/recipe.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { RecipeService } from './recipe.service'; 3 | 4 | describe('RecipeService', () => { 5 | let service: RecipeService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [RecipeService], 10 | }).compile(); 11 | 12 | service = module.get(RecipeService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/recipe/recipe.service.ts: -------------------------------------------------------------------------------- 1 | import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; 2 | import { RecipeDto } from './dto/recipe.dto'; 3 | import { Recipe } from './entity/recipe'; 4 | import { v4 } from 'uuid'; 5 | 6 | @Injectable() 7 | export class RecipeService { 8 | private _recipes: Recipe[] = []; 9 | 10 | async getRecipes(): Promise { 11 | return this._recipes; 12 | } 13 | 14 | async getRecipe(id: string): Promise { 15 | const recipe = this._recipes.find((r) => r.id === id); 16 | if (!recipe) { 17 | throw new HttpException('NotFound', HttpStatus.NOT_FOUND); 18 | } 19 | return recipe; 20 | } 21 | 22 | async createRecipe(recipe: RecipeDto): Promise { 23 | const recipeEntity = { ...recipe, id: v4() }; 24 | this._recipes.push(recipeEntity); 25 | } 26 | 27 | async updateDescription(id: string, description: string): Promise { 28 | const recipeIndex = this._recipes.findIndex((r) => r.id === id); 29 | if (recipeIndex < 0) { 30 | throw new HttpException('NotFound', HttpStatus.NOT_FOUND); 31 | } 32 | this._recipes[recipeIndex] = { ...this._recipes[recipeIndex], description }; 33 | } 34 | 35 | async deleteRecipe(id: string): Promise { 36 | this._recipes = this._recipes.filter((r) => r.id !== id); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | --------------------------------------------------------------------------------