diff --git a/package.json b/package.json index 07d6ab1..3ab8d98 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,9 @@ "dist/**/*" ], "scripts": { - "build": "rm -rf dist/* && yarn build:esm && yarn build:cjs", - "build:esm": "tsc -p tsconfig.esm.json && cp package.json dist/esm", - "build:cjs": "tsc -p tsconfig.cjs.json && cp package.json dist/cjs", + "build": "rm -rf dist/* && yarn build:esm && yarn build:cjs && package.json dist", + "build:esm": "tsc -p tsconfig.esm.json", + "build:cjs": "tsc -p tsconfig.cjs.json", "test": "jest", "fmt": "prettier --write \"./**/*.ts\"", "lint": "eslint ./src" diff --git a/src/api/router.ts b/src/api/router.ts index 10ac927..1642184 100644 --- a/src/api/router.ts +++ b/src/api/router.ts @@ -5,11 +5,11 @@ import { payloadJSON, payloadSearchParams, } from "../types"; -import { version } from "../../package.json"; import fetch from "cross-fetch"; import log from "loglevel"; -import { logPrefix } from "../utils"; +import { getVersion, logPrefix } from "../utils"; +const CLIENT_VERSION = getVersion(); const BASE_URL = "https://api.dune.com/api"; enum RequestMethod { @@ -86,7 +86,7 @@ export class Router { method, headers: { "x-dune-api-key": this.apiKey, - "User-Agent": `client-sdk@${version} (https://www.npmjs.com/package/@duneanalytics/client-sdk)`, + "User-Agent": `client-sdk@${CLIENT_VERSION} (https://www.npmjs.com/package/@duneanalytics/client-sdk)`, "Content-Type": content_type, }, // conditionally add the body property diff --git a/src/utils.ts b/src/utils.ts index f8f45d9..db69a88 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -41,3 +41,15 @@ export function withDefaults(obj: T, defaults: Partial): T { } return result as T; } + +export function getVersion(): string { + try { + // @ts-expect-error: cheeky hack + import { version } from "../package.json"; + return version; + } catch { + // @ts-expect-error: cheeky hack + import { version } from "../../package.json"; + return version; + } +} diff --git a/tsconfig.json b/tsconfig.json index 377010d..c14df76 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,6 +22,6 @@ "rootDir": "src" }, - "include": ["src"], + "include": ["src", "package.json"], "exclude": ["node_modules", "dist", "tests"] // Added "dist" to avoid recompiling the output }