├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .prettierignore ├── .prettierrc ├── .vscode └── extensions.json ├── README.md ├── apps ├── .gitkeep ├── api-gateway │ ├── .eslintrc.json │ ├── jest.config.ts │ ├── project.json │ ├── src │ │ ├── app │ │ │ ├── .gitkeep │ │ │ ├── app.controller.spec.ts │ │ │ ├── app.controller.ts │ │ │ ├── app.module.ts │ │ │ ├── app.service.spec.ts │ │ │ └── app.service.ts │ │ ├── assets │ │ │ └── .gitkeep │ │ ├── auth │ │ │ ├── auth.controller.ts │ │ │ ├── auth.module.ts │ │ │ └── auth.service.ts │ │ ├── environments │ │ │ ├── environment.prod.ts │ │ │ └── environment.ts │ │ ├── main.ts │ │ └── payment │ │ │ ├── payment.controller.ts │ │ │ ├── payment.service.ts │ │ │ └── payments.module.ts │ ├── tsconfig.app.json │ ├── tsconfig.json │ └── tsconfig.spec.json ├── auth-microservice │ ├── .eslintrc.json │ ├── jest.config.ts │ ├── project.json │ ├── src │ │ ├── app │ │ │ ├── .gitkeep │ │ │ ├── app.controller.spec.ts │ │ │ ├── app.controller.ts │ │ │ ├── app.module.ts │ │ │ ├── app.service.spec.ts │ │ │ ├── app.service.ts │ │ │ └── users.repository.ts │ │ ├── assets │ │ │ └── .gitkeep │ │ ├── environments │ │ │ ├── environment.prod.ts │ │ │ └── environment.ts │ │ └── main.ts │ ├── tsconfig.app.json │ ├── tsconfig.json │ └── tsconfig.spec.json └── payments-microservice │ ├── .eslintrc.json │ ├── jest.config.ts │ ├── project.json │ ├── src │ ├── app │ │ ├── .gitkeep │ │ ├── app.controller.spec.ts │ │ ├── app.controller.ts │ │ ├── app.module.ts │ │ ├── app.service.spec.ts │ │ └── app.service.ts │ ├── assets │ │ └── .gitkeep │ ├── environments │ │ ├── environment.prod.ts │ │ └── environment.ts │ └── main.ts │ ├── tsconfig.app.json │ ├── tsconfig.json │ └── tsconfig.spec.json ├── jest.config.ts ├── jest.preset.js ├── libs ├── .gitkeep └── shared │ ├── .babelrc │ ├── .eslintrc.json │ ├── README.md │ ├── jest.config.ts │ ├── project.json │ ├── src │ ├── index.ts │ └── lib │ │ ├── dto │ │ ├── create-user.dto.ts │ │ ├── index.ts │ │ └── make-payment.dto.ts │ │ ├── entities │ │ ├── index.ts │ │ └── user.entity.ts │ │ ├── shared.spec.ts │ │ └── shared.ts │ ├── tsconfig.json │ ├── tsconfig.lib.json │ └── tsconfig.spec.json ├── nx.json ├── package-lock.json ├── package.json ├── tools ├── generators │ └── .gitkeep └── tsconfig.tools.json └── tsconfig.base.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "ignorePatterns": ["**/*"], 4 | "plugins": ["@nrwl/nx"], 5 | "overrides": [ 6 | { 7 | "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], 8 | "rules": { 9 | "@nrwl/nx/enforce-module-boundaries": [ 10 | "error", 11 | { 12 | "enforceBuildableLibDependency": true, 13 | "allow": [], 14 | "depConstraints": [ 15 | { 16 | "sourceTag": "*", 17 | "onlyDependOnLibsWithTags": ["*"] 18 | } 19 | ] 20 | } 21 | ] 22 | } 23 | }, 24 | { 25 | "files": ["*.ts", "*.tsx"], 26 | "extends": ["plugin:@nrwl/nx/typescript"], 27 | "rules": {} 28 | }, 29 | { 30 | "files": ["*.js", "*.jsx"], 31 | "extends": ["plugin:@nrwl/nx/javascript"], 32 | "rules": {} 33 | }, 34 | { 35 | "files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"], 36 | "env": { 37 | "jest": true 38 | }, 39 | "rules": {} 40 | } 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | yarn-error.log 34 | testem.log 35 | /typings 36 | 37 | # System Files 38 | .DS_Store 39 | Thumbs.db 40 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | # Add files here to ignore them from prettier formatting 2 | 3 | /dist 4 | /coverage 5 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true 3 | } 4 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "nrwl.angular-console", 4 | "esbenp.prettier-vscode", 5 | "firsttris.vscode-jest-runner", 6 | "dbaeumer.vscode-eslint" 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # NestjsMicroservices 4 | 5 | This project was generated using [Nx](https://nx.dev). 6 | 7 |

8 | 9 | 🔎 **Smart, Fast and Extensible Build System** 10 | 11 | ## Adding capabilities to your workspace 12 | 13 | Nx supports many plugins which add capabilities for developing different types of applications and different tools. 14 | 15 | These capabilities include generating applications, libraries, etc as well as the devtools to test, and build projects as well. 16 | 17 | Below are our core plugins: 18 | 19 | - [React](https://reactjs.org) 20 | - `npm install --save-dev @nrwl/react` 21 | - Web (no framework frontends) 22 | - `npm install --save-dev @nrwl/web` 23 | - [Angular](https://angular.io) 24 | - `npm install --save-dev @nrwl/angular` 25 | - [Nest](https://nestjs.com) 26 | - `npm install --save-dev @nrwl/nest` 27 | - [Express](https://expressjs.com) 28 | - `npm install --save-dev @nrwl/express` 29 | - [Node](https://nodejs.org) 30 | - `npm install --save-dev @nrwl/node` 31 | 32 | There are also many [community plugins](https://nx.dev/community) you could add. 33 | 34 | ## Generate an application 35 | 36 | Run `nx g @nrwl/react:app my-app` to generate an application. 37 | 38 | > You can use any of the plugins above to generate applications as well. 39 | 40 | When using Nx, you can create multiple applications and libraries in the same workspace. 41 | 42 | ## Generate a library 43 | 44 | Run `nx g @nrwl/react:lib my-lib` to generate a library. 45 | 46 | > You can also use any of the plugins above to generate libraries as well. 47 | 48 | Libraries are shareable across libraries and applications. They can be imported from `@nestjs-microservices/mylib`. 49 | 50 | ## Development server 51 | 52 | Run `nx serve my-app` for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files. 53 | 54 | ## Code scaffolding 55 | 56 | Run `nx g @nrwl/react:component my-component --project=my-app` to generate a new component. 57 | 58 | ## Build 59 | 60 | Run `nx build my-app` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. 61 | 62 | ## Running unit tests 63 | 64 | Run `nx test my-app` to execute the unit tests via [Jest](https://jestjs.io). 65 | 66 | Run `nx affected:test` to execute the unit tests affected by a change. 67 | 68 | ## Running end-to-end tests 69 | 70 | Run `nx e2e my-app` to execute the end-to-end tests via [Cypress](https://www.cypress.io). 71 | 72 | Run `nx affected:e2e` to execute the end-to-end tests affected by a change. 73 | 74 | ## Understand your workspace 75 | 76 | Run `nx graph` to see a diagram of the dependencies of your projects. 77 | 78 | ## Further help 79 | 80 | Visit the [Nx Documentation](https://nx.dev) to learn more. 81 | 82 | 83 | 84 | ## ☁ Nx Cloud 85 | 86 | ### Distributed Computation Caching & Distributed Task Execution 87 | 88 |

89 | 90 | Nx Cloud pairs with Nx in order to enable you to build and test code more rapidly, by up to 10 times. Even teams that are new to Nx can connect to Nx Cloud and start saving time instantly. 91 | 92 | Teams using Nx gain the advantage of building full-stack applications with their preferred framework alongside Nx’s advanced code generation and project dependency graph, plus a unified experience for both frontend and backend developers. 93 | 94 | Visit [Nx Cloud](https://nx.app/) to learn more. 95 | -------------------------------------------------------------------------------- /apps/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/apps/.gitkeep -------------------------------------------------------------------------------- /apps/api-gateway/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["../../.eslintrc.json"], 3 | "ignorePatterns": ["!**/*"], 4 | "overrides": [ 5 | { 6 | "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], 7 | "rules": {} 8 | }, 9 | { 10 | "files": ["*.ts", "*.tsx"], 11 | "rules": {} 12 | }, 13 | { 14 | "files": ["*.js", "*.jsx"], 15 | "rules": {} 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /apps/api-gateway/jest.config.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | export default { 3 | displayName: 'api-gateway', 4 | preset: '../../jest.preset.js', 5 | globals: { 6 | 'ts-jest': { 7 | tsconfig: '/tsconfig.spec.json', 8 | }, 9 | }, 10 | testEnvironment: 'node', 11 | transform: { 12 | '^.+\\.[tj]s$': 'ts-jest', 13 | }, 14 | moduleFileExtensions: ['ts', 'js', 'html'], 15 | coverageDirectory: '../../coverage/apps/api-gateway', 16 | }; 17 | -------------------------------------------------------------------------------- /apps/api-gateway/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "api-gateway", 3 | "$schema": "../../node_modules/nx/schemas/project-schema.json", 4 | "sourceRoot": "apps/api-gateway/src", 5 | "projectType": "application", 6 | "targets": { 7 | "build": { 8 | "executor": "@nrwl/webpack:webpack", 9 | "outputs": ["{options.outputPath}"], 10 | "options": { 11 | "target": "node", 12 | "compiler": "tsc", 13 | "outputPath": "dist/apps/api-gateway", 14 | "main": "apps/api-gateway/src/main.ts", 15 | "tsConfig": "apps/api-gateway/tsconfig.app.json", 16 | "assets": ["apps/api-gateway/src/assets"] 17 | }, 18 | "configurations": { 19 | "production": { 20 | "optimization": true, 21 | "extractLicenses": true, 22 | "inspect": false, 23 | "fileReplacements": [ 24 | { 25 | "replace": "apps/api-gateway/src/environments/environment.ts", 26 | "with": "apps/api-gateway/src/environments/environment.prod.ts" 27 | } 28 | ] 29 | } 30 | } 31 | }, 32 | "serve": { 33 | "executor": "@nrwl/js:node", 34 | "options": { 35 | "buildTarget": "api-gateway:build" 36 | }, 37 | "configurations": { 38 | "production": { 39 | "buildTarget": "api-gateway:build:production" 40 | } 41 | } 42 | }, 43 | "lint": { 44 | "executor": "@nrwl/linter:eslint", 45 | "outputs": ["{options.outputFile}"], 46 | "options": { 47 | "lintFilePatterns": ["apps/api-gateway/**/*.ts"] 48 | } 49 | }, 50 | "test": { 51 | "executor": "@nrwl/jest:jest", 52 | "outputs": ["coverage/apps/api-gateway"], 53 | "options": { 54 | "jestConfig": "apps/api-gateway/jest.config.ts", 55 | "passWithNoTests": true 56 | } 57 | } 58 | }, 59 | "tags": [] 60 | } 61 | -------------------------------------------------------------------------------- /apps/api-gateway/src/app/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/apps/api-gateway/src/app/.gitkeep -------------------------------------------------------------------------------- /apps/api-gateway/src/app/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | 3 | import { AppController } from './app.controller'; 4 | import { AppService } from './app.service'; 5 | 6 | describe('AppController', () => { 7 | let app: TestingModule; 8 | 9 | beforeAll(async () => { 10 | app = await Test.createTestingModule({ 11 | controllers: [AppController], 12 | providers: [AppService], 13 | }).compile(); 14 | }); 15 | 16 | describe('getData', () => { 17 | it('should return "Welcome to api-gateway!"', () => { 18 | const appController = app.get(AppController); 19 | expect(appController.getData()).toEqual({ 20 | message: 'Welcome to api-gateway!', 21 | }); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /apps/api-gateway/src/app/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | 3 | import { AppService } from './app.service'; 4 | 5 | @Controller() 6 | export class AppController { 7 | constructor(private readonly appService: AppService) {} 8 | 9 | @Get() 10 | getData() { 11 | return this.appService.getData(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /apps/api-gateway/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AuthModule } from '../auth/auth.module'; 3 | import { PaymentModule } from '../payment/payments.module'; 4 | 5 | import { AppController } from './app.controller'; 6 | import { AppService } from './app.service'; 7 | 8 | @Module({ 9 | imports: [AuthModule, PaymentModule], 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }) 13 | export class AppModule {} 14 | -------------------------------------------------------------------------------- /apps/api-gateway/src/app/app.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test } from '@nestjs/testing'; 2 | 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppService', () => { 6 | let service: AppService; 7 | 8 | beforeAll(async () => { 9 | const app = await Test.createTestingModule({ 10 | providers: [AppService], 11 | }).compile(); 12 | 13 | service = app.get(AppService); 14 | }); 15 | 16 | describe('getData', () => { 17 | it('should return "Welcome to api-gateway!"', () => { 18 | expect(service.getData()).toEqual({ message: 'Welcome to api-gateway!' }); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /apps/api-gateway/src/app/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getData(): { message: string } { 6 | return { message: 'Welcome to api-gateway!' }; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /apps/api-gateway/src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/apps/api-gateway/src/assets/.gitkeep -------------------------------------------------------------------------------- /apps/api-gateway/src/auth/auth.controller.ts: -------------------------------------------------------------------------------- 1 | import { Body, Controller, Post, ValidationPipe } from '@nestjs/common'; 2 | import { AuthService } from './auth.service'; 3 | import { CreateUserDto } from '@nestjs-microservices/shared/dto'; 4 | 5 | @Controller('auth') 6 | export class AuthController { 7 | constructor(private readonly authService: AuthService) {} 8 | 9 | @Post('sign-up') 10 | createUser(@Body(ValidationPipe) createUserDto: CreateUserDto) { 11 | return this.authService.createUser(createUserDto); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /apps/api-gateway/src/auth/auth.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ClientsModule, Transport } from '@nestjs/microservices'; 3 | import { AuthController } from './auth.controller'; 4 | import { AuthService } from './auth.service'; 5 | 6 | @Module({ 7 | imports: [ 8 | ClientsModule.register([ 9 | { 10 | name: 'AUTH_MICROSERVICE', 11 | transport: Transport.KAFKA, 12 | options: { 13 | client: { 14 | clientId: 'auth', 15 | brokers: ['localhost:9092'], 16 | }, 17 | producerOnlyMode: true, 18 | consumer: { 19 | groupId: 'auth-consumer', 20 | }, 21 | }, 22 | }, 23 | ]), 24 | ], 25 | providers: [AuthService], 26 | controllers: [AuthController], 27 | }) 28 | export class AuthModule {} 29 | -------------------------------------------------------------------------------- /apps/api-gateway/src/auth/auth.service.ts: -------------------------------------------------------------------------------- 1 | import { Inject, Injectable } from '@nestjs/common'; 2 | import { ClientKafka } from '@nestjs/microservices'; 3 | import { CreateUserDto } from '@nestjs-microservices/shared/dto'; 4 | 5 | @Injectable() 6 | export class AuthService { 7 | constructor( 8 | @Inject('AUTH_MICROSERVICE') private readonly authClient: ClientKafka 9 | ) {} 10 | 11 | createUser(createUserDto: CreateUserDto) { 12 | this.authClient.emit('create_user', JSON.stringify(createUserDto)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /apps/api-gateway/src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true, 3 | }; 4 | -------------------------------------------------------------------------------- /apps/api-gateway/src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: false, 3 | }; 4 | -------------------------------------------------------------------------------- /apps/api-gateway/src/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This is not a production server yet! 3 | * This is only a minimal backend to get started. 4 | */ 5 | 6 | import { Logger } from '@nestjs/common'; 7 | import { NestFactory } from '@nestjs/core'; 8 | 9 | import { AppModule } from './app/app.module'; 10 | 11 | async function bootstrap() { 12 | const app = await NestFactory.create(AppModule); 13 | const globalPrefix = 'api'; 14 | app.setGlobalPrefix(globalPrefix); 15 | const port = process.env.PORT || 3333; 16 | await app.listen(port); 17 | Logger.log( 18 | `🚀 Application is running on: http://localhost:${port}/${globalPrefix}` 19 | ); 20 | } 21 | 22 | bootstrap(); 23 | -------------------------------------------------------------------------------- /apps/api-gateway/src/payment/payment.controller.ts: -------------------------------------------------------------------------------- 1 | import { Body, Controller, Post, ValidationPipe } from '@nestjs/common'; 2 | import { PaymentService } from './payment.service'; 3 | import { MakePaymentDto } from '@nestjs-microservices/shared/dto'; 4 | 5 | @Controller('payments') 6 | export class PaymentController { 7 | constructor(private readonly paymentService: PaymentService) {} 8 | 9 | @Post('pay') 10 | makePayment(@Body(ValidationPipe) makePaymentDto: MakePaymentDto) { 11 | return this.paymentService.makePayment(makePaymentDto); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /apps/api-gateway/src/payment/payment.service.ts: -------------------------------------------------------------------------------- 1 | import { Inject, Injectable } from '@nestjs/common'; 2 | import { ClientKafka } from '@nestjs/microservices'; 3 | import { MakePaymentDto } from '@nestjs-microservices/shared/dto'; 4 | 5 | @Injectable() 6 | export class PaymentService { 7 | constructor( 8 | @Inject('PAYMENT_MICROSERVICE') private readonly paymentClient: ClientKafka 9 | ) {} 10 | 11 | makePayment(makePaymentDto: MakePaymentDto) { 12 | this.paymentClient.emit('process_payment', JSON.stringify(makePaymentDto)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /apps/api-gateway/src/payment/payments.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ClientsModule, Transport } from '@nestjs/microservices'; 3 | import { PaymentController } from './payment.controller'; 4 | import { PaymentService } from './payment.service'; 5 | 6 | @Module({ 7 | imports: [ 8 | ClientsModule.register([ 9 | { 10 | name: 'PAYMENT_MICROSERVICE', 11 | transport: Transport.KAFKA, 12 | options: { 13 | client: { 14 | clientId: 'payment', 15 | brokers: ['localhost:9092'], 16 | }, 17 | consumer: { 18 | groupId: 'payment-consumer', 19 | }, 20 | }, 21 | }, 22 | ]), 23 | ], 24 | providers: [PaymentService], 25 | controllers: [PaymentController], 26 | }) 27 | export class PaymentModule {} 28 | -------------------------------------------------------------------------------- /apps/api-gateway/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../dist/out-tsc", 5 | "module": "commonjs", 6 | "types": ["node"], 7 | "emitDecoratorMetadata": true, 8 | "target": "es2015" 9 | }, 10 | "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], 11 | "include": ["**/*.ts"] 12 | } 13 | -------------------------------------------------------------------------------- /apps/api-gateway/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.base.json", 3 | "files": [], 4 | "include": [], 5 | "references": [ 6 | { 7 | "path": "./tsconfig.app.json" 8 | }, 9 | { 10 | "path": "./tsconfig.spec.json" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /apps/api-gateway/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../dist/out-tsc", 5 | "module": "commonjs", 6 | "types": ["jest", "node"] 7 | }, 8 | "include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] 9 | } 10 | -------------------------------------------------------------------------------- /apps/auth-microservice/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["../../.eslintrc.json"], 3 | "ignorePatterns": ["!**/*"], 4 | "overrides": [ 5 | { 6 | "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], 7 | "rules": {} 8 | }, 9 | { 10 | "files": ["*.ts", "*.tsx"], 11 | "rules": {} 12 | }, 13 | { 14 | "files": ["*.js", "*.jsx"], 15 | "rules": {} 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /apps/auth-microservice/jest.config.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | export default { 3 | displayName: 'auth-microservice', 4 | preset: '../../jest.preset.js', 5 | globals: { 6 | 'ts-jest': { 7 | tsconfig: '/tsconfig.spec.json', 8 | }, 9 | }, 10 | testEnvironment: 'node', 11 | transform: { 12 | '^.+\\.[tj]s$': 'ts-jest', 13 | }, 14 | moduleFileExtensions: ['ts', 'js', 'html'], 15 | coverageDirectory: '../../coverage/apps/auth-microservice', 16 | }; 17 | -------------------------------------------------------------------------------- /apps/auth-microservice/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "auth-microservice", 3 | "$schema": "../../node_modules/nx/schemas/project-schema.json", 4 | "sourceRoot": "apps/auth-microservice/src", 5 | "projectType": "application", 6 | "targets": { 7 | "build": { 8 | "executor": "@nrwl/webpack:webpack", 9 | "outputs": ["{options.outputPath}"], 10 | "options": { 11 | "target": "node", 12 | "compiler": "tsc", 13 | "outputPath": "dist/apps/auth-microservice", 14 | "main": "apps/auth-microservice/src/main.ts", 15 | "tsConfig": "apps/auth-microservice/tsconfig.app.json", 16 | "assets": ["apps/auth-microservice/src/assets"] 17 | }, 18 | "configurations": { 19 | "production": { 20 | "optimization": true, 21 | "extractLicenses": true, 22 | "inspect": false, 23 | "fileReplacements": [ 24 | { 25 | "replace": "apps/auth-microservice/src/environments/environment.ts", 26 | "with": "apps/auth-microservice/src/environments/environment.prod.ts" 27 | } 28 | ] 29 | } 30 | } 31 | }, 32 | "serve": { 33 | "executor": "@nrwl/js:node", 34 | "options": { 35 | "buildTarget": "auth-microservice:build" 36 | }, 37 | "configurations": { 38 | "production": { 39 | "buildTarget": "auth-microservice:build:production" 40 | } 41 | } 42 | }, 43 | "lint": { 44 | "executor": "@nrwl/linter:eslint", 45 | "outputs": ["{options.outputFile}"], 46 | "options": { 47 | "lintFilePatterns": ["apps/auth-microservice/**/*.ts"] 48 | } 49 | }, 50 | "test": { 51 | "executor": "@nrwl/jest:jest", 52 | "outputs": ["coverage/apps/auth-microservice"], 53 | "options": { 54 | "jestConfig": "apps/auth-microservice/jest.config.ts", 55 | "passWithNoTests": true 56 | } 57 | } 58 | }, 59 | "tags": [] 60 | } 61 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/app/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/apps/auth-microservice/src/app/.gitkeep -------------------------------------------------------------------------------- /apps/auth-microservice/src/app/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | 3 | import { AppController } from './app.controller'; 4 | import { AppService } from './app.service'; 5 | 6 | describe('AppController', () => { 7 | let app: TestingModule; 8 | 9 | beforeAll(async () => { 10 | app = await Test.createTestingModule({ 11 | controllers: [AppController], 12 | providers: [AppService], 13 | }).compile(); 14 | }); 15 | 16 | describe('getData', () => { 17 | it('should return "Welcome to auth-microservice!"', () => { 18 | const appController = app.get(AppController); 19 | expect(appController.getData()).toEqual({ 20 | message: 'Welcome to auth-microservice!', 21 | }); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/app/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { CreateUserDto } from '@nestjs-microservices/shared/dto'; 2 | import { Controller, ParseIntPipe, ValidationPipe } from '@nestjs/common'; 3 | import { EventPattern, MessagePattern, Payload } from '@nestjs/microservices'; 4 | 5 | import { AppService } from './app.service'; 6 | 7 | @Controller() 8 | export class AppController { 9 | constructor(private readonly appService: AppService) {} 10 | 11 | @EventPattern('create_user') 12 | handleUserCreate(@Payload(ValidationPipe) data: CreateUserDto) { 13 | this.appService.createUser(data); 14 | } 15 | 16 | @MessagePattern('get_user') 17 | handleGetUser(@Payload('userId', ParseIntPipe) userId: number) { 18 | return this.appService.getUser(userId); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | 3 | import { AppController } from './app.controller'; 4 | import { AppService } from './app.service'; 5 | import { UsersRepository } from './users.repository'; 6 | 7 | @Module({ 8 | imports: [], 9 | controllers: [AppController], 10 | providers: [AppService, UsersRepository], 11 | }) 12 | export class AppModule {} 13 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/app/app.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test } from '@nestjs/testing'; 2 | 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppService', () => { 6 | let service: AppService; 7 | 8 | beforeAll(async () => { 9 | const app = await Test.createTestingModule({ 10 | providers: [AppService], 11 | }).compile(); 12 | 13 | service = app.get(AppService); 14 | }); 15 | 16 | describe('getData', () => { 17 | it('should return "Welcome to auth-microservice!"', () => { 18 | expect(service.getData()).toEqual({ 19 | message: 'Welcome to auth-microservice!', 20 | }); 21 | }); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/app/app.service.ts: -------------------------------------------------------------------------------- 1 | import { CreateUserDto } from '@nestjs-microservices/shared/dto'; 2 | import { User } from '@nestjs-microservices/shared/entities'; 3 | import { Injectable } from '@nestjs/common'; 4 | import { UsersRepository } from './users.repository'; 5 | 6 | @Injectable() 7 | export class AppService { 8 | constructor(private readonly usersRepository: UsersRepository) {} 9 | 10 | createUser(data: CreateUserDto): void { 11 | this.usersRepository.save(data); 12 | } 13 | 14 | getUser(id: number): User { 15 | return this.usersRepository.findOne(id); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/app/users.repository.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { User } from '@nestjs-microservices/shared/entities'; 3 | 4 | @Injectable() 5 | export class UsersRepository { 6 | private readonly users: User[] = []; 7 | 8 | save(user: User) { 9 | this.users.push({ ...user, id: this.users.length + 1 }); 10 | } 11 | 12 | findOne(id: number) { 13 | return this.users.find((u) => u.id === id) || null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/apps/auth-microservice/src/assets/.gitkeep -------------------------------------------------------------------------------- /apps/auth-microservice/src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true, 3 | }; 4 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: false, 3 | }; 4 | -------------------------------------------------------------------------------- /apps/auth-microservice/src/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This is not a production server yet! 3 | * This is only a minimal backend to get started. 4 | */ 5 | 6 | import { NestFactory } from '@nestjs/core'; 7 | import { MicroserviceOptions, Transport } from '@nestjs/microservices'; 8 | 9 | import { AppModule } from './app/app.module'; 10 | 11 | async function bootstrap() { 12 | const app = await NestFactory.createMicroservice( 13 | AppModule, 14 | { 15 | transport: Transport.KAFKA, 16 | 17 | options: { 18 | client: { 19 | brokers: ['localhost:9092'], 20 | }, 21 | consumer: { 22 | groupId: 'auth-consumer', 23 | }, 24 | }, 25 | } 26 | ); 27 | await app.listen(); 28 | } 29 | 30 | bootstrap(); 31 | -------------------------------------------------------------------------------- /apps/auth-microservice/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../dist/out-tsc", 5 | "module": "commonjs", 6 | "types": ["node"], 7 | "emitDecoratorMetadata": true, 8 | "target": "es2015" 9 | }, 10 | "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], 11 | "include": ["**/*.ts"] 12 | } 13 | -------------------------------------------------------------------------------- /apps/auth-microservice/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.base.json", 3 | "files": [], 4 | "include": [], 5 | "references": [ 6 | { 7 | "path": "./tsconfig.app.json" 8 | }, 9 | { 10 | "path": "./tsconfig.spec.json" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /apps/auth-microservice/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../dist/out-tsc", 5 | "module": "commonjs", 6 | "types": ["jest", "node"] 7 | }, 8 | "include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] 9 | } 10 | -------------------------------------------------------------------------------- /apps/payments-microservice/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["../../.eslintrc.json"], 3 | "ignorePatterns": ["!**/*"], 4 | "overrides": [ 5 | { 6 | "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], 7 | "rules": {} 8 | }, 9 | { 10 | "files": ["*.ts", "*.tsx"], 11 | "rules": {} 12 | }, 13 | { 14 | "files": ["*.js", "*.jsx"], 15 | "rules": {} 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /apps/payments-microservice/jest.config.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | export default { 3 | displayName: 'payments-microservice', 4 | preset: '../../jest.preset.js', 5 | globals: { 6 | 'ts-jest': { 7 | tsconfig: '/tsconfig.spec.json', 8 | }, 9 | }, 10 | testEnvironment: 'node', 11 | transform: { 12 | '^.+\\.[tj]s$': 'ts-jest', 13 | }, 14 | moduleFileExtensions: ['ts', 'js', 'html'], 15 | coverageDirectory: '../../coverage/apps/payments-microservice', 16 | }; 17 | -------------------------------------------------------------------------------- /apps/payments-microservice/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "payments-microservice", 3 | "$schema": "../../node_modules/nx/schemas/project-schema.json", 4 | "sourceRoot": "apps/payments-microservice/src", 5 | "projectType": "application", 6 | "targets": { 7 | "build": { 8 | "executor": "@nrwl/webpack:webpack", 9 | "outputs": ["{options.outputPath}"], 10 | "options": { 11 | "target": "node", 12 | "compiler": "tsc", 13 | "outputPath": "dist/apps/payments-microservice", 14 | "main": "apps/payments-microservice/src/main.ts", 15 | "tsConfig": "apps/payments-microservice/tsconfig.app.json", 16 | "assets": ["apps/payments-microservice/src/assets"] 17 | }, 18 | "configurations": { 19 | "production": { 20 | "optimization": true, 21 | "extractLicenses": true, 22 | "inspect": false, 23 | "fileReplacements": [ 24 | { 25 | "replace": "apps/payments-microservice/src/environments/environment.ts", 26 | "with": "apps/payments-microservice/src/environments/environment.prod.ts" 27 | } 28 | ] 29 | } 30 | } 31 | }, 32 | "serve": { 33 | "executor": "@nrwl/js:node", 34 | "options": { 35 | "buildTarget": "payments-microservice:build" 36 | }, 37 | "configurations": { 38 | "production": { 39 | "buildTarget": "payments-microservice:build:production" 40 | } 41 | } 42 | }, 43 | "lint": { 44 | "executor": "@nrwl/linter:eslint", 45 | "outputs": ["{options.outputFile}"], 46 | "options": { 47 | "lintFilePatterns": ["apps/payments-microservice/**/*.ts"] 48 | } 49 | }, 50 | "test": { 51 | "executor": "@nrwl/jest:jest", 52 | "outputs": ["coverage/apps/payments-microservice"], 53 | "options": { 54 | "jestConfig": "apps/payments-microservice/jest.config.ts", 55 | "passWithNoTests": true 56 | } 57 | } 58 | }, 59 | "tags": [] 60 | } 61 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/app/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/apps/payments-microservice/src/app/.gitkeep -------------------------------------------------------------------------------- /apps/payments-microservice/src/app/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | 3 | import { AppController } from './app.controller'; 4 | import { AppService } from './app.service'; 5 | 6 | describe('AppController', () => { 7 | let app: TestingModule; 8 | 9 | beforeAll(async () => { 10 | app = await Test.createTestingModule({ 11 | controllers: [AppController], 12 | providers: [AppService], 13 | }).compile(); 14 | }); 15 | 16 | describe('getData', () => { 17 | it('should return "Welcome to payments-microservice!"', () => { 18 | const appController = app.get(AppController); 19 | expect(appController.getData()).toEqual({ 20 | message: 'Welcome to payments-microservice!', 21 | }); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/app/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { MakePaymentDto } from '@nestjs-microservices/shared/dto'; 2 | import { Controller, ValidationPipe } from '@nestjs/common'; 3 | import { EventPattern, Payload } from '@nestjs/microservices'; 4 | 5 | import { AppService } from './app.service'; 6 | 7 | @Controller() 8 | export class AppController { 9 | constructor(private readonly appService: AppService) {} 10 | 11 | @EventPattern('process_payment') 12 | handleProcessPayment(@Payload(ValidationPipe) data: MakePaymentDto) { 13 | this.appService.processPayment(data); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { ClientsModule, Transport } from '@nestjs/microservices'; 3 | 4 | import { AppController } from './app.controller'; 5 | import { AppService } from './app.service'; 6 | 7 | @Module({ 8 | imports: [ 9 | ClientsModule.register([ 10 | { 11 | name: 'AUTH_MICROSERVICE', 12 | transport: Transport.KAFKA, 13 | options: { 14 | client: { 15 | clientId: 'auth', 16 | brokers: ['localhost:9092'], 17 | }, 18 | consumer: { 19 | groupId: 'auth-consumer', 20 | }, 21 | }, 22 | }, 23 | ]), 24 | ], 25 | controllers: [AppController], 26 | providers: [AppService], 27 | }) 28 | export class AppModule {} 29 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/app/app.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test } from '@nestjs/testing'; 2 | 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppService', () => { 6 | let service: AppService; 7 | 8 | beforeAll(async () => { 9 | const app = await Test.createTestingModule({ 10 | providers: [AppService], 11 | }).compile(); 12 | 13 | service = app.get(AppService); 14 | }); 15 | 16 | describe('getData', () => { 17 | it('should return "Welcome to payments-microservice!"', () => { 18 | expect(service.getData()).toEqual({ 19 | message: 'Welcome to payments-microservice!', 20 | }); 21 | }); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/app/app.service.ts: -------------------------------------------------------------------------------- 1 | import { MakePaymentDto } from '@nestjs-microservices/shared/dto'; 2 | import { User } from '@nestjs-microservices/shared/entities'; 3 | import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; 4 | import { ClientKafka } from '@nestjs/microservices'; 5 | 6 | @Injectable() 7 | export class AppService implements OnModuleInit { 8 | constructor( 9 | @Inject('AUTH_MICROSERVICE') private readonly authClient: ClientKafka 10 | ) {} 11 | 12 | processPayment(makePaymentDto: MakePaymentDto) { 13 | const { userId, amount } = makePaymentDto; 14 | console.log('process payment'); 15 | this.authClient 16 | .send('get_user', JSON.stringify({ userId })) 17 | .subscribe((user: User) => { 18 | console.log( 19 | `process payment for user ${user.name} - amount: ${amount}` 20 | ); 21 | }); 22 | } 23 | 24 | onModuleInit() { 25 | this.authClient.subscribeToResponseOf('get_user'); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/apps/payments-microservice/src/assets/.gitkeep -------------------------------------------------------------------------------- /apps/payments-microservice/src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true, 3 | }; 4 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: false, 3 | }; 4 | -------------------------------------------------------------------------------- /apps/payments-microservice/src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { MicroserviceOptions, Transport } from '@nestjs/microservices'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | 6 | async function bootstrap() { 7 | const app = await NestFactory.createMicroservice( 8 | AppModule, 9 | { 10 | transport: Transport.KAFKA, 11 | options: { 12 | client: { 13 | brokers: ['localhost:9092'], 14 | }, 15 | consumer: { 16 | groupId: 'payment-consumer', 17 | }, 18 | }, 19 | } 20 | ); 21 | await app.listen(); 22 | } 23 | 24 | bootstrap(); 25 | -------------------------------------------------------------------------------- /apps/payments-microservice/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../dist/out-tsc", 5 | "module": "commonjs", 6 | "types": ["node"], 7 | "emitDecoratorMetadata": true, 8 | "target": "es2015" 9 | }, 10 | "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], 11 | "include": ["**/*.ts"] 12 | } 13 | -------------------------------------------------------------------------------- /apps/payments-microservice/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.base.json", 3 | "files": [], 4 | "include": [], 5 | "references": [ 6 | { 7 | "path": "./tsconfig.app.json" 8 | }, 9 | { 10 | "path": "./tsconfig.spec.json" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /apps/payments-microservice/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../dist/out-tsc", 5 | "module": "commonjs", 6 | "types": ["jest", "node"] 7 | }, 8 | "include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] 9 | } 10 | -------------------------------------------------------------------------------- /jest.config.ts: -------------------------------------------------------------------------------- 1 | import { getJestProjects } from '@nrwl/jest'; 2 | 3 | export default { 4 | projects: getJestProjects(), 5 | }; 6 | -------------------------------------------------------------------------------- /jest.preset.js: -------------------------------------------------------------------------------- 1 | const nxPreset = require('@nrwl/jest/preset').default; 2 | 3 | module.exports = { ...nxPreset }; 4 | -------------------------------------------------------------------------------- /libs/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/libs/.gitkeep -------------------------------------------------------------------------------- /libs/shared/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]] 3 | } 4 | -------------------------------------------------------------------------------- /libs/shared/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["../../.eslintrc.json"], 3 | "ignorePatterns": ["!**/*"], 4 | "overrides": [ 5 | { 6 | "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], 7 | "rules": {} 8 | }, 9 | { 10 | "files": ["*.ts", "*.tsx"], 11 | "rules": {} 12 | }, 13 | { 14 | "files": ["*.js", "*.jsx"], 15 | "rules": {} 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /libs/shared/README.md: -------------------------------------------------------------------------------- 1 | # shared 2 | 3 | This library was generated with [Nx](https://nx.dev). 4 | 5 | ## Running unit tests 6 | 7 | Run `nx test shared` to execute the unit tests via [Jest](https://jestjs.io). 8 | 9 | ## Running lint 10 | 11 | Run `nx lint shared` to execute the lint via [ESLint](https://eslint.org/). 12 | -------------------------------------------------------------------------------- /libs/shared/jest.config.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | export default { 3 | displayName: 'shared', 4 | preset: '../../jest.preset.js', 5 | globals: { 6 | 'ts-jest': { 7 | tsconfig: '/tsconfig.spec.json', 8 | }, 9 | }, 10 | testEnvironment: 'node', 11 | transform: { 12 | '^.+\\.[tj]sx?$': 'ts-jest', 13 | }, 14 | moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], 15 | coverageDirectory: '../../coverage/libs/shared', 16 | }; 17 | -------------------------------------------------------------------------------- /libs/shared/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shared", 3 | "$schema": "../../node_modules/nx/schemas/project-schema.json", 4 | "sourceRoot": "libs/shared/src", 5 | "projectType": "library", 6 | "targets": { 7 | "lint": { 8 | "executor": "@nrwl/linter:eslint", 9 | "outputs": ["{options.outputFile}"], 10 | "options": { 11 | "lintFilePatterns": ["libs/shared/**/*.ts"] 12 | } 13 | }, 14 | "test": { 15 | "executor": "@nrwl/jest:jest", 16 | "outputs": ["coverage/libs/shared"], 17 | "options": { 18 | "jestConfig": "libs/shared/jest.config.ts", 19 | "passWithNoTests": true 20 | } 21 | } 22 | }, 23 | "tags": [] 24 | } 25 | -------------------------------------------------------------------------------- /libs/shared/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './lib/shared'; 2 | -------------------------------------------------------------------------------- /libs/shared/src/lib/dto/create-user.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsEmail, IsNotEmpty, IsString } from 'class-validator'; 2 | 3 | export class CreateUserDto { 4 | @IsString() 5 | @IsNotEmpty() 6 | name: string; 7 | 8 | @IsEmail() 9 | @IsNotEmpty() 10 | email: string; 11 | } 12 | -------------------------------------------------------------------------------- /libs/shared/src/lib/dto/index.ts: -------------------------------------------------------------------------------- 1 | export * from './create-user.dto'; 2 | export * from './make-payment.dto'; 3 | -------------------------------------------------------------------------------- /libs/shared/src/lib/dto/make-payment.dto.ts: -------------------------------------------------------------------------------- 1 | import { IsNotEmpty, IsNumber } from 'class-validator'; 2 | 3 | export class MakePaymentDto { 4 | @IsNotEmpty() 5 | @IsNumber() 6 | userId: number; 7 | 8 | @IsNotEmpty() 9 | @IsNumber() 10 | amount: number; 11 | } 12 | -------------------------------------------------------------------------------- /libs/shared/src/lib/entities/index.ts: -------------------------------------------------------------------------------- 1 | export * from './user.entity'; 2 | -------------------------------------------------------------------------------- /libs/shared/src/lib/entities/user.entity.ts: -------------------------------------------------------------------------------- 1 | export class User { 2 | id?: number; 3 | 4 | name: string; 5 | 6 | email: string; 7 | } 8 | -------------------------------------------------------------------------------- /libs/shared/src/lib/shared.spec.ts: -------------------------------------------------------------------------------- 1 | import { shared } from './shared'; 2 | 3 | describe('shared', () => { 4 | it('should work', () => { 5 | expect(shared()).toEqual('shared'); 6 | }); 7 | }); 8 | -------------------------------------------------------------------------------- /libs/shared/src/lib/shared.ts: -------------------------------------------------------------------------------- 1 | export function shared(): string { 2 | return 'shared'; 3 | } 4 | -------------------------------------------------------------------------------- /libs/shared/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.base.json", 3 | "files": [], 4 | "include": [], 5 | "references": [ 6 | { 7 | "path": "./tsconfig.lib.json" 8 | }, 9 | { 10 | "path": "./tsconfig.spec.json" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /libs/shared/tsconfig.lib.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "commonjs", 5 | "outDir": "../../dist/out-tsc", 6 | "declaration": true, 7 | "types": ["node"] 8 | }, 9 | "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], 10 | "include": ["**/*.ts"] 11 | } 12 | -------------------------------------------------------------------------------- /libs/shared/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../../dist/out-tsc", 5 | "module": "commonjs", 6 | "types": ["jest", "node"] 7 | }, 8 | "include": [ 9 | "jest.config.ts", 10 | "**/*.test.ts", 11 | "**/*.spec.ts", 12 | "**/*.test.tsx", 13 | "**/*.spec.tsx", 14 | "**/*.test.js", 15 | "**/*.spec.js", 16 | "**/*.test.jsx", 17 | "**/*.spec.jsx", 18 | "**/*.d.ts" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /nx.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/nx/schemas/nx-schema.json", 3 | "npmScope": "nestjs-microservices", 4 | "affected": { 5 | "defaultBase": "master" 6 | }, 7 | "tasksRunnerOptions": { 8 | "default": { 9 | "runner": "@nrwl/nx-cloud", 10 | "options": { 11 | "cacheableOperations": [ 12 | "build", 13 | "lint", 14 | "test", 15 | "e2e" 16 | ], 17 | "accessToken": "ZDQ3OWQ5MzQtODViZC00NzQ3LTg4YjktMjU1YTNiOGFmZGQzfHJlYWQtd3JpdGU=" 18 | } 19 | } 20 | }, 21 | "targetDefaults": { 22 | "build": { 23 | "dependsOn": [ 24 | "^build" 25 | ], 26 | "inputs": [ 27 | "production", 28 | "^production" 29 | ] 30 | }, 31 | "test": { 32 | "inputs": [ 33 | "default", 34 | "^production", 35 | "{workspaceRoot}/jest.preset.js" 36 | ] 37 | }, 38 | "lint": { 39 | "inputs": [ 40 | "default", 41 | "{workspaceRoot}/.eslintrc.json" 42 | ] 43 | } 44 | }, 45 | "namedInputs": { 46 | "default": [ 47 | "{projectRoot}/**/*", 48 | "sharedGlobals" 49 | ], 50 | "production": [ 51 | "default", 52 | "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)", 53 | "!{projectRoot}/tsconfig.spec.json", 54 | "!{projectRoot}/jest.config.[jt]s", 55 | "!{projectRoot}/.eslintrc.json" 56 | ], 57 | "sharedGlobals": [] 58 | }, 59 | "defaultProject": "api-gateway" 60 | } 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-microservices", 3 | "version": "0.0.0", 4 | "license": "MIT", 5 | "scripts": { 6 | "start": "nx serve", 7 | "build": "nx build", 8 | "test": "nx test" 9 | }, 10 | "private": true, 11 | "dependencies": { 12 | "@nestjs/common": "^9.0.0", 13 | "@nestjs/core": "^9.0.0", 14 | "@nestjs/microservices": "^9.1.2", 15 | "@nestjs/platform-express": "^9.0.0", 16 | "class-transformer": "^0.5.1", 17 | "class-validator": "^0.13.2", 18 | "kafkajs": "^2.2.0", 19 | "reflect-metadata": "^0.1.13", 20 | "rxjs": "^7.0.0", 21 | "tslib": "^2.3.0" 22 | }, 23 | "devDependencies": { 24 | "@nestjs/schematics": "^9.0.0", 25 | "@nestjs/testing": "^9.0.0", 26 | "@nrwl/cli": "14.8.2", 27 | "@nrwl/eslint-plugin-nx": "14.8.2", 28 | "@nrwl/jest": "14.8.2", 29 | "@nrwl/linter": "14.8.2", 30 | "@nrwl/nest": "14.8.2", 31 | "@nrwl/node": "14.8.2", 32 | "@nrwl/nx-cloud": "latest", 33 | "@nrwl/workspace": "14.8.2", 34 | "@types/jest": "28.1.1", 35 | "@types/node": "16.11.7", 36 | "@typescript-eslint/eslint-plugin": "^5.36.1", 37 | "@typescript-eslint/parser": "^5.36.1", 38 | "eslint": "~8.15.0", 39 | "eslint-config-prettier": "8.1.0", 40 | "jest": "28.1.1", 41 | "jest-environment-jsdom": "28.1.1", 42 | "nx": "14.8.2", 43 | "prettier": "^2.6.2", 44 | "ts-jest": "28.0.5", 45 | "ts-node": "10.9.1", 46 | "typescript": "~4.8.2" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tools/generators/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vijitail/nestjs-kafka-microservices/40eed1b27c67809ff2656dcd040f55ddd98545a9/tools/generators/.gitkeep -------------------------------------------------------------------------------- /tools/tsconfig.tools.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.base.json", 3 | "compilerOptions": { 4 | "outDir": "../dist/out-tsc/tools", 5 | "rootDir": ".", 6 | "module": "commonjs", 7 | "target": "es5", 8 | "types": ["node"], 9 | "importHelpers": false 10 | }, 11 | "include": ["**/*.ts"] 12 | } 13 | -------------------------------------------------------------------------------- /tsconfig.base.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "rootDir": ".", 5 | "sourceMap": true, 6 | "declaration": false, 7 | "moduleResolution": "node", 8 | "emitDecoratorMetadata": true, 9 | "experimentalDecorators": true, 10 | "importHelpers": true, 11 | "target": "es2015", 12 | "module": "esnext", 13 | "lib": ["es2017", "dom"], 14 | "skipLibCheck": true, 15 | "skipDefaultLibCheck": true, 16 | "baseUrl": ".", 17 | "paths": { 18 | "@nestjs-microservices/shared": ["libs/shared/src/index.ts"], 19 | "@nestjs-microservices/shared/dto": ["libs/shared/src/lib/dto/index.ts"], 20 | "@nestjs-microservices/shared/entities": [ 21 | "libs/shared/src/lib/entities/index.ts" 22 | ] 23 | } 24 | }, 25 | "exclude": ["node_modules", "tmp"] 26 | } 27 | --------------------------------------------------------------------------------