Skip to content

Commit ee8d5ff

Browse files
mik3flyclaude
andcommitted
feat(client): send a rektradar-sdk User-Agent for API attribution
The client only sent Accept (+ optional Authorization), so SDK traffic to api.rektradar.io was indistinguishable from raw curl / browser fetch / internal calls in server logs -- we couldn't measure real SDK adoption. Add `User-Agent: rektradar-sdk/<version>` to every request. In browsers User-Agent is a forbidden header and is silently dropped (no error); it takes effect in Node/undici, where real server-side integrations live, so /v1 calls from the SDK now show up as rektradar-sdk/* in nginx logs. SDK_VERSION is module-local (not exported) to keep the public api-surface unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent da19371 commit ee8d5ff

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/client.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ describe("RektRadar", () => {
2929
expect(fetchImpl.mock.calls[0]![1]?.headers?.Authorization).toBeUndefined();
3030
});
3131

32+
it("sends a rektradar-sdk User-Agent for server-log attribution", async () => {
33+
const fetchImpl = vi.fn<FetchLike>().mockResolvedValue(
34+
mockResponse({ address: "0xABC", score: 1, flags: [] }),
35+
);
36+
const rr = new RektRadar({ fetch: fetchImpl });
37+
await rr.token("0xABC");
38+
const ua = fetchImpl.mock.calls[0]![1]?.headers?.["User-Agent"];
39+
expect(ua).toMatch(/^rektradar-sdk\/\d/);
40+
});
41+
3242
it("passes the since param to rugs()", async () => {
3343
const fetchImpl = vi.fn<FetchLike>().mockResolvedValue(
3444
mockResponse({ rugs: [], summary: null, dataDelaySeconds: 0 }),

src/client.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import type {
1111

1212
export const DEFAULT_BASE_URL = "https://api.rektradar.io";
1313

14+
// SDK version, sent as a User-Agent so API usage from the SDK is attributable
15+
// in server logs (vs raw curl / browser fetch). Keep in sync with package.json
16+
// on each release. In browsers `User-Agent` is a forbidden header and is
17+
// silently dropped (no error); it takes effect in Node/undici, which is where
18+
// real server-side integrations live.
19+
const SDK_VERSION = "0.1.6";
20+
1421
/** Thrown on a non-2xx API response. */
1522
export class RektRadarError extends Error {
1623
readonly status: number;
@@ -56,7 +63,10 @@ export class RektRadar {
5663
}
5764

5865
private async get<T>(path: string): Promise<T> {
59-
const headers: Record<string, string> = { Accept: "application/json" };
66+
const headers: Record<string, string> = {
67+
Accept: "application/json",
68+
"User-Agent": `rektradar-sdk/${SDK_VERSION}`,
69+
};
6070
if (this.apiKey) {
6171
headers.Authorization = `Bearer ${this.apiKey}`;
6272
}

0 commit comments

Comments
 (0)