Skip to content

Commit

Permalink
update links in README to v5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Mar 29, 2024
1 parent edc6221 commit c266537
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<img src="https://edge.bundlejs.com/?q=itty-router/Router&badge&badge-style=flat-square" alt="bundle size" />
</a>
<a href="https://github.com/kwhitley/itty-router/actions/workflows/verify.yml" target="_blank">
<img src="https://img.shields.io/github/actions/workflow/status/kwhitley/itty-router/verify.yml?branch=v4.x&style=flat-square" alt="build status" />
<img src="https://img.shields.io/github/actions/workflow/status/kwhitley/itty-router/verify.yml?branch=v5.x&style=flat-square" alt="build status" />
</a>
<a href="https://coveralls.io/github/kwhitley/itty-router?branch=v4.x" target="_blank">
<img src="https://img.shields.io/coveralls/github/kwhitley/itty-router/v4.x?style=flat-square" alt="code coverage" />
<a href="https://coveralls.io/github/kwhitley/itty-router?branch=v5.x" target="_blank">
<img src="https://img.shields.io/coveralls/github/kwhitley/itty-router/v5.x?style=flat-square" alt="code coverage" />
</a>
<a href="https://npmjs.com/package/itty-router" target="_blank">
<img src="https://img.shields.io/npm/dw/itty-router?style=flat-square" alt="weekly downloads" />
Expand Down Expand Up @@ -47,19 +47,16 @@

An ultra-tiny API microrouter, for use when [size matters](https://github.com/TigersWay/cloudflare-playground) (e.g. [Cloudflare Workers](https://developers.cloudflare.com/workers/)).




## Features

- Tiny. Routers from [~450 bytes](https://itty.dev/itty-router/routers/ittyrouter) to a [~970 bytes](https://itty.dev/itty-router/routers/autorouter) batteries-included version (~240-500x smaller than Express.js).
- [TypeScript](https://itty.dev/itty-router/typescript). Powerfully (and flexibly) typed for any environment.
- [Route-parsing](https://itty.dev/itty-router/route-patterns) & [query parsing](https://itty.dev/itty-router/route-patterns#query).
- [Middleware](https://itty.dev/itty-router/middleware). Use ours or write your own.
- [100% Test Coverage](https://coveralls.io/github/kwhitley/itty-router?branch=v5.x). Bulletproof for production peace-of-mind.
- Web Standards. Use it [anywhere, in any environment](https://itty.dev/itty-router/runtimes).
- No assumptions. Return anything; pass in anything.
- Dead-simple user-code. We want _your_ code to be tiny too.
- Future-proof. HTTP methods not-yet-invented already work with it.
- [Route-parsing](https://itty.dev/itty-router/route-patterns) & [query parsing](https://itty.dev/itty-router/route-patterns#query).
- [Middleware](https://itty.dev/itty-router/middleware) - use ours or write your own.
- [Supports Nesting](https://itty.dev/itty-router/nesting).

## Example

Expand Down
26 changes: 15 additions & 11 deletions example/types/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,37 @@ type UserRequest = {
user: string
} & IRequestStrict

const router = IttyRouter()
const router = IttyRouter<IRequestStrict>()

const withUser: RequestHandler<UserRequest> = (request) => {
const withUser: RequestHandler = (request) => {
request.user = 'Kevin'
}

router
// upstream request sees the request as IRequest (default), so anything goes
.get('/', (request) => {
request.user = 123 // allowed because IRequest
request.user = 123 // not OK
})

// then we add the middleware defined above as <UserRequest>
.all('*', withUser)

// and now downstream requests expect a UserRequest
.get('/', (request) => {
request.user = 123 // NOT VALID
.all('*', withUser, (request) => {
request.user = 'Kevin'
request.user = 123 // NOT ok
})

// and if we ever need to restore control, add the generic back in
.get<IRequest>('/', (request) => {
request.user = 123 // now this is ok
.get<UserRequest, []>('/', (request) => {
request.user = 'Kevin'
request.user = 123 // NOT ok
})

.get('/', (request) => {
request.user = 123 // and so is this
request.user = 'Kevin' // still ok
request.user = 123 // NOT ok
})

router.get('/', (request) => {
request.user = 123 // NOT ok
})

export default router
4 changes: 2 additions & 2 deletions src/types/IttyRouterType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { Route } from './Route'
import { RouteEntry } from './RouteEntry'
import { CustomRoutes } from './CustomRoutes'

export type IttyRouterType<R = IRequest, A extends any[] = any[], Output = any> = {
export type IttyRouterType<R = IRequest, A extends any[] = any[], ResponseType = any> = {
__proto__: IttyRouterType<R>
routes: RouteEntry[]
fetch: <Args extends any[] = A>(request: RequestLike, ...extra: Args) => Promise<Output>
fetch: <Args extends any[] = A>(request: RequestLike, ...extra: Args) => Promise<ResponseType>
all: Route<R, A>
delete: Route<R, A>
get: Route<R, A>
Expand Down
4 changes: 2 additions & 2 deletions src/types/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { RequestHandler } from './RequestHandler'

export type Route<
R = IRequest,
A extends Array<any> = any[]
A extends Array<any> = any[],
> = <
RequestType = R,
Args extends Array<any> = A
Args extends Array<any> = A,
>(
path: string,
...handlers: RequestHandler<RequestType, Args>[]
Expand Down
4 changes: 2 additions & 2 deletions src/types/RouterType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { IttyRouterType } from './IttyRouterType'
import { RequestHandler } from './RequestHandler'
import { ResponseHandler } from './ResponseHandler'

export type RouterType<R = IRequest, Args extends any[] = any[]> = {
export type RouterType<R = IRequest, Args extends any[] = any[], ResponseType = any> = {
before?: RequestHandler<any>[]
catch?: ErrorHandler
finally?: ResponseHandler[]
} & IttyRouterType<R, Args>
} & IttyRouterType<R, Args, ResponseType>

0 comments on commit c266537

Please sign in to comment.