Bringing it Together
Understand the project’s code that we’ve written so far.
We'll cover the following...
We'll cover the following...
Creating a unified response type
Let’s combine all the things we’ve learned so far. The one element we should add is a unified response. We’ll use the following HTTP/REST-inspired type:
In the type above, the status will be a 2xx, 4xx, or 5xx number signifying success or failure (similar to HTTP response code), and a message will give additional information. Let’s look at them below. We’ll also add some pure helper functions to create responses.
We could factor out some duplication here, but this example is good enough for now. Let’s review an example where we use these functions:
Let’s explain what we did in the code above:
- Lines 1–18: The
userResponsefunction builds our user. It logs a message, which is a stand-in for a call to a database, and builds a response. The response is either a201in case of success or