Skip to content

Commit f095607

Browse files
ckorhonenclaude
andcommitted
refactor: use build-time version injection instead of createRequire
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>
1 parent 4f09f4d commit f095607

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/client.ts

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

4-
const require = createRequire(import.meta.url)
5-
const { version } = require("../package.json") as { version: string }
3+
declare const __VERSION__: string
64

75
const DEFAULT_BASE_URL = "https://api.opensea.io"
86
const DEFAULT_TIMEOUT_MS = 30_000
9-
const USER_AGENT = `opensea-cli/${version}`
7+
const USER_AGENT = `opensea-cli/${__VERSION__}`
108

119
export class OpenSeaClient {
1210
private apiKey: string

test/client.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("OpenSeaClient", () => {
4141
method: "GET",
4242
headers: expect.objectContaining({
4343
Accept: "application/json",
44-
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+/),
44+
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+$/),
4545
"x-api-key": "test-key",
4646
}),
4747
}),
@@ -96,7 +96,7 @@ describe("OpenSeaClient", () => {
9696
method: "POST",
9797
headers: expect.objectContaining({
9898
Accept: "application/json",
99-
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+/),
99+
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+$/),
100100
"x-api-key": "test-key",
101101
}),
102102
}),
@@ -116,7 +116,7 @@ describe("OpenSeaClient", () => {
116116
headers: expect.objectContaining({
117117
Accept: "application/json",
118118
"Content-Type": "application/json",
119-
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+/),
119+
"User-Agent": expect.stringMatching(/^opensea-cli\/\d+\.\d+\.\d+$/),
120120
"x-api-key": "test-key",
121121
}),
122122
body: JSON.stringify({ name: "test" }),

tsup.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
import { readFileSync } from "node:fs"
12
import { defineConfig } from "tsup"
23

4+
const pkg = JSON.parse(readFileSync("./package.json", "utf-8")) as {
5+
version: string
6+
}
7+
38
export default defineConfig([
49
{
510
entry: { cli: "src/cli.ts" },
611
format: ["esm"],
712
clean: true,
813
sourcemap: true,
914
target: "node18",
15+
define: {
16+
__VERSION__: JSON.stringify(pkg.version),
17+
},
1018
banner: {
1119
js: "#!/usr/bin/env node",
1220
},
@@ -17,5 +25,8 @@ export default defineConfig([
1725
dts: true,
1826
sourcemap: true,
1927
target: "node18",
28+
define: {
29+
__VERSION__: JSON.stringify(pkg.version),
30+
},
2031
},
2132
])

vitest.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import { readFileSync } from "node:fs"
12
import { defineConfig } from "vitest/config"
23

4+
const pkg = JSON.parse(readFileSync("./package.json", "utf-8")) as {
5+
version: string
6+
}
7+
38
export default defineConfig({
9+
define: {
10+
__VERSION__: JSON.stringify(pkg.version),
11+
},
412
test: {
513
coverage: {
614
provider: "v8",

0 commit comments

Comments
 (0)