Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(call): add user agent based on package version #76

Merged
merged 1 commit into from
Feb 25, 2024
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
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
Loading