Skip to content

Commit

Permalink
fix(call): add user agent based on package version (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Feb 25, 2024
1 parent 84618a3 commit a0aad7c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}'",
Expand Down
6 changes: 6 additions & 0 deletions src/set-version.js
Original file line number Diff line number Diff line change
@@ -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}`);
8 changes: 7 additions & 1 deletion src/utils/internal/call.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down

0 comments on commit a0aad7c

Please sign in to comment.