Skip to content

Commit

Permalink
fix(usr-agent): fix user agent to conform with the new format (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajimae authored Aug 11, 2021
1 parent 075176a commit 606c28e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ yarn.lock*
# Dependencies
node_modules
.npm
.npmrc
.released-packages

# Integration tests dependencies cache
Expand Down Expand Up @@ -38,3 +39,4 @@ setupEnv.sh
# training files
training-tmp/
loadtest/
.npmignore
11 changes: 8 additions & 3 deletions packages/sdk-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
},
"dependencies": {
"@commercetools/platform-sdk": "^1.16.0",
"node-fetch": "^2.6.1"
"node-fetch": "^2.6.1",
"querystring": "^0.2.1",
"root-require": "^0.3.1"
},
"typings": "dist/index.d.ts",
"types": "dist/index.d.ts",
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/node": "^15.9.0",
Expand All @@ -47,14 +51,15 @@
"dotenv": "^10.0.0",
"jest": "^27.0.3",
"jest-junit": "^12.1.0",
"microbundle": "^0.13.3",
"nock": "12.0.3",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.2",
"typescript": "^4.3.2"
},
"scripts": {
"prebuild": "rimraf dist/**",
"build": "rimraf dist && tsc",
"build": "microbundle",
"prebuild": "rimraf dist && tsc",
"test": "jest --maxWorkers=2"
}
}
11 changes: 2 additions & 9 deletions packages/sdk-client/src/builder/ClientBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
PasswordAuthMiddlewareOptions,
QueueMiddlewareOptions,
RefreshAuthMiddlewareOptions,
UserAgentMiddlewareOptions,
} from '../types/sdk.d'
import { default as createClient } from '../sdk-client/client'
import { default as createHttpMiddleware } from '../sdk-middleware-http/http'
Expand Down Expand Up @@ -165,14 +164,8 @@ export default class ClientBuilder {
return this
}

