DEV Community

Igor Dubinin
Igor Dubinin

Posted on

Microservices for beginners. Common code. Typescript.

Whole series:

Microservices for beginners

Microservices for beginners. Front-end service. Vue js. Socket.io.

Microservices for beginners. Api Gateway service. Nest js. Kafka.

Microservices for beginners. User service. Nest js. Mongodb. Kafka.

Microservices for beginners. Message service. Nest js. Mongodb. Kafka.

Microservices for beginners. Spam service. Python. Scikit-learn. Kafka.

Microservices for beginners. Toxic service. Python. Tensorflow. Kafka.

Microservices for beginners. Common code. Typescript.

Full code - link

In previous articles I described how to create several services and how to work with them. But sometimes microservices in one project contain a lot of similar code, and I want to reuse it. For example I use a common type for user entities in the api gateway and in the user service - WebUserDto.

export class WebUserDto {
  @ApiProperty({ description: 'Id of user', nullable: false })
  id: string;

  @ApiProperty({ description: 'Login of user', nullable: false })
  login: string;

  @ApiProperty({ description: 'Email of user', nullable: false })
  email: string;

  @ApiProperty({ description: 'Flag of user active', nullable: false })
  active: boolean;

  @ApiProperty({ description: 'Date of user creation', nullable: false })
  created_at: Date;
}
Enter fullscreen mode Exit fullscreen mode

So I made a new project in Github and put all the common DTO inside and built a typescript library.

Installation requirements

Installation

npm install typescript
npm install --save @nestjs/swagger
Enter fullscreen mode Exit fullscreen mode

Initiation tsconfig.json

npx tsc --init
Enter fullscreen mode Exit fullscreen mode

Building

npx tsc
Enter fullscreen mode Exit fullscreen mode

Right now I can use common code as npm library in other projects, in package.json - "micro-dto": "github:Igorok/micro-dto#main".

Top comments (0)