diff --git a/src/client.ts b/src/client.ts index 41eba54..270d799 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,7 +1,10 @@ import type { OpenSeaClientConfig } from "./types/index.js" +declare const __VERSION__: string + const DEFAULT_BASE_URL = "https://api.opensea.io" const DEFAULT_TIMEOUT_MS = 30_000 +const USER_AGENT = `opensea-cli/${__VERSION__}` export class OpenSeaClient { private apiKey: string @@ -18,6 +21,14 @@ export class OpenSeaClient { this.verbose = config.verbose ?? false } + private get defaultHeaders(): Record { + return { + Accept: "application/json", + "User-Agent": USER_AGENT, + "x-api-key": this.apiKey, + } + } + async get(path: string, params?: Record): Promise { const url = new URL(`${this.baseUrl}${path}`) @@ -35,10 +46,7 @@ export class OpenSeaClient { const response = await fetch(url.toString(), { method: "GET", - headers: { - Accept: "application/json", - "x-api-key": this.apiKey, - }, + headers: this.defaultHeaders, signal: AbortSignal.timeout(this.timeoutMs), }) @@ -69,10 +77,7 @@ export class OpenSeaClient { } } - const headers: Record = { - Accept: "application/json", - "x-api-key": this.apiKey, - } + const headers: Record = { ...this.defaultHeaders } if (body) { headers["Content-Type"] = "application/json" diff --git a/test/client.test.ts b/test/client.test.ts index d068dde..a4bf53b 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -39,10 +39,11 @@ describe("OpenSeaClient", () => { "https://api.opensea.io/api/v2/test", expect.objectContaining({ method: "GET", - headers: { + headers: expect.objectContaining({ Accept: "application/json", + "User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+$/), "x-api-key": "test-key", - }, + }), }), ) expect(result).toEqual(mockResponse) @@ -93,10 +94,11 @@ describe("OpenSeaClient", () => { "https://api.opensea.io/api/v2/refresh", expect.objectContaining({ method: "POST", - headers: { + headers: expect.objectContaining({ Accept: "application/json", + "User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+$/), "x-api-key": "test-key", - }, + }), }), ) expect(result).toEqual(mockResponse) @@ -111,11 +113,12 @@ describe("OpenSeaClient", () => { "https://api.opensea.io/api/v2/create", expect.objectContaining({ method: "POST", - headers: { + headers: expect.objectContaining({ Accept: "application/json", "Content-Type": "application/json", + "User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+$/), "x-api-key": "test-key", - }, + }), body: JSON.stringify({ name: "test" }), }), ) diff --git a/tsup.config.ts b/tsup.config.ts index 2b0a0ea..aaf3f77 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,5 +1,10 @@ +import { readFileSync } from "node:fs" import { defineConfig } from "tsup" +const pkg = JSON.parse(readFileSync("./package.json", "utf-8")) as { + version: string +} + export default defineConfig([ { entry: { cli: "src/cli.ts" }, @@ -7,6 +12,9 @@ export default defineConfig([ clean: true, sourcemap: true, target: "node18", + define: { + __VERSION__: JSON.stringify(pkg.version), + }, banner: { js: "#!/usr/bin/env node", }, @@ -17,5 +25,8 @@ export default defineConfig([ dts: true, sourcemap: true, target: "node18", + define: { + __VERSION__: JSON.stringify(pkg.version), + }, }, ]) diff --git a/vitest.config.ts b/vitest.config.ts index 0de54ef..4bbf5f6 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,6 +1,14 @@ +import { readFileSync } from "node:fs" import { defineConfig } from "vitest/config" +const pkg = JSON.parse(readFileSync("./package.json", "utf-8")) as { + version: string +} + export default defineConfig({ + define: { + __VERSION__: JSON.stringify(pkg.version), + }, test: { coverage: { provider: "v8",