├── .eslintrc.js
├── .gitignore
├── .prettierrc
├── README.md
├── nest-cli.json
├── package-lock.json
├── package.json
├── src
├── app.controller.spec.ts
├── app.controller.ts
├── app.module.ts
├── app.service.ts
├── main.ts
└── products
│ ├── dto
│ ├── create-product.dto.ts
│ └── update-product.dto.ts
│ ├── products.controller.spec.ts
│ ├── products.controller.ts
│ ├── products.module.ts
│ ├── products.service.ts
│ └── schemas
│ └── product.schema.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 | sourceType: 'module',
6 | },
7 | plugins: ['@typescript-eslint/eslint-plugin'],
8 | extends: [
9 | 'plugin:@typescript-eslint/recommended',
10 | 'prettier/@typescript-eslint',
11 | 'plugin:prettier/recommended',
12 | ],
13 | root: true,
14 | env: {
15 | node: true,
16 | jest: true,
17 | },
18 | rules: {
19 | '@typescript-eslint/interface-name-prefix': 'off',
20 | '@typescript-eslint/explicit-function-return-type': 'off',
21 | '@typescript-eslint/explicit-module-boundary-types': 'off',
22 | '@typescript-eslint/no-explicit-any': 'off',
23 | },
24 | };
25 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /node_modules
4 |
5 | # Logs
6 | logs
7 | *.log
8 | npm-debug.log*
9 | yarn-debug.log*
10 | yarn-error.log*
11 | lerna-debug.log*
12 |
13 | # OS
14 | .DS_Store
15 |
16 | # Tests
17 | /coverage
18 | /.nyc_output
19 |
20 | # IDEs and editors
21 | /.idea
22 | .project
23 | .classpath
24 | .c9/
25 | *.launch
26 | .settings/
27 | *.sublime-workspace
28 |
29 | # IDE - VSCode
30 | .vscode/*
31 | !.vscode/settings.json
32 | !.vscode/tasks.json
33 | !.vscode/launch.json
34 | !.vscode/extensions.json
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
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 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
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 | "collection": "@nestjs/schematics",
3 | "sourceRoot": "src"
4 | }
5 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nest-rest-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": "^7.5.1",
25 | "@nestjs/core": "^7.5.1",
26 | "@nestjs/mongoose": "^7.1.0",
27 | "@nestjs/platform-express": "^7.5.1",
28 | "mongoose": "^5.10.15",
29 | "reflect-metadata": "^0.1.13",
30 | "rimraf": "^3.0.2",
31 | "rxjs": "^6.6.3"
32 | },
33 | "devDependencies": {
34 | "@nestjs/cli": "^7.5.1",
35 | "@nestjs/schematics": "^7.1.3",
36 | "@nestjs/testing": "^7.5.1",
37 | "@types/express": "^4.17.8",
38 | "@types/jest": "^26.0.15",
39 | "@types/mongoose": "^5.10.1",
40 | "@types/node": "^14.14.6",
41 | "@types/supertest": "^2.0.10",
42 | "@typescript-eslint/eslint-plugin": "^4.6.1",
43 | "@typescript-eslint/parser": "^4.6.1",
44 | "eslint": "^7.12.1",
45 | "eslint-config-prettier": "^6.15.0",
46 | "eslint-plugin-prettier": "^3.1.4",
47 | "jest": "^26.6.3",
48 | "prettier": "^2.1.2",
49 | "supertest": "^6.0.0",
50 | "ts-jest": "^26.4.3",
51 | "ts-loader": "^8.0.8",
52 | "ts-node": "^9.0.0",
53 | "tsconfig-paths": "^3.9.0",
54 | "typescript": "^4.0.5"
55 | },
56 | "jest": {
57 | "moduleFileExtensions": [
58 | "js",
59 | "json",
60 | "ts"
61 | ],
62 | "rootDir": "src",
63 | "testRegex": ".*\\.spec\\.ts$",
64 | "transform": {
65 | "^.+\\.(t|j)s$": "ts-jest"
66 | },
67 | "collectCoverageFrom": [
68 | "**/*.(t|j)s"
69 | ],
70 | "coverageDirectory": "../coverage",
71 | "testEnvironment": "node"
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/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 'Hello NestJS!'
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { MongooseModule } from '@nestjs/mongoose';
3 | import { AppController } from './app.controller';
4 | import { AppService } from './app.service';
5 | import { ProductsModule } from './products/products.module';
6 |
7 | @Module({
8 | imports: [
9 | ProductsModule,
10 | MongooseModule.forRoot(`mongodb+srv://vladilen:vlad1234@cluster0.a2hy1.mongodb.net/products?retryWrites=true&w=majority`)
11 | ],
12 | controllers: [AppController],
13 | providers: [AppService],
14 | })
15 | export class AppModule {}
16 |
--------------------------------------------------------------------------------
/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 | await app.listen(3000);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/src/products/dto/create-product.dto.ts:
--------------------------------------------------------------------------------
1 | export class CreateProductDto {
2 | readonly title: string
3 | readonly price: number
4 | }
--------------------------------------------------------------------------------
/src/products/dto/update-product.dto.ts:
--------------------------------------------------------------------------------
1 | export class UpdateProductDto {
2 | readonly title: string
3 | readonly price: number
4 | }
--------------------------------------------------------------------------------
/src/products/products.controller.spec.ts:
--------------------------------------------------------------------------------
1 | import { Test, TestingModule } from '@nestjs/testing';
2 | import { ProductsController } from './products.controller';
3 |
4 | describe('ProductsController', () => {
5 | let controller: ProductsController;
6 |
7 | beforeEach(async () => {
8 | const module: TestingModule = await Test.createTestingModule({
9 | controllers: [ProductsController],
10 | }).compile();
11 |
12 | controller = module.get(ProductsController);
13 | });
14 |
15 | it('should be defined', () => {
16 | expect(controller).toBeDefined();
17 | });
18 | });
19 |
--------------------------------------------------------------------------------
/src/products/products.controller.ts:
--------------------------------------------------------------------------------
1 | import {
2 | Body,
3 | Controller,
4 | Delete,
5 | Get,
6 | Header,
7 | HttpCode,
8 | HttpStatus,
9 | Param,
10 | Post,
11 | Put,
12 | } from '@nestjs/common';
13 | import { CreateProductDto } from './dto/create-product.dto';
14 | import { UpdateProductDto } from './dto/update-product.dto';
15 | import { ProductsService } from './products.service';
16 | import { Product } from './schemas/product.schema';
17 |
18 | // express
19 | // app.use((req, res, next) => {
20 | // res.status(201).end('Poka')
21 | // })
22 |
23 | @Controller('products')
24 | export class ProductsController {
25 |
26 | constructor(private readonly productsService: ProductsService) {
27 | }
28 |
29 | // @Get()
30 | // // @Redirect('https://google.com', 301)
31 | // getAll(@Req() req: Request, @Res() res: Response): string {
32 | // res.status(201).end('Poke')
33 | // return 'getAll'
34 | // }
35 |
36 | @Get()
37 | getAll(): Promise {
38 | return this.productsService.getAll()
39 | }
40 |
41 | @Get(':id')
42 | getOne(@Param('id') id: string): Promise {
43 | return this.productsService.getById(id)
44 | }
45 |
46 | @Post()
47 | @HttpCode(HttpStatus.CREATED)
48 | @Header('Cache-Control', 'none')
49 | create(@Body() createProductDto: CreateProductDto): Promise {
50 | return this.productsService.create(createProductDto)
51 | }
52 |
53 | @Delete(':id')
54 | remove(@Param('id') id: string): Promise {
55 | return this.productsService.remove(id)
56 | }
57 |
58 | @Put(':id')
59 | update(@Body() updateProductDto: UpdateProductDto, @Param('id') id: string): Promise {
60 | return this.productsService.update(id, updateProductDto)
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/products/products.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { MongooseModule } from '@nestjs/mongoose';
3 | import { ProductsService } from './products.service';
4 | import { ProductsController } from './products.controller';
5 | import { Product, ProductSchema } from './schemas/product.schema';
6 |
7 | @Module({
8 | providers: [ProductsService],
9 | controllers: [ProductsController],
10 | imports: [
11 | MongooseModule.forFeature([
12 | {name: Product.name, schema: ProductSchema}
13 | ])
14 | ]
15 | })
16 | export class ProductsModule {
17 | }
--------------------------------------------------------------------------------
/src/products/products.service.ts:
--------------------------------------------------------------------------------
1 | import { Injectable } from '@nestjs/common';
2 | import { InjectModel } from '@nestjs/mongoose';
3 | import { Model } from 'mongoose';
4 | import { CreateProductDto } from './dto/create-product.dto';
5 | import { Product, ProductDocument } from './schemas/product.schema';
6 | import { UpdateProductDto } from './dto/update-product.dto';
7 |
8 | @Injectable()
9 | export class ProductsService {
10 | constructor(@InjectModel(Product.name) private productModel: Model) {
11 | }
12 |
13 | async getAll(): Promise {
14 | return this.productModel.find().exec()
15 | }
16 |
17 | async getById(id: string): Promise {
18 | return this.productModel.findById(id)
19 | }
20 |
21 | async create(productDto: CreateProductDto): Promise {
22 | const newProduct = new this.productModel(productDto)
23 | return newProduct.save()
24 | }
25 |
26 | async remove(id: string): Promise {
27 | return this.productModel.findByIdAndRemove(id)
28 | }
29 |
30 | async update(id: string, productDto: UpdateProductDto): Promise {
31 | return this.productModel.findByIdAndUpdate(id, productDto, {new: true})
32 | }
33 | }
--------------------------------------------------------------------------------
/src/products/schemas/product.schema.ts:
--------------------------------------------------------------------------------
1 | import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
2 | import { Document } from 'mongoose'
3 |
4 | export type ProductDocument = Product & Document
5 |
6 | @Schema()
7 | export class Product {
8 | @Prop()
9 | title: string
10 |
11 | @Prop()
12 | price: number
13 | }
14 |
15 | export const ProductSchema = SchemaFactory.createForClass(Product)
--------------------------------------------------------------------------------
/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 | }
15 | }
16 |
--------------------------------------------------------------------------------