Skip to content

Commit d91f188

Browse files
committed
Add User-agent to packagephobia requests
1 parent 68f0565 commit d91f188

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/lib/api/packagephobia/packagephobia.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { USER_AGENT } from "../user-agent";
12
import type PackagephobiaResponse from "./PackagephobiaResponse";
23
import type PackagephobiaResults from "./PackagephobiaResult";
34

@@ -7,6 +8,9 @@ async function getPackage(spec: string): Promise<PackagephobiaResponse | null> {
78
`https://packagephobia.com/v2/api.json?p=${spec}`,
89
{
910
signal: AbortSignal.timeout(7_500),
11+
headers: {
12+
"User-Agent": USER_AGENT,
13+
},
1014
},
1115
);
1216

src/lib/api/user-agent.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { VERCEL_ENV, VERCEL_DEPLOY_ID, VERCEL_GIT_COMMIT_SHA } = process.env;
2+
3+
/**
4+
* Be nice to APIs and inform them who we are, with traceability when deployed on Vercel.
5+
*/
6+
export const USER_AGENT = (() => {
7+
const parts = [`env=${VERCEL_ENV ?? "development"}`];
8+
9+
if (VERCEL_DEPLOY_ID) {
10+
parts.push(`deploy=${VERCEL_DEPLOY_ID}`);
11+
}
12+
if (VERCEL_GIT_COMMIT_SHA) {
13+
parts.push(`commit=${VERCEL_GIT_COMMIT_SHA}`);
14+
}
15+
16+
return `npm-diff.app (${parts.join("; ")}; +https://github.com/oBusk/npm-diff.app)`;
17+
})();

0 commit comments

Comments
 (0)