Skip to content

Commit

Permalink
fix: follow standard user-agent header format
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Dec 18, 2023
1 parent bb6f504 commit d62e100
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/synchronizer/src/handlers/apiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ export type ApiRepoIdData = {
export type ClientConfig = {
name: string;
version: string;
os?: string;
additionalData?: Record<string, string>;
};

export class ApiHandler {
Expand Down Expand Up @@ -210,6 +212,8 @@ export class ApiHandler {
this._clientConfig = {
name: clientConfig?.name || 'unknown',
version: clientConfig?.version || 'unknown',
os: clientConfig?.os || '',
additionalData: clientConfig?.additionalData || {},
};
}

Expand Down Expand Up @@ -293,7 +297,7 @@ export class ApiHandler {
headers: {
'Content-Type': 'application/json',
Authorization: this.formatAuthorizationHeader(tokenInfo),
'User-Agent': `${this._clientConfig.name}; ${this._clientConfig.version}`,
'User-Agent': this.formatUserAgentHeader(this._clientConfig),
},
body: JSON.stringify({
query,
Expand All @@ -310,4 +314,18 @@ export class ApiHandler {
: 'Bearer';
return `${tokenType} ${tokenInfo.accessToken}`;
}

private formatUserAgentHeader(clientConfig: ClientConfig) {
const product = `${clientConfig.name}/${clientConfig.version}`;

const comment = [];
if (clientConfig.os) {
comment.push(clientConfig.os);
}
if (clientConfig.additionalData) {
comment.push(Object.entries(clientConfig.additionalData).map(([key, value]) => `${key}=${value}`));
}

return `${product}${comment.length > 0 ? ` (${comment.join('; ')})` : ''}`;
}
}

0 comments on commit d62e100

Please sign in to comment.