Skip to content

Commit

Permalink
feat: make use of class-transformer to build nested types (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbystedt authored Oct 31, 2023
1 parent c34b4bd commit 5f21812
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 611 deletions.
672 changes: 230 additions & 442 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"@aws-sdk/client-kinesis": "^3.438.0",
"@nestjs/axios": "^3.0.0",
"@nestjs/axios": "^3.0.1",
"@nestjs/common": "^10.2.7",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.7",
Expand Down Expand Up @@ -58,23 +58,23 @@
},
"devDependencies": {
"@golevelup/ts-jest": "^0.4.0",
"@nestjs/cli": "^10.2.0",
"@nestjs/schematics": "^10.0.2",
"@nestjs/cli": "^10.2.1",
"@nestjs/schematics": "^10.0.3",
"@nestjs/testing": "^10.2.7",
"@types/cron": "^2.4.0",
"@types/ejs": "^3.1.4",
"@types/express": "^4.17.20",
"@types/express-session": "^1.17.9",
"@types/jest": "^29.5.6",
"@types/jest": "^29.5.7",
"@types/lodash.merge": "^4.6.8",
"@types/node": "^20.8.9",
"@types/node": "^20.8.10",
"@types/passport": "^1.0.14",
"@types/passport-http": "^0.3.10",
"@types/passport-jwt": "^3.0.12",
"@types/supertest": "^2.0.15",
"@types/uuid": "^9.0.6",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"eslint": "^8.52.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
Expand Down
3 changes: 2 additions & 1 deletion src/intention/action.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HEADER_BROKER_TOKEN } from '../constants';
import { ActionGuardRequest } from './action-guard-request.interface';
import { IntentionRepository } from '../persistence/interfaces/intention.repository';
import { IntentionDto } from '../intention/dto/intention.dto';
import { plainToInstance } from 'class-transformer';

