From 2736c51ececc2b6e57503eaf899d7c322fb05528 Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Fri, 29 Mar 2024 14:28:20 -0500 Subject: [PATCH 1/6] fixed some missing types in 5.0.0 --- example/router-matchup.ts | 24 ++++++++++++++++++++++++ src/AutoRouter.ts | 11 ++++++++--- src/IttyRouter.ts | 5 +++-- src/Router.ts | 7 ++++--- src/types.ts | 1 + src/types/AutoRouterType.ts | 16 ++++++++++++++++ src/types/IttyRouterType.ts | 6 +++++- 7 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 example/router-matchup.ts create mode 100644 src/types/AutoRouterType.ts diff --git a/example/router-matchup.ts b/example/router-matchup.ts new file mode 100644 index 0000000..be510bb --- /dev/null +++ b/example/router-matchup.ts @@ -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() +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) + +// 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/src/AutoRouter.ts b/src/AutoRouter.ts index 6f8a93b..7ba4470 100644 --- a/src/AutoRouter.ts +++ b/src/AutoRouter.ts @@ -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 => Router({ before: [ withParams, ...before diff --git a/src/IttyRouter.ts b/src/IttyRouter.ts index c8a8338..c6305ea 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 88c8be1..89caaf2 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) @@ -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 ] diff --git a/src/types.ts b/src/types.ts index 92af5f8..f44c156 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 0000000..4268f1f --- /dev/null +++ b/src/types/AutoRouterType.ts @@ -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 + +export type AutoRouterType = { + missing?: RequestHandler + format?: ResponseHandler +} & RouterType diff --git a/src/types/IttyRouterType.ts b/src/types/IttyRouterType.ts index 37930da..e94ce9d 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 From 544e370f1941e56e4805649eb03a96e70dc7abd0 Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Fri, 29 Mar 2024 14:37:32 -0500 Subject: [PATCH 2/6] router matchup fixed? --- example/router-matchup.ts | 4 ++++ src/AutoRouter.ts | 5 ++--- src/types/AutoRouterType.ts | 13 +++++-------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/example/router-matchup.ts b/example/router-matchup.ts index be510bb..2e7ab96 100644 --- a/example/router-matchup.ts +++ b/example/router-matchup.ts @@ -17,6 +17,10 @@ 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 diff --git a/src/AutoRouter.ts b/src/AutoRouter.ts index 7ba4470..e239920 100644 --- a/src/AutoRouter.ts +++ b/src/AutoRouter.ts @@ -1,9 +1,8 @@ import { Router } from './Router' import { error } from './error' import { json } from './json' -import { AutoRouterOptions, AutoRouterType, RouterType } from './types' +import { AutoRouterOptions, AutoRouterType, IRequest } from './types' import { withParams } from './withParams' -import { IRequest } from './types' export const AutoRouter = < RequestType extends IRequest = IRequest, @@ -15,7 +14,7 @@ export const AutoRouter = < finally: f = [], before = [], ...options }: AutoRouterOptions = {} -): RouterType => Router({ +): AutoRouterType => Router({ before: [ withParams, ...before diff --git a/src/types/AutoRouterType.ts b/src/types/AutoRouterType.ts index 4268f1f..1921c81 100644 --- a/src/types/AutoRouterType.ts +++ b/src/types/AutoRouterType.ts @@ -1,16 +1,13 @@ -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 - -export type AutoRouterType = { +export type AutoRouterType< + R = IRequest, + Args extends any[] = any[], + ResponseType = any +> = { missing?: RequestHandler format?: ResponseHandler } & RouterType From e1333ca6839e498dd9eb0c41effa5cb74e3525f1 Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Fri, 29 Mar 2024 14:39:36 -0500 Subject: [PATCH 3/6] moved folder --- {example => examples}/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 {example => examples}/router-matchup.ts | 0 {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 {example => examples}/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 {example => examples}/types/route-level-request.ts | 0 {example => examples}/types/router.ts | 0 22 files changed, 0 insertions(+), 0 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%) rename {example => examples}/router-matchup.ts (100%) 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%) 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/example/router-matchup.ts b/examples/router-matchup.ts similarity index 100% rename from example/router-matchup.ts rename to examples/router-matchup.ts 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 From 97e013ba20bf26b1841a18c66d71cc7a355a8e1b Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Fri, 29 Mar 2024 14:41:18 -0500 Subject: [PATCH 4/6] CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0217528..2a0c53e 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) From d0fa69098f353854636dfd0a7cede9c8b891c72d Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Fri, 29 Mar 2024 14:42:30 -0500 Subject: [PATCH 5/6] ts-ignore added back in --- src/Router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Router.ts b/src/Router.ts index 89caaf2..d561804 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -26,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 ] From 64fadeed67fb20d95e0099060b34e4ecbdf1b4ff Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Fri, 29 Mar 2024 14:46:17 -0500 Subject: [PATCH 6/6] released v5.0.2 - types fix in rotuer --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c6874c7..07ba4ff 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",