How to use @sanity/client - 6 common examples

To help you get started, we’ve selected a few @sanity/client examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sanity-io / sanity / packages / @sanity / base / src / client / index.js View on Github external
import config from 'config:sanity'
import configureClient from 'part:@sanity/base/configure-client?'
import 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 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`
// eslint-disable-next-line import/no-commonjs
module.exports = configuredClient
github sanity-io / sanity / packages / @sanity / base / src / client / index.js View on Github external
import 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 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`
// eslint-disable-next-line import/no-commonjs
module.exports = configuredClient
github sanity-io / sanity / packages / @sanity / desk-tool / examples / testbed / mock / sanityClient.js View on Github external
patch(id, operations) {
    return new Patch(id, operations, this)
  },
  listen() {
github staccx / sanity-query-helper / src / sanityQueryHelper.js View on Github external
get client() {
    return sanityClient(this.sanityOptions)
  }
github sanity-io / sanity / packages / @sanity / core / src / actions / dataset / strengthenReferences.js View on Github external
export default async function strengthenReferences(options) {
  const {importId, dataset} = options
  const progress = options.progress || noop
  const timeout = 605000
  const concurrency = 10
  const client = sanityClient(Object.assign({}, options.client.config(), {dataset, timeout}))
  const getReferenceDoc = ({prevImportMapId}) =>
    client.fetch(importMapQuery, {importId, prevImportMapId}).then(docs => docs[0])

  let referenceDoc = await getReferenceDoc({prevImportMapId: 'none'})
  while (referenceDoc) {
    debug('Found refmap with ID %s, processing', referenceDoc._id)
    await promiseEach(referenceDoc.referenceMaps, unsetRefMapKeys, {concurrency})

    debug('Unset weak flag within %d documents', referenceDoc.referenceMaps.length)
    debug('Deleting refmap with ID %s', referenceDoc._id)
    await client.delete(referenceDoc._id)
    referenceDoc = await getReferenceDoc({prevImportMapId: referenceDoc._id})

    progress(referenceDoc.referenceMaps.length)
  }
github sanity-io / sanity / packages / @sanity / cli / src / util / clientWrapper.js View on Github external
export default function clientWrapper(manifest, configPath) {
  const requester = client.requester.clone()
  requester.use(authErrors())

  return function(opts = {}) {
    const {requireUser, requireProject, api} = {...defaults, ...opts}
    const userConfig = getUserConfig()
    const userApiConf = userConfig.get('api')
    const token = envAuthToken || userConfig.get('authToken')
    const apiHost = apiHosts[sanityEnv]
    const apiConfig = Object.assign(
      {},
      userApiConf || {},
      (manifest && manifest.api) || {},
      api || {}
    )

    if (apiHost) {

@sanity/client

Client for retrieving, creating and patching data from Sanity.io

MIT
Latest version published 19 days ago

Package Health Score

89 / 100
Full package analysis