Custom Response Models
Introduction
In this section, we’ll focus on creating custom response models and demonstrate how to use them in your API operations. We’ll also incorporate predefined response models from the TypeSpec HTTP library.
Introduction to Custom Response Models
Custom response models allow you to define structured responses for your API operations. They help ensure consistency and clarity in your API responses. TypeSpec defines response models for common HTTP responses in the HTTP library, which we can incorporate into our custom response models.
Common HTTP Status Codes and TypeSpec Response Models
Here are some common HTTP status codes and their equivalent TypeSpec response models from the TypeSpec HTTP library:
HTTP Status Code | Meaning | TypeSpec Response Model |
---|---|---|
200 OK | The request was successful, and the server returned the requested resource. | OkResponse |
201 Created | The request was successful, and a new resource was created. | CreatedResponse |
204 No Content | The request was successful, but there is no content to return. | NoContentResponse |
400 Bad Request | The server could not understand the request due to invalid syntax. | BadRequestResponse |
401 Unauthorized | The client must authenticate itself to get the requested response. | UnauthorizedResponse |
403 Forbidden | The client does not have access rights to the content. | ForbiddenResponse |
404 Not Found | The server cannot find the requested resource. | NotFoundResponse |
Benefits of Using Custom Response Models
- Reducing Duplication: By defining common response structures once, you can reuse them across multiple operations.
- Improving Readability: Custom response models make your API definitions clearer and easier to understand.
- Minimizing Errors: Consistent response models help reduce the likelihood of errors in your API responses.
Creating Custom Response Models
Let’s start by defining and extending some custom response models. These models will incorporate existing response models from the TypeSpec HTTP library to ensure consistency.
Example: Defining and Extending Custom Response Models
In this example:
PetListResponse
extendsOkResponse
and includes a body with an array ofPet
objects.PetResponse
extendsOkResponse
and includes a body with a singlePet
object.PetCreatedResponse
extendsCreatedResponse
and includes a body with a newly createdPet
object.PetErrorResponse
extendsBadRequestResponse
and includes a body with aValidationError
object.PetNotFoundResponse
extendsNotFoundResponse
and includes a body with aNotFoundError
object.PetUnauthorizedResponse
extendsUnauthorizedResponse
and includes a body with anUnauthorizedError
object.PetSuccessResponse
extendsOkResponse
and includes a body with a success message.PetNoContentResponse
extendsNoContentResponse
for situations where the request succeeded but there is no content to return.
Note: Base response models like OkResponse
, CreatedResponse
, BadRequestResponse
, NotFoundResponse
, and UnauthorizedResponse
are imported from the TypeSpec HTTP data types library, which we’re importing in our project as @typespec/http
.
Using Custom Response Models in Operations
Now that we have defined our custom response models, let’s use them in our API operations.
Example: Applying Custom Response Models to Operations
In this example:
- The
listPets
operation uses thePetListResponse
custom response model. - The
getPet
operation uses thePetResponse
andPetNotFoundResponse
custom response models. - The
createPet
operation uses thePetCreatedResponse
,PetAcceptedResponse
,PetErrorResponse
, andPetUnauthorizedResponse
custom response models. - The
updatePet
operation uses thePetResponse
,PetErrorResponse
,PetUnauthorizedResponse
,PetNotFoundResponse
, andInternalServerErrorResponse
custom response models. - The
deletePet
operation uses thePetNoContentResponse
andPetUnauthorizedResponse
custom response models.
Note that we could also define custom response models for the Toys
operations, similar to the Pets
operations. But for brevity, we’re omitting them in this example.
Conclusion
In this section, we focused on creating custom response models in your REST API. By defining and extending custom response models, we can reduce duplication, improve readability, and minimize errors in our API responses. We also incorporated existing response models from the TypeSpec HTTP library to ensure consistency and clarity.