From c05e38a60ee5f6331119e663c7ed36ba7e0278e8 Mon Sep 17 00:00:00 2001 From: Tom Gobich Date: Sat, 15 Jun 2024 20:33:27 -0400 Subject: [PATCH] feat: added ability to instantiate a DTO instance without initial data --- README.md | 4 ++-- stubs/make/dto/main.stub | 3 ++- test-files/expectations/account.txt | 3 ++- test-files/expectations/some_test.txt | 3 ++- test-files/expectations/test.txt | 3 ++- test-files/expectations/user.txt | 3 ++- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f0cb1a6..229c009 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ > Easily make and generate DTOs from Lucid Models - [![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] Converting Lucid Models to DTO files can be a tedious task. @@ -268,7 +267,8 @@ export default class AccountDto { declare isBudgetable: boolean declare balanceDisplay: string - constructor(account: Account) { + constructor(account?: Account) { + if (!account) return this.id = account.id this.userId = account.userId this.accountTypeId = account.accountTypeId diff --git a/stubs/make/dto/main.stub b/stubs/make/dto/main.stub index 1617a4b..fdad873 100644 --- a/stubs/make/dto/main.stub +++ b/stubs/make/dto/main.stub @@ -11,7 +11,8 @@ import {{ model.name }} from '#models/{{ string.snakeCase(model.name) }}' export default class {{ dto.className }} {{{ '{' }}}{{ #each dto.properties as property }} {{{ property.declaration }}}{{ /each }} - constructor({{ model.variable }}: {{ model.name }}) {{{ '{' }}}{{ #each dto.properties as property }} + constructor({{ model.variable }}?: {{ model.name }}) { + if (!{{ model.variable }}) return{{ #each dto.properties as property }} this.{{ property.name }} = {{{ property.valueSetter }}}{{ /each }} } diff --git a/test-files/expectations/account.txt b/test-files/expectations/account.txt index 3995e65..9deba29 100644 --- a/test-files/expectations/account.txt +++ b/test-files/expectations/account.txt @@ -29,7 +29,8 @@ export default class AccountDto { declare isBudgetable: boolean declare balanceDisplay: string - constructor(account: Account) { + constructor(account?: Account) { + if (!account) return this.id = account.id this.userId = account.userId this.accountTypeId = account.accountTypeId diff --git a/test-files/expectations/some_test.txt b/test-files/expectations/some_test.txt index 16e6051..2c62eda 100644 --- a/test-files/expectations/some_test.txt +++ b/test-files/expectations/some_test.txt @@ -4,7 +4,8 @@ export default class SomeTestDto { declare id: number declare createdAt: string - constructor(test: Test) { + constructor(test?: Test) { + if (!test) return this.id = test.id this.createdAt = test.createdAt.toISO()! } diff --git a/test-files/expectations/test.txt b/test-files/expectations/test.txt index 03e8d62..35dd472 100644 --- a/test-files/expectations/test.txt +++ b/test-files/expectations/test.txt @@ -4,7 +4,8 @@ export default class TestDto { declare id: number declare createdAt: string - constructor(test: Test) { + constructor(test?: Test) { + if (!test) return this.id = test.id this.createdAt = test.createdAt.toISO()! } diff --git a/test-files/expectations/user.txt b/test-files/expectations/user.txt index c17fcf5..2236e98 100644 --- a/test-files/expectations/user.txt +++ b/test-files/expectations/user.txt @@ -18,7 +18,8 @@ export default class UserDto { declare stockPurchases: StockPurchaseDto[] declare stocks: StockDto[] - constructor(user: User) { + constructor(user?: User) { + if (!user) return this.id = user.id this.fullName = user.fullName this.email = user.email