diff --git a/package.json b/package.json index 1400463d04b7..e97b52179e54 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/http/browserMiddleware.js b/src/http/browserMiddleware.js new file mode 100644 index 000000000000..d7faf17f9b95 --- /dev/null +++ b/src/http/browserMiddleware.js @@ -0,0 +1 @@ +module.exports = [] diff --git a/src/http/nodeMiddleware.js b/src/http/nodeMiddleware.js new file mode 100644 index 000000000000..71d79af65733 --- /dev/null +++ b/src/http/nodeMiddleware.js @@ -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 diff --git a/src/http/request.js b/src/http/request.js index f67582721b09..4e3afe1c3d1a 100644 --- a/src/http/request.js +++ b/src/http/request.js @@ -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)