Adding an API endpoint for updating data
The final piece of the puzzle is to add a PUT request handler function for dealing with updates. Similar to the POST function, we must provide the request body using the createRequest helper.
The form of our PUT request means that id is passed as a parameter in the URL, like this:
Here’s a reminder of how the URL must be passed:
PUT http://localhost:1234/api/birthday/abc123
To make SvelteKit register the abc123 value as a parameter, we need to create a new route directory at src/routes/api/birthday/[id]. SvelteKit will then know that abc123 matches the [id] bit of the directory path. The directory itself will then contain the +server.js file and its tests.
Let’s start with the tests:
- Create a new file named
src/routes/api/birthday/[id]/server.test.jsand add the following imports. This has everything we’ve used previously for theGETandPOSTfunctions, together with an import for thePUTfunction...