@sanity/client 7.20.0 → 7.21.0

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/dist/index.d.cts CHANGED
@@ -811,6 +811,32 @@ export declare interface ClientConfig {
811
811
  * Lineage token for recursion control
812
812
  */
813
813
  lineage?: string
814
+ /**
815
+ * A custom request handler that intercepts all HTTP requests made by the client.
816
+ *
817
+ * Useful for logging, adding custom headers, refreshing auth tokens, rate limiting, etc.
818
+ *
819
+ * When using `withConfig()`, the new handler **replaces** the previous one (it does not
820
+ * wrap it). To compose handlers, you can chain them manually:
821
+ *
822
+ * ```ts
823
+ * const parent = createClient({...config, _requestHandler: handlerA})
824
+ * const child = parent.withConfig({
825
+ * _requestHandler: (req, defaultRequester) =>
826
+ * handlerB(req, (opts) => handlerA(opts, defaultRequester)),
827
+ * })
828
+ * ```
829
+ *
830
+ * Setting `_requestHandler` to `undefined` via `withConfig()` removes the handler.
831
+ *
832
+ * Note: This only applies to HTTP requests. Real-time listener connections
833
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
834
+ *
835
+ * @internal
836
+ * @deprecated Don't use outside of Sanity internals
837
+ * @see {@link RequestHandler}
838
+ */
839
+ _requestHandler?: RequestHandler
814
840
  }
815
841
 
816
842
  declare type ClientConfigResource =
@@ -2153,6 +2179,7 @@ export declare class LiveClient {
2153
2179
  events({
2154
2180
  includeDrafts,
2155
2181
  tag: _tag,
2182
+ waitFor,
2156
2183
  }?: {
2157
2184
  includeDrafts?: boolean
2158
2185
  /**
@@ -2161,6 +2188,11 @@ export declare class LiveClient {
2161
2188
  * @defaultValue `undefined`
2162
2189
  */
2163
2190
  tag?: string
2191
+ /**
2192
+ * Delays events until after a Sanity Function has processed them and called the callback endpoint.
2193
+ * When omitted, events are delivered immediately.
2194
+ */
2195
+ waitFor?: 'function'
2164
2196
  }): Observable<LiveEvent>
2165
2197
  }
2166
2198
 
