@koa/router 15.0.0 → 15.1.0
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 +33 -41
- package/dist/index.d.mts +843 -3
- package/dist/index.d.ts +843 -3
- package/dist/index.js +132 -60
- package/dist/index.mjs +128 -60
- package/package.json +26 -10
- package/dist/layer.d.mts +0 -761
- package/dist/layer.d.ts +0 -761
- package/dist/layer.js +0 -457
- package/dist/layer.mjs +0 -436
- package/dist/router.d.mts +0 -3
- package/dist/router.d.ts +0 -3
- package/dist/router.js +0 -1371
- package/dist/router.mjs +0 -1340
- package/dist/types.d.mts +0 -3
- package/dist/types.d.ts +0 -3
- package/dist/types.js +0 -18
- package/dist/types.mjs +0 -0
package/README.md
CHANGED
|
@@ -66,14 +66,38 @@ yarn add @koa/router
|
|
|
66
66
|
|
|
67
67
|
### Basic Usage
|
|
68
68
|
|
|
69
|
+
Types are **automatically inferred** - no explicit type annotations needed:
|
|
70
|
+
|
|
69
71
|
```typescript
|
|
70
|
-
import Router
|
|
72
|
+
import Router from '@koa/router';
|
|
71
73
|
|
|
72
74
|
const router = new Router();
|
|
73
75
|
|
|
74
|
-
//
|
|
75
|
-
router.get('/:id', (ctx
|
|
76
|
-
const id = ctx.params.id; //
|
|
76
|
+
// ctx and next are automatically inferred!
|
|
77
|
+
router.get('/:id', (ctx, next) => {
|
|
78
|
+
const id = ctx.params.id; // ✅ Inferred as string
|
|
79
|
+
ctx.request.params.id; // ✅ Also available
|
|
80
|
+
ctx.body = { id }; // ✅ Works
|
|
81
|
+
return next(); // ✅ Works
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Also works for router.use()
|
|
85
|
+
router.use((ctx, next) => {
|
|
86
|
+
ctx.state.startTime = Date.now();
|
|
87
|
+
return next();
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Explicit Types (Optional)
|
|
92
|
+
|
|
93
|
+
For cases where you need explicit types:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import Router, { RouterContext } from '@koa/router';
|
|
97
|
+
import type { Next } from 'koa';
|
|
98
|
+
|
|
99
|
+
router.get('/:id', (ctx: RouterContext, next: Next) => {
|
|
100
|
+
const id = ctx.params.id;
|
|
77
101
|
ctx.body = { id };
|
|
78
102
|
});
|
|
79
103
|
```
|
|
@@ -194,11 +218,13 @@ type MyParamMiddleware = RouterParameterMiddleware<
|
|
|
194
218
|
|
|
195
219
|
### Type Safety Features
|
|
196
220
|
|
|
221
|
+
- ✅ **Full type inference** - `ctx` and `next` are inferred automatically in route handlers
|
|
197
222
|
- ✅ **Full generic support** - `Router<StateT, ContextT>` for custom state and context types
|
|
198
|
-
- ✅ **Type-safe parameters** - `ctx.params` is fully typed
|
|
223
|
+
- ✅ **Type-safe parameters** - `ctx.params` is fully typed and always defined
|
|
199
224
|
- ✅ **Type-safe state** - `ctx.state` respects your state type
|
|
200
225
|
- ✅ **Type-safe middleware** - Middleware functions are fully typed
|
|
201
226
|
- ✅ **Type-safe HTTP methods** - Methods support generic type extensions
|
|
227
|
+
- ✅ **Custom HTTP method inference** - Use `as const` with `methods` option for typed custom methods
|
|
202
228
|
- ✅ **Compatible with @types/koa-router** - Matches official type structure
|
|
203
229
|
|
|
204
230
|
## Quick Start
|
|
@@ -318,42 +344,6 @@ apiRouter.post('/users', createUser);
|
|
|
318
344
|
// router.purge() won't work here because PURGE is not in the methods array
|
|
319
345
|
```
|
|
320
346
|
|
|
321
|
-
**Using Less Common HTTP Methods:**
|
|
322
|
-
|
|
323
|
-
All standard HTTP methods from Node.js are automatically available. Here's an example using `PATCH` and `PURGE`:
|
|
324
|
-
|
|
325
|
-
```javascript
|
|
326
|
-
const router = new Router();
|
|
327
|
-
|
|
328
|
-
// PATCH method (standard HTTP method for partial updates)
|
|
329
|
-
router.patch('/users/:id', async (ctx) => {
|
|
330
|
-
// Partial update
|
|
331
|
-
ctx.body = { message: 'User partially updated' };
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
// PURGE method (standard HTTP method, commonly used for cache invalidation)
|
|
335
|
-
router.purge('/cache/:key', async (ctx) => {
|
|
336
|
-
// Clear cache
|
|
337
|
-
await clearCache(ctx.params.key);
|
|
338
|
-
ctx.body = { message: 'Cache cleared' };
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
// COPY method (standard HTTP method)
|
|
342
|
-
router.copy('/files/:source', async (ctx) => {
|
|
343
|
-
await copyFile(ctx.params.source, ctx.request.body.destination);
|
|
344
|
-
ctx.body = { message: 'File copied' };
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
// Limiting which methods the router responds to
|
|
348
|
-
const apiRouter = new Router({
|
|
349
|
-
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'] // Only these methods
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
apiRouter.get('/users', getUsers);
|
|
353
|
-
apiRouter.post('/users', createUser);
|
|
354
|
-
// router.purge() won't work here because PURGE is not in the methods array
|
|
355
|
-
```
|
|
356
|
-
|
|
357
347
|
**Note:** HEAD requests are automatically supported for all GET routes. When you define a GET route, HEAD requests will execute the same handler and return the same headers but with an empty body.
|
|
358
348
|
|
|
359
349
|
### Named Routes
|
|
@@ -1109,6 +1099,8 @@ describe('Router', () => {
|
|
|
1109
1099
|
|
|
1110
1100
|
## Migration Guides
|
|
1111
1101
|
|
|
1102
|
+
For detailed migration information, see **[FULL_MIGRATION_TO_V15+.md](./FULL_MIGRATION_TO_V15+.md)**.
|
|
1103
|
+
|
|
1112
1104
|
**Breaking Changes:**
|
|
1113
1105
|
|
|
1114
1106
|
- Custom regex patterns in parameters (`:param(regex)`) are **no longer supported** due to path-to-regexp v8. Use validation in handlers or middleware instead.
|