itty-router 2.4.10 → 2.5.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/CHANGELOG.md +2 -0
- package/README.md +105 -5
- package/dist/itty-router.d.ts +18 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
## Changelog
|
|
2
2
|
Until this library makes it to a production release of v1.x, **minor versions may contain breaking changes to the API**. After v1.x, semantic versioning will be honored, and breaking changes will only occur under the umbrella of a major version bump.
|
|
3
3
|
|
|
4
|
+
[@SupremeTechnopriest)(https://github.com/SupremeTechnopriest) - improved TypeScript support and documentation! :D
|
|
5
|
+
- **v2.5.0** - improved TypeScript docs/types (thanks [@SupremeTechnopriest](https://github.com/SupremeTechnopriest)!)
|
|
4
6
|
- **v2.4.9** - fixed the cursed "optional" file format capturing bug - RIP all the bytes lost
|
|
5
7
|
- **v2.4.6** - fixed README issues
|
|
6
8
|
- **v2.4.1** - fixed type errors introduced with 2.4.0
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[![npm package][npm-image]][npm-url]
|
|
4
4
|
[![minified + gzipped size][gzip-image]][gzip-url]
|
|
5
|
-
|
|
5
|
+

|
|
6
6
|
[![Coverage Status][coveralls-image]][coveralls-url]
|
|
7
7
|
[![Open Issues][issues-image]][issues-url]
|
|
8
8
|
<a href="https://npmjs.com/package/itty-router" target="\_parent">
|
|
@@ -19,14 +19,15 @@
|
|
|
19
19
|
<img alt="Join the discussion on Github" src="https://img.shields.io/badge/Github%20Discussions%20%26%20Support-Chat%20now!-blue" />
|
|
20
20
|
</a>-->
|
|
21
21
|
|
|
22
|
-
It's an itty bitty router, designed for express-like routing within [Cloudflare Workers](https://developers.cloudflare.com/workers/) (or anywhere else). Like... it's super tiny ([~
|
|
22
|
+
It's an itty bitty router, designed for express-like routing within [Cloudflare Workers](https://developers.cloudflare.com/workers/) (or anywhere else). Like... it's super tiny ([~500 bytes](https://bundlephobia.com/package/itty-router)), with zero dependencies. For reals.
|
|
23
23
|
|
|
24
24
|
### Addons & Related Libraries
|
|
25
25
|
1. [itty-router-extras](https://www.npmjs.com/package/itty-router-extras) - adds quality-of-life improvements and utility functions for simplifying request/response code (e.g. middleware, cookies, body parsing, json handling, errors, and an itty version with automatic exception handling)!
|
|
26
26
|
2. [itty-durable](https://github.com/kwhitley/itty-durable) - (EXPERIMENTAL/alpha) creates a more direct object-like API for interacting with [Cloudflare Durable Objects](https://developers.cloudflare.com/workers/learning/using-durable-objects).
|
|
27
27
|
|
|
28
28
|
## Features
|
|
29
|
-
- [x] Tiny ([~
|
|
29
|
+
- [x] Tiny ([~500 bytes](https://bundlephobia.com/package/itty-router) compressed), with zero dependencies.
|
|
30
|
+
- [x] [TypeScript support](#typescript)!
|
|
30
31
|
- [x] Full sync/async support. Use it when you need it!
|
|
31
32
|
- [x] Route params, with wildcards and optionals (e.g. `/api/:collection/:id?`)
|
|
32
33
|
- [x] Query parsing (e.g. `?page=3&foo=bar`)
|
|
@@ -377,6 +378,106 @@ router.get('/test', () => new Response('You can still define routes normally as
|
|
|
377
378
|
await router.handle({ method: 'GET', url: 'https:nowhere.com/custom-a123' }) // { id: "a123" }
|
|
378
379
|
```
|
|
379
380
|
|
|
381
|
+
### Typescript
|
|
382
|
+
|
|
383
|
+
For Typescript projects, the Router can be adorned with two generics: A custom request interface and a custom methods interface.
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
import { Router, Route, Request } from 'itty-router'
|
|
387
|
+
|
|
388
|
+
type MethodType = 'GET' | 'POST' | 'PUPPY'
|
|
389
|
+
|
|
390
|
+
interface IRequest extends Request {
|
|
391
|
+
method: MethodType // method is required to be on the interface
|
|
392
|
+
url: string // url is required to be on the interface
|
|
393
|
+
optional?: string
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
interface IMethods {
|
|
397
|
+
get: Route
|
|
398
|
+
post: Route
|
|
399
|
+
puppy: Route
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const router = Router<IRequest, IMethods>()
|
|
403
|
+
|
|
404
|
+
router.get('/', (request: IRequest) => {})
|
|
405
|
+
router.post('/', (request: IRequest) => {})
|
|
406
|
+
router.puppy('/', (request: IRequest) => {})
|
|
407
|
+
|
|
408
|
+
addEventListener('fetch', (event: FetchEvent) => {
|
|
409
|
+
event.respondWith(router.handle(event.request))
|
|
410
|
+
})
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
Both generics are optional. `TRequest` defaults to `Request` and `TMethods` defaults to `{}`.
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
import { Router, Route } from 'itty-router'
|
|
417
|
+
|
|
418
|
+
type MethodType = 'GET' | 'POST' | 'PUPPY'
|
|
419
|
+
|
|
420
|
+
interface IRequest extends Request {
|
|
421
|
+
method: MethodType
|
|
422
|
+
url: string
|
|
423
|
+
optional?: string
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
interface IMethods {
|
|
427
|
+
get: Route
|
|
428
|
+
post: Route
|
|
429
|
+
puppy: Route
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const router = Router() // Valid
|
|
433
|
+
const router = Router<IRequest>() // Valid
|
|
434
|
+
const router = Router<Request, IMethods>() // Valid
|
|
435
|
+
const router = Router<void, IMethods>() // Valid
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
The router will also accept any string as a method, not just those provided on the `TMethods` type.
|
|
439
|
+
|
|
440
|
+
```ts
|
|
441
|
+
import { Router, Route } from 'itty-router'
|
|
442
|
+
|
|
443
|
+
interface IMethods {
|
|
444
|
+
get: Route
|
|
445
|
+
post: Route
|
|
446
|
+
puppy: Route
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const router = Router<void, IMethods>()
|
|
450
|
+
|
|
451
|
+
router.puppy('/', request => {}) // Valid
|
|
452
|
+
router.kitten('/', request => {}) // Also Valid
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
The `itty-router` package also exports an interface containing all of the HTTP methods.
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
import { Router, Route, IHTTPMethods } from 'itty-router'
|
|
459
|
+
|
|
460
|
+
const router = Router<void, IHTTPMethods>()
|
|
461
|
+
|
|
462
|
+
router.get('/', request => {}) // Exposed via IHTTPMethods
|
|
463
|
+
router.puppy('/', request => {}) // Valid but not strongly typed
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
You can also extend `IHTTPMethods` with your own custom methods so they will be strongly typed.
|
|
467
|
+
|
|
468
|
+
```ts
|
|
469
|
+
import { Router, Route, IHTTPMethods } from 'itty-router'
|
|
470
|
+
|
|
471
|
+
interface IMethods extends IHTTPMethods {
|
|
472
|
+
puppy: Route
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const router = Router<void, IMethods>()
|
|
476
|
+
|
|
477
|
+
router.get('/', request => {}) // Exposed via IHTTPMethods
|
|
478
|
+
router.puppy('/', request => {}) // Strongly typed
|
|
479
|
+
```
|
|
480
|
+
|
|
380
481
|
## Testing and Contributing
|
|
381
482
|
1. Fork repo
|
|
382
483
|
1. Install dev dependencies via `yarn`
|
|
@@ -436,8 +537,6 @@ This trick allows methods (e.g. "get", "post") to by defined dynamically by the
|
|
|
436
537
|
[issues-url]:https://github.com/kwhitley/itty-router/issues
|
|
437
538
|
[npm-image]:https://img.shields.io/npm/v/itty-router.svg
|
|
438
539
|
[npm-url]:http://npmjs.org/package/itty-router
|
|
439
|
-
[travis-image]:https://app.travis-ci.com/kwhitley/itty-router.svg?branch=v2.x
|
|
440
|
-
[travis-url]:https://travis-ci.org/kwhitley/itty-router
|
|
441
540
|
[david-image]:https://david-dm.org/kwhitley/itty-router/status.svg
|
|
442
541
|
[david-url]:https://david-dm.org/kwhitley/itty-router
|
|
443
542
|
[coveralls-image]:https://coveralls.io/repos/github/kwhitley/itty-router/badge.svg?branch=v2.x
|
|
@@ -453,6 +552,7 @@ These folks are the real heroes, making open source the powerhouse that it is!
|
|
|
453
552
|
- [@mvasigh](https://github.com/mvasigh) - proxy hack wizard behind itty, coding partner in crime, maker of the entire doc site, etc, etc.
|
|
454
553
|
- [@taralx](https://github.com/taralx) - router internal code-golfing refactor for performance and character savings
|
|
455
554
|
- [@hunterloftis](https://github.com/hunterloftis) - router.handle() method now accepts extra arguments and passed them to route functions
|
|
555
|
+
- [@SupremeTechnopriest](https://github.com/SupremeTechnopriest) - improved TypeScript support and documentation! :D
|
|
456
556
|
#### Fixes
|
|
457
557
|
- [@taralx](https://github.com/taralx) - QOL fixes for contributing (dev dep fix and test file consistency) <3
|
|
458
558
|
- [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
|
package/dist/itty-router.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type Obj = {
|
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export interface RouteHandler<TRequest> {
|
|
6
|
-
(request: TRequest
|
|
6
|
+
(request: TRequest, ...args: any): any
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface Route {
|
|
@@ -29,10 +29,23 @@ export interface Request {
|
|
|
29
29
|
text?(): Promise<any>
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
export
|
|
33
|
-
|
|
32
|
+
export interface IHTTPMethods {
|
|
33
|
+
get: Route
|
|
34
|
+
head: Route
|
|
35
|
+
post: Route
|
|
36
|
+
put: Route
|
|
37
|
+
delete: Route
|
|
38
|
+
connect: Route
|
|
39
|
+
options: Route
|
|
40
|
+
trace: Route
|
|
41
|
+
patch: Route
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type Router<TRequest = Request, TMethods = {}> = {
|
|
45
|
+
handle: (request: TRequest, ...extra: any) => any
|
|
34
46
|
routes: RouteEntry<TRequest>[]
|
|
35
|
-
|
|
47
|
+
all: Route
|
|
48
|
+
} & TMethods & {
|
|
36
49
|
[any:string]: Route
|
|
37
50
|
}
|
|
38
51
|
|
|
@@ -41,4 +54,4 @@ export interface RouterOptions<TRequest> {
|
|
|
41
54
|
routes?: RouteEntry<TRequest>[]
|
|
42
55
|
}
|
|
43
56
|
|
|
44
|
-
export function Router<TRequest>(options?:RouterOptions<TRequest>): Router<TRequest>
|
|
57
|
+
export function Router<TRequest = Request, TMethods = {}>(options?:RouterOptions<TRequest>): Router<TRequest, TMethods>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itty-router",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "Tiny, zero-dependency router with route param and query parsing - built for Cloudflare Workers, but works everywhere!",
|
|
5
5
|
"main": "./dist/itty-router.min.js",
|
|
6
6
|
"types": "./dist/itty-router.d.ts",
|