From de48cc6517c54ae4efb4225126d893c527da0a5a Mon Sep 17 00:00:00 2001 From: okjodom Date: Wed, 27 Nov 2024 16:48:20 +0300 Subject: [PATCH] refactor: dtos --- .../src/dto/configure-nostr-relays.dto.ts | 40 ----------- .../common/src/dto/create-offramp-swap.dto.ts | 61 ----------------- libs/common/src/dto/find-swap.dto.ts | 12 ---- libs/common/src/dto/index.ts | 11 +-- libs/common/src/dto/list-swaps.dto.ts | 15 ----- ...encrypted-nostr-dm.dto.ts => nostr.dto.ts} | 32 ++++++++- libs/common/src/dto/solowallet.dto.ts | 2 +- ...{create-onramp-swap.dto.ts => swap.dto.ts} | 67 +++++++++++++++++++ 8 files changed, 102 insertions(+), 138 deletions(-) delete mode 100644 libs/common/src/dto/configure-nostr-relays.dto.ts delete mode 100644 libs/common/src/dto/create-offramp-swap.dto.ts delete mode 100644 libs/common/src/dto/find-swap.dto.ts delete mode 100644 libs/common/src/dto/list-swaps.dto.ts rename libs/common/src/dto/{send-encrypted-nostr-dm.dto.ts => nostr.dto.ts} (56%) rename libs/common/src/dto/{create-onramp-swap.dto.ts => swap.dto.ts} (57%) diff --git a/libs/common/src/dto/configure-nostr-relays.dto.ts b/libs/common/src/dto/configure-nostr-relays.dto.ts deleted file mode 100644 index 1f6b914..0000000 --- a/libs/common/src/dto/configure-nostr-relays.dto.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { - IsBoolean, - IsString, - IsNotEmpty, - IsDefined, - ValidateNested, - Matches, -} from 'class-validator'; -import { Type } from 'class-transformer'; -import { ApiProperty } from '@nestjs/swagger'; -import { ConfigureNostrRelaysRequest, NostrRelay } from '../types'; - -class NostrRelayDto implements NostrRelay { - @IsNotEmpty() - @IsString() - @Matches(/^wss?:\/\/.+/, { - message: 'Socket must be a valid WebSocket URL (ws:// or wss://)', - }) - @Type(() => String) - @ApiProperty() - socket: string; - - @IsBoolean() - @Type(() => Boolean) - @ApiProperty() - read: boolean; - - @IsBoolean() - @Type(() => Boolean) - @ApiProperty() - write: boolean; -} - -export class ConfigureNostrRelaysDto implements ConfigureNostrRelaysRequest { - @IsDefined() - @ValidateNested({ each: true }) - @Type(() => NostrRelayDto) - @ApiProperty({ type: [NostrRelayDto] }) - relays: NostrRelayDto[]; -} diff --git a/libs/common/src/dto/create-offramp-swap.dto.ts b/libs/common/src/dto/create-offramp-swap.dto.ts deleted file mode 100644 index 5e21d98..0000000 --- a/libs/common/src/dto/create-offramp-swap.dto.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Type } from 'class-transformer'; -import { - IsString, - IsOptional, - IsNotEmpty, - Validate, - IsEnum, - IsDefined, - ValidateNested, -} from 'class-validator'; -import { ApiProperty } from '@nestjs/swagger'; -import { - Currency, - IsStringifiedNumberConstraint, - OfframpSwapRequest, - OfframpSwapTarget, - SupportedCurrencies, -} from '../types'; -import { QuoteDto } from './quote.dto'; -import { MobileMoneyDto } from './payments.dto'; -import { TransformToCurrency } from './transforms'; - -class OfframpSwapTargetDto implements OfframpSwapTarget { - @IsEnum(Currency) - @TransformToCurrency() - @ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' }) - currency: Currency; - - @IsDefined() - @ValidateNested() - @Type(() => MobileMoneyDto) - @ApiProperty({ type: MobileMoneyDto }) - payout: MobileMoneyDto; -} - -export class CreateOfframpSwapDto implements OfframpSwapRequest { - @IsOptional() - @ValidateNested() - @Type(() => QuoteDto) - @ApiProperty({ type: QuoteDto }) - quote: QuoteDto; - - @IsNotEmpty() - @IsString() - @Type(() => String) - @ApiProperty() - reference: string; - - @IsNotEmpty() - @IsString() - @Validate(IsStringifiedNumberConstraint) - @Type(() => String) - @ApiProperty() - amountFiat: string; - - @IsNotEmpty() - @ValidateNested() - @Type(() => OfframpSwapTargetDto) - @ApiProperty({ type: OfframpSwapTargetDto }) - target: OfframpSwapTargetDto; -} diff --git a/libs/common/src/dto/find-swap.dto.ts b/libs/common/src/dto/find-swap.dto.ts deleted file mode 100644 index a35e918..0000000 --- a/libs/common/src/dto/find-swap.dto.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Type } from 'class-transformer'; -import { IsString, IsNotEmpty } from 'class-validator'; -import { type FindSwapRequest } from '@bitsacco/common'; -import { ApiProperty } from '@nestjs/swagger'; - -export class FindSwapDto implements FindSwapRequest { - @IsNotEmpty() - @IsString() - @Type(() => String) - @ApiProperty() - id: string; -} diff --git a/libs/common/src/dto/index.ts b/libs/common/src/dto/index.ts index 603e645..9f63ccc 100644 --- a/libs/common/src/dto/index.ts +++ b/libs/common/src/dto/index.ts @@ -1,12 +1,7 @@ -// swap -export * from './find-swap.dto'; -export * from './list-swaps.dto'; -export * from './create-onramp-swap.dto'; -export * from './create-offramp-swap.dto'; + +export * from './swap.dto'; export * from './quote.dto'; -// nostr -export * from './configure-nostr-relays.dto'; -export * from './send-encrypted-nostr-dm.dto'; +export * from './nostr.dto'; export * from './sms.dto'; export * from './shares.dto'; export * from './solowallet.dto'; diff --git a/libs/common/src/dto/list-swaps.dto.ts b/libs/common/src/dto/list-swaps.dto.ts deleted file mode 100644 index b44b819..0000000 --- a/libs/common/src/dto/list-swaps.dto.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { IsNumber, Min } from 'class-validator'; -import { PaginatedRequest } from '@bitsacco/common'; -import { ApiProperty } from '@nestjs/swagger'; - -export class ListSwapsDto implements PaginatedRequest { - @ApiProperty() - @IsNumber() - @Min(0) - page: number; - - @ApiProperty() - @IsNumber() - @Min(1) - size: number; -} diff --git a/libs/common/src/dto/send-encrypted-nostr-dm.dto.ts b/libs/common/src/dto/nostr.dto.ts similarity index 56% rename from libs/common/src/dto/send-encrypted-nostr-dm.dto.ts rename to libs/common/src/dto/nostr.dto.ts index ff6b282..9e7b4ce 100644 --- a/libs/common/src/dto/send-encrypted-nostr-dm.dto.ts +++ b/libs/common/src/dto/nostr.dto.ts @@ -5,10 +5,11 @@ import { IsDefined, ValidateNested, IsOptional, + Matches, } from 'class-validator'; import { Type } from 'class-transformer'; import { ApiProperty } from '@nestjs/swagger'; -import { NostrDirectMessageRequest, NostrRecipient } from '../types'; +import { ConfigureNostrRelaysRequest, NostrDirectMessageRequest, NostrRecipient, NostrRelay } from '../types'; class NostrRecipientDto implements NostrRecipient { @IsOptional() @@ -44,3 +45,32 @@ export class SendEncryptedNostrDmDto implements NostrDirectMessageRequest { @ApiProperty() retry: boolean; } + +class NostrRelayDto implements NostrRelay { + @IsNotEmpty() + @IsString() + @Matches(/^wss?:\/\/.+/, { + message: 'Socket must be a valid WebSocket URL (ws:// or wss://)', + }) + @Type(() => String) + @ApiProperty() + socket: string; + + @IsBoolean() + @Type(() => Boolean) + @ApiProperty() + read: boolean; + + @IsBoolean() + @Type(() => Boolean) + @ApiProperty() + write: boolean; +} + +export class ConfigureNostrRelaysDto implements ConfigureNostrRelaysRequest { + @IsDefined() + @ValidateNested({ each: true }) + @Type(() => NostrRelayDto) + @ApiProperty({ type: [NostrRelayDto] }) + relays: NostrRelayDto[]; +} diff --git a/libs/common/src/dto/solowallet.dto.ts b/libs/common/src/dto/solowallet.dto.ts index cd24e73..7a2d15b 100644 --- a/libs/common/src/dto/solowallet.dto.ts +++ b/libs/common/src/dto/solowallet.dto.ts @@ -1,7 +1,7 @@ import { Type } from 'class-transformer'; import { IsNotEmpty, IsString, ValidateNested } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; -import { CreateOnrampSwapDto } from './create-onramp-swap.dto'; +import { CreateOnrampSwapDto } from './swap.dto'; import { DepositFundsRequest } from '../types'; export class DepositFundsRequestDto implements DepositFundsRequest { diff --git a/libs/common/src/dto/create-onramp-swap.dto.ts b/libs/common/src/dto/swap.dto.ts similarity index 57% rename from libs/common/src/dto/create-onramp-swap.dto.ts rename to libs/common/src/dto/swap.dto.ts index b7624ef..29fcd86 100644 --- a/libs/common/src/dto/create-onramp-swap.dto.ts +++ b/libs/common/src/dto/swap.dto.ts @@ -6,6 +6,8 @@ import { IsDefined, IsEnum, ValidateNested, + IsNumber, + Min, } from 'class-validator'; import { Type } from 'class-transformer'; import { ApiProperty } from '@nestjs/swagger'; @@ -16,6 +18,10 @@ import { Currency, OnrampSwapTarget, SupportedCurrencies, + FindSwapRequest, + OfframpSwapRequest, + OfframpSwapTarget, + PaginatedRequest, } from '../types'; import { Bolt11InvoiceDto, MobileMoneyDto } from './payments.dto'; import { TransformToCurrency } from './transforms'; @@ -74,3 +80,64 @@ export class CreateOnrampSwapDto implements OnrampSwapRequest { @ApiProperty({ type: OnrampSwapTargetDto }) target: OnrampSwapTargetDto; } + + +class OfframpSwapTargetDto implements OfframpSwapTarget { + @IsEnum(Currency) + @TransformToCurrency() + @ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' }) + currency: Currency; + + @IsDefined() + @ValidateNested() + @Type(() => MobileMoneyDto) + @ApiProperty({ type: MobileMoneyDto }) + payout: MobileMoneyDto; +} + +export class CreateOfframpSwapDto implements OfframpSwapRequest { + @IsOptional() + @ValidateNested() + @Type(() => QuoteDto) + @ApiProperty({ type: QuoteDto }) + quote: QuoteDto; + + @IsNotEmpty() + @IsString() + @Type(() => String) + @ApiProperty() + reference: string; + + @IsNotEmpty() + @IsString() + @Validate(IsStringifiedNumberConstraint) + @Type(() => String) + @ApiProperty() + amountFiat: string; + + @IsNotEmpty() + @ValidateNested() + @Type(() => OfframpSwapTargetDto) + @ApiProperty({ type: OfframpSwapTargetDto }) + target: OfframpSwapTargetDto; +} + +export class FindSwapDto implements FindSwapRequest { + @IsNotEmpty() + @IsString() + @Type(() => String) + @ApiProperty() + id: string; +} + +export class ListSwapsDto implements PaginatedRequest { + @ApiProperty() + @IsNumber() + @Min(0) + page: number; + + @ApiProperty() + @IsNumber() + @Min(1) + size: number; +}