-
-
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.
Add missing generics to Router stages (in options) (#241)
* checking in stuff * updated type sandbox * cleaned up global request type * updated CHANGELOG * released v5.0.13 - fixed types: router and autorouter stages should use router-level generics * linting
- Loading branch information
Showing
18 changed files
with
157 additions
and
58 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
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,33 @@ | ||
import { error } from 'console' | ||
import { Router } from '../../src/Router' | ||
import { withParams } from '../../src/withParams' | ||
import { IRequest, IRequestStrict, RequestHandler } from '../../src/types' | ||
|
||
// 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] | ||
|
||
const router = Router<IRequestStrict, Args, Response>({ | ||
before: [ | ||
(request, env) => { | ||
env.age = 123 // valid | ||
env.foo // invalid | ||
request.foo // invalid | ||
request.body // valid | ||
}, | ||
withParams, | ||
], | ||
catch: error, | ||
finally: [ | ||
(response, request, env) => { | ||
response.whatever // valid (any) | ||
env.age = 123 // valid | ||
env.foo // invalid | ||
request.foo // invalid | ||
}, | ||
] | ||
}) |
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,51 @@ | ||
import { IttyRouter } from '../../src/IttyRouter' | ||
import { IRequest, IRequestStrict, RequestHandler } from '../../src/types' | ||
|
||
// 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 = IttyRouter<IRequestStrict, Args>() | ||
|
||
router | ||
// before middleware | ||
.get('/', (request, env) => { | ||
request.user = 'kevin' // invalid (strict) | ||
env.age = 123 // valid | ||
}) | ||
|
||
// route-level overrides | ||
.get<IRequest, Args>('/', (request, env) => { | ||
request.foo = 'bar' // valid | ||
env.whatever = 123 // invalid | ||
env.age = 123 // valid | ||
}) | ||
// route-level overrides | ||
.get<IRequest, AlternativeArgs>('/', (request, env) => { | ||
request.foo = 'bar' // valid | ||
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' // invalid | ||
}) |
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
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
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
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,4 +1,6 @@ | ||
import { IRequest } from './IRequest' | ||
|
||
export type RequestHandler<R = IRequest, Args extends Array<any> = any[]> = | ||
(request: R, ...args: Args) => any | ||
export type RequestHandler< | ||
RequestType = IRequest, | ||
Args extends Array<any> = any[] | ||
> = (request: RequestType, ...args: Args) => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { IRequest } from './IRequest' | ||
|
||
export type ResponseHandler< | ||
ResponseType = Response, | ||
ResponseType = any, | ||
RequestType = IRequest, | ||
Args extends any[] = any[] | ||
> = ( | ||
response: ResponseType & any, | ||
request: RequestType & any, | ||
response: ResponseType, | ||
request: RequestType, | ||
...args: Args | ||
) => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { IRequest } from './IRequest' | ||
import { RequestHandler } from './RequestHandler' | ||
|
||
export type RouteEntry = [ | ||
export type RouteEntry<RequestType = IRequest, Args extends any[] = any[]> = [ | ||
httpMethod: string, | ||
match: RegExp, | ||
handlers: RequestHandler[], | ||
handlers: RequestHandler<RequestType, Args>[], | ||
path?: string, | ||
] |
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,10 +1,15 @@ | ||
import { StatusError } from 'StatusError' | ||
import { ErrorHandler } from './ErrorHandler' | ||
import { IRequest } from './IRequest' | ||
import { IttyRouterOptions } from './IttyRouterOptions' | ||
import { RequestHandler } from './RequestHandler' | ||
import { ResponseHandler } from './ResponseHandler' | ||
|
||
export type RouterOptions = { | ||
before?: RequestHandler<any>[] | ||
catch?: ErrorHandler | ||
finally?: ResponseHandler[] | ||
export type RouterOptions< | ||
RequestType = IRequest, | ||
Args extends any[] = [], | ||
> = { | ||
before?: RequestHandler<RequestType, Args>[] | ||
catch?: ErrorHandler<StatusError, RequestType, Args> | ||
finally?: ResponseHandler<any, RequestType, Args>[] | ||
} & IttyRouterOptions |
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,11 +1,16 @@ | ||
import { StatusError } from 'StatusError' | ||
import { ErrorHandler } from './ErrorHandler' | ||
import { IRequest } from './IRequest' | ||
import { IttyRouterType } from './IttyRouterType' | ||
import { RequestHandler } from './RequestHandler' | ||
import { ResponseHandler } from './ResponseHandler' | ||
|
||
export type RouterType<R = IRequest, Args extends any[] = any[], ResponseType = any> = { | ||
before?: RequestHandler<any>[] | ||
catch?: ErrorHandler | ||
finally?: ResponseHandler[] | ||
} & IttyRouterType<R, Args, ResponseType> | ||
export type RouterType< | ||
RequestType = IRequest, | ||
Args extends any[] = any[], | ||
ResponseType = any | ||
> = { | ||
before?: RequestHandler<RequestType, Args>[] | ||
catch?: ErrorHandler<StatusError, RequestType, Args> | ||
finally?: ResponseHandler<any, RequestType, Args>[] | ||
} & IttyRouterType<RequestType, Args, ResponseType> |