Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,6 +21,14 @@ export class OpenSeaClient {
this.verbose = config.verbose ?? false
}

private get defaultHeaders(): Record<string, string> {
return {
Accept: "application/json",
"User-Agent": USER_AGENT,
"x-api-key": this.apiKey,
}
}

async get<T>(path: string, params?: Record<string, unknown>): Promise<T> {
const url = new URL(`${this.baseUrl}${path}`)

Expand All @@ -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),
})

Expand Down Expand Up @@ -69,10 +77,7 @@ export class OpenSeaClient {
}
}

const headers: Record<string, string> = {
Accept: "application/json",
"x-api-key": this.apiKey,
}
const headers: Record<string, string> = { ...this.defaultHeaders }

if (body) {
headers["Content-Type"] = "application/json"
Expand Down
15 changes: 9 additions & 6 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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" }),
}),
)
Expand Down
11 changes: 11 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
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" },
format: ["esm"],
clean: true,
sourcemap: true,
target: "node18",
define: {
__VERSION__: JSON.stringify(pkg.version),
},
banner: {
js: "#!/usr/bin/env node",
},
Expand All @@ -17,5 +25,8 @@ export default defineConfig([
dts: true,
sourcemap: true,
target: "node18",
define: {
__VERSION__: JSON.stringify(pkg.version),
},
},
])
8 changes: 8 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down