You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+22-21Lines changed: 22 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,13 +25,13 @@ You can use `Group` to help organize your routes and cut down on the repetitiven
25
25
26
26
```swift
27
27
app.register {
28
-
Group(path: "api", "v1") {
29
-
Group(path: "movies") {
28
+
Group("api", "v1") {
29
+
Group("movies") {
30
30
GET("latest") { ... }
31
31
GET("popular") { ... }
32
32
GET(":movie") { ... }
33
33
}
34
-
Group(path: "books") {
34
+
Group("books") {
35
35
GET("new") { ... }
36
36
GET("trending") { ... }
37
37
GET(":book") { ... }
@@ -40,20 +40,19 @@ app.register {
40
40
}
41
41
```
42
42
43
-
### Middleware Groups
43
+
### Middleware
44
44
45
-
Sometimes you may want to wrap certain routes in middleware. A common use case requiring middleware could be authentication. You have have some routes that require middleware, and others that do now. Adding middleware using `@RouteBuilder` is similar to wrapping routes in `Group(path:)`.
45
+
Sometimes you may want to wrap certain routes in middleware. A common use case requiring middleware could be authentication. You have have some routes that require middleware, and others that do now. Adding middleware using `@RouteBuilder` is similar to adding view modifiers in SwiftUI.
46
46
47
47
```swift
48
48
app.register {
49
-
Group(path: "api", "v1") {
50
-
Group(middleware: AuthenticationMiddleware()) {
51
-
Group(path: "profile") {
52
-
GET("favorites") { ... }
53
-
GET("friends") { ... }
54
-
}
49
+
Group("api", "v1") {
50
+
Group("profile") {
51
+
GET("favorites") { ... }
52
+
GET("friends") { ... }
55
53
}
56
-
Group(path: "books") {
54
+
.middleware(AuthenticationMiddleware())
55
+
Group("books") {
57
56
GET("new") { ... }
58
57
GET("trending") { ... }
59
58
GET(":book") { ... }
@@ -64,15 +63,17 @@ app.register {
64
63
65
64
In the above example, you'll see that we've only wrapped our `/profile/*` endpoints in our `AuthenticationMiddleware`, while all of the `/book/*` endpoints have no middleware associated with them.
66
65
67
-
If you have more than one middleware... no worries, `Group(middleware:)` accepts a variadic amount of middleware.
66
+
If you have more than one middleware... no worries, `.middleware(_:)` accepts a variadic amount of middleware.
Remember that order matters here. Incoming requests will always execute middleware from top to bottom. So in the above example, the order of an incoming request would be as follows ➡️ `Logging`, `Authentication`, `Validator`. Outgoing respones will always execute middleware in the reverse order. ➡️ `Validator`, `Authentication`, `Logging`.
@@ -84,7 +85,7 @@ Often times, as your routes grow, a single large definition can become unwieldly
This package ships with a few conveniences for creating routes. You can use `GET`, `POST`, `PUT`, `PATCH`, and `DELETE` to cut down on the verbosity of defining your routes. If you need more fine grained control, you can always fall back to using a `Route` directly in your `@RouteBuilder`
0 commit comments