From cc8bea8ed96956c923e523e21b47afe868383cbe Mon Sep 17 00:00:00 2001 From: "Kevin R. Whitley" Date: Fri, 29 Mar 2024 14:47:10 -0500 Subject: [PATCH] V5 types fix (#230) * 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 --- CHANGELOG.md | 3 ++ .../bun-autorouter-advanced.ts | 0 {example => examples}/bun-autorouter.ts | 0 {example => examples}/bun-router.ts | 0 {example => examples}/bun.ts | 0 {example => examples}/cors-types.ts | 0 {example => examples}/custom-router-props.ts | 0 {example => examples}/index.ts | 0 {example => examples}/node-import.js | 0 {example => examples}/node-require.js | 0 {example => examples}/request-types.ts | 0 examples/router-matchup.ts | 28 +++++++++++++++++++ {example => examples}/runtimes/bun.ts | 0 {example => examples}/runtimes/express.js | 0 {example => examples}/runtimes/node.js | 0 {example => examples}/types/content.ts | 0 {example => examples}/types/extra-props.ts | 0 .../types/global-and-route-level-request.ts | 0 {example => examples}/types/global-request.ts | 0 {example => examples}/types/middleware.ts | 0 {example => examples}/types/requestlike.ts | 0 .../types/route-level-request.ts | 0 {example => examples}/types/router.ts | 0 package.json | 2 +- src/AutoRouter.ts | 10 +++++-- src/IttyRouter.ts | 5 ++-- src/Router.ts | 5 ++-- src/types.ts | 1 + src/types/AutoRouterType.ts | 13 +++++++++ src/types/IttyRouterType.ts | 6 +++- 30 files changed, 64 insertions(+), 9 deletions(-) rename {example => examples}/bun-autorouter-advanced.ts (100%) rename {example => examples}/bun-autorouter.ts (100%) rename {example => examples}/bun-router.ts (100%) rename {example => examples}/bun.ts (100%) rename {example => examples}/cors-types.ts (100%) rename {example => examples}/custom-router-props.ts (100%) rename {example => examples}/index.ts (100%) rename {example => examples}/node-import.js (100%) rename {example => examples}/node-require.js (100%) rename {example => examples}/request-types.ts (100%) create mode 100644 examples/router-matchup.ts rename {example => examples}/runtimes/bun.ts (100%) rename {example => examples}/runtimes/express.js (100%) rename {example => examples}/runtimes/node.js (100%) rename {example => examples}/types/content.ts (100%) rename {example => examples}/types/extra-props.ts (100%) rename {example => examples}/types/global-and-route-level-request.ts (100%) rename {example => examples}/types/global-request.ts (100%) rename {example => examples}/types/middleware.ts (100%) rename {example => examples}/types/requestlike.ts (100%) rename {example => examples}/types/route-level-request.ts (100%) rename {example => examples}/types/router.ts (100%) create mode 100644 src/types/AutoRouterType.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0217528e..2a0c53ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/example/bun-autorouter-advanced.ts b/examples/bun-autorouter-advanced.ts similarity index 100% rename from example/bun-autorouter-advanced.ts rename to examples/bun-autorouter-advanced.ts diff --git a/example/bun-autorouter.ts b/examples/bun-autorouter.ts similarity index 100% rename from example/bun-autorouter.ts rename to examples/bun-autorouter.ts diff --git a/example/bun-router.ts b/examples/bun-router.ts similarity index 100% rename from example/bun-router.ts rename to examples/bun-router.ts diff --git a/example/bun.ts b/examples/bun.ts similarity index 100% rename from example/bun.ts rename to examples/bun.ts diff --git a/example/cors-types.ts b/examples/cors-types.ts similarity index 100% rename from example/cors-types.ts rename to examples/cors-types.ts diff --git a/example/custom-router-props.ts b/examples/custom-router-props.ts similarity index 100% rename from example/custom-router-props.ts rename to examples/custom-router-props.ts diff --git a/example/index.ts b/examples/index.ts similarity index 100% rename from example/index.ts rename to examples/index.ts diff --git a/example/node-import.js b/examples/node-import.js similarity index 100% rename from example/node-import.js rename to examples/node-import.js diff --git a/example/node-require.js b/examples/node-require.js similarity index 100% rename from example/node-require.js rename to examples/node-require.js diff --git a/example/request-types.ts b/examples/request-types.ts similarity index 100% rename from example/request-types.ts rename to examples/request-types.ts diff --git a/examples/router-matchup.ts b/examples/router-matchup.ts new file mode 100644 index 00000000..2e7ab969 --- /dev/null +++ b/examples/router-matchup.ts @@ -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() +const router = Router() +const ittyrouter = IttyRouter() + +// 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 + diff --git a/example/runtimes/bun.ts b/examples/runtimes/bun.ts similarity index 100% rename from example/runtimes/bun.ts rename to examples/runtimes/bun.ts diff --git a/example/runtimes/express.js b/examples/runtimes/express.js similarity index 100% rename from example/runtimes/express.js rename to examples/runtimes/express.js diff --git a/example/runtimes/node.js b/examples/runtimes/node.js similarity index 100% rename from example/runtimes/node.js rename to examples/runtimes/node.js diff --git a/example/types/content.ts b/examples/types/content.ts similarity index 100% rename from example/types/content.ts rename to examples/types/content.ts diff --git a/example/types/extra-props.ts b/examples/types/extra-props.ts similarity index 100% rename from example/types/extra-props.ts rename to examples/types/extra-props.ts diff --git a/example/types/global-and-route-level-request.ts b/examples/types/global-and-route-level-request.ts similarity index 100% rename from example/types/global-and-route-level-request.ts rename to examples/types/global-and-route-level-request.ts diff --git a/example/types/global-request.ts b/examples/types/global-request.ts similarity index 100% rename from example/types/global-request.ts rename to examples/types/global-request.ts diff --git a/example/types/middleware.ts b/examples/types/middleware.ts similarity index 100% rename from example/types/middleware.ts rename to examples/types/middleware.ts diff --git a/example/types/requestlike.ts b/examples/types/requestlike.ts similarity index 100% rename from example/types/requestlike.ts rename to examples/types/requestlike.ts diff --git a/example/types/route-level-request.ts b/examples/types/route-level-request.ts similarity index 100% rename from example/types/route-level-request.ts rename to examples/types/route-level-request.ts diff --git a/example/types/router.ts b/examples/types/router.ts similarity index 100% rename from example/types/router.ts rename to examples/types/router.ts diff --git a/package.json b/package.json index c6874c7b..07ba4ff2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/AutoRouter.ts b/src/AutoRouter.ts index 6f8a93bd..e239920f 100644 --- a/src/AutoRouter.ts +++ b/src/AutoRouter.ts @@ -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 => Router({ before: [ withParams, ...before diff --git a/src/IttyRouter.ts b/src/IttyRouter.ts index c8a8338b..c6305ea9 100644 --- a/src/IttyRouter.ts +++ b/src/IttyRouter.ts @@ -8,8 +8,9 @@ import { export const IttyRouter = < RequestType extends IRequest = IRequest, - Args extends any[] = any[] ->({ base = '', routes = [], ...other }: IttyRouterOptions = {}): IttyRouterType => + Args extends any[] = any[], + ResponseType = any +>({ base = '', routes = [], ...other }: IttyRouterOptions = {}): IttyRouterType => ({ __proto__: new Proxy({}, { // @ts-expect-error (we're adding an expected prop "path" to the get) diff --git a/src/Router.ts b/src/Router.ts index 88c8be14..d561804d 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -8,8 +8,9 @@ import { export const Router = < RequestType = IRequest, - Args extends any[] = any[] ->({ base = '', routes = [], ...other }: RouterOptions = {}): RouterType => + Args extends any[] = any[], + ResponseType = any +>({ base = '', routes = [], ...other }: RouterOptions = {}): RouterType => ({ __proto__: new Proxy({}, { // @ts-expect-error (we're adding an expected prop "path" to the get) diff --git a/src/types.ts b/src/types.ts index 92af5f81..f44c156b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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' diff --git a/src/types/AutoRouterType.ts b/src/types/AutoRouterType.ts new file mode 100644 index 00000000..1921c815 --- /dev/null +++ b/src/types/AutoRouterType.ts @@ -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 diff --git a/src/types/IttyRouterType.ts b/src/types/IttyRouterType.ts index 37930da6..e94ce9db 100644 --- a/src/types/IttyRouterType.ts +++ b/src/types/IttyRouterType.ts @@ -5,7 +5,11 @@ import { Route } from './Route' import { RouteEntry } from './RouteEntry' import { CustomRoutes } from './CustomRoutes' -export type IttyRouterType = { +export type IttyRouterType< + R = IRequest, + A extends any[] = any[], + ResponseType = any +> = { __proto__: IttyRouterType routes: RouteEntry[] fetch: (request: RequestLike, ...extra: Args) => Promise