-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
5 changed files
with
106 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IRequestStrict } from 'IttyRouter' | ||
import { Router } from 'Router' | ||
|
||
const router = Router<IRequestStrict>() | ||
|
||
type FooRequest = { | ||
foo: string | ||
} & IRequestStrict | ||
|
||
router | ||
.get('/basic', () => new Response('Success!')) | ||
.get('/text', () => 'Success!') | ||
// .get('/params/:foo', ({ foo }) => foo) // should NOT work | ||
.get<FooRequest>('/params/:foo', ({ foo }) => foo) // should work | ||
.get('/json', () => ({ foo: 'bar' })) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IRequestStrict } from 'IttyRouter' | ||
import { Router } from 'Router' | ||
|
||
type FooRequest = { | ||
foo: string | ||
} & IRequestStrict | ||
|
||
const router = Router<FooRequest>() | ||
|
||
router | ||
.get('/', (request) => { | ||
request.foo // should work | ||
// request.bar // should NOT work | ||
}) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { IRequestStrict, IRequest, IttyRouter, RequestHandler } from 'IttyRouter' | ||
|
||
type UserRequest = { | ||
user: string | ||
} & IRequestStrict | ||
|
||
const router = IttyRouter() | ||
|
||
const withUser: RequestHandler<UserRequest> = (request) => { | ||
request.user = 'Kevin' | ||
} | ||
|
||
router | ||
// upstream request sees the request as IRequest (default), so anything goes | ||
.get('/', (request) => { | ||
request.user = 123 // allowed because IRequest | ||
}) | ||
|
||
// 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 | ||
}) | ||
|
||
// and if we ever need to restore control, add the generic back in | ||
.get<IRequest>('/', (request) => { | ||
request.user = 123 // now this is ok | ||
}) | ||
|
||
.get('/', (request) => { | ||
request.user = 123 // and so is this | ||
}) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IRequestStrict } from 'IttyRouter' | ||
import { Router } from 'Router' | ||
|
||
type FooRequest = { | ||
foo: string | ||
} & IRequestStrict | ||
|
||
const router = Router() | ||
|
||
router | ||
.get<FooRequest>('/', (request) => { | ||
request.foo // should work | ||
// request.bar // should NOT work | ||
}) | ||
|
||
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