feat: add User-Agent header to all HTTP requests (DIS-41)#34
Conversation
Add User-Agent: opensea-cli/<version> header to all requests made by OpenSeaClient. Version is dynamically read from package.json using createRequire. Updates both get() and post() methods and corresponding test assertions. Co-Authored-By: Chris K <ckorhonen@gmail.com>
Original prompt from Chris K |
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Co-Authored-By: Chris K <ckorhonen@gmail.com>
ckorhonen
left a comment
There was a problem hiding this comment.
Code Review
Overall: Approve with suggestions — All 151 tests pass. Clean, well-scoped change.
Suggestions
-
Replace
createRequirewith build-time version injection. ThecreateRequire(import.meta.url)+require("../package.json")pattern works, but introduces a synchronous CJSrequire()in an ESM-only codebase and depends on file-system layout. Recommend using tsup'sdefineoption instead:// tsup.config.ts define: { __VERSION__: JSON.stringify(pkg.version) } // client.ts declare const __VERSION__: string const USER_AGENT = `opensea-cli/${__VERSION__}`
This eliminates the runtime dependency on file layout and is zero-cost (inlined at build time).
-
Test regex should anchor the end —
/^opensea-cli\/\d+\.\d+\.\d+/should be/^opensea-cli\/\d+\.\d+\.\d+$/to prevent matching malformed values likeopensea-cli/1.2.3-garbage. -
Minor: Header duplication — Both
get()andpost()now have identical header sets. Consider extracting a privatedefaultHeadersgetter for maintainability.
I've pushed a commit with fixes 1 and 2.
Replace the runtime createRequire/package.json pattern with tsup's define option for compile-time version injection. Also anchors the test regex with $ to prevent false-positive matches. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Replace createRequire with build-time __VERSION__ via tsup define - Add __VERSION__ define to vitest config for test compatibility - Anchor test regex with $ to prevent matching malformed versions - Extract private defaultHeaders getter to reduce header duplication Co-Authored-By: Chris K <ckorhonen@gmail.com>
Co-Authored-By: Chris K <ckorhonen@gmail.com>
Add User-Agent header to all HTTP requests
Summary
Adds a
User-Agent: opensea-cli/<version>header to every HTTP request made byOpenSeaClient, enabling server-side traffic segmentation by channel (CLI vs MCP vs other consumers) in Datadog/analytics.defineoption (__VERSION__), avoiding any runtime dependency on file-system layouttsup.config.tsandvitest.config.tsreadpackage.jsonand define__VERSION__so the constant is available in builds and testsdefaultHeadersgetter centralizes the shared header set (Accept,User-Agent,x-api-key), used by bothget()andpost()expect.objectContainingwith a strictly-anchored regex (/^opensea-cli\/\d+\.\d+\.\d+$/) to validate the header valueConfidence: 🟢 High — small, well-scoped change. Lint, type-check, and all 151 tests pass locally.
Updates since last revision
Addressed review feedback from @ckorhonen:
createRequire/require("../package.json")with tsupdefine: { __VERSION__: JSON.stringify(pkg.version) }. Eliminates the CJSrequire()call in an ESM-only codebase and removes runtime dependency ondist/↔package.jsonrelative path.$to regex end (/^opensea-cli\/\d+\.\d+\.\d+$/) to reject malformed values likeopensea-cli/1.2.3-garbage.defaultHeadersgetter — private getter onOpenSeaClientremoves header duplication betweenget()andpost().Review & Testing Checklist for Human
__VERSION__is correctly inlined after build: runnpm run build && grep 'opensea-cli/' dist/index.js— should show the literal version string (e.g.opensea-cli/0.4.1), not__VERSION__post()spread ({ ...this.defaultHeaders }) correctly includesContent-Typewhen a body is provided — the test covers this but worth a quick manual check of the logicNotes
expect.objectContaininginstead of exact header matching to accommodate the new header without breaking if header order changes. This is marginally less strict than before.$-anchored regex means pre-release versions (e.g.1.2.3-beta.1) would fail the test assertion — this is intentional per reviewer preference for strict semver matching.