Type for request and response. Should they be shared?

Let's say I have 2 apis - getTask and createTask For createTask I need the request to be of the type -
{
title: string,
description: string,
dueDate: Date
journeyId: ObjectId
}
{
title: string,
description: string,
dueDate: Date
journeyId: ObjectId
}
For getTask, the response is of the type -
{
_id: ObjectId,
journey: {
_id: ObjectId,
name: string
},
dueDate: Date,
createdAt: Date,
updatedAt: Date,
}
{
_id: ObjectId,
journey: {
_id: ObjectId,
name: string
},
dueDate: Date,
createdAt: Date,
updatedAt: Date,
}
Questions - 1. Should I be using 2 different types or try to create one parent type from which I use a Partial type for the createTask request? If so, there would be issues with journeyId (in request) and journey (in response) where they should ideally be part of one field in both types rather than having 2 fields which refer to the same thing. 2. If 1 is a depends on, what would it depend on? Also for some context - I am using Mongodb with api calls being made via Mongodb App Services (Realm). I am looking to move over to the T3 stack soon.
2 Replies
Neto
Neto2y ago
You can use different models for writing and reading data I'm just not sure about mongodb idea of typing it's data And unless you need to specify exactly the shape of one entry, you can let typescript infer it's type
jingleberry
jingleberry2y ago
Two separate types is fine although you can use some TS magic to have one be derived from the other... (but is it worth it?) It really depends on how closely they relate to one another. If its super basic then sure, combine them. But for more complicated things its much easier to have separate models for different operations.
Want results from more Discord servers?
Add your server