Skip to content

fix(api): let createAPIHandler take the request event - #22

Open
everton-dgn wants to merge 1 commit into
solidjs:mainfrom
everton-dgn:fix/event-first-api-handler
Open

everton-dgn wants to merge 1 commit into
solidjs:mainfrom
everton-dgn:fix/event-first-api-handler

Conversation

@everton-dgn

Copy link
Copy Markdown

Closes #21

start.middleware in @solidjs/vite-plugin calls each function as (event, next) since solidjs/solid-vite-plugin#401. createAPIHandler read its first argument as a Request, so the README setup (export default [createAPIHandler(routes)] in src/middleware.ts) threw TypeError: Invalid URL on every request: the event has no url.

The handler now takes either shape. When the first argument has a request property, it reads event.request at call time (so a request swapped in by an earlier middleware is the one matched) and hands that same event to the route handler, with params written onto it. No request-event scope is needed on that path. getEvent still takes precedence when passed, and a bare Request keeps reading the scope exactly as before. Dispatch already called next() with no arguments, which is what the plugin's next requires, so that part is unchanged.

The return type's first parameter is now Request | { request: Request }. It is structural on purpose: RequestEvent from @solidjs/web is an interface, so it is not assignable to APIEvent and its index signature. With this type, createAPIHandler(routes) is assignable to StartMiddleware and still to the old (request, next) shape. The check is "request" in input rather than instanceof Request, so a Request from another realm or a polyfill keeps working.

Also updated: the module and createAPIHandler JSDoc, the getEvent doc, the README paragraph on dispatch, and the getScopedEvent error, which pointed at ssr.middleware (an option the plugin no longer has) and now mentions passing the event.

Testing: two new cases in test/api.spec.ts. One passes { request, user } with no scope and checks the handler gets that event and params lands on it, then that getEvent still wins. The other checks that an unmatched request and a declining page GET both call next() with no arguments. Both failed on main with TypeError: Invalid URL at src/api.ts:193. With the fix, pnpm build, pnpm test (all 115 tests) and npm pack --dry-run pass, and prettier --check is clean on the touched TypeScript files. I also type-checked createAPIHandler(routes) against StartMiddleware from @solidjs/vite-plugin 3.0.0-next.49: it fails before the change and passes after. Includes a patch changeset.

#19 (HEAD with a declining GET) edits the decline check a few lines below; the two changes don't overlap.

start.middleware in @solidjs/vite-plugin now calls (event, next), and
the handler read its first argument as a Request, so
new URL(request.url) threw. Read event.request when given an event and
hand that event to handlers. getEvent still wins, and a bare Request
keeps reading the request-event scope.

Update the README and JSDoc, and point the scope error at passing the
event.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createAPIHandler fails in start.middleware now that the plugin passes the request event

1 participant