@resora/plugin-clear-router 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,39 @@
|
|
|
1
1
|
# @resora/plugin-clear-router
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Bridge plugins for integrating `resora` resources with `clear-router` handlers and controllers without modifying either package.
|
|
4
4
|
|
|
5
5
|
## Why
|
|
6
6
|
|
|
7
|
-
`resora` resources are thenables, while `clear-router` resolves
|
|
7
|
+
`resora` resources are thenables, while `clear-router` automatically resolves and serializes values returned from route handlers and controller actions.
|
|
8
|
+
|
|
9
|
+
These plugins bind the active `clear-router` request context to Resora before your handler executes, allowing resources to work seamlessly when returned directly from routes or controllers.
|
|
10
|
+
|
|
11
|
+
Without the plugin, returning resources may not have access to the active request lifecycle or response context.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash [pnpm]
|
|
16
|
+
pnpm add resora @resora/plugin-clear-router
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
OR
|
|
20
|
+
|
|
21
|
+
```bash [npm]
|
|
22
|
+
npm install resora @resora/plugin-clear-router
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
OR
|
|
26
|
+
|
|
27
|
+
```bash [yarn]
|
|
28
|
+
yarn add resora @resora/plugin-clear-router
|
|
29
|
+
```
|
|
8
30
|
|
|
9
31
|
## Usage
|
|
10
32
|
|
|
33
|
+
Register the plugin before defining routes.
|
|
34
|
+
|
|
35
|
+
### Express
|
|
36
|
+
|
|
11
37
|
```ts
|
|
12
38
|
import { registerPlugin } from 'resora';
|
|
13
39
|
import { clearRouterExpressPlugin } from '@resora/plugin-clear-router';
|
|
@@ -15,31 +41,102 @@ import { clearRouterExpressPlugin } from '@resora/plugin-clear-router';
|
|
|
15
41
|
registerPlugin(clearRouterExpressPlugin);
|
|
16
42
|
```
|
|
17
43
|
|
|
44
|
+
### Fastify
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { registerPlugin } from 'resora';
|
|
48
|
+
import { clearRouterFastifyPlugin } from '@resora/plugin-clear-router';
|
|
49
|
+
|
|
50
|
+
registerPlugin(clearRouterFastifyPlugin);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### H3
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { registerPlugin } from 'resora';
|
|
57
|
+
import { clearRouterH3Plugin } from '@resora/plugin-clear-router';
|
|
58
|
+
|
|
59
|
+
registerPlugin(clearRouterH3Plugin);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Hono
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { registerPlugin } from 'resora';
|
|
66
|
+
import { clearRouterHonoPlugin } from '@resora/plugin-clear-router';
|
|
67
|
+
|
|
68
|
+
registerPlugin(clearRouterHonoPlugin);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Koa
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { registerPlugin } from 'resora';
|
|
75
|
+
import { clearRouterKoaPlugin } from '@resora/plugin-clear-router';
|
|
76
|
+
|
|
77
|
+
registerPlugin(clearRouterKoaPlugin);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Generic Runtime
|
|
81
|
+
|
|
82
|
+
Use the generic plugin when your runtime does not have a dedicated adapter.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { registerPlugin } from 'resora';
|
|
86
|
+
import { clearRouterPlugin } from '@resora/plugin-clear-router';
|
|
87
|
+
|
|
88
|
+
registerPlugin(clearRouterPlugin);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Returning Resources from Controllers
|
|
92
|
+
|
|
93
|
+
Once registered, resources can be returned directly from controller methods.
|
|
94
|
+
|
|
18
95
|
```ts
|
|
19
96
|
import { Router, Controller } from 'clear-router';
|
|
20
97
|
import { Resource } from 'resora';
|
|
21
98
|
|
|
22
99
|
class UserController extends Controller {
|
|
23
100
|
index() {
|
|
24
|
-
return new Resource({
|
|
101
|
+
return new Resource({
|
|
102
|
+
id: 1,
|
|
103
|
+
name: 'Ada',
|
|
104
|
+
});
|
|
25
105
|
}
|
|
26
106
|
}
|
|
27
107
|
|
|
28
108
|
Router.get('/users', [UserController, 'index']);
|
|
29
109
|
```
|
|
30
110
|
|
|
111
|
+
## Returning Resources from Inline Handlers
|
|
112
|
+
|
|
113
|
+
Resources also work in direct route callbacks.
|
|
114
|
+
|
|
31
115
|
```ts
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
116
|
+
import { Router } from 'clear-router';
|
|
117
|
+
import { Resource } from 'resora';
|
|
34
118
|
|
|
35
|
-
|
|
119
|
+
Router.get('/users', () => {
|
|
120
|
+
return new Resource({
|
|
121
|
+
id: 1,
|
|
122
|
+
name: 'Ada',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
36
125
|
```
|
|
37
126
|
|
|
38
127
|
## Exports
|
|
39
128
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
129
|
+
| Export | Description |
|
|
130
|
+
| -------------------------- | --------------------------- |
|
|
131
|
+
| `clearRouterExpressPlugin` | Express runtime integration |
|
|
132
|
+
| `clearRouterFastifyPlugin` | Fastify runtime integration |
|
|
133
|
+
| `clearRouterH3Plugin` | H3 runtime integration |
|
|
134
|
+
| `clearRouterHonoPlugin` | Hono runtime integration |
|
|
135
|
+
| `clearRouterKoaPlugin` | Koa runtime integration |
|
|
136
|
+
| `clearRouterPlugin` | Generic runtime integration |
|
|
137
|
+
|
|
138
|
+
## Notes
|
|
139
|
+
|
|
140
|
+
Plugins are opt-in and do not modify `clear-router` or `resora` globally unless explicitly registered.
|
|
141
|
+
|
|
142
|
+
Only register the plugin matching the runtime used by your Clear Router application.
|