Skip to content

Commit

Permalink
V5 types fix (#230)
Browse files Browse the repository at this point in the history
* fixed some missing types in 5.0.0

* router matchup fixed?

* moved folder

* CHANGELOG

* ts-ignore added back in

* released v5.0.2 - types fix in rotuer
  • Loading branch information
kwhitley committed Mar 29, 2024
1 parent 89f5dd7 commit cc8bea8
Show file tree
Hide file tree
Showing 30 changed files with 64 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

- **v5.0.2**
- fixed: AutoRouter was missing the router-level generics support of the other 2 routers.
- fixed: All 3 routers had their 3rd generic argument, ResponseType added per the spec.
- **v5.0.0**
- BREAKING: router.fetch replaces router.handle (now deprecated)
- BREAKING: "createCors" has been deprecated in favor of "cors" (new options & requirements)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions examples/router-matchup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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)

// autorouter tests
autorouter.missing
autorouter.format

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

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itty-router",
"version": "5.0.1",
"version": "5.0.2",
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",
"main": "./index.js",
"module": "./index.mjs",
Expand Down
10 changes: 7 additions & 3 deletions src/AutoRouter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Router } from './Router'
import { error } from './error'
import { json } from './json'
import { AutoRouterOptions } from './types'
import { AutoRouterOptions, AutoRouterType, IRequest } from './types'
import { withParams } from './withParams'

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({
): AutoRouterType<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
5 changes: 3 additions & 2 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 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
13 changes: 13 additions & 0 deletions src/types/AutoRouterType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
> = {
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 cc8bea8

Please sign in to comment.