Repository navigation
fix(api): let createAPIHandler take the request event - #22
Open
everton-dgn wants to merge 1 commit into
Open
everton-dgn wants to merge 1 commit into
everton-dgn wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #21
start.middlewarein @solidjs/vite-plugin calls each function as(event, next)since solidjs/solid-vite-plugin#401.createAPIHandlerread its first argument as aRequest, so the README setup (export default [createAPIHandler(routes)]insrc/middleware.ts) threwTypeError: Invalid URLon every request: the event has nourl.The handler now takes either shape. When the first argument has a
requestproperty, it readsevent.requestat call time (so a request swapped in by an earlier middleware is the one matched) and hands that same event to the route handler, withparamswritten onto it. No request-event scope is needed on that path.getEventstill takes precedence when passed, and a bareRequestkeeps reading the scope exactly as before. Dispatch already callednext()with no arguments, which is what the plugin'snextrequires, so that part is unchanged.The return type's first parameter is now
Request | { request: Request }. It is structural on purpose:RequestEventfrom @solidjs/web is an interface, so it is not assignable toAPIEventand its index signature. With this type,createAPIHandler(routes)is assignable toStartMiddlewareand still to the old(request, next)shape. The check is"request" in inputrather thaninstanceof Request, so aRequestfrom another realm or a polyfill keeps working.Also updated: the module and
createAPIHandlerJSDoc, thegetEventdoc, the README paragraph on dispatch, and thegetScopedEventerror, which pointed atssr.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 andparamslands on it, then thatgetEventstill wins. The other checks that an unmatched request and a declining pageGETboth callnext()with no arguments. Both failed onmainwithTypeError: Invalid URLatsrc/api.ts:193. With the fix,pnpm build,pnpm test(all 115 tests) andnpm pack --dry-runpass, andprettier --checkis clean on the touched TypeScript files. I also type-checkedcreateAPIHandler(routes)againstStartMiddlewarefrom @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.