-
-
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
32 changed files
with
194 additions
and
116 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,22 @@ | ||
import { IRequestStrict, IRequest, RequestHandler } from 'types' | ||
import { IttyRouter } from 'IttyRouter' | ||
import { withContent } from 'withContent' | ||
import { HasContent } from 'types' | ||
|
||
type User = { | ||
user: string | ||
} | ||
|
||
const router = IttyRouter() | ||
|
||
router | ||
// upstream request sees the request as IRequest (default), so anything goes | ||
.post<HasContent<User>>('/', withContent, ({ content }) => { | ||
content.user | ||
}) | ||
|
||
.get('/pollution', (request) => { | ||
request.content.user | ||
}) | ||
|
||
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
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
// routers | ||
export * from './types/AutoRouterOptions' | ||
export * from './types/CustomRoutes' | ||
export * from './types/ErrorHandler' | ||
export * from './types/IRequest' | ||
export * from './types/IRequestStrict' | ||
export * from './types/IttyRouterOptions' | ||
export * from './types/IttyRouterType' | ||
export * from './types/RequestHandler' | ||
export * from './types/ResponseHandler' | ||
export * from './types/RequestLike' | ||
export * from './types/ResponseHandler' | ||
export * from './types/Route' | ||
export * from './types/RouteEntry' | ||
export * from './types/RouterOptions' | ||
export * from './types/RouterType' | ||
|
||
// withContent | ||
export * from './types/HasContent' | ||
|
||
// createResponse | ||
export * from './types/ResponseFormatter' | ||
|
||
// errors | ||
export * from './types/ErrorFormatter' | ||
|
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,8 @@ | ||
import { RequestHandler } from './RequestHandler' | ||
import { ResponseHandler } from './ResponseHandler' | ||
import { RouterOptions } from './RouterOptions' | ||
|
||
export type AutoRouterOptions = { | ||
missing?: RequestHandler | ||
format?: ResponseHandler | ||
} & RouterOptions |
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,5 @@ | ||
import { Route } from './Route' | ||
|
||
export type CustomRoutes<R = Route> = { | ||
[key: string]: R | ||
} |
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,11 @@ | ||
interface ErrorLike extends Error { | ||
status?: number | ||
[any: string]: any | ||
} | ||
|
||
type ErrorBody = string | object | ||
|
||
export interface ErrorFormatter { | ||
(statusCode?: number, body?: ErrorBody): Response | ||
(error: ErrorLike): Response | ||
} |
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,4 @@ | ||
import { IRequest } from './IRequest' | ||
|
||
export type ErrorHandler<ErrorType = Error, RequestType = IRequest, Args extends any[] = any[]> = | ||
(response: ErrorType, 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export type GenericTraps = Record<string, 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { IRequestStrict } from './IRequestStrict' | ||
|
||
export type HasContent<ContentType> = { | ||
content: ContentType | ||
} & IRequestStrict |
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,4 @@ | ||
import { GenericTraps } from './GenericTraps' | ||
import { IRequestStrict } from './IRequestStrict' | ||
|
||
export type IRequest = IRequestStrict & GenericTraps |
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,12 @@ | ||
export type IRequestStrict = { | ||
method: string | ||
url: string | ||
route: string | ||
params: { | ||
[key: string]: string | ||
} | ||
query: { | ||
[key: string]: string | string[] | undefined | ||
} | ||
proxy?: any | ||
} & Request |
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,6 @@ | ||
import { RouteEntry } from './RouteEntry' | ||
|
||
export type IttyRouterOptions = { | ||
base?: string | ||
routes?: RouteEntry[] | ||
} & Record<string, 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { CustomRoutes } from './CustomRoutes' | ||
import { IRequest } from './IRequest' | ||
import { RequestLike } from './RequestLike' | ||
import { Route } from './Route' | ||
import { RouteEntry } from './RouteEntry' | ||
|
||
export type IttyRouterType<R = IRequest, A extends any[] = any[], Output = any> = { | ||
__proto__: IttyRouterType<R> | ||
routes: RouteEntry[] | ||
fetch: <Args extends any[] = A>(request: RequestLike, ...extra: Args) => Promise<Output> | ||
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>> |
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,4 @@ | ||
import { IRequest } from './IRequest' | ||
|
||
export type RequestHandler<R = IRequest, Args extends Array<any> = any[]> = | ||
(request: R, ...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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { GenericTraps } from './GenericTraps' | ||
|
||
export type RequestLike = { | ||
method: string | ||
url: string | ||
} & GenericTraps |
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,3 @@ | ||
export interface ResponseFormatter { | ||
(body?: any, options?: ResponseInit): Response | ||
} |
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,4 @@ | ||
import { IRequest } from './IRequest' | ||
|
||
export type ResponseHandler<ResponseType = Response, RequestType = IRequest, Args extends any[] = any[]> = | ||
(response: ResponseType & any, request: RequestType & any, ...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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IRequest } from './IRequest' | ||
import { IttyRouterType } from './IttyRouterType' | ||
import { RequestHandler } from './RequestHandler' | ||
|
||
export 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> |
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,8 @@ | ||
import { RequestHandler } from './RequestHandler' | ||
|
||
export type RouteEntry = [ | ||
httpMethod: string, | ||
match: RegExp, | ||
handlers: RequestHandler[], | ||
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ErrorHandler } from './ErrorHandler' | ||
import { IttyRouterOptions } from './IttyRouterOptions' | ||
import { RequestHandler } from './RequestHandler' | ||
import { ResponseHandler } from './ResponseHandler' | ||
|
||
export type RouterOptions = { | ||
before?: RequestHandler<any>[] | ||
catch?: ErrorHandler | ||
finally?: ResponseHandler[] | ||
} & 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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[]> = { | ||
before?: RequestHandler<any>[] | ||
catch?: ErrorHandler | ||
finally?: ResponseHandler[] | ||
} & IttyRouterType<R, Args> |
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,4 @@ | ||
import { IRequest } from './IttyRouter' | ||
import { IRequest } from './types' | ||
|
||
type KVPair = [string, 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
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