Skip to content

Commit

Permalink
feat(base): deprecate studio client without a specified apiVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Mar 24, 2021
1 parent 47db879 commit 75318d1
Showing 1 changed file with 70 additions and 18 deletions.
88 changes: 70 additions & 18 deletions packages/@sanity/base/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
import generateHelpUrl from '@sanity/generate-help-url'
import config from 'config:sanity'
import configureClient from 'part:@sanity/base/configure-client?'
import sanityClient, {SanityClient} from '@sanity/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 fallbackConfig = {projectId: 'UNSPECIFIED', dataset: 'UNSPECIFIED'}
const apiConfig = {...fallbackConfig, ...config.api, withCredentials: true, useCdn: false}
const client = sanityClient(apiConfig)
const apiConfig = {
...fallbackConfig,
...config.api,
withCredentials: true,
useCdn: false,
apiVersion: '1',
}

const client = sanityClient(apiConfig)
const configuredClient = experimental(
configureClient ? configureClient(sanityClient(apiConfig)) : client
)

const getKeys = (obj) => Object.keys(Object.getPrototypeOf(obj)).concat(Object.keys(obj))
const instances = [configuredClient]

const wrappedClient = {
config(newConfig, silence = false) {
if (!newConfig) {
return configuredClient.config()
}

if (!silence) {
// eslint-disable-next-line no-console
console.warn(
new Error(
`Setting configuration on the global studio client is deprecated - please use \`withConfig()\` instead - see ${generateHelpUrl(
'studio-client-global-config'
)}`
)
)
}

// Don't allow overriding apiVersion on instantiated clients
const {apiVersion, ...rest} = newConfig
instances.forEach((instance) => instance.config(rest))
return wrappedClient
},

withConfig: (newConfig) => {
if (!newConfig || !newConfig.apiVersion) {
throw new Error(
`Client \`withConfig()\` called without an \`apiVersion\` - see ${generateHelpUrl(
'studio-client-specify-api-version'
)}`
)
}

const newClient = configuredClient.clone().config(newConfig)
instances.push(newClient)
return newClient
},
}

function experimental(original: SanityClient): SanityClient {
let useExperimental = false
try {
Expand All @@ -39,13 +78,26 @@ function experimental(original: SanityClient): SanityClient {
return original
}

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

Object.defineProperty(wrappedClient, key, {
get() {
// eslint-disable-next-line no-console
console.warn(
// Using Error to get a stack that makes it easier to see where it originates from
new Error(
`Used property "${key}" on versionless client - this is deprecated. Please specify API version using \`withConfig\` - see ${generateHelpUrl(
'studio-client-specify-api-version'
)}`
)
)

return configuredClient[key]
},
})
})

// Expose as CJS to allow Node scripts to consume it without `.default`
Expand Down

0 comments on commit 75318d1

Please sign in to comment.