Define a Response Structure

Response Structure

When working on implementation between Backend and Frontend what is the best decision is to define a response Structure.

What does this mean? Backend know structure which has to send and Frontend knows how to react to data.

Suggestion: Response should follow between services and/or between Frontend-Backend the following format which should be standardized across all:

interface Response {
  status: number
  message: string
  errors?: Record<string, unknown>[]
  data?: Record<string, unknown>[]
}

Only two fields are always required to be present in response status and message.

An example how the response may look like is below:

{
  status: 400,
  message: "Bad Request",
  errors?: [
    {
      field: "firstName",
      error: "Not valid"
    }
  ],
  data?: [
    {
      firstName: "FirstName",
      lastName: "LastName"
    }
  ],
}

Keep pushing forward and savor every step of your coding journey.