dimanche 27 mai 2018

Naming or patterns for rest API in Typescript

I'm writing REST API in Nest.js, trying to do it OOP way. I declare all my types and found problem with some code redundancy.

Let's start with some module, like users.

I have:

1: Users controller (easy)

2: Users service (easy)

3: User schema - Mongoose Schema with it's declaration of document structure

4: IUser interface which is declaring parametrs of my user (email, mail) 5: UserModel which is extending IUser and Mongoose's Document

6: Joi validation schema of user's payload for POST user creation

What I miss is an interface of payload (actually the same as Joi's), so I can manipulate on POST body, but in typescript.

In some examples, IUser will match User payload, but eventually I will have differences (password exists in payload, hash exists in db etc)

Is it normal to do so many interfaces and create UserPayload? Or should I extend IUser with lot of optional fields?

Can I see any patterns or example apps Is user payload the same as DTO?

Aucun commentaire:

Enregistrer un commentaire