Skip to content

Commit 8603789

Browse files
feat: add User-Agent header to all HTTP requests (DIS-41)
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>
1 parent 7a06fc4 commit 8603789

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { createRequire } from "node:module"
12
import type { OpenSeaClientConfig } from "./types/index.js"
23

4+
const require = createRequire(import.meta.url)
5+
const { version } = require("../package.json") as { version: string }
6+
37
const DEFAULT_BASE_URL = "https://api.opensea.io"
48
const DEFAULT_TIMEOUT_MS = 30_000
9+
const USER_AGENT = `opensea-cli/${version}`
510

611
export class OpenSeaClient {
712
private apiKey: string
@@ -37,6 +42,7 @@ export class OpenSeaClient {
3742
method: "GET",
3843
headers: {
3944
Accept: "application/json",
45+
"User-Agent": USER_AGENT,
4046
"x-api-key": this.apiKey,
4147
},
4248
signal: AbortSignal.timeout(this.timeoutMs),
@@ -71,6 +77,7 @@ export class OpenSeaClient {
7177

7278
const headers: Record<string, string> = {
7379
Accept: "application/json",
80+
"User-Agent": USER_AGENT,
7481
"x-api-key": this.apiKey,
7582
}
7683

test/client.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ describe("OpenSeaClient", () => {
3939
"https://api.opensea.io/api/v2/test",
4040
expect.objectContaining({
4141
method: "GET",
42-
headers: {
42+
headers: expect.objectContaining({
4343
Accept: "application/json",
44+
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+/),
4445
"x-api-key": "test-key",
45-
},
46+
}),
4647
}),
4748
)
4849
expect(result).toEqual(mockResponse)
@@ -93,10 +94,11 @@ describe("OpenSeaClient", () => {
9394
"https://api.opensea.io/api/v2/refresh",
9495
expect.objectContaining({
9596
method: "POST",
96-
headers: {
97+
headers: expect.objectContaining({
9798
Accept: "application/json",
99+
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+/),
98100
"x-api-key": "test-key",
99-
},
101+
}),
100102
}),
101103
)
102104
expect(result).toEqual(mockResponse)
@@ -111,11 +113,12 @@ describe("OpenSeaClient", () => {
111113
"https://api.opensea.io/api/v2/create",
112114
expect.objectContaining({
113115
method: "POST",
114-
headers: {
116+
headers: expect.objectContaining({
115117
Accept: "application/json",
116118
"Content-Type": "application/json",
119+
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+/),
117120
"x-api-key": "test-key",
118-
},
121+
}),
119122
body: JSON.stringify({ name: "test" }),
120123
}),
121124
)

0 commit comments

Comments
 (0)