@sanity/client 7.1.0-views.1 → 7.1.0-views.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -142,6 +142,15 @@ export async function updateDocumentTitle(_id, title) {
142
142
  - [Archive Release Action](#archive-release)
143
143
  - [Unarchive Release Action](#unarchive-release)
144
144
  - [Delete Release Action](#delete-release)
145
+ - [Views API](#views-api)
146
+ - [Quickstart](#quickstart-1)
147
+ - [Creating a views client instance](#creating-a-views-client-instance)
148
+ - [ESM](#esm-1)
149
+ - [CommonJS](#commonjs-1)
150
+ - [TypeScript](#typescript-1)
151
+ - [Other frameworks, type safety, etc](#other-frameworks-type-safety-etc)
152
+ - [Using a view](#using-a-view)
153
+ - [Developing a view](#developing-a-view)
145
154
  - [License](#license)
146
155
  - [From `v5`](#from-v5)
147
156
  - [The default `useCdn` is changed to `true`](#the-default-usecdn-is-changed-to-true)
@@ -2260,6 +2269,139 @@ const result = await client.agent.action.patch({
2260
2269
  })
2261
2270
  ```
2262
2271
 
2272
+ ### Views API
2273
+
2274
+ #### Quickstart
2275
+
2276
+ ```js
2277
+ import {createViewClient} from '@sanity/client/views'
2278
+
2279
+ const config: ViewClientConfig = {
2280
+ apiVersion: '2025-06-25', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
2281
+ }
2282
+ const client = createViewClient(config)
2283
+
2284
+ const data = await client.fetch('viewId', 'count(*)') // this will now query the view, or the overrides if specified
2285
+ console.log(`Number of documents: ${data}`)
2286
+ ```
2287
+
2288
+ #### Creating a views client instance
2289
+
2290
+ ```js
2291
+ const client = createViewClient(options)
2292
+ ```
2293
+
2294
+ Initializes a new Sanity View Client. Required options are `apiVersion`.
2295
+
2296
+ ##### [ESM](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/)
2297
+
2298
+ ```js
2299
+ import {createViewClient} from '@sanity/client/views'
2300
+
2301
+ const client = createViewClient({
2302
+ apiVersion: '2025-06-25', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
2303
+ })
2304
+
2305
+ const data = await client.fetch('viewId', 'count(*)')
2306
+ console.log(`Number of documents: ${data}`)
2307
+ ```
2308
+
2309
+ ##### [CommonJS]
2310
+
2311
+ ```js
2312
+ const { createViewClient } = require('@sanity/client/views')
2313
+
2314
+ const client = createViewClient({
2315
+ apiVersion: '2025-06-25', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
2316
+ })
2317
+
2318
+ client.fetch('viewId', 'count(*)')
2319
+ .then((data) => console.log(`Number of documents: ${data}`))
2320
+ .catch(console.error);
2321
+ ```
2322
+
2323
+ ##### [TypeScript]
2324
+
2325
+ ```ts
2326
+ import {createViewClient, ViewClientConfig} from '@sanity/client/views'
2327
+
2328
+ const config: ViewClientConfig = {
2329
+ apiVersion: '2025-06-25', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
2330
+ }
2331
+ const client = createViewClient(config)
2332
+
2333
+ const data = await client.fetch<number>('viewId', 'count(*)')
2334
+ console.log(`Number of documents: ${data}`)
2335
+ ```
2336
+
2337
+ ##### Other frameworks, type safety, etc
2338
+ The Views client supports the same conventions as the `sanityClient`. You can follow the same patterns [documented for the sanity client](#creating-a-client-instance) to achieve integration with your framework, typesafety, etc.
2339
+
2340
+ #### Using a view
2341
+
2342
+ Once the View client has been initialized you can use the `fetch` method to query a view. The `fetch` method required two arguments: `viewId` and `query`.
2343
+
2344
+ ```js
2345
+ const data = await client.fetch('viewId', 'count(*)')
2346
+ console.log(`Number of documents: ${data}`)
2347
+ ```
2348
+
2349
+ If the view you are querying is a private view you will need to specify a `token` in the client config.
2350
+
2351
+ ```js
2352
+ const client = createViewClient({
2353
+ apiVersion: '2025-06-25', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
2354
+ token: process.env.SANITY_API_TOKEN,
2355
+ })
2356
+
2357
+ const data = await client.fetch('viewId', 'count(*)')
2358
+ console.log(`Number of documents: ${data}`)
2359
+ ```
2360
+
2361
+ The fetch method can also accept parameters and options, the same as the sanity client.
2362
+
2363
+ ```js
2364
+ const data = await client.fetch('viewId', 'count(*[_type == $type])', {type: 'post'})
2365
+ console.log(`Number of documents: ${data}`)
2366
+ ```
2367
+
2368
+ Unlike the sanity client the views client does not support the `dataset`, `projectId`, or `useProjectHostname` options as it does not belong to any single project or dataset. `apiCdn` is not supported, and all requests will be routed via the CDN. Other options are the same as `sanityClient`.
2369
+
2370
+ #### Developing a view
2371
+
2372
+ Views are deployed in the form of a series of view connections which can take some minutes to build. In order to facilitate an improved developer workflow the views client supports a `viewOverrides` option, which will allow the development of views locally at the cost of some performance.
2373
+
2374
+ To do this we must specify the new option `viewOverrides` on instantiation of the client. Note that a token is required to access this development workflow.
2375
+
2376
+ ```js
2377
+ const config: ViewClientConfig = {
2378
+ apiVersion: '2025-06-25', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
2379
+ token: process.env.SANITY_API_TOKEN, // this is required to use the override functionality
2380
+ viewOverrides: [
2381
+ {
2382
+ resourceType: ViewResourceType.View,
2383
+ resourceId: 'vw2x09caysNaZdjksWEsToG43meSg', // the view id you are developing
2384
+ connections: [
2385
+ {
2386
+ query: '*[_type == "location"]', // the query to use for this connection - this is what you would deploy later
2387
+ resourceType: ViewResourceType.Dataset, // currently only dataset is supported
2388
+ resourceId: '469jfrel.eu_data', // the project and dataset to use for this connection, note the format is `projectId.datasetName`
2389
+ },
2390
+ {
2391
+ query: '*[_type == "location"]', // You can stack as many connections as you require
2392
+ resourceType: ViewResourceType.Dataset,
2393
+ resourceId: '469jfrel.us_data',
2394
+ }
2395
+ ]
2396
+ }
2397
+ ]
2398
+ }
2399
+ const client = createViewClient(config)
2400
+
2401
+ const data = await client.fetch('viewId', 'count(*)') // this query is now run against the result of all the queries above
2402
+ console.log(`Number of documents: ${data}`)
2403
+ ```
2404
+
2263
2405
  ## License
2264
2406
 
2265
2407
  MIT © [Sanity.io](https://www.sanity.io/)
@@ -296,7 +296,7 @@ const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before",
296
296
  if (config["~experimental_resource"])
297
297
  throw new Error(`\`${service}\` does not support resource-based operations`);
298
298
  };
299
- var ViewResourceType = /* @__PURE__ */ ((ViewResourceType2) => (ViewResourceType2.Dataset = "dataset", ViewResourceType2))(ViewResourceType || {});
299
+ var ViewResourceType = /* @__PURE__ */ ((ViewResourceType2) => (ViewResourceType2.Dataset = "dataset", ViewResourceType2.View = "view", ViewResourceType2))(ViewResourceType || {});
300
300
  function once(fn) {
301
301
  let didCall = !1, returnValue;
302
302
  return (...args) => (didCall || (returnValue = fn(...args), didCall = !0), returnValue);