Skip to content

Commit 4cfbfe5

Browse files
rexxarsbjoerge
authored andcommitted
[base] Expose client as CommonJS (#133)
1 parent 275cc61 commit 4cfbfe5

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

packages/@sanity/base/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
},
4545
"devDependencies": {
4646
"@sanity/check": "^0.108.0",
47+
"@sanity/plugin-loader": "^0.108.0",
4748
"chai": "^3.5.0",
4849
"chai-as-promised": "^6.0.0",
4950
"eslint": "^3.19.0",

packages/@sanity/base/src/client/index.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,30 @@ import sanityClient from '@sanity/client'
22
import config from 'config:sanity'
33
import configureClient from 'part:@sanity/base/configure-client?'
44

5+
const deprecationMessage = `[deprecation] The Sanity client is now exposed in CommonJS format.
6+
7+
For instance, change:
8+
\`const client = require('part:@sanity/base/client').default\`
9+
10+
To the following:
11+
\`const client = require('part:@sanity/base/client')\`
12+
`
13+
514
const apiConfig = {...config.api, withCredentials: true}
615
const client = sanityClient(apiConfig)
716

8-
export default configureClient ? configureClient(sanityClient(apiConfig)) : client
17+
const configuredClient = configureClient
18+
? configureClient(sanityClient(apiConfig))
19+
: client
20+
21+
// Warn when people use `.default`
22+
Object.defineProperty(configuredClient, 'default', {
23+
get() {
24+
// eslint-disable-next-line no-console
25+
console.warn(deprecationMessage)
26+
return configuredClient
27+
}
28+
})
29+
30+
// Expose as CJS to allow Node scripts to consume it without `.default`
31+
module.exports = configuredClient
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {describe, it} from 'mocha'
2+
import {expect} from 'chai'
3+
import client from '../src/client'
4+
5+
describe('client', () => {
6+
it('should be exposed in CommonJS format', done => {
7+
expect(client.fetch).to.be.a('function')
8+
done()
9+
})
10+
11+
it('should still expose client on .default, but give warning', done => {
12+
expect(client.default.fetch).to.be.a('function')
13+
done()
14+
})
15+
})

packages/@sanity/base/test/init.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import chai from 'chai'
22
import chaiAsPromised from 'chai-as-promised'
3+
import pluginLoader from '@sanity/plugin-loader'
4+
5+
pluginLoader({
6+
overrides: {
7+
'config:sanity': [{api: {projectId: 'abc123', dataset: 'hei'}}]
8+
}
9+
})
310

411
chai.should()
512
chai.use(chaiAsPromised)

packages/@sanity/base/test/locale.test.js

-8
This file was deleted.

0 commit comments

Comments
 (0)