Skip to content

Commit 83d677d

Browse files
committed
adds chapter 6.8 - http methods API
1 parent 44cc3ab commit 83d677d

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

chapters/ch06.8-adding-verbs-api.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## Adding HTTP methods to the Router
2+
3+
In this exercise, we will improve our `TrieRouter` API by implementing support for HTTP verbs (GET, POST, PUT, DELETE, etc.) directly instead of using raw strings. Let's look at our current implementation of the `TrieRouter`:
4+
5+
```js
6+
trieRouter.addRoute("GET", "/users", () => {});
7+
trieRouter.addRoute("GET", "/", () => {});
8+
```
9+
10+
As you could already feel, this approach is not very flexible and uses raw strings, which can lead to typing errors, and has no auto-completion support unfortunately. Let's improve this by adding support for HTTP methods directly to the `TrieRouter` API.
11+
12+
Firstly, we've added dedicated methods for each HTTP method in the `TrieRouter` class. This allows users to define routes more intuitively using method-specific calls like `trieRouter.get('/home', handler)` for the GET method and `trieRouter.post('/home', handler)` for the POST method.
13+
14+
### Update the `TrieRouter` class
15+
16+
```js
17+
const HTTP_METHODS = { ... };
18+
19+
class TrieRouter {
20+
...
21+
22+
#addRoute(path, method, handler) {
23+
...
24+
}
25+
26+
get(path, handler) {
27+
this.#addRoute(path, HTTP_METHODS.GET, handler);
28+
}
29+
30+
post(path, handler) {
31+
this.#addRoute(path, HTTP_METHODS.POST, handler);
32+
}
33+
34+
put(path, handler) {
35+
this.#addRoute(path, HTTP_METHODS.PUT, handler);
36+
}
37+
38+
delete(path, handler) {
39+
this.#addRoute(path, HTTP_METHODS.DELETE, handler);
40+
}
41+
42+
patch(path, handler) {
43+
this.#addRoute(path, HTTP_METHODS.PATCH, handler);
44+
}
45+
46+
head(path, handler) {
47+
this.#addRoute(path, HTTP_METHODS.HEAD, handler);
48+
}
49+
50+
options(path, handler) {
51+
this.#addRoute(path, HTTP_METHODS.OPTIONS, handler);
52+
}
53+
54+
connect(path, handler) {
55+
this.#addRoute(path, HTTP_METHODS.CONNECT, handler);
56+
}
57+
58+
trace(path, handler) {
59+
this.#addRoute(path, HTTP_METHODS.TRACE, handler);
60+
}
61+
...
62+
}
63+
```
64+
65+
Firstly, we've added dedicated methods for each HTTP method in the `TrieRouter` class. This allows users to define routes more intuitively using method-specific calls like `trieRouter.get('/home', handler)` for the GET method and `trieRouter.post('/home', handler)` for the POST method.
66+
67+
In each of these methods, we call the existing `addRoute` method, passing the appropriate HTTP method from the `HTTP_METHODS` object.
68+
69+
This change allows for a consistent and clear way to find routes based on the HTTP method.
70+
71+
Sedcondly, we've made the `addRoute` method private by prefixing it with a `#`. This means that the `#addRoute` method can now only be accessed from within the `TrieRouter` class and not from outside.
72+
73+
Now, to test the new API, let's update our previous example:
74+
75+
```js
76+
const trieRouter = new TrieRouter();
77+
78+
trieRouter.get("/users", function get1() {});
79+
trieRouter.post("/users", function post1() {});
80+
trieRouter.put("/users", function put1() {});
81+
trieRouter.delete("/users", function delete1() {});
82+
83+
console.log(trieRouter.findRoute("/users/e", HTTP_METHODS.GET)); // null
84+
console.log(trieRouter.findRoute("/users", HTTP_METHODS.POST)); // function post1() {}
85+
console.log(trieRouter.findRoute("/users", HTTP_METHODS.PUT)); // function put1() {}
86+
console.log(trieRouter.findRoute("/users", HTTP_METHODS.TRACE)); // undefined
87+
```
88+
89+
Looks good, and now we have a more intuitive way to define routes based on HTTP methods. Let's move on to the next exercise to add support for route parameters.

0 commit comments

Comments
 (0)