Skip to content
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
4 changes: 3 additions & 1 deletion release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"bump-patch-for-minor-pre-major": true,
"include-component-in-tag": false,
"packages": {
".": {}
".": {
"extra-files": ["src/version.ts"]
}
}
}
5 changes: 5 additions & 0 deletions src/http-client.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
/**
Expand Down Expand Up @@ -105,6 +108,8 @@ export class HttpClient {
const headers: Record<string, string> = {
Authorization: `Bearer ${this.token}`,
Accept: 'application/json',
'User-Agent': USER_AGENT,
'X-Capawesome-Client': API_CLIENT,
};
let body: FormData | string | undefined;
if (options.formData) {
Expand Down
1 change: 1 addition & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VERSION = '0.1.7'; // x-release-please-version
17 changes: 17 additions & 0 deletions test/http-client.test.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand All @@ -18,6 +20,21 @@ describe('HttpClient', () => {
expect((init.headers as Record<string, string>).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<string, string>;
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' });
Expand Down
Loading