Skip to content

Commit

Permalink
[client] Set User-Agent in non-browser environments (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Sep 14, 2017
1 parent 1262dd3 commit 7931275
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/@sanity/client/src/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ const middleware = [
observable({implementation: SanityObservable})
]

// Don't include debug middleware in browsers
// Node-specifics
if (process.env.BROWSERIFY_ENV !== 'build') {
// Only include debug middleware in browsers
const debug = require('get-it/lib/middleware/debug')
middleware.unshift(debug({verbose: true, namespace: 'sanity:client'}))

// Assign user agent in node
const headers = require('get-it/lib/middleware/headers')
const pkg = require('../../package.json')
middleware.unshift(headers({'User-Agent': `${pkg.name} ${pkg.version}`}))
}

const request = getIt(middleware)
Expand Down
12 changes: 12 additions & 0 deletions packages/@sanity/client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,18 @@ test('includes token if set', t => {
.then(t.end)
})

test('includes user agent in node', t => {
const pkg = require('../package.json')
const reqheaders = {'User-Agent': `${pkg.name} ${pkg.version}`}
nock(projectHost(), {reqheaders})
.get('/v1/data/doc/foo/bar')
.reply(200, {documents: []})

getClient().getDocument('bar')
.catch(t.ifError)
.then(t.end)
})

// Don't rely on this unless you're working at Sanity Inc ;)
test('can use alternative http requester', t => {
const requester = () => sanityObservable.of({
Expand Down

0 comments on commit 7931275

Please sign in to comment.