-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { RequestHandler } from '../../src/types' | ||
import { IRequestStrict } from '../../src/types' | ||
import { Router } from '../../src/Router' | ||
import { IttyRouter } from '../../src/IttyRouter' | ||
import { AutoRouter } from '../../src/AutoRouter' | ||
import { IRequest } from 'itty-router' | ||
|
||
// we define our environment | ||
type Environment = { age: number } | ||
type Pet = { name: string } | ||
|
||
// and now both args combined (that Workers send to the .fetch()) | ||
type Args = [Environment] | ||
type AlternativeArgs = [Pet] | ||
|
||
// creating some middleware that needs access to CF variables | ||
export const withUser: RequestHandler<IRequest, Args> = | ||
(request, env) => { | ||
request.user = 'Kevin' | ||
env.age = 123 | ||
env.name = 'Kevin' // invalid | ||
} | ||
|
||
const router = Router<IRequestStrict, Args>() | ||
|
||
router | ||
// before middleware | ||
.get('/', (request, env) => { | ||
request.user = 'kevin' // invalid (strict) | ||
env.whatever = 123 // valid (any) | ||
env.age = 123 // valid (any) | ||
}) | ||
|
||
// route-level overrides | ||
.get<IRequest, Args>('/', (request, env) => { | ||
request.foo = 'bar' // invalid | ||
env.whatever = 123 // invalid | ||
env.age = 123 // valid | ||
}) | ||
// route-level overrides | ||
.get<IRequest, AlternativeArgs>('/', (request, env) => { | ||
request.foo = 'bar' // invalid | ||
env.age = 123 // invalid | ||
env.name = 'Mittens' // valid | ||
}) | ||
|
||
// after middleware | ||
.get('/', withUser, (request, env) => { | ||
request.user = 'Kevin' | ||
env.age = 123 // valid | ||
env.whatever = 123 // invalid | ||
}) | ||
|
||
.get('/', (request, env) => { | ||
env.age = 'foo' // valid (any) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,37 @@ | ||
import { IRequestStrict, IRequest, RequestHandler } from '../../src/types' | ||
import { IttyRouter } from 'IttyRouter' | ||
import { IttyRouter } from '../../src/IttyRouter' | ||
|
||
type UserRequest = { | ||
user: string | ||
} & IRequestStrict | ||
|
||
// we define a *strict* router for this demo | ||
const router = IttyRouter<IRequestStrict>() | ||
|
||
const withUser: RequestHandler = (request) => { | ||
// middleware with explicit request-type (generic) | ||
const withUser: RequestHandler<UserRequest> = (request) => { | ||
request.user = 'Kevin' | ||
} | ||
|
||
router | ||
// upstream request sees the request as IRequest (default), so anything goes | ||
// request will be IRequestStrict here, thus no user property | ||
.get('/', (request) => { | ||
request.user = 123 // not OK | ||
request.user = 'Kevin' // invalid | ||
}) | ||
|
||
// then we add the middleware defined above as <UserRequest> | ||
// then we add the middleware defined above, allowing the handler chain to inherit the request type | ||
.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<UserRequest, []>('/', (request) => { | ||
request.user = 'Kevin' | ||
request.user = 123 // NOT ok | ||
}) | ||
|
||
// request will be back to IRequestStrict here, thus no user property | ||
.get('/', (request) => { | ||
request.user = 'Kevin' // still ok | ||
request.user = 123 // NOT ok | ||
request.user = 'Kevin' // invalid | ||
}) | ||
|
||
router.get('/', (request) => { | ||
request.user = 123 // NOT ok | ||
// and if we ever need to restore control, add the generic back in | ||
.get<UserRequest>('/', (request) => { | ||
request.user = 'Kevin' | ||
}) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters