Skip to content

Commit

Permalink
fixed some missing types in 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Mar 29, 2024
1 parent 89f5dd7 commit 2736c51
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 9 deletions.
24 changes: 24 additions & 0 deletions example/router-matchup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AutoRouter } from 'AutoRouter'
import { IttyRouter } from 'IttyRouter'
import { Router } from 'Router'
import { IRequest } from 'types'

// setup
type FooResponse = { foo: string }
const request = new Request('https://foo.bar')

// test
const autorouter = AutoRouter<IRequest, [], FooResponse>()
const router = Router<IRequest, [], FooResponse>()
const ittyrouter = IttyRouter<IRequest, [], FooResponse>()

// test getters
autorouter.get('/throw', (a) => a.b.c)
router.get('/throw', (a) => a.b.c)
ittyrouter.get('/throw', (a) => a.b.c)

// test response formatting
const autorouterResponse = (await autorouter.fetch(request)).foo
const routerResponse = (await router.fetch(request)).foo
const ittyrouterResponse = (await ittyrouter.fetch(request)).foo

11 changes: 8 additions & 3 deletions src/AutoRouter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { Router } from './Router'
import { error } from './error'
import { json } from './json'
import { AutoRouterOptions } from './types'
import { AutoRouterOptions, AutoRouterType, RouterType } from './types'
import { withParams } from './withParams'
import { IRequest } from './types'

export const AutoRouter = ({
export const AutoRouter = <
RequestType extends IRequest = IRequest,
Args extends any[] = any[],
ResponseType = any
>({
format = json,
missing = () => error(404),
finally: f = [],
before = [],
...options }: AutoRouterOptions = {}
) => Router({
): RouterType<RequestType, Args, ResponseType> => Router({
before: [
withParams,
...before
Expand Down
5 changes: 3 additions & 2 deletions src/IttyRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {

export const IttyRouter = <
RequestType extends IRequest = IRequest,
Args extends any[] = any[]
>({ base = '', routes = [], ...other }: IttyRouterOptions = {}): IttyRouterType<RequestType, Args> =>
Args extends any[] = any[],
ResponseType = any
>({ base = '', routes = [], ...other }: IttyRouterOptions = {}): IttyRouterType<RequestType, Args, ResponseType> =>
({
__proto__: new Proxy({}, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
Expand Down
7 changes: 4 additions & 3 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {

export const Router = <
RequestType = IRequest,
Args extends any[] = any[]
>({ base = '', routes = [], ...other }: RouterOptions = {}): RouterType<RequestType, Args> =>
Args extends any[] = any[],
ResponseType = any
>({ base = '', routes = [], ...other }: RouterOptions = {}): RouterType<RequestType, Args, ResponseType> =>
({
__proto__: new Proxy({}, {
// @ts-expect-error (we're adding an expected prop "path" to the get)
Expand All @@ -25,7 +26,7 @@ export const Router = <
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-ignore

handlers, // embed handlers
path, // embed clean route path
]
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// routers
export * from './types/AutoRouterOptions'
export * from './types/AutoRouterType'
export * from './types/CustomRoutes'
export * from './types/ErrorHandler'
export * from './types/IRequest'
Expand Down
16 changes: 16 additions & 0 deletions src/types/AutoRouterType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ErrorHandler } from './ErrorHandler'
import { IRequest } from './IRequest'
import { RequestHandler } from './RequestHandler'
import { ResponseHandler } from './ResponseHandler'
import { RouterType } from './RouterType'

// export type AutoRouterType<
// R = IRequest,
// Args extends any[] = any[],
// ResponseType = any
// > = (options?: AutoRouterOptions) => RouterType<R, Args, ResponseType>

export type AutoRouterType<R = IRequest, Args extends any[] = any[], ResponseType = any> = {
missing?: RequestHandler
format?: ResponseHandler
} & RouterType<R, Args, ResponseType>
6 changes: 5 additions & 1 deletion src/types/IttyRouterType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { Route } from './Route'
import { RouteEntry } from './RouteEntry'
import { CustomRoutes } from './CustomRoutes'

export type IttyRouterType<R = IRequest, A extends any[] = any[], ResponseType = any> = {
export 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>
Expand Down

0 comments on commit 2736c51

Please sign in to comment.