Skip to content

Commit a376ca5

Browse files
committed
feat: set UserAgent version to 0.0.0-dev when developing
1 parent 7074cac commit a376ca5

File tree

7 files changed

+58
-23
lines changed

7 files changed

+58
-23
lines changed

examples/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Nutrient DWS Processor API Key
2-
NUTRIENT_API_KEY=your_api_key_here
2+
NUTRIENT_API_KEY=your_api_key_here
3+
NODE_ENV=development

src/__tests__/integration.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { NutrientClient } from '../client';
1111
import { BuildActions } from '../build';
1212
import type { NutrientClientOptions, OutputTypeMap } from '../types';
1313
import 'dotenv/config';
14-
import { ResultValidator, sampleDOCX, samplePDF, samplePNG, TestDocumentGenerator } from './helpers';
14+
import {
15+
ResultValidator,
16+
sampleDOCX,
17+
samplePDF,
18+
samplePNG,
19+
TestDocumentGenerator,
20+
} from './helpers';
1521
import { getPdfPageCount, processFileInput } from '../inputs';
1622

1723
// Skip integration tests in CI/automated environments unless explicitly enabled with valid API key
@@ -317,13 +323,15 @@ describeIntegration('Integration Tests with Live API - Direct Methods', () => {
317323
it('should handle network timeouts', async () => {
318324
const timeoutClient = new NutrientClient({
319325
apiKey: process.env['NUTRIENT_API_KEY'] ?? '',
320-
timeout: 1
321-
})
326+
timeout: 1,
327+
});
322328

323-
await expect(timeoutClient.convert(sampleDOCX, 'pdf')).rejects.toThrow('Network request failed');
329+
await expect(timeoutClient.convert(sampleDOCX, 'pdf')).rejects.toThrow(
330+
'Network request failed',
331+
);
324332
}, 15000);
325333
});
326-
})
334+
});
327335

328336
describeIntegration('Integration Tests with Live API- Workflow', () => {
329337
let client: NutrientClient;

src/__tests__/unit/http.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe('HTTP Layer', () => {
376376
try {
377377
await errorPromise;
378378
} catch (error) {
379-
console.error(error)
379+
console.error(error);
380380
thrownError = error as NetworkError;
381381
}
382382

@@ -596,10 +596,12 @@ describe('HTTP Layer', () => {
596596
);
597597

598598
// Verify the User-Agent format is correct
599-
const mockCalls = (mockedAxios as jest.Mock).mock.calls as unknown[][];
599+
const mockCalls = (mockedAxios as unknown as jest.Mock).mock.calls as unknown[][];
600600
const firstCallArgs = mockCalls[0] as [{ headers?: { 'User-Agent'?: string } }];
601601
const userAgent = firstCallArgs[0]?.headers?.['User-Agent'];
602-
expect(userAgent).toMatch(/^nutrient-dws-client-typescript\/\d+\.\d+\.\d+$/);
602+
expect(userAgent).toMatch(
603+
/^nutrient-dws-client-typescript\/\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/,
604+
);
603605
});
604606
});
605607
});