@@ -4770,6 +4802,44 @@ export declare interface ReplaceVersionAction {
4770
4802
  /** @public */
4771
4803
  export declare const requester: Requester
4772
4804
 
4805
+ /**
4806
+ * A function that intercepts HTTP requests made by the client.
4807
+ *
4808
+ * Receives the resolved request options, a `defaultRequester` function that
4809
+ * executes the request through the normal pipeline, and a `client` instance
4810
+ * without a `_requestHandler` (to avoid recursive interception).
4811
+ *
4812
+ * The consumer can:
4813
+ * - Modify request options before calling `defaultRequester`
4814
+ * - Transform the response stream (e.g. via `pipe`)
4815
+ * - Skip `defaultRequester` entirely and return a custom Observable
4816
+ * - Use `client` to make additional requests (e.g. refresh an auth token on 401)
4817
+ *
4818
+ * When set via `withConfig()`, the new handler **replaces** (not wraps) the previous one.
4819
+ *
4820
+ * Note: This only applies to HTTP requests. Real-time listener connections
4821
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
4822
+ *
4823
+ * @param request - The resolved request options including `url`
4824
+ * @param defaultRequester - Executes the request through the normal pipeline
4825
+ * @param client - A client instance with the same configuration but without a `_requestHandler`,
4826
+ * useful for making side requests (e.g. token refresh) without triggering the handler recursively
4827
+ *
4828
+ * @internal
4829
+ * @deprecated Don't use outside of Sanity internals
4830
+ */
4831
+ export declare type RequestHandler = (
4832
+ request: RequestOptions & {
4833
+ url: string
4834
+ },
4835
+ defaultRequester: (
4836
+ options: RequestOptions & {
4837
+ url: string
4838
+ },
4839
+ ) => Observable<HttpRequestEvent>,
4840
+ client: SanityClient,
4841
+ ) => Observable<HttpRequestEvent>
4842
+
4773
4843
  /** @internal */
4774
4844
  export declare interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
4775
4845
  url?: string
package/dist/index.d.ts CHANGED
@@ -811,6 +811,32 @@ export declare interface ClientConfig {
811
811
  * Lineage token for recursion control
812
812
  */
813
813
  lineage?: string
814
+ /**
815
+ * A custom request handler that intercepts all HTTP requests made by the client.
816
+ *
817
+ * Useful for logging, adding custom headers, refreshing auth tokens, rate limiting, etc.
818
+ *
819
+ * When using `withConfig()`, the new handler **replaces** the previous one (it does not
820
+ * wrap it). To compose handlers, you can chain them manually:
821
+ *
822
+ * ```ts
823
+ * const parent = createClient({...config, _requestHandler: handlerA})
824
+ * const child = parent.withConfig({
825
+ * _requestHandler: (req, defaultRequester) =>
826
+ * handlerB(req, (opts) => handlerA(opts, defaultRequester)),
827
+ * })
828
+ * ```
829
+ *
830
+ * Setting `_requestHandler` to `undefined` via `withConfig()` removes the handler.
831
+ *
832
+ * Note: This only applies to HTTP requests. Real-time listener connections
833
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
834
+ *
835
+ * @internal
836
+ * @deprecated Don't use outside of Sanity internals
837
+ * @see {@link RequestHandler}
838
+ */
839
+ _requestHandler?: RequestHandler
814
840
  }
815
841
 
816
842
  declare type ClientConfigResource =
@@ -2153,6 +2179,7 @@ export declare class LiveClient {
2153
2179
  events({
2154
2180
  includeDrafts,
2155
2181
  tag: _tag,
2182
+ waitFor,
2156
2183
  }?: {
2157
2184
  includeDrafts?: boolean
2158
2185
  /**
@@ -2161,6 +2188,11 @@ export declare class LiveClient {
2161
2188
  * @defaultValue `undefined`
2162
2189
  */
2163
2190
  tag?: string
2191
+ /**
2192
+ * Delays events until after a Sanity Function has processed them and called the callback endpoint.
2193
+ * When omitted, events are delivered immediately.
2194
+ */
2195
+ waitFor?: 'function'
2164
2196
  }): Observable<LiveEvent>
2165
2197
  }
2166
2198
 
@@ -4770,6 +4802,44 @@ export declare interface ReplaceVersionAction {
4770
4802
  /** @public */
4771
4803
  export declare const requester: Requester
4772
4804
 
4805
+ /**
4806
+ * A function that intercepts HTTP requests made by the client.
4807
+ *
4808
+ * Receives the resolved request options, a `defaultRequester` function that
4809
+ * executes the request through the normal pipeline, and a `client` instance
4810
+ * without a `_requestHandler` (to avoid recursive interception).
4811
+ *
4812
+ * The consumer can:
4813
+ * - Modify request options before calling `defaultRequester`
4814
+ * - Transform the response stream (e.g. via `pipe`)
4815
+ * - Skip `defaultRequester` entirely and return a custom Observable
4816
+ * - Use `client` to make additional requests (e.g. refresh an auth token on 401)
4817
+ *
4818
+ * When set via `withConfig()`, the new handler **replaces** (not wraps) the previous one.
4819
+ *
4820
+ * Note: This only applies to HTTP requests. Real-time listener connections
4821
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
4822
+ *
4823
+ * @param request - The resolved request options including `url`
4824
+ * @param defaultRequester - Executes the request through the normal pipeline
4825
+ * @param client - A client instance with the same configuration but without a `_requestHandler`,
4826
+ * useful for making side requests (e.g. token refresh) without triggering the handler recursively
4827
+ *
4828
+ * @internal
4829
+ * @deprecated Don't use outside of Sanity internals
4830
+ */
4831
+ export declare type RequestHandler = (
4832
+ request: RequestOptions & {
4833
+ url: string
4834
+ },
4835
+ defaultRequester: (
4836
+ options: RequestOptions & {
4837
+ url: string
4838
+ },
4839
+ ) => Observable<HttpRequestEvent>,
4840
+ client: SanityClient,
4841
+ ) => Observable<HttpRequestEvent>
4842
+
4773
4843
  /** @internal */
4774
4844
  export declare interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
4775
4845
  url?: string
package/dist/index.js CHANGED
@@ -1269,7 +1269,8 @@ class LiveClient {
1269
1269
  */
1270
1270
  events({
1271
1271
  includeDrafts = !1,
1272
- tag: _tag
1272
+ tag: _tag,
1273
+ waitFor
1273
1274
  } = {}) {
1274
1275
  const {
1275
1276
  projectId,
@@ -1288,7 +1289,7 @@ class LiveClient {
1288
1289
  "The live events API requires a token or withCredentials when 'includeDrafts: true'. Please update your client configuration. The token should have the lowest possible access role."
1289
1290
  );
1290
1291
  const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
1291
- tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
1292
+ tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true"), waitFor && url.searchParams.set("waitFor", waitFor);
1292
1293
  const esOptions = {};
1293
1294
  includeDrafts && withCredentials && (esOptions.withCredentials = !0), (includeDrafts && token || configHeaders) && (esOptions.headers = {}, includeDrafts && token && (esOptions.headers.Authorization = `Bearer ${token}`), configHeaders && Object.assign(esOptions.headers, configHeaders));
1294
1295
  const key = `${url.href}::${JSON.stringify(esOptions)}`, existing = eventsCache.get(key);
@@ -2186,13 +2187,22 @@ class ObservableSanityClient {
2186
2187
  * Private properties
2187
2188
  */
2188
2189
  #clientConfig;
2190
+ #originalHttpRequest;
2189
2191
  #httpRequest;
2190
2192
  /**
2191
2193
  * Instance properties
2192
2194
  */
2193
2195
  listen = _listen;
2194
2196
  constructor(httpRequest, config = defaultConfig) {
2195
- this.config(config), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2197
+ this.config(config), this.#originalHttpRequest = httpRequest;
2198
+ const requestHandler = config._requestHandler;
2199
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2200
+ let bareClient;
2201
+ return (options, requester2) => {
2202
+ const opts = options;
2203
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2204
+ };
2205
+ })() : httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2196
2206
  video: new ObservableMediaLibraryVideoClient(this, this.#httpRequest)
2197
2207
  }, this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest), this.agent = {
2198
2208
  action: new ObservableAgentsActionClient(this, this.#httpRequest)
@@ -2202,7 +2212,7 @@ class ObservableSanityClient {
2202
2212
  * Clone the client - returns a new instance
2203
2213
  */
2204
2214
  clone() {
2205
- return new ObservableSanityClient(this.#httpRequest, this.config());
2215
+ return new ObservableSanityClient(this.#originalHttpRequest, this.config());
2206
2216
  }
2207
2217
  config(newConfig) {
2208
2218
  if (newConfig === void 0)
@@ -2220,7 +2230,7 @@ class ObservableSanityClient {
2220
2230
  */
2221
2231
  withConfig(newConfig) {
2222
2232
  const thisConfig = this.config();
2223
- return new ObservableSanityClient(this.#httpRequest, {
2233
+ return new ObservableSanityClient(this.#originalHttpRequest, {
2224
2234
  ...thisConfig,
2225
2235
  ...newConfig,
2226
2236
  stega: {
@@ -2450,13 +2460,22 @@ class SanityClient {
2450
2460
  * Private properties
2451
2461
  */
2452
2462
  #clientConfig;
2463
+ #originalHttpRequest;
2453
2464
  #httpRequest;
2454
2465
  /**
2455
2466
  * Instance properties
2456
2467
  */
2457
2468
  listen = _listen;
2458
2469
  constructor(httpRequest, config = defaultConfig) {
2459
- this.config(config), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2470
+ this.config(config), this.#originalHttpRequest = httpRequest;
2471
+ const requestHandler = config._requestHandler;
2472
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2473
+ let bareClient;
2474
+ return (options, requester2) => {
2475
+ const opts = options;
2476
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2477
+ };
2478
+ })() : httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2460
2479
  video: new MediaLibraryVideoClient(this, this.#httpRequest)
2461
2480
  }, this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.agent = {
2462
2481
  action: new AgentActionsClient(this, this.#httpRequest)
@@ -2466,7 +2485,7 @@ class SanityClient {
2466
2485
  * Clone the client - returns a new instance
2467
2486
  */
2468
2487
  clone() {
2469
- return new SanityClient(this.#httpRequest, this.config());
2488
+ return new SanityClient(this.#originalHttpRequest, this.config());
2470
2489
  }
2471
2490
  config(newConfig) {
2472
2491
  if (newConfig === void 0)
@@ -2484,7 +2503,7 @@ class SanityClient {
2484
2503
  */
2485
2504
  withConfig(newConfig) {
2486
2505
  const thisConfig = this.config();
2487
- return new SanityClient(this.#httpRequest, {
2506
+ return new SanityClient(this.#originalHttpRequest, {
2488
2507
  ...thisConfig,
2489
2508
  ...newConfig,
2490
2509
  stega: {
@@ -2755,7 +2774,7 @@ function defineDeprecatedCreateClient(createClient2) {
2755
2774
  return printNoDefaultExport(), createClient2(config);
2756
2775
  };
2757
2776
  }
2758
- var name = "@sanity/client", version = "7.20.0";
2777
+ var name = "@sanity/client", version = "7.21.0";
2759
2778
  const middleware = [
2760
2779
  debug({ verbose: !0, namespace: "sanity:client" }),
2761
2780
  headers({ "User-Agent": `${name} ${version}` }),