diff --git a/README.md b/README.md index 5b4af4a..b7764ba 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,12 @@ You will need: * [Node.js](http://nodejs.org/) * [npm](https://www.npmjs.org/) +If you are on Apple Silicon, switch to Rosetta mode first: +`env /usr/bin/arch -x86_64 /bin/zsh --login` + +You should then be able to install node: +`asdf install nodejs 12.10.0` + Then run: `npm install` - Install dependencies diff --git a/lib/client.js b/lib/client.js index 1902af0..8b35a74 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,6 +1,6 @@ /* global URL */ import pkgJson from '../package.json' -import { when, isObject, isString, debounce } from './utils' +import { when, isObject, isString, debounce, updateUserAgentWithAppId } from './utils' import IdleState from './utils/idleState' import Tracker from './tracker' import NativePromise from 'native-promise-only' @@ -557,6 +557,9 @@ export default class Client { options = { url: options } } + const appId = this._metadata && this._metadata.appId + options.headers = updateUserAgentWithAppId(options.headers, appId) + const doneEventKey = `${requestKey}.done` const failEventKey = `${requestKey}.fail` diff --git a/lib/utils.js b/lib/utils.js index adde86f..7dd6f0a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,21 @@ import NativePromise from 'native-promise-only' +import pkgJson from '../package.json' + const Promise = window.Promise || NativePromise +export function updateUserAgentWithAppId (headers, appId) { + if (!appId) { + return headers + } + const originalUserAgent = headers && headers['User-Agent'] ? headers['User-Agent'] : '' + const zafSdkUserAgentString = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` + const userAgent = originalUserAgent ? `${originalUserAgent} ${zafSdkUserAgentString}` : zafSdkUserAgentString + return { + ...headers, + 'User-Agent': userAgent + } +} + export function isObject (obj) { return (obj !== null && typeof obj === 'object') } diff --git a/package.json b/package.json index 0d311d9..9a9e2a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zendesk_app_framework_sdk", - "version": "2.0.37", + "version": "2.0.38", "main": "build/zaf_sdk.min.js", "description": "The Zendesk App Framework (ZAF) SDK is a JavaScript library that simplifies cross-frame communication between iframed apps and ZAF.", "homepage": "http://developer.zendesk.com", diff --git a/spec/utils_spec.js b/spec/utils_spec.js index 374caba..8e27b02 100644 --- a/spec/utils_spec.js +++ b/spec/utils_spec.js @@ -1,6 +1,7 @@ /* eslint-env mocha */ /* global expect Promise */ import * as Utils from '../lib/utils' +import pkgJson from '../package.json' describe('Utils', () => { let params @@ -151,4 +152,58 @@ describe('Utils', () => { expect(Utils.isObject(['a'])).to.equal(true) }) }) + + describe('.updateUserAgentWithAppId', () => { + before(() => { + // Mock pkgJson + global.pkgJson = { version: '1.0.0' } + }) + + describe('with a valid app id', () => { + let appId + + beforeEach(() => { + appId = '1' + }) + + it('should append the app id to the user agent', () => { + const headers = { 'User-Agent': 'Mozilla/5.0' } + const updatedHeaders = Utils.updateUserAgentWithAppId(headers, appId) + const expectedUserAgent = `Mozilla/5.0 zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` + expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent) + }) + + it('should handle an undefined headers object', () => { + const updatedHeaders = Utils.updateUserAgentWithAppId(undefined, appId) + const expectedUserAgent = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` + expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent) + }) + + it('should handle a headers object with no User-Agent', () => { + const headers = {} + const updatedHeaders = Utils.updateUserAgentWithAppId(headers, appId) + const expectedUserAgent = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` + expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent) + }) + }) + + describe('with an undefined app id', () => { + it('should not append the app id to the user agent', () => { + const headers = { 'User-Agent': 'Mozilla/5.0' } + const updatedHeaders = Utils.updateUserAgentWithAppId(headers) + expect(updatedHeaders['User-Agent']).to.equal('Mozilla/5.0') + }) + + it('should handle an undefined headers object', () => { + const updatedHeaders = Utils.updateUserAgentWithAppId() + expect(updatedHeaders).to.be.undefined() + }) + + it('should handle a headers object with no User-Agent', () => { + const headers = {} + const updatedHeaders = Utils.updateUserAgentWithAppId(headers) + expect(updatedHeaders['User-Agent']).to.equal(undefined) + }) + }) + }) })