├── .gitignore ├── .prettierrc ├── README.md ├── nest-cli.json ├── nodemon-debug.json ├── nodemon.json ├── package-lock.json ├── package.json ├── src ├── app.controller.spec.ts ├── app.controller.ts ├── app.module.ts ├── app.service.ts ├── main.ts └── product │ ├── dto │ └── product.dto.ts │ ├── interfaces │ └── product.interface.ts │ ├── product.controller.spec.ts │ ├── product.controller.ts │ ├── product.module.ts │ ├── product.service.spec.ts │ ├── product.service.ts │ └── schemas │ └── product.schema.ts ├── test ├── app.e2e-spec.ts └── jest-e2e.json ├── tsconfig.build.json ├── tsconfig.json └── tslint.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master 6 | [travis-url]: https://travis-ci.org/nestjs/nest 7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux 8 | [linux-url]: https://travis-ci.org/nestjs/nest 9 | 10 |

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

11 |

12 | NPM Version 13 | Package License 14 | NPM Downloads 15 | Travis 16 | Linux 17 | Coverage 18 | Gitter 19 | Backers on Open Collective 20 | Sponsors on Open Collective 21 | 22 | 23 |

24 | 26 | 27 | ## Description 28 | 29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 30 | 31 | ## Installation 32 | 33 | ```bash 34 | $ npm install 35 | ``` 36 | 37 | ## Running the app 38 | 39 | ```bash 40 | # development 41 | $ npm run start 42 | 43 | # watch mode 44 | $ npm run start:dev 45 | 46 | # production mode 47 | $ npm run start:prod 48 | ``` 49 | 50 | ## Test 51 | 52 | ```bash 53 | # unit tests 54 | $ npm run test 55 | 56 | # e2e tests 57 | $ npm run test:e2e 58 | 59 | # test coverage 60 | $ npm run test:cov 61 | ``` 62 | 63 | ## Support 64 | 65 | 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). 66 | 67 | ## Stay in touch 68 | 69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 70 | - Website - [https://nestjs.com](https://nestjs.com/) 71 | - Twitter - [@nestframework](https://twitter.com/nestframework) 72 | 73 | ## License 74 | 75 | Nest is [MIT licensed](LICENSE). 76 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "ts", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /nodemon-debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts" 6 | } 7 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": ["src/**/*.spec.ts"], 5 | "exec": "ts-node -r tsconfig-paths/register src/main.ts" 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nest-products-api", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "license": "MIT", 7 | "scripts": { 8 | "build": "tsc -p tsconfig.build.json", 9 | "format": "prettier --write \"src/**/*.ts\"", 10 | "start": "ts-node -r tsconfig-paths/register src/main.ts", 11 | "start:dev": "nodemon", 12 | "start:debug": "nodemon --config nodemon-debug.json", 13 | "prestart:prod": "rimraf dist && npm run build", 14 | "start:prod": "node dist/main.js", 15 | "lint": "tslint -p tsconfig.json -c tslint.json", 16 | "test": "jest", 17 | "test:watch": "jest --watch", 18 | "test:cov": "jest --coverage", 19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 20 | "test:e2e": "jest --config ./test/jest-e2e.json" 21 | }, 22 | "dependencies": { 23 | "@nestjs/common": "^6.0.0", 24 | "@nestjs/core": "^6.0.0", 25 | "@nestjs/mongoose": "^6.0.0", 26 | "@nestjs/platform-express": "^6.0.0", 27 | "mongoose": "^5.4.20", 28 | "reflect-metadata": "^0.1.12", 29 | "rimraf": "^2.6.2", 30 | "rxjs": "^6.3.3" 31 | }, 32 | "devDependencies": { 33 | "@nestjs/testing": "^6.0.0", 34 | "@types/express": "^4.16.0", 35 | "@types/jest": "^23.3.13", 36 | "@types/mongoose": "^5.3.24", 37 | "@types/node": "^10.12.18", 38 | "@types/supertest": "^2.0.7", 39 | "jest": "^23.6.0", 40 | "nodemon": "^1.18.9", 41 | "prettier": "^1.15.3", 42 | "supertest": "^3.4.1", 43 | "ts-jest": "^23.10.5", 44 | "ts-node": "^7.0.1", 45 | "tsconfig-paths": "^3.7.0", 46 | "tslint": "5.12.1", 47 | "typescript": "^3.2.4" 48 | }, 49 | "jest": { 50 | "moduleFileExtensions": [ 51 | "js", 52 | "json", 53 | "ts" 54 | ], 55 | "rootDir": "src", 56 | "testRegex": ".spec.ts$", 57 | "transform": { 58 | "^.+\\.(t|j)s$": "ts-jest" 59 | }, 60 | "coverageDirectory": "../coverage", 61 | "testEnvironment": "node" 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let appController: AppController; 7 | 8 | beforeEach(async () => { 9 | const app: TestingModule = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | 14 | appController = app.get(AppController); 15 | }); 16 | 17 | describe('root', () => { 18 | it('should return "Hello World!"', () => { 19 | expect(appController.getHello()).toBe('Hello World!'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | 4 | @Controller() 5 | export class AppController { 6 | constructor(private readonly appService: AppService) {} 7 | 8 | @Get() 9 | getHello(): string { 10 | return this.appService.getHello(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | import { MongooseModule } from "@nestjs/mongoose"; 5 | import { ProductModule } from './product/product.module'; 6 | 7 | @Module({ 8 | imports: [ 9 | MongooseModule.forRoot('mongodb://localhost/products-nest', { 10 | useNewUrlParser: true 11 | }), 12 | ProductModule 13 | ], 14 | controllers: [AppController], 15 | providers: [AppService], 16 | }) 17 | export class AppModule {} 18 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /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 | app.enableCors(); 7 | await app.listen(3000); 8 | console.log('Server on port', 3000) 9 | } 10 | bootstrap(); 11 | -------------------------------------------------------------------------------- /src/product/dto/product.dto.ts: -------------------------------------------------------------------------------- 1 | export class CreateProductDTO { 2 | readonly name: string; 3 | readonly description: string; 4 | readonly imageURL: string; 5 | readonly price: number; 6 | readonly createdAt: Date; 7 | } -------------------------------------------------------------------------------- /src/product/interfaces/product.interface.ts: -------------------------------------------------------------------------------- 1 | import { Document } from "mongoose"; 2 | 3 | export interface Product extends Document { 4 | readonly name: string; 5 | readonly description: string; 6 | readonly imageURL: string; 7 | readonly price: number; 8 | readonly createdAt: Date; 9 | } -------------------------------------------------------------------------------- /src/product/product.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { ProductController } from './product.controller'; 3 | 4 | describe('Product Controller', () => { 5 | let controller: ProductController; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | controllers: [ProductController], 10 | }).compile(); 11 | 12 | controller = module.get(ProductController); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(controller).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/product/product.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Post, Res, HttpStatus, Body, Get, Param, NotFoundException, Delete, Query, Put } from '@nestjs/common'; 2 | import { ProductService } from "./product.service"; 3 | 4 | import { CreateProductDTO } from "./dto/product.dto"; 5 | 6 | @Controller('product') 7 | export class ProductController { 8 | 9 | constructor(private productService: ProductService) { } 10 | 11 | // Add Product: /product/create 12 | @Post('/create') 13 | async createProduct(@Res() res, @Body() createProductDTO: CreateProductDTO) { 14 | const product = await this.productService.createProduct(createProductDTO); 15 | return res.status(HttpStatus.OK).json({ 16 | message: 'Product Successfully Created', 17 | product 18 | }); 19 | } 20 | 21 | // Get Products /product 22 | // @Get('/list') 23 | @Get('/') 24 | async getProducts(@Res() res) { 25 | const products = await this.productService.getProducts(); 26 | return res.status(HttpStatus.OK).json(products); 27 | } 28 | 29 | // GET single product: /product/5c9d46100e2e5c44c444b2d1 30 | @Get('/:productID') 31 | async getProduct(@Res() res, @Param('productID') productID) { 32 | const product = await this.productService.getProduct(productID); 33 | if (!product) throw new NotFoundException('Product does not exist!'); 34 | return res.status(HttpStatus.OK).json(product); 35 | } 36 | 37 | // Delete Product: /delete?productID=5c9d45e705ea4843c8d0e8f7 38 | @Delete('/delete') 39 | async deleteProduct(@Res() res, @Query('productID') productID) { 40 | const productDeleted = await this.productService.deleteProduct(productID); 41 | if (!productDeleted) throw new NotFoundException('Product does not exist!'); 42 | return res.status(HttpStatus.OK).json({ 43 | message: 'Product Deleted Successfully', 44 | productDeleted 45 | }); 46 | } 47 | 48 | // Update Product: /update?productID=5c9d45e705ea4843c8d0e8f7 49 | @Put('/update') 50 | async updateProduct(@Res() res, @Body() createProductDTO: CreateProductDTO, @Query('productID') productID) { 51 | const updatedProduct = await this.productService.updateProduct(productID, createProductDTO); 52 | if (!updatedProduct) throw new NotFoundException('Product does not exist!'); 53 | return res.status(HttpStatus.OK).json({ 54 | message: 'Product Updated Successfully', 55 | updatedProduct 56 | }); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/product/product.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ProductService } from './product.service'; 3 | import { ProductController } from './product.controller'; 4 | // Mongoose 5 | import { MongooseModule } from '@nestjs/mongoose'; 6 | import { ProductSchema } from './schemas/product.schema'; 7 | 8 | @Module({ 9 | imports: [MongooseModule.forFeature([{name: 'Product', schema: ProductSchema}])], 10 | providers: [ProductService], 11 | controllers: [ProductController] 12 | }) 13 | export class ProductModule {} 14 | -------------------------------------------------------------------------------- /src/product/product.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { ProductService } from './product.service'; 3 | 4 | describe('ProductService', () => { 5 | let service: ProductService; 6 | 7 | beforeEach(async () => { 8 | const module: TestingModule = await Test.createTestingModule({ 9 | providers: [ProductService], 10 | }).compile(); 11 | 12 | service = module.get(ProductService); 13 | }); 14 | 15 | it('should be defined', () => { 16 | expect(service).toBeDefined(); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /src/product/product.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | import { Model } from "mongoose"; 4 | import { InjectModel } from "@nestjs/mongoose"; 5 | 6 | import { Product } from "./interfaces/product.interface"; 7 | import { CreateProductDTO } from "./dto/product.dto"; 8 | 9 | @Injectable() 10 | export class ProductService { 11 | 12 | constructor(@InjectModel('Product') private readonly productModel: Model) {} 13 | 14 | // Get all products 15 | async getProducts(): Promise { 16 | const products = await this.productModel.find(); 17 | return products; 18 | } 19 | 20 | // Get a single Product 21 | async getProduct(productID: string): Promise { 22 | const product = await this.productModel.findById(productID); 23 | return product; 24 | } 25 | 26 | // Post a single product 27 | async createProduct(createProductDTO: CreateProductDTO): Promise { 28 | const newProduct = new this.productModel(createProductDTO); 29 | return newProduct.save(); 30 | } 31 | 32 | // Delete Product 33 | async deleteProduct(productID: string): Promise { 34 | const deletedProduct = await this.productModel.findOneAndDelete(productID); 35 | return deletedProduct; 36 | } 37 | 38 | // Put a single product 39 | async updateProduct(productID: string, createProductDTO: CreateProductDTO): Promise { 40 | const updatedProduct = await this.productModel 41 | .findByIdAndUpdate(productID, createProductDTO, {new: true}); 42 | return updatedProduct; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/product/schemas/product.schema.ts: -------------------------------------------------------------------------------- 1 | import { Schema } from "mongoose"; 2 | 3 | export const ProductSchema = new Schema({ 4 | name: String, 5 | description: String, 6 | imageURL: String, 7 | price: Number, 8 | createdAt: { type: Date, default: Date.now } 9 | }); 10 | 11 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import * as request from 'supertest'; 3 | import { AppModule } from './../src/app.module'; 4 | 5 | describe('AppController (e2e)', () => { 6 | let app; 7 | 8 | beforeEach(async () => { 9 | const moduleFixture: TestingModule = await Test.createTestingModule({ 10 | imports: [AppModule], 11 | }).compile(); 12 | 13 | app = moduleFixture.createNestApplication(); 14 | await app.init(); 15 | }); 16 | 17 | it('/ (GET)', () => { 18 | return request(app.getHttpServer()) 19 | .get('/') 20 | .expect(200) 21 | .expect('Hello World!'); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /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", "**/*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 | "target": "es6", 9 | "sourceMap": true, 10 | "outDir": "./dist", 11 | "baseUrl": "./" 12 | }, 13 | "exclude": ["node_modules"] 14 | } 15 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": ["tslint:recommended"], 4 | "jsRules": { 5 | "no-unused-expression": true 6 | }, 7 | "rules": { 8 | "quotemark": [true, "single"], 9 | "member-access": [false], 10 | "ordered-imports": [false], 11 | "max-line-length": [true, 150], 12 | "member-ordering": [false], 13 | "interface-name": [false], 14 | "arrow-parens": false, 15 | "object-literal-sort-keys": false 16 | }, 17 | "rulesDirectory": [] 18 | } 19 | --------------------------------------------------------------------------------