0

I'm a beginner at NestJs and I want to create a dto of the following struct:

I want to create an API that can return this object using DTOs.

export let Week = [
    {
        DayName : "TuesDay",
        TimeZone: [
            {
                First: {
                        Start: 18,
                        end: 19,
                        isReserved: false,
                        reserver: "someone"
                },
                Second: {
                        Start: 19,
                        end: 20,
                        isReserved: false,
                        reserver: "someone"
                },
                Third: {
                        Start: 20,
                        end: 21,
                        isReserved: false,
                        reserver: "someone"
                }
            }
        ]
    },
    {
        DayName : "Wednesday",
        TimeZone: [
            {
                First: {
                        Start: 18,
                        end: 19,
                        isReserved: false,
                        reserver: "someone"
                },
                Second: {
                        Start: 19,
                        end: 20,
                        isReserved: false,
                        reserver: "someone"
                },
                Third: {
                        Start: 20,
                        end: 21,
                        isReserved: false,
                        reserver: "someone"
                }
            }
        ]
    },
    {
        DayName : "Friday",
        TimeZone: [
            {
                First: {
                        Start: 18,
                        end: 19,
                        isReserved: false,
                        reserver: "someone"
                },
                Second: {
                        Start: 19,
                        end: 20,
                        isReserved: false,
                        reserver: "someone"
                },
                Third: {
                        Start: 20,
                        end: 21,
                        isReserved: false,
                        reserver: "someone"
                }
            }
        ]
    }
]

I tried the following dto but I got an error in the service provider :

I create 3 classes OneSessionResponseDto, SessionsResponseDto, and WeekResponseDto so I can use them as a dto of the object above.

export class OneSessionResponseDto {
    start: number;
    end: number;
    isReserved: boolean;
    reserver: string;
}

export class SessionsResponseDto {
    First: OneSessionResponseDto;
    Second: OneSessionResponseDto;
    Third: OneSessionResponseDto;
}

export class WeekResponseDto {
    DayName: string;
    TimeZone: SessionsResponseDto;
}

this is the service provider class:

@Injectable()
export class AppService {
    private week = Week;
    getWeek() : WeekResponseDto[] {
        return (this.week); <= the error
    }
}

I got this error here:

is missing the following properties from type 'SessionsResponseDto': First, Second, Third

DarkSide77
  • 719
  • 1
  • 4
  • 21
  • 1
    What exactly is the purpose of the DTO? If you want to return this object why dont you just return it? Doesnt it already have the correct format? – Code Spirit Jun 10 '22 at 15:40
  • yeah but I want to use it for validation when I implement the POST and PUT method. – DarkSide77 Jun 10 '22 at 15:50
  • @CodeSpirit I have a question is can I use dto to make the code clean or just for validation? – DarkSide77 Jun 10 '22 at 15:52
  • 1
    `DTO` stands for Data Transfer Object and is nothing than a design pattern / concept. Adding `DTO` to your classnames or not makes no difference but is generally done like this to indicate which classes/objects are actually DTOs. Validation on the other hand is done by `class-validator` library like described in nestjs docs that uses classes and annnotations. The Classes you are trying to validate are DTOs because they are objects that are used to transfer data. But asking if DTOs make your code cleaner is almost the same as asking if using arrays would make your code cleaner. It depends. – Code Spirit Jun 10 '22 at 15:57

1 Answers1

1

You almost have it. You need to set TimeZone's type to SessionsResponseDto[] because it's an array. Then change OneSessionResponseDto#start to OneSessionResponseDto#Start (the capitalization is important). You can see a working solution in the typoescript playground here

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147