Skip to content

Commit

Permalink
feat(core): Add getUserAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaledgarbaya authored May 17, 2017
1 parent 07f2282 commit ada655b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/get-user-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import platform from 'platform'

export default function getUserAgentHeader (sdk, application, integration) {
const headerParts = []

headerParts.push(application ? `app ${application}` : '')
headerParts.push(integration ? `integration ${integration}` : '')
headerParts.push(`sdk ${sdk}`)
headerParts.push(`platform ${platform.name}/${platform.version}`)
headerParts.push(`os ${platform.os.family}/${platform.os.version || '0.0.0'}`)
return headerParts.filter((item) => item !== '').join('; ')
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"es6-promise": "^4.0.5",
"lodash": "^4.17.4",
"npmlog": "^4.0.2",
"platform": "1.3.3",
"qs": "^6.1.0"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/user-agent-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import test from 'blue-tape'
import getUserAgent from '../../lib/get-user-agent'

test('Parse User agent correctly', (t) => {
t.plan(1)
const headerRegEx = /(app|sdk|platform|integration|os) (\S+\/\d.\d.\d)(-\w+)?/igm
const userAgent = getUserAgent('contentful.js/1.0.0', 'myApplication/1.0.0', 'myIntegration/1.0.0')
t.equal(userAgent.match(headerRegEx).length, 5)
})
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2850,6 +2850,10 @@ pkg-dir@^1.0.0:
dependencies:
find-up "^1.0.0"

[email protected]:
version "1.3.3"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.3.tgz#646c77011899870b6a0903e75e997e8e51da7461"

pluralize@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-1.2.1.tgz#d1a21483fd22bb41e58a12fa3421823140897c45"
Expand Down

0 comments on commit ada655b

Please sign in to comment.