@Injectable()
export class ActionGuard implements CanActivate {
Expand All @@ -27,7 +28,7 @@ export class ActionGuard implements CanActivate {
message: 'Intention not found',
});
}
const intention = IntentionDto.plainToInstance(intObj);
const intention = plainToInstance(IntentionDto, intObj);
const action = IntentionDto.projectAction(intention, token);
const errors = await validate(action, {
whitelist: true,
Expand Down
36 changes: 13 additions & 23 deletions src/intention/dto/action.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiHideProperty } from '@nestjs/swagger';
import { plainToInstance } from 'class-transformer';
import { Type } from 'class-transformer';
import {
IsArray,
IsBoolean,
Expand All @@ -15,31 +15,10 @@ import { ServiceDto } from './service.dto';
import { TransactionDto } from './transaction.dto';
import { CloudDto } from './cloud.dto';
import { PackageDto } from './package.dto';
import { UrlDto } from './url.dto';

@Entity()
export class ActionDto {
static plainToInstance(object: any): ActionDto {
if (object.cloud) {
object.cloud = CloudDto.plainToInstance(object.cloud);
}
if (object.package) {
object.package = plainToInstance(PackageDto, object.package);
}
if (object.service) {
object.service = plainToInstance(ServiceDto, object.service);
}
if (object.transaction) {
object.transaction = plainToInstance(TransactionDto, object.transaction);
}
if (object.trace) {
object.trace = plainToInstance(TransactionDto, object.trace);
}
if (object.user) {
object.user = plainToInstance(UserDto, object.user);
}
return object;
}

@IsString()
@IsIn([
'backup',
Expand Down Expand Up @@ -75,11 +54,13 @@ export class ActionDto {
@ValidateNested()
@IsOptional()
@Column(() => CloudDto)
@Type(() => CloudDto)
cloud?: CloudDto;

@ValidateNested()
@IsDefined()
@Column(() => ServiceDto)
@Type(() => ServiceDto)
service: ServiceDto;

@IsOptional()
Expand All @@ -91,20 +72,29 @@ export class ActionDto {
@ValidateNested()
@IsOptional()
@Column(() => PackageDto)
@Type(() => PackageDto)
package?: PackageDto;

@ValidateNested()
@IsOptional()
@ApiHideProperty()
@Column(() => TransactionDto)
@Type(() => TransactionDto)
transaction?: TransactionDto;

@ValidateNested()
@IsOptional()
@ApiHideProperty()
@Column(() => TransactionDto)
@Type(() => TransactionDto)
trace?: TransactionDto;

@ValidateNested()
@IsOptional()
@Column(() => UrlDto)
@Type(() => UrlDto)
url?: UrlDto;

@ValidateNested()
@IsOptional()
@ApiHideProperty()
Expand Down
67 changes: 0 additions & 67 deletions src/intention/dto/action.util.ts

This file was deleted.

28 changes: 6 additions & 22 deletions src/intention/dto/cloud-object.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { plainToInstance } from 'class-transformer';
import { Type } from 'class-transformer';
import { IsOptional, IsString, ValidateNested } from 'class-validator';
import { Entity, Column } from 'typeorm';

Expand Down Expand Up @@ -57,30 +57,10 @@ class CloudObjectServiceDto {

@Entity()
export class CloudObjectDto {
static plainToInstance(object: any): CloudObjectDto {
if (object.account) {
object.account = plainToInstance(CloudObjectAccountDto, object.account);
}
if (object.instance) {
object.instance = plainToInstance(
CloudObjectInstanceDto,
object.instance,
);
}
if (object.machine) {
object.machine = plainToInstance(CloudObjectMachineDto, object.machine);
}
if (object.project) {
object.project = plainToInstance(CloudObjectProjectDto, object.project);
}
if (object.service) {
object.service = plainToInstance(CloudObjectServiceDto, object.service);
}
return plainToInstance(CloudObjectDto, object);
}
@ValidateNested()
@IsOptional()
@Column(() => CloudObjectAccountDto)
@Type(() => CloudObjectAccountDto)
account?: CloudObjectAccountDto;

@IsString()
Expand All @@ -91,16 +71,19 @@ export class CloudObjectDto {
@ValidateNested()
@IsOptional()
@Column(() => CloudObjectInstanceDto)
@Type(() => CloudObjectInstanceDto)
instance?: CloudObjectInstanceDto;

@ValidateNested()
@IsOptional()
@Column(() => CloudObjectMachineDto)
@Type(() => CloudObjectMachineDto)
machine?: CloudObjectMachineDto;

@ValidateNested()
@IsOptional()
@Column(() => CloudObjectProjectDto)
@Type(() => CloudObjectProjectDto)
project?: CloudObjectProjectDto;

@IsString()
Expand All @@ -116,5 +99,6 @@ export class CloudObjectDto {
@ValidateNested()
@IsOptional()
@Column()
@Type(() => CloudObjectServiceDto)
service?: CloudObjectServiceDto;
}
13 changes: 3 additions & 10 deletions src/intention/dto/cloud.dto.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { plainToInstance } from 'class-transformer';
import { Type } from 'class-transformer';
import { IsDefined, IsOptional, ValidateNested } from 'class-validator';
import { Entity, Column } from 'typeorm';

import { CloudObjectDto } from './cloud-object.dto';

@Entity()
export class CloudDto {
static plainToInstance(object: any): CloudDto {
if (object.source) {
object.source = CloudObjectDto.plainToInstance(object.source);
}
if (object.target) {
object.target = CloudObjectDto.plainToInstance(object.target);
}
return plainToInstance(CloudDto, object);
}
@ValidateNested()
@IsOptional()
@Column(() => CloudObjectDto)
@Type(() => CloudObjectDto)
source: CloudObjectDto;

@IsDefined()
@ValidateNested()
@Column(() => CloudObjectDto)
@Type(() => CloudObjectDto)
target: CloudObjectDto;
}
Loading

0 comments on commit 5f21812

Please sign in to comment.