Chapter 10 - Manage the slideshows through an API¶
In this chapter, you will create an API to manage the slideshow.
The purpose of an API is to allow other interfaces to interact with your application. This client (= interface) could be a Telegram Bot, another UI made by one of your friends, a distant server that uses your application, etc.
In this chapter, you'll use the cURL command-line tool to get, create, update and delete your slideshows through your API.
Steps¶
Slideshows creation and the use of generated IDs¶
Up until now, you have always hardcoded the slideshows with their IDs and their slides.
In a real world application, these IDs are hardly ever defined by the end user. They are generated and stored by the application for you to avoid conflicts and data loss.
An UUID will be used for unique IDs when slideshows are created.
You will generate UUIDs with Node.js later.
Create the types to create and update a slideshow¶
Create two news CreateSlideshow
and UpdateSlideshow
types to create and update a slideshow. As mentioned earlier, the application is in charge to generate the IDs of your slideshows.
The only thing that differentiate a Slideshow
types and the new CreateSlideshow
and UpdateSlideshow
types is the omission of the ID from the slideshow.
Thanks to TypeScript, omitting some fields from another type is very easy with the Omit
utility type.
src/types/create-slideshow.type.ts | |
---|---|
src/types/update-slideshow.type.ts | |
---|---|
Update the service to create, update, and delete slideshows¶
Update the AppService
to allow to create, update, and delete slideshows.
You may notice the createSlideshow
and updateSlideshow
methods return the created/updated slideshow. It is good practice to return the created/updated object to the client so they knows what has changed on the API side and how to get the newly created object with its ID.
- The
Map
is now empty. - The object is typed with the
CreateSlideshow
type for correctness. - The ID of the slideshow is generated with the
randomUUID
function from thecrypto
Node.js module. - The new slideshow is created with its ID and its slides.
- The slideshow is saved in the
Map
. - The API returns the newly created object to the user.
- The object is typed with the
UpdateSlideshow
type for correctness. - The keyword
this
refers to the classAppService
. It means you can use other functions defined in the same class, such as thegetSlideshow
method defined earlier. - If the requested slideshow was not found, it throws an error as it is not possible to update a non-existent slideshow.
- The slideshow is updated and returned to the end user.
- Return
true
if the element in theMap
has been found and deleted,false
if the element has not been found.
Update the controller to get, create, update and delete slideshows¶
In this update, a lot is going on.
When working with APIs on the Web, they often use the HTTP protocol.
The HTTP protocol defines a few "verbs" that are used to define specific actions with your API. For now, you'll use the following request methods:
GET
: Get a resource from an endpoint. Actually, when you access all your webpages, aGET
request is sent to the server that will give you an HTML file in return.POST
: Create a new resource on an endpoint. When submitting a form on a website, it often sends aPOST
request with the form data to be saved on the server.PATCH
: Update parts of a resource. ThePUT
request method should be used when updating the entire resource but is often used asPATCH
as well.DELETE
: Delete a resource. As the deletion often means the resource doesn't exist anymore, the end user is only informed the resource was deleted without any more details.
For a list of all the available method request, check the MDN Web Docs HTTP request methods documentation.
The same path can have multiple endpoints for each request method.
For example, the /api/slideshows
path offers two endpoints: one to get all the slideshows (with a GET
request method) and a second to create a new slideshow (with a POST
request method).
The most common format used with APIs is JSON. The API accepts and returns JSON payloads.
Each request can return a status code. The status codes are classified in five categories:
- Informational responses (
100
–199
) - Successful responses (
200
–299
) - Redirection messages (
300
–399
) - Client error responses (
400
–499
) - Server error responses (
500
–599
)
The most common responses are:
200 OK
: The request succeeded.201 Created
: The request succeeded, and a new resource was created as a result.204 No Content
: There is no content to send for this request.301 Moved Permanently
: The URL of the requested resource has been changed permanently. The new URL is given in the response.400 Bad Request
: The server cannot or will not process the request due to something that is perceived to be a client error.401 Unauthorized
: The client must authenticate itself to get the requested response.404 Not Found
: The server cannot find the requested resource.409 Conflict
: This response is sent when a request conflicts with the current state of the server.429 Too Many Requests
: The user has sent too many requests in a given amount of time ("rate limiting").500 Internal Server Error
: The server has encountered a situation it does not know how to handle.
For a list of all the available status codes, check the MDN Web Docs HTTP response status codes.
- Add a redirection from the root path (
/
) to/slideshows
. - Change the old path to access slideshows (
/
) to/slideshows
. - Change the old path to access a slideshow (
/:id
) to/slideshows/:id
. - If a slideshow is not found, redirect to
/slideshows
. - A new route
/api/slideshows
allows to get the slideshows as JSON. - The service is used the same as with Handlebars.
- This transform the
Map
object to a standard JavaScript object that can be converted to JSON. - The
NotFoundException
exception is a NestJS built-in exception to return a HTTP response with a 404 status code. - The
POST
endpoint allows to create a new slideshow on the same endpoint path of theGET
/api/slideshows
endpoint. - The
Body
decorator allows to extract the body from the request sent by the user. - The
PATCH
endpoint allows to update a slideshow on the same endpoint path of theGET
/api/slideshows/:id
endpoint. - The Try-Catch Statement allows to catch errors thrown from the
try
block. - If an exception is thrown, the code in the
catch
block is executed. - The
DELETE
endpoint allows to delete a slideshow on the same endpoint path of theGET
/api/slideshows/:id
endpoint. - As the deletion of a slideshow doesn't return the deleted slideshow, a 204 response means no content is expected.
- If the deletion was unsuccessful, return a 404 response with the
NotFoundException
exception.
Update the main template¶
Update the main template to access the slideshow on /slideshows/{{this.id}}
.
Try out the API¶
Start the application and access http://localhost:3000.
You should be redirected to http://localhost:3000/slideshows with no slideshows.
Let's create a new slideshow with our API!
The following commands use cURL to make the requests on your API. After each command, the command is briefly explained with the lines to look at and the output of the command.
Create a slideshow¶
Let's create a new slideshow with the data from your first slideshow slideshow1
.
2
The URL to make the request to.3
The request method to use.4
Enable the verbose mode to have all details of the request.5
A header sent to the API. In this case, theContent-Type
is set toapplication/json
informing the API that the payload is in JSON format.6
Tells the request has a body.7-21
The body of the request with a JSON object representing the slideshow. You notice only the slides are set with the same data as your previousslideshow1
.
The output should be similar to this.
The >
lines are the ones from your request with all the HTTP request data. The <
lines are the ones from the API.
1
cURL informs you thePOST
is not necessary as you've defined a body. When a body is specified, cURL makes aPOST
request by default.4
The request method and path.5
The origin of the request.6
The name of the browser or tool that did the request.7
cURL accepts any kind of data that the API can send to it.8
The content type of the body.9
The content size in bytes.12
The response status code.13
A custom header stating the API is powered by Express.14
The content type of the response and its charset.15
The content size in bytes.17
The time of the response.22
The slideshow JSON object response from the API with its ID.
Access http://localhost:3000/api/slideshows. Your browser should display a JSON object of all slideshows, including your new slideshow.
Access http://localhost:3000/slideshows. You should see the list of the slideshows, including your new slideshow.
Get the slideshow¶
Using the ID from the previous command output, get the slideshow just created.
In a terminal, execute the following command(s). | |
---|---|
2
The URL to make the request to.3
The request method to use.4
Enable the verbose mode to have all details of the request.
The output should be similar to this.
The >
lines are the ones from your request with all the HTTP request data. The <
lines are the ones from the API.
1
cURL informs you theGET
is not necessary as there is no body to the request.4
The request method and path.5
The origin of the request.6
The name of the browser or tool that did the request.7
cURL accepts any kind of data that the API can send to it.10
The response status code.11
A custom header stating the API is powered by Express.12
The content type of the response and its charset.13
The content size in bytes.15
The time of the response.20
The slideshow JSON object response from the API.
Access http://localhost:3000/api/slideshows/{id}. Your browser should display a JSON object of the slideshow.
Access http://localhost:3000/slideshows/{id}. You should see the slideshow playing the two images.
Update the slideshow¶
Using the ID from the previous command output, update the slideshow with with the data from your second slideshow slideshow2
.
2
The URL to make the request to.3
The request method to use.4
Enable the verbose mode to have all details of the request.5
A header sent to the API. In this case, theContent-Type
is set toapplication/json
informing the API that the payload is in JSON format.6
Tells the request has a body.7-21
The body of the request with a JSON object representing the slideshow. You notice only the slides are set with the same data as your previousslideshow2
.
The output should be similar to this.
The >
lines are the ones from your request with all the HTTP request data. The <
lines are the ones from the API.
3
The request method and path.4
The origin of the request.5
The name of the browser or tool that did the request.6
cURL accepts any kind of data that the API can send to it.7
The content type of the body.8
The content size in bytes.11
The response status code.12
A custom header stating the API is powered by Express.13
The content type of the response and its charset.14
The content size in bytes.16
The time of the response.21
The slideshow JSON object response from the API with its ID.
Access http://localhost:3000/api/slideshows/{id}. Your browser should display a JSON object of the updated slideshow.
Access http://localhost:3000/slideshows/{id}. You should see the slideshow playing the image and the video.
Delete the slideshow¶
Using the ID from the previous command output, delete the slideshow.
In a terminal, execute the following command(s). | |
---|---|
2
The URL to make the request to.3
The request method to use.4
Enable the verbose mode to have all details of the request.
The output should be similar to this.
The >
lines are the ones from your request with all the HTTP request data. The <
lines are the ones from the API.
3
The request method and path.4
The origin of the request.5
The name of the browser or tool that did the request.6
cURL accepts any kind of data that the API can send to it.9
The response status code.10
A custom header stating the API is powered by Express.11
The time of the response.
Access http://localhost:3000/api/slideshows. Your browser should display a JSON object of all slideshows. Your slideshow should not appear as it was deleted.
Access http://localhost:3000/slideshows. You should see the list of the slideshows. Your slideshow should not appear as it was deleted.
Get a non-existent slideshow¶
Try to access a non-existent slideshow.
In a terminal, execute the following command(s). | |
---|---|
The output should be similar to this.
The >
lines are the ones from your request with all the HTTP request data. The <
lines are the ones from the API.
3
The request method and path.4
The origin of the request.5
The name of the browser or tool that did the request.6
cURL accepts any kind of data that the API can send to it.7
The content type of the body.8
The content size in bytes.11
The response status code.12
A custom header stating the API is powered by Express.13
The content type of the response and its charset.14
The content size in bytes.16
The time of the response.21
TheNotFoundException
exception message from the exception thrown by NestJS.
Access http://localhost:3000/api/slideshows/non-existing-slideshow. Your browser should display a JSON object with a 404 error.
Access http://localhost:3000/slideshows/non-existing-slideshow. You should be redirected to http://localhost:3000/slideshows.
Restart your application and notice the data loss¶
Create a new slideshow.
Stop your NestJS application with Ctrl+C in your terminal.
Restart your NestJS application with npm run start:dev
.
Try to access the slideshows.
You should notice that the slideshow you created earlier has been lost. This is because all your slideshows are stored in memory. This means on a restart, everything is lost. You'll see how to store your slideshows in a database in the next chapter.
To stop your NestJS application, press Ctrl+C in your terminal.
Summary¶
Congrats! You can now manage your slideshows with the help of your API. The API allows you and others users to use your application with their own tools. It helps to separate the views rendering and the business logic of the application.