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
25 changes: 25 additions & 0 deletions src/box-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ function getFullURL(defaultBasePath: string, url: string) {
return defaultBasePath + url;
}

/**
* Get the box bundle version from environment or global variable
* @returns {string|null} The bundle version or null if not found
*/
function getBoxBundleVersion(): string | null {
if (typeof process !== 'undefined' && process.env?.NPM_BOX_VERSION) {
return process.env.NPM_BOX_VERSION;
}

if (
typeof globalThis !== 'undefined' &&
(globalThis as any).__BOX_PACKAGE_VERSION
) {
return (globalThis as any).__BOX_PACKAGE_VERSION;
}

return null;
}

/**
* Construct the X-Box-UA header to send analytics identifiers
* @param {Object} [client] Analytics client information
Expand All @@ -186,6 +205,12 @@ function constructBoxUAHeader(client: any /* FIXME */) {
analyticsIdentifiers.client = `${client.name}/${client.version}`;
}

// Add bundle information if box-node-sdk is used through npm-box meta-package
const bundleVersion = getBoxBundleVersion();
if (bundleVersion) {
analyticsIdentifiers['bundle'] = `box/${bundleVersion}`;
}

return Object.keys(analyticsIdentifiers)
.map((k) => `${k}=${analyticsIdentifiers[k]}`)
.join('; ');
Expand Down
78 changes: 78 additions & 0 deletions tests/manual-sdk/lib/box-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,84 @@ describe('box-client', function () {
return client._makeRequest({});
});

it('should attach bundle info in X-Box-UA header when NPM_BOX_VERSION env var is set', function () {
var originalEnvValue = process.env.NPM_BOX_VERSION;
process.env.NPM_BOX_VERSION = '1.2.3';

sandbox
.stub(apiSessionFake, 'getAccessToken')
.returns(Promise.resolve(FAKE_ACCESS_TOKEN));

var expectedHeader = `agent=box-node-sdk/${pkg.version}; env=Node/${process.version.slice(1)}; bundle=box/1.2.3`;

sandbox
.mock(requestManagerFake)
.expects('makeRequest')
.withArgs({
headers: sinon.match({
'X-Box-UA': expectedHeader,
}),
})
.returns(Promise.resolve(fakeOKResponse));

var promise = basicClient._makeRequest({});

// Clean up after the promise completes
return promise.then(function() {
if (originalEnvValue !== undefined) {
process.env.NPM_BOX_VERSION = originalEnvValue;
} else {
delete process.env.NPM_BOX_VERSION;
}
}).catch(function(err) {
if (originalEnvValue !== undefined) {
process.env.NPM_BOX_VERSION = originalEnvValue;
} else {
delete process.env.NPM_BOX_VERSION;
}
throw err;
});
});

it('should attach bundle info in X-Box-UA header when __BOX_PACKAGE_VERSION global is set', function () {
var originalGlobalValue = globalThis.__BOX_PACKAGE_VERSION;
globalThis.__BOX_PACKAGE_VERSION = '2.3.4';

sandbox
.stub(apiSessionFake, 'getAccessToken')
.returns(Promise.resolve(FAKE_ACCESS_TOKEN));

var expectedHeader = `agent=box-node-sdk/${pkg.version}; env=Node/${process.version.slice(1)}; bundle=box/2.3.4`;

sandbox
.mock(requestManagerFake)
.expects('makeRequest')
.withArgs({
headers: sinon.match({
'X-Box-UA': expectedHeader,
}),
})
.returns(Promise.resolve(fakeOKResponse));

var promise = basicClient._makeRequest({});

// Clean up after the promise completes
return promise.then(function() {
if (originalGlobalValue !== undefined) {
globalThis.__BOX_PACKAGE_VERSION = originalGlobalValue;
} else {
delete globalThis.__BOX_PACKAGE_VERSION;
}
}).catch(function(err) {
if (originalGlobalValue !== undefined) {
globalThis.__BOX_PACKAGE_VERSION = originalGlobalValue;
} else {
delete globalThis.__BOX_PACKAGE_VERSION;
}
throw err;
});
});

it('should not overwrite the "BoxAPI" header when it already exists', function () {
var explicitBoxApiHeader =
'shared_link=box.com/alreadyset&shared_link_password=456',
Expand Down
Loading