|
| 1 | +# ActionRouter |
| 2 | + |
| 3 | +`ActionRouter` is the event-driven router behind the higher-level `Router` API. It accepts props-based handlers and exposes entry, import, and view routing helpers. |
| 4 | + |
| 5 | +```typescript |
| 6 | +import ActionRouter from '@stackpress/ingest/plugin/ActionRouter'; |
| 7 | + |
| 8 | +const router = new ActionRouter({ appName: 'example' }); |
| 9 | + |
| 10 | +router.get('/users/:id', ({ req, res, ctx }) => { |
| 11 | + res.setJSON({ |
| 12 | + id: req.data('id'), |
| 13 | + app: ctx.appName |
| 14 | + }); |
| 15 | +}); |
| 16 | +``` |
| 17 | + |
| 18 | +## Constructor |
| 19 | + |
| 20 | +```typescript |
| 21 | +const router = new ActionRouter(context); |
| 22 | +``` |
| 23 | + |
| 24 | +`context` is stored on the router and passed to handlers as `ctx`. `ActionRouter` provides one direct, props-based routing model that can also host entry, import, and view helpers without changing the handler shape. |
| 25 | + |
| 26 | +## Properties |
| 27 | + |
| 28 | +| Property | Type | Description | |
| 29 | +| --- | --- | --- | |
| 30 | +| `context` | `X` | Context passed to route handlers | |
| 31 | +| `routes` | `Map<string, Route>` | Registered route metadata | |
| 32 | +| `entry` | `EntryRouter<R, S, X, C, P>` | Entry-file routing helper | |
| 33 | +| `import` | `ImportRouter<R, S, X, C, P>` | Dynamic import routing helper for lazy loading and tooling | |
| 34 | +| `view` | `ViewRouter<R, S, X, C, P>` | Template routing helper | |
| 35 | + |
| 36 | +## Route methods |
| 37 | + |
| 38 | +Use the HTTP verb helpers or `route()` to register handlers. |
| 39 | + |
| 40 | +```typescript |
| 41 | +router.get('/users', ({ res }) => { |
| 42 | + res.setJSON({ users: [] }); |
| 43 | +}); |
| 44 | + |
| 45 | +router.patch('/users/:id', ({ req, res }) => { |
| 46 | + res.setJSON({ id: req.data('id'), patched: true }); |
| 47 | +}); |
| 48 | + |
| 49 | +router.options('/users', ({ res }) => { |
| 50 | + res.setJSON({ allow: ['GET', 'POST', 'PATCH', 'OPTIONS'] }); |
| 51 | +}); |
| 52 | + |
| 53 | +router.head('/users', ({ res }) => { |
| 54 | + res.code = 200; |
| 55 | +}); |
| 56 | + |
| 57 | +router.post('/users', async ({ req, res }) => { |
| 58 | + await req.load(); |
| 59 | + res.setJSON({ created: req.data() }, 201); |
| 60 | +}); |
| 61 | + |
| 62 | +router.all('/health', ({ res }) => { |
| 63 | + res.setJSON({ ok: true }); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +### Parameters |
| 68 | + |
| 69 | +| Parameter | Type | Description | |
| 70 | +| --- | --- | --- | |
| 71 | +| `path` | `string` | Route path | |
| 72 | +| `action` | `ActionRouterAction<R, S, X, C, P>` | Props-based route handler | |
| 73 | +| `priority` | `number` | Optional listener priority. Can be negative. Higher numbers run first, then ties follow definition order. | |
| 74 | + |
| 75 | +### Returns |
| 76 | + |
| 77 | +The verb helpers ultimately return the route metadata created by `route()`: method, path, and listener details. |
| 78 | + |
| 79 | +## Event methods |
| 80 | + |
| 81 | +### `emit(event, req, res)` |
| 82 | + |
| 83 | +Runs the matching route tasks for an event string. |
| 84 | + |
| 85 | +```typescript |
| 86 | +const status = await router.emit('GET /users/123', request, response); |
| 87 | +``` |
| 88 | + |
| 89 | +### `eventName(method, path)` |
| 90 | + |
| 91 | +Builds the internal event name used for matching. |
| 92 | + |
| 93 | +```typescript |
| 94 | +const event = router.eventName('GET', '/users/:id'); |
| 95 | +``` |
| 96 | + |
| 97 | +### `use(otherRouter)` |
| 98 | + |
| 99 | +Merges another `ActionRouter` into the current one. |
| 100 | + |
| 101 | +```typescript |
| 102 | +const api = new ActionRouter(context); |
| 103 | +api.get('/api/health', ({ res }) => res.setJSON({ ok: true })); |
| 104 | + |
| 105 | +router.use(api); |
| 106 | +``` |
| 107 | + |
| 108 | +## Routing modes |
| 109 | + |
| 110 | +### Inline handlers |
| 111 | + |
| 112 | +```typescript |
| 113 | +router.get('/users', ({ res }) => { |
| 114 | + res.setJSON({ users: [] }); |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +### Entry files |
| 119 | + |
| 120 | +Entry routes point to a module path. The default export receives a single props object. |
| 121 | + |
| 122 | +```typescript |
| 123 | +router.entry.get('/users/:id', './routes/user.js'); |
| 124 | +``` |
| 125 | + |
| 126 | +```typescript |
| 127 | +// ./routes/user.js |
| 128 | +export default function UserDetail({ req, res }) { |
| 129 | + res.setJSON({ id: req.data('id') }); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### Lazy imports |
| 134 | + |
| 135 | +```typescript |
| 136 | +router.import.get('/users', () => import('./routes/users.js')); |
| 137 | +``` |
| 138 | + |
| 139 | +This mode is useful for lazy loading, but it is also useful when builders need explicit route import boundaries. |
| 140 | + |
| 141 | +### View routes |
| 142 | + |
| 143 | +```typescript |
| 144 | +router.view.render = async (filePath, props) => { |
| 145 | + return await renderTemplate(filePath, props); |
| 146 | +}; |
| 147 | + |
| 148 | +router.view.engine = async (filePath, req, res) => { |
| 149 | + const props = res.data(); |
| 150 | + const html = await router.view.render(filePath, { |
| 151 | + ...req.data(), |
| 152 | + props |
| 153 | + }); |
| 154 | + res.setHTML(html); |
| 155 | +}; |
| 156 | + |
| 157 | +router.view.get('/profile', './views/profile.hbs'); |
| 158 | +``` |
| 159 | + |
| 160 | +## Example |
| 161 | + |
| 162 | +```typescript |
| 163 | +import ActionRouter from '@stackpress/ingest/plugin/ActionRouter'; |
| 164 | + |
| 165 | +const router = new ActionRouter({ version: '1.0.0' }); |
| 166 | + |
| 167 | +router.get('/health', ({ res, ctx }) => { |
| 168 | + res.setJSON({ ok: true, version: ctx.version }); |
| 169 | +}); |
| 170 | + |
| 171 | +router.get('/users/:id', ({ req, res }) => { |
| 172 | + res.setJSON({ id: req.data('id') }); |
| 173 | +}); |
| 174 | +``` |
| 175 | + |
| 176 | +## Related |
| 177 | + |
| 178 | +- [Router](./Router.md) |
| 179 | +- [EntryRouter](./EntryRouter.md) |
| 180 | +- [ImportRouter](./ImportRouter.md) |
| 181 | +- [ViewRouter](./ViewRouter.md) |
0 commit comments