Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
4 replies
jairrard

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
}

For getTask, the response is of the type -
{
  _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.
Was this page helpful?