0.0.9 • Published 2 years ago

@jest-automation/dto v0.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

DTO & Builder Pattern

This library provides functionality to help create data types and builder classes for them.

A DTO is a class with the @Property decorator applied to at least one property.

For example:

import { Property } from '@jest-automation/dto';

export class CreateUserDto {
  @Property
  username!: string;

  @Property
  password!: string;

  @Property
  email?: string;
}

Now it's possible to generate a builder for this DTO. By passing a DTO Class to the Builder function, a new class will be created with builder functions corresponding to the DTO properties.

import { CreateUserDto } from './somewhere';
import { Builder } from '@jest-automation/dto';

const CreateUserBuilder = Builder(CreateUserDTO);

const builder = new CreateUserBuilder();

The builder will share property names with the underlying DTO, which will be methods that take a value to fill in the DTO, and which return the builder instance to allow for chaining.

builder.username('fredici').password('1234');

Type information is generated preventing the wrong type being used for a param unless explicitely bypassed

builder.username(1); // tsc error
builder.username(1 as unknown as string); // ok

To retrieve the underlying DTO, call the build method.

const result = builder.build();

If the package class-validators is being used, then their validation will be executed by the build method. To disable validaton, pass false to the build method.

const result = builder.build(false);
0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago