diff --git a/package.json b/package.json index be171fb..96393cd 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ ], "scripts": { "dev": "esrun --watch src/__playground.ts", + "prebuild": "node src/set-version.js", "build": "microbundle", "prepare": "microbundle", "format": "prettier --write . '**/*.{json,md,js,ts,tsx}'", diff --git a/src/set-version.js b/src/set-version.js new file mode 100644 index 0000000..4f6a161 --- /dev/null +++ b/src/set-version.js @@ -0,0 +1,6 @@ +import packageJson from "../package.json" assert { type: "json" }; + +const { version } = packageJson; + +process.env.PACKAGE_VERSION = version; +console.log(`ℹ️ Set User-Agent header version variable to ${version}`); diff --git a/src/utils/internal/call.ts b/src/utils/internal/call.ts index 5d2ee5f..6f85f42 100644 --- a/src/utils/internal/call.ts +++ b/src/utils/internal/call.ts @@ -1,3 +1,5 @@ +const packageVersion = process.env?.["PACKAGE_VERSION"] ?? "Unknown"; + /** * Fetch an HTTP resource. This is publicly exposed in the * event you would like to access an endpoint that this @@ -13,7 +15,11 @@ export const call = async < }) => { const { url } = config; - const rawResponse = await fetch(url); + const headers = new Headers({ + "User-Agent": `RetroAchievements-api-js/${packageVersion}` + }); + + const rawResponse = await fetch(url, { headers }); if (!rawResponse.ok) { throw new Error(