feat: add requireContentType and appendAcceptQuery utils#1446
Conversation
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughAdds RFC 10008 HTTP ChangesQuery utilities implementation
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related issues
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
Follow-up to QUERY method support (#1445) for RFC 10008. - `setResponseAcceptQuery` / `getResponseAcceptQuery`: advertise and read the supported query media types via the `Accept-Query` response header, serialized as a Structured Fields (RFC 8941) List. - `requireContentType`: opt-in request `Content-Type` validation throwing `400` (missing), `422` (malformed), or `415` (unsupported), with wildcard support (`*/*`, `type/*`). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Follow-up to QUERY method support (#1445) for RFC 10008. - `setResponseAcceptQuery`: advertise the supported query media types via the `Accept-Query` response header, serialized as a Structured Fields (RFC 8941) List. - `requireContentType`: opt-in request `Content-Type` validation throwing `400` (missing), `422` (malformed), or `415` (unsupported), with wildcard support (`*/*`, `type/*`). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Follow-up to QUERY method support (#1445) for RFC 10008. - `setAcceptQuery`: advertise the supported query media types via the `Accept-Query` response header, serialized as a Structured Fields (RFC 8941) List. - `requireContentType`: opt-in request `Content-Type` validation throwing `400` (missing), `422` (malformed), or `415` (unsupported), with wildcard support (`*/*`, `type/*`). Co-Authored-By: Claude Opus 4.8 <[email protected]>
requireContentType and setAcceptQuery utils
There was a problem hiding this comment.
🧹 Nitpick comments (2)
test/query.test.ts (1)
102-120: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winConsider adding a test for the bare
*wildcard.
mediaTypeMatcheshandlesaccepted === "*"(line 121 inquery.ts), but no test covers this case. The*/*wildcard is tested, but the bare*variant is not.🧪 Suggested test
it("supports the */* wildcard", async () => { t.app.query("/any", (event) => requireContentType(event, "*/*")); const res = await t.fetch("/any", { method: "QUERY", body: "x", headers: { "content-type": "text/plain" }, }); expect(res.status).toBe(200); }); + + it("supports the bare * wildcard", async () => { + t.app.query("/star", (event) => requireContentType(event, "*")); + const res = await t.fetch("/star", { + method: "QUERY", + body: "x", + headers: { "content-type": "text/plain" }, + }); + expect(res.status).toBe(200); + });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/query.test.ts` around lines 102 - 120, Add a test in query.test.ts covering the bare "*" wildcard path, since mediaTypeMatches explicitly supports accepted === "*" in query.ts but current coverage only checks "application/*" and "*/*". Mirror the existing query tests by registering a handler with requireContentType(event, "*") and asserting a QUERY request with a representative content-type (for example text/plain) returns 200.src/utils/query.ts (1)
91-93: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider using
kprefix for constants per project convention.
SF_TOKEN_REandSF_KEY_REdon't follow thekprefix convention used elsewhere in the codebase (e.g.,kEventRes,kMalformedURL). As per coding guidelines, symbol constants should use thekprefix (e.g.,kNotFound,kHandled).♻️ Optional refactor
-const SF_TOKEN_RE = /^[A-Za-z*][\w!#$%&'*+.^`|~:/-]*$/; -const SF_KEY_RE = /^[a-z*][a-z0-9_.*-]*$/; +const kSfTokenRe = /^[A-Za-z*][\w!#$%&'*+.^`|~:/-]*$/; +const kSfKeyRe = /^[a-z*][a-z0-9_.*-]*$/;And update references in
serializeMediaTypeaccordingly.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/utils/query.ts` around lines 91 - 93, Rename the constant regex symbols in query.ts to follow the project’s k-prefix convention by changing SF_TOKEN_RE and SF_KEY_RE to kSF_TOKEN_RE and kSF_KEY_RE, then update all references in serializeMediaType and any nearby usage to match the new names. Keep the behavior unchanged and ensure the new identifiers remain consistent with other constants like kEventRes and kMalformedURL.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/utils/query.ts`:
- Around line 91-93: Rename the constant regex symbols in query.ts to follow the
project’s k-prefix convention by changing SF_TOKEN_RE and SF_KEY_RE to
kSF_TOKEN_RE and kSF_KEY_RE, then update all references in serializeMediaType
and any nearby usage to match the new names. Keep the behavior unchanged and
ensure the new identifiers remain consistent with other constants like kEventRes
and kMalformedURL.
In `@test/query.test.ts`:
- Around line 102-120: Add a test in query.test.ts covering the bare "*"
wildcard path, since mediaTypeMatches explicitly supports accepted === "*" in
query.ts but current coverage only checks "application/*" and "*/*". Mirror the
existing query tests by registering a handler with requireContentType(event,
"*") and asserting a QUERY request with a representative content-type (for
example text/plain) returns 200.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c35ccbe0-d06b-4b2f-90d4-c7081e8cdfd6
📒 Files selected for processing (5)
docs/2.utils/1.request.mdsrc/index.tssrc/utils/query.tstest/query.test.tstest/unit/package.test.ts
- match accepted media types that carry parameters (strip params both sides) - return 422 for structurally malformed Content-Type (empty type/subtype) - accumulate Accept-Query across multiple setAcceptQuery calls instead of overwriting - render requireContentType `Throws:` JSDoc as a proper bulleted list Co-Authored-By: Claude Opus 4.8 <[email protected]>
The function appends to an existing Accept-Query header rather than overwriting it, so the name should reflect accumulation semantics.
requireContentType and setAcceptQuery utilsrequireContentType and appendAcceptQuery utils
- examples/query.mjs imported `setAcceptQuery`, but the util shipped as `appendAcceptQuery` (#1446) — the example failed to run. Verified the fixed example end-to-end (QUERY, 415, cacheable GET follow-up). - docs: add an "HTTP QUERY Method" example page walking through `app.query()`, `appendAcceptQuery`, `requireContentType`, and the cacheable-GET / `Content-Location` pattern; link it from the routing guide and the examples index. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Follow-up to QUERY method support (#1445), implementing the header/validation utilities from the RFC 10008 plan in #1441.
appendAcceptQuery(event, mediaTypes)Advertises the query formats a resource accepts via the
Accept-Queryresponse header, serialized as an RFC 8941 Structured Fields List. Base media types become tokens;;name=valueparameters are emitted as quoted strings (validated tokens/keys, escaped quotes/backslashes, empty-list is a no-op).requireContentType(event, acceptedTypes)Opt-in
Content-Typevalidation returning the matched media type. Throws per RFC:400— missingContent-Type422— malformedContent-Type415— unsupported media typeSupports
*/*,*, andtype/*wildcards.Notes
src/utils/query.ts; exports added tosrc/index.ts.docs/2.utils/1.request.md(automd).test/query.test.ts(web + node matrix); package-exports snapshot updated.pnpm lint+pnpm vitest rungreen (1315 passed).A
getResponseAcceptQueryreader was considered but dropped — reading back a header the same handler just set has no real use; the serialization is the part that carries value.Remaining from #1441: PR 4 (CORS preflight / cache-header / proxy body-forwarding audits).
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes
Tests