TypeScript
TypeScript API - itty-router
AutoRouterOptions
Options for AutoRouter
type AutoRouterOptions = {
missing?: RequestHandler
format?: ResponseHandler
} & RouterOptionsErrorHandler
An advanced error handler, used as the catch stage in AutoRouter and Router. Unlike a normal error handler attached to the .catch(err) block of a Promise chain, this one has access to the original Error as well as the Request (and other args) passed to the .fetch() of the router. This allows for controlled logging of thrown errors.
type ErrorHandler<
ErrorType extends Error = StatusError,
RequestType = IRequest, Args extends any[] = any[]
> = (error: ErrorType, request: RequestType, ...args: Args) => anyGenericTraps internal
Utility type to allow for unspecified attributes on any object. Used in IRequest.
type GenericTraps = Record<string, any>HasContent
Utility type for extending Request types when expecting POST/PATCH/etc. content after using withContent.
type HasContent<ContentType> = {
content: ContentType
} & IRequestStrictIRequest
IRequest is the default request type in itty, and is the union of IRequestStrict and GenericTraps. As a result, typical requests have access to both native Request attributes, as well as anything else you may store there.
type IRequest = IRequestStrict & GenericTrapsIRequestStrict
IRequestStrict strictly extends Request with the known attributes that itty embeds, such as route, params, and query. Use this instead of IRequest for stricter typings in your API.
type IRequestStrict = {
route: string
params: {
[key: string]: string
}
query: {
[key: string]: string | string[] | undefined
}
proxy?: any
} & RequestIttyRouterOptions
Options for IttyRouter. The generic traps are used to allow any instantiated router to accept unknown properties (which will remain on the route itself). With this, you can export the router itself, while adding expected properites from your runtime (e.g. { port: 3000 } and such).
type IttyRouterOptions = {
base?: string
routes?: RouteEntry[]
} & GenericTrapsIttyRouterType internal
Hopefully you'll never need to use this.
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<ResponseType>
all: Route<R, A>
delete: Route<R, A>
get: Route<R, A>
head: Route<R, A>
options: Route<R, A>
patch: Route<R, A>
post: Route<R, A>
put: Route<R, A>
} & CustomRoutes<Route<R, A>> & GenericTrapsRequestHandler
A generic request handler, as used in route definitions, middleware, and the before stage of AutoRouter and Router.
type RequestHandler<R = IRequest, Args extends Array<any> = any[]> =
(request: R, ...args: Args) => anyRequestLike internal
The bare minimum object type for use in the router's .fetch() method.
export type RequestLike = {
method: string
url: string
} & GenericTrapsResponseFormatter internal
Used to format content into a valid Response object within createResponse().
export type ResponseFormatter =
(body?: any, options?: ResponseInit) => ResponseResponseHandler
This is for downstream handlers in the finally stage of AutoRouter and Router. Each ResponseHandler has access to a response, a request, and any additional arguments provided to the router's .fetch() method.
type ResponseHandler<
ResponseType = Response,
RequestType = IRequest,
Args extends any[] = any[]
> = (
response: ResponseType & any,
request: RequestType & any,
...args: Args
) => anyRoute internal
This allows you to overwrite request/args using generics at the route-level. Bonus points if you can follow this.
type Route<
R = IRequest,
A extends Array<any> = any[],
> = <
RequestType = R,
Args extends Array<any> = A,
>(
path: string,
...handlers: RequestHandler<RequestType, Args>[]
) => IttyRouterType<RequestType, Args>RouteEntry advanced
If you plan to manually modify the .routes collection on a router manually, this is the format you'll need for each entry.
type RouteEntry = [
httpMethod: string,
match: RegExp,
handlers: RequestHandler[],
path?: string,
]RouterOptions
Options for Router. This adds a before, catch, and finally stage to IttyRouterOptions.
type RouterOptions = {
before?: RequestHandler<any>[]
catch?: ErrorHandler
finally?: ResponseHandler[]
} & IttyRouterOptions
RequestHandler,ErrorHandler,ResponseHandler,IttyRouterOptions
RouterType
Type for Router. This adds a before, catch, and finally stage to IttyRouterType.
type RouterType<R = IRequest, Args extends any[] = any[], ResponseType = any> = {
before?: RequestHandler<any>[]
catch?: ErrorHandler
finally?: ResponseHandler[]
} & IttyRouterType<R, Args, ResponseType>
RequestHandler,ErrorHandler,ResponseHandler,IttyRouterType
