Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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`

Expand Down
15 changes: 15 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -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}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon the version in pkgJson doesn't reflect the actual version, this PR didn't touch on the version as well

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Sheen, should I remove this? Note that I've bumped the version in package.json in this commit but I can remove if you prefer.

Copy link
Contributor

@Shengming-Yan Shengming-Yan Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon when customer refer to zaf_sdk in their app, it is using the release version now. So this version is unrelavant unless we can manually keep align with the release version.

With regarding to shall we keep this, as reviewing the purpose of this PR, wonder do you need this info for internal debug only? In that case, probably considering adding x-zendesk-app-framework-sdk-version header for this instead of chain the existing user agent? As all x-zendesk- headers will be filtered(unless in allowlist) out before zendesk proxy dispatch the request, so it will have zero customer impact and in the meantime, internal can still track the zaf-sdk version in the log

const userAgent = originalUserAgent ? `${originalUserAgent} ${zafSdkUserAgentString}` : zafSdkUserAgentString
return {
...headers,
'User-Agent': userAgent
}
}

export function isObject (obj) {
return (obj !== null && typeof obj === 'object')
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
55 changes: 55 additions & 0 deletions spec/utils_spec.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
})
})
})
})
Loading