-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
37 changed files
with
256 additions
and
135 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IRequestStrict, IRequest, RequestHandler } from 'types' | ||
import { IttyRouter } from 'IttyRouter' | ||
import { withContent } from 'withContent' | ||
import { HasContent } from 'types' | ||
|
||
const router = IttyRouter({ | ||
port: 3000, | ||
}) | ||
|
||
router.port = 4000 | ||
router.port.toLowerCase() | ||
router.puppy() // should not be typed... RIP | ||
|
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,11 @@ | ||
import { IttyRouter } from 'IttyRouter' | ||
|
||
const router = IttyRouter() | ||
|
||
const request = new Request('https://foo.bar') | ||
|
||
router.fetch(request) // should be OK | ||
router.fetch({ method: 'GET', url: 'foo' }) // should be OK | ||
router.fetch({ method: 'GET' }) // should NOT be OK | ||
|
||
|
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 { Router } from 'Router' | ||
|
||
const router = Router({ | ||
port: 4000, | ||
before: [], | ||
}) | ||
|
||
router.finally = () => {} // should NOT work |
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 |
---|---|---|
|
@@ -26,3 +26,6 @@ export * from './withParams' | |
|
||
// CORS | ||
export * from './cors' | ||
|
||
// TYPES | ||
export * from './types' |
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,7 @@ | ||
import { StatusError } from '../StatusError' | ||
import { IRequest } from './IRequest' | ||
|
||
export type ErrorHandler< | ||
ErrorType extends Error = StatusError, | ||
RequestType = IRequest, Args extends any[] = any[] | ||
> = (error: 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,10 @@ | ||
export type IRequestStrict = { | ||
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,7 @@ | ||
import { GenericTraps } from './GenericTraps' | ||
import { RouteEntry } from './RouteEntry' | ||
|
||
export type IttyRouterOptions = { | ||
base?: string | ||
routes?: RouteEntry[] | ||
} & GenericTraps |
Oops, something went wrong.