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
- They can send responses so you don't have to handle every case.
8
+
Middlewares allow you to reuse logic by handling tasks like validation, access control, or response formatting. They offer several features:
12
9
13
-
NOTE: Schema validators are also middlewares.
10
+
- Passing data to other middlewares.
11
+
- Preventing further execution based on conditions.
12
+
- Sending responses, so you don't need to handle every case manually.
13
+
14
+
Note: Schema validators are also treated as middlewares.
14
15
15
16
## Basic
16
17
@@ -28,20 +29,20 @@ builder
28
29
});
29
30
```
30
31
31
-
- If the middleware returns `{ next: true }`, it can also add something to `data` type-safely.
32
-
- The above example returns `{ next: true, randomValue }` thus `data.randomValue` is accessible in the next request handler.
33
-
- If the middleware returns `{ next: false }` it sends a response thus it needs `statusCode`.
34
-
But having `result` is also advised for a good **discriminated union**.
32
+
In this example:
33
+
34
+
- If the middleware returns `{ next: true }`, the execution proceeds to the next handler. You can also type-safely add data, as shown with `randomValue` in `data.randomValue`
35
+
- If the middleware returns `{ next: false }`, it sends an immediate response, which requires a `statusCode`. It's recommended to include a `result: 'some-error' as const`for having a proper discriminated union in the response.
35
36
36
37
## Middleware Chaining
37
38
38
-
Some middlewares need to use schema validation.
39
-
This, however, prevents you to just move the request handler out of the request handler building.
40
-
To solve this, there are two concepts you can use:
41
-
- `.buildLink()`Instead of the final middleware, you can use this to get a chain link.
42
-
- `.chain(fooLink).chain(barLink)` to bring multiple links into the request handler building.
39
+
Some middlewares need to use data coming from schema validators. In order to use the data from the schema validation you have to keep the middleware function in the builder code.
40
+
This, however, prevents you to just move the middleware function out of the builder.
41
+
To solve this, Cuple introduces a wrapper called **link**, and you can connect links with each other.
42
+
- `.buildLink()`Creates a link from a build chain. Use this instead of the final middleware (get, post, ...).
43
+
- `.chain(fooLink).chain(barLink)` to bring multiple links into the build chain
43
44
44
-
Here's an example:
45
+
### Example:
45
46
46
47
```ts
47
48
const authLink =builder
@@ -70,3 +71,87 @@ const routes = {
70
71
}),
71
72
};
72
73
```
74
+
75
+
## Dependent chain links (Since 1.0.10)
76
+
77
+
Middlewares can depend on the output of previous middlewares.
78
+
For example, a role-check middleware might depend on the result of an authentication middleware.
0 commit comments