src/__tests__/unit/utils.test.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ describe('Utility Functions', () => {
77
describe('getLibraryVersion()', () => {
88
it('should return a valid semver version string', () => {
99
const version = getLibraryVersion();
10-
10+
1111
expect(version).toBeDefined();
1212
expect(typeof version).toBe('string');
1313
expect(version.length).toBeGreaterThan(0);
14-
14+
1515
// Check if it matches semver pattern (major.minor.patch)
1616
const semverPattern = /^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/;
1717
expect(version).toMatch(semverPattern);
1818
});
1919

2020
it('should return the version from package.json', () => {
2121
const version = getLibraryVersion();
22-
22+
2323
// The version should match whatever is in package.json
2424
// We don't hardcode the expected version to avoid breaking on version updates
2525
expect(version).toMatch(/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/);
@@ -28,47 +28,67 @@ describe('Utility Functions', () => {
2828
});
2929

3030
describe('getUserAgent()', () => {
31+
const originalEnv = process.env;
32+
33+
beforeEach(() => {
34+
jest.resetModules(); // Important to clear cached modules
35+
process.env = { ...originalEnv };
36+
});
37+
38+
afterEach(() => {
39+
process.env = originalEnv; // Restore original environment
40+
});
41+
3142
it('should return a properly formatted User-Agent string', () => {
3243
const userAgent = getUserAgent();
33-
44+
3445
expect(userAgent).toBeDefined();
3546
expect(typeof userAgent).toBe('string');
3647
expect(userAgent.length).toBeGreaterThan(0);
3748
});
3849

3950
it('should follow the expected User-Agent format', () => {
4051
const userAgent = getUserAgent();
41-
52+
4253
// Should match: nutrient-dws-client-typescript/VERSION
4354
const expectedPattern = /^nutrient-dws-client-typescript\/\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/;
4455
expect(userAgent).toMatch(expectedPattern);
4556
});
4657

58+
it('should follow the expected User-Agent format when in development', () => {
59+
process.env = { ...originalEnv, NODE_ENV: 'development' };
60+
const userAgent = getUserAgent();
61+
62+
// Should match: nutrient-dws-client-typescript/VERSION-dev
63+
const expectedPattern = /^nutrient-dws-client-typescript\/0\.0\.0-dev$/;
64+
expect(userAgent).toMatch(expectedPattern);
65+
});
66+
4767
it('should include the correct library name', () => {
4868
const userAgent = getUserAgent();
49-
69+
5070
expect(userAgent).toContain('nutrient-dws-client-typescript');
5171
});
5272

5373
it('should include the current library version', () => {
5474
const userAgent = getUserAgent();
5575
const version = getLibraryVersion();
56-
76+
5777
expect(userAgent).toContain(version);
5878
});
5979

6080
it('should have consistent format across multiple calls', () => {
6181
const userAgent1 = getUserAgent();
6282
const userAgent2 = getUserAgent();
63-
83+
6484
expect(userAgent1).toBe(userAgent2);
6585
});
6686

6787
it('should return the expected User-Agent format with current version', () => {
6888
const userAgent = getUserAgent();
6989
const version = getLibraryVersion();
70-
90+
7191
expect(userAgent).toBe(`nutrient-dws-client-typescript/${version}`);
7292
});
7393
});
74-
});
94+
});

src/http.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ function convertError<Method extends Methods, Endpoint extends Endpoints<Method>
324324
if (request) {
325325
const sanitizedHeaders = config.headers;
326326
if (sanitizedHeaders) {
327-
delete sanitizedHeaders['Authorization']
327+
delete sanitizedHeaders['Authorization'];
328328
}
329329
// Network error (request made but no response)
330330
return new NetworkError('Network request failed', {
@@ -336,7 +336,8 @@ function convertError<Method extends Methods, Endpoint extends Endpoints<Method>
336336
}
337337

338338
// Request setup error
339-
return new ValidationError('Request configuration error', { message,
339+
return new ValidationError('Request configuration error', {
340+
message,
340341
endpoint: config.endpoint,
341342
method: config.method,
342343
data: config.data,
@@ -350,4 +351,4 @@ function convertError<Method extends Methods, Endpoint extends Endpoints<Method>
350351
method: config.method,
351352
data: config.data,
352353
});
353-
}
354+
}

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* Utility functions for the Nutrient DWS TypeScript Client
33
*/
4-
export { getLibraryVersion, getUserAgent } from './version';
4+
export { getLibraryVersion, getUserAgent } from './version';

src/utils/version.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import packageJson from '../../package.json';
88
* @returns The version string from package.json
99
*/
1010
export function getLibraryVersion(): string {
11+
if (process.env['NODE_ENV'] === 'development') {
12+
return '0.0.0-dev';
13+
}
1114
return packageJson.version;
1215
}
1316

0 commit comments

Comments
 (0)