diff --git a/release-please-config.json b/release-please-config.json index cfd1d28..6c63eb4 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -5,6 +5,8 @@ "bump-patch-for-minor-pre-major": true, "include-component-in-tag": false, "packages": { - ".": {} + ".": { + "extra-files": ["src/version.ts"] + } } } diff --git a/src/http-client.ts b/src/http-client.ts index 10aab29..a735a38 100644 --- a/src/http-client.ts +++ b/src/http-client.ts @@ -1,4 +1,5 @@ import { CapawesomeCloudError } from './errors'; +import { VERSION } from './version'; export const DEFAULT_BASE_URL = 'https://api.cloud.capawesome.io'; export const DEFAULT_TIMEOUT = 60_000; @@ -7,6 +8,8 @@ export const DEFAULT_MAX_RETRIES = 3; const RETRY_BASE_DELAY = 300; const RETRY_MAX_DELAY = 10_000; const IDEMPOTENT_METHODS = new Set(['GET', 'HEAD', 'PUT', 'DELETE']); +const API_CLIENT = `cloud-node/${VERSION}`; +const USER_AGENT = `capawesome-cloud-node/${VERSION}`; export interface HttpClientOptions { /** @@ -105,6 +108,8 @@ export class HttpClient { const headers: Record = { Authorization: `Bearer ${this.token}`, Accept: 'application/json', + 'User-Agent': USER_AGENT, + 'X-Capawesome-Client': API_CLIENT, }; let body: FormData | string | undefined; if (options.formData) { diff --git a/src/version.ts b/src/version.ts new file mode 100644 index 0000000..f78b328 --- /dev/null +++ b/src/version.ts @@ -0,0 +1 @@ +export const VERSION = '0.1.7'; // x-release-please-version diff --git a/test/http-client.test.ts b/test/http-client.test.ts index 084700e..f4e8a2e 100644 --- a/test/http-client.test.ts +++ b/test/http-client.test.ts @@ -1,5 +1,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; +import packageJson from '../package.json'; import { CapawesomeCloud, CapawesomeCloudError } from '../src/index'; +import { VERSION } from '../src/version'; import { mockFetchJson } from './helpers'; afterEach(() => { @@ -18,6 +20,21 @@ describe('HttpClient', () => { expect((init.headers as Record).Authorization).toBe('Bearer test-token'); }); + it('names itself in the User-Agent and the client header', async () => { + const { getLastRequest } = mockFetchJson([]); + const client = new CapawesomeCloud({ token: 't' }); + + await client.apps.list({ organizationId: 'org-1' }); + + const headers = getLastRequest().init.headers as Record; + expect(headers['User-Agent']).toBe(`capawesome-cloud-node/${VERSION}`); + expect(headers['X-Capawesome-Client']).toBe(`cloud-node/${VERSION}`); + }); + + it('names the version of package.json', () => { + expect(VERSION).toBe(packageJson.version); + }); + it('respects a custom base url', async () => { const { getLastRequest } = mockFetchJson([]); const client = new CapawesomeCloud({ token: 't', baseUrl: 'https://example.com' });