From 6c6afc1a0ace183fdfc7a940b5bd1bcae2a0b536 Mon Sep 17 00:00:00 2001 From: Kevin Whitley Date: Tue, 2 Apr 2024 17:18:02 -0500 Subject: [PATCH] released v5.0.7 - withParams has more explicit check to prevent binding null --- CHANGELOG.md | 2 ++ examples/runtimes/node-autorouter.js | 42 ++++++++++++++++++++++++++++ package.json | 2 +- rollup.config.mjs | 2 +- src/withParams.ts | 2 +- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 examples/runtimes/node-autorouter.js diff --git a/CHANGELOG.md b/CHANGELOG.md index e24346fe..a7f2ee28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Changelog +- **v5.0.7** + - fixed: withParams could attempt to bind null (collision with node adapter) - **v5.0.6** - fixed: corsify as replacing status codes (now mutates original response) - **v5.0.5** diff --git a/examples/runtimes/node-autorouter.js b/examples/runtimes/node-autorouter.js new file mode 100644 index 00000000..de7a315a --- /dev/null +++ b/examples/runtimes/node-autorouter.js @@ -0,0 +1,42 @@ +const { error } = require("../../dist/index") +const { AutoRouter } = require("../../dist/AutoRouter") +const { createServerAdapter } = require("@whatwg-node/server") +const http = require("http") + +const router = AutoRouter({ + catch: (err) => { + console.log('ERROR', err.message, err.stack) + + return error(500, err.stack) + } +}) + +router.get('*', () => "Hello itty-router v5") + +const serverAdapter = createServerAdapter(async (request) => { + // this works + + const url = new URL(request.url) + + console.log({ + url: request.url, + newURL: url, + method: request.method, + }) + + request.query = {} + request.params = {} + request.route = '/foo' + request.proxy = new Proxy(request, {}) + + const slimRequest = { + method: request.method, + url: request.url, + } + + return await router.fetch(request) +}) + +const httpServer = http.createServer(serverAdapter) + +httpServer.listen(5000) diff --git a/package.json b/package.json index ee298381..20047c30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "itty-router", - "version": "5.0.6", + "version": "5.0.7", "description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.", "main": "./index.js", "module": "./index.mjs", diff --git a/rollup.config.mjs b/rollup.config.mjs index 7ff25f9c..bf56f130 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -53,7 +53,7 @@ export default async () => { ], plugins: [ typescript({ sourceMap: true }), - terser(), + // terser(), bundleSize(), copy({ targets: [ diff --git a/src/withParams.ts b/src/withParams.ts index edae593a..2388a883 100644 --- a/src/withParams.ts +++ b/src/withParams.ts @@ -3,7 +3,7 @@ import { IRequest } from './types' export const withParams = (request: IRequest): void => { request.proxy = new Proxy(request.proxy || request, { get: (obj, prop) => obj[prop] !== undefined - ? obj[prop].bind?.(request) || obj[prop] + ? obj[prop]?.bind?.(request) || obj[prop] : obj?.params?.[prop] }) }