withUserAgentMiddleware(options: UserAgentMiddlewareOptions): ClientBuilder {
this.userAgentMiddleware = createUserAgentMiddleware({
libraryName: options.libraryName || '',
libraryVersion: options.libraryVersion || '',
contactUrl: options.contactUrl || '',
contactEmail: options.contactEmail || '',
...options,
})
withUserAgentMiddleware(): ClientBuilder {
this.userAgentMiddleware = createUserAgentMiddleware()
return this
}

Expand Down
6 changes: 4 additions & 2 deletions packages/sdk-client/src/http-user-agent/create-user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ function getSystemInfo() {
if (isBrowser()) return window.navigator.userAgent

const nodeVersion = process.version.slice(1)
const platformInfo = `(${process.platform}; ${process.arch})`
return `Node.js/${nodeVersion} ${platformInfo}`
// const platformInfo = `(${process.platform}; ${process.arch})`
// return `Node.js/${nodeVersion} ${platformInfo}`

return `node.js/${nodeVersion}`
}

export default function createUserAgent(options: HttpUserAgentOptions) {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export { default as createHttpClient } from './sdk-middleware-http/http'

export { createApiBuilderFromCtpClient } from '@commercetools/platform-sdk'
export { default as ApiRootBuilder } from './builder/ClientBuilder';
export { default as ClientBuilder} from './builder/ClientBuilder';
20 changes: 5 additions & 15 deletions packages/sdk-client/src/sdk-middleware-user-agent/user-agent.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import { default as createHttpUserAgent} from '../http-user-agent/create-user-agent'

import { default as createHttpUserAgent } from '../http-user-agent/create-user-agent'
import {
Dispatch,
Middleware,
MiddlewareRequest,
MiddlewareResponse,
MiddlewareResponse
} from '../types/sdk'

type UserAgentMiddlewareOptions = {
libraryName?: string
libraryVersion?: string
contactUrl?: string
contactEmail?: string
}

export default function createUserAgentMiddleware(
options: UserAgentMiddlewareOptions
): Middleware {
const { version } = require('root-require')('package.json')
export default function createUserAgentMiddleware(): Middleware {
const userAgent = createHttpUserAgent({
name: 'commercetools-js-sdk',
...options,
name: `commercetools-sdk-javascript-v2/${version}`,
})

return (next: Dispatch): Dispatch => (
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-client/src/types/sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export type AnonymousAuthMiddlewareOptions = {
credentials: {
clientId: string
clientSecret: string
anonymousId: string
anonymousId?: string
}
scopes?: Array<string>
// For internal usage only
Expand Down
46 changes: 17 additions & 29 deletions packages/sdk-client/test/create-user-agent.test/user-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('for browser', () => {
},
}
const userAgent = createHttpUserAgent({
name: 'commercetools-node-sdk',
name: 'commercetools-sdk-javascript',
version: '1.0.0',
libraryName: 'my-awesome-library',
libraryVersion: '1.0.0',
Expand All @@ -27,7 +27,7 @@ describe('for browser', () => {
global.window = originalWindow

test('has sdk info', () => {
expect(userAgent).toMatch('commercetools-node-sdk/1.0.0')
expect(userAgent).toMatch('commercetools-sdk-javascript')
})
test('has browser info', () => {
// because we use jsdom
Expand All @@ -46,7 +46,7 @@ describe('for browser', () => {

describe('for node', () => {
const userAgent = createHttpUserAgent({
name: 'commercetools-node-sdk',
name: 'commercetools-sdk-javascript',
version: '1.0.0',
libraryName: 'my-awesome-library',
libraryVersion: '1.0.0',
Expand All @@ -55,10 +55,10 @@ describe('for node', () => {
})

test('has sdk info', () => {
expect(userAgent).toMatch('commercetools-node-sdk/1.0.0')
expect(userAgent).toMatch('commercetools-sdk-javascript')
})
test('has node info', () => {
expect(userAgent).toMatch(`Node.js/${process.version.slice(1)}`)
expect(userAgent).toMatch(`node.js/`)
})
test('has library info', () => {
expect(userAgent).toMatch('my-awesome-library/1.0.0')
Expand Down Expand Up @@ -91,63 +91,51 @@ describe('validation', () => {
})

describe('optional information', () => {
test('create user agent with only library name (missing version)', () => {
const { version } = require('root-require')('package.json')
test('create user agent with the correct SDK', () => {
const userAgent = createHttpUserAgent({
name: 'commercetools-node-sdk',
libraryName: 'my-awesome-library',
name: `commercetools-sdk-javascript-v2/${version}`
})
expect(userAgent).toBe(
`commercetools-node-sdk Node.js/${process.version.slice(1)} (${
process.platform
}; ${process.arch}) my-awesome-library`
`commercetools-sdk-javascript-v2/${version} node.js/${process.version.slice(1)}`
)
})
test('create user agent with library name and version', () => {
const userAgent = createHttpUserAgent({
name: 'commercetools-node-sdk',
name: `commercetools-sdk-javascript-v2/${version}`,
libraryName: 'my-awesome-library',
libraryVersion: '1.0.0',
})
expect(userAgent).toBe(
`commercetools-node-sdk Node.js/${process.version.slice(1)} (${
process.platform
}; ${process.arch}) my-awesome-library/1.0.0`
`commercetools-sdk-javascript-v2/${version} node.js/${process.version.slice(1)} my-awesome-library/1.0.0`
)
})
test('create user agent with contact url', () => {
const userAgent = createHttpUserAgent({
name: 'commercetools-node-sdk',
name: `commercetools-sdk-javascript-v2/${version}`,
contactUrl: 'https://commercetools.com',
})
expect(userAgent).toBe(
`commercetools-node-sdk Node.js/${process.version.slice(1)} (${
process.platform
}; ${process.arch}) (+https://commercetools.com)`
`commercetools-sdk-javascript-v2/${version} node.js/${process.version.slice(1)} (+https://commercetools.com)`
)
})
test('create user agent with contact email', () => {
const userAgent = createHttpUserAgent({
name: 'commercetools-node-sdk',
name: `commercetools-sdk-javascript-v2/${version}`,
contactEmail: 'helpdesk@commercetools.com',
})
expect(userAgent).toBe(
`commercetools-node-sdk Node.js/${process.version.slice(1)} (${
process.platform
}; ${process.arch}) (+helpdesk@commercetools.com)`
`commercetools-sdk-javascript-v2/${version} node.js/${process.version.slice(1)} (+helpdesk@commercetools.com)`
)
})
test('create user agent with full contact info', () => {
const userAgent = createHttpUserAgent({
name: 'commercetools-node-sdk',
name: `commercetools-sdk-javascript-v2/${version}`,
contactUrl: 'https://commercetools.com',
contactEmail: 'helpdesk@commercetools.com',
})
expect(userAgent).toBe(
`commercetools-node-sdk Node.js/${process.version.slice(1)} (${
process.platform
}; ${
process.arch
}) (+https://commercetools.com; +helpdesk@commercetools.com)`
`commercetools-sdk-javascript-v2/${version} node.js/${process.version.slice(1)} (+https://commercetools.com; +helpdesk@commercetools.com)`
)
})
})
20 changes: 3 additions & 17 deletions packages/sdk-client/test/user-agent.test/user-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import {
import { createUserAgentMiddleware } from '../../src/sdk-middleware-user-agent'

describe('UserAgent', () => {
const userAgentMiddleware = createUserAgentMiddleware({
libraryName: 'my-awesome-library',
libraryVersion: '1.0.0',
contactUrl: 'https://commercetools.com',
contactEmail: 'helpdesk@commercetools.com',
})
const userAgentMiddleware = createUserAgentMiddleware()
const request: MiddlewareRequest = {
method: 'GET',
uri: '/foo',
Expand All @@ -33,25 +28,16 @@ describe('UserAgent', () => {
expect(headers.Authorization).toBe('123')
})
test('has sdk info', () => {
expect(headers['User-Agent']).toMatch('commercetools-js-sdk')
expect(headers['User-Agent']).toMatch('commercetools-sdk-javascript')
})
test('has browser info', () => {
// because we use jsdom
expect(headers['User-Agent']).toMatch('Node.js')
expect(headers['User-Agent']).toMatch('node.js')
})
test('has browser version', () => {
// because we use jsdom
expect(headers['User-Agent']).toMatch(process.version.slice(1))
})
test('has library info', () => {
expect(headers['User-Agent']).toMatch('my-awesome-library/1.0.0')
})
test('has library url', () => {
expect(headers['User-Agent']).toMatch('https://commercetools.com')
})
test('has contact info', () => {
expect(headers['User-Agent']).toMatch('helpdesk@commercetools.com')
})
test('do not change response object', () => {
expect(res).toEqual({ ...res })
})
Expand Down

0 comments on commit 606c28e

Please sign in to comment.