Skip to content

Commit

Permalink
[base] Expose client as CommonJS (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Aug 24, 2017
1 parent 275cc61 commit 4cfbfe5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/@sanity/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"devDependencies": {
"@sanity/check": "^0.108.0",
"@sanity/plugin-loader": "^0.108.0",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"eslint": "^3.19.0",
Expand Down
25 changes: 24 additions & 1 deletion packages/@sanity/base/src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@ import sanityClient from '@sanity/client'
import config from 'config:sanity'
import configureClient from 'part:@sanity/base/configure-client?'

const deprecationMessage = `[deprecation] The Sanity client is now exposed in CommonJS format.
For instance, change:
\`const client = require('part:@sanity/base/client').default\`
To the following:
\`const client = require('part:@sanity/base/client')\`
`

const apiConfig = {...config.api, withCredentials: true}
const client = sanityClient(apiConfig)

export default configureClient ? configureClient(sanityClient(apiConfig)) : client
const configuredClient = configureClient
? configureClient(sanityClient(apiConfig))
: client

// Warn when people use `.default`
Object.defineProperty(configuredClient, 'default', {
get() {
// eslint-disable-next-line no-console
console.warn(deprecationMessage)
return configuredClient
}
})

// Expose as CJS to allow Node scripts to consume it without `.default`
module.exports = configuredClient
15 changes: 15 additions & 0 deletions packages/@sanity/base/test/client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {describe, it} from 'mocha'
import {expect} from 'chai'
import client from '../src/client'

describe('client', () => {
it('should be exposed in CommonJS format', done => {
expect(client.fetch).to.be.a('function')
done()
})

it('should still expose client on .default, but give warning', done => {
expect(client.default.fetch).to.be.a('function')
done()
})
})
7 changes: 7 additions & 0 deletions packages/@sanity/base/test/init.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import pluginLoader from '@sanity/plugin-loader'

pluginLoader({
overrides: {
'config:sanity': [{api: {projectId: 'abc123', dataset: 'hei'}}]
}
})

chai.should()
chai.use(chaiAsPromised)
8 changes: 0 additions & 8 deletions packages/@sanity/base/test/locale.test.js

This file was deleted.

0 comments on commit 4cfbfe5

Please sign in to comment.