Skip to content

Commit

Permalink
[client] Make node-specific middleware be excluded from bundles (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Sep 19, 2017
1 parent 91b17b1 commit f25d3a8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"prepublish": "in-publish && npm run build || not-in-publish",
"test": "NODE_ENV=test tape -r babel-register test/*.test.js"
},
"browser": {
"./src/http/nodeMiddleware.js": "./src/http/browserMiddleware.js",
"./lib/http/nodeMiddleware.js": "./lib/http/browserMiddleware.js"
},
"dependencies": {
"@sanity/eventsource": "^0.111.5",
"@sanity/generate-help-url": "^0.111.5",
Expand Down
1 change: 1 addition & 0 deletions src/http/browserMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = []
10 changes: 10 additions & 0 deletions src/http/nodeMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const debug = require('get-it/lib/middleware/debug')
const headers = require('get-it/lib/middleware/headers')
const pkg = require('../../package.json')

const middleware = [
debug({verbose: true, namespace: 'sanity:client'}),
headers({'User-Agent': `${pkg.name} ${pkg.version}`})
]

module.exports = middleware
19 changes: 5 additions & 14 deletions src/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,16 @@ const httpError = ({
}
})

const middleware = [
// Environment-specific middleware.
const envSpecific = require('./nodeMiddleware')

const middleware = envSpecific.concat([
jsonRequest(),
jsonResponse(),
progress(),
httpError,
observable({implementation: SanityObservable})
]

// 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

0 comments on commit f25d3a8

Please sign in to comment.