@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.
@@ -810,6 +810,32 @@ export declare interface ClientConfig {
810
810
  * Lineage token for recursion control
811
811
  */
812
812
  lineage?: string
813
+ /**
814
+ * A custom request handler that intercepts all HTTP requests made by the client.
815
+ *
816
+ * Useful for logging, adding custom headers, refreshing auth tokens, rate limiting, etc.
817
+ *
818
+ * When using `withConfig()`, the new handler **replaces** the previous one (it does not
819
+ * wrap it). To compose handlers, you can chain them manually:
820
+ *
821
+ * ```ts
822
+ * const parent = createClient({...config, _requestHandler: handlerA})
823
+ * const child = parent.withConfig({
824
+ * _requestHandler: (req, defaultRequester) =>
825
+ * handlerB(req, (opts) => handlerA(opts, defaultRequester)),
826
+ * })
827
+ * ```
828
+ *
829
+ * Setting `_requestHandler` to `undefined` via `withConfig()` removes the handler.
830
+ *
831
+ * Note: This only applies to HTTP requests. Real-time listener connections
832
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
833
+ *
834
+ * @internal
835
+ * @deprecated Don't use outside of Sanity internals
836
+ * @see {@link RequestHandler}
837
+ */
838
+ _requestHandler?: RequestHandler
813
839
  }
814
840
 
815
841
  declare type ClientConfigResource =
@@ -2161,6 +2187,7 @@ export declare class LiveClient {
2161
2187
  events({
2162
2188
  includeDrafts,
2163
2189
  tag: _tag,
2190
+ waitFor,
2164
2191
  }?: {
2165
2192
  includeDrafts?: boolean
2166
2193
  /**
@@ -2169,6 +2196,11 @@ export declare class LiveClient {
2169
2196
  * @defaultValue `undefined`
2170
2197
  */
2171
2198
  tag?: string
2199
+ /**
2200
+ * Delays events until after a Sanity Function has processed them and called the callback endpoint.
2201
+ * When omitted, events are delivered immediately.
2202
+ */
2203
+ waitFor?: 'function'
2172
2204
  }): Observable<LiveEvent>
2173
2205
  }
2174
2206
 
@@ -4778,6 +4810,44 @@ export declare interface ReplaceVersionAction {
4778
4810
  /** @public */
4779
4811
  export declare const requester: Requester
4780
4812
 
4813
+ /**
4814
+ * A function that intercepts HTTP requests made by the client.
4815
+ *
4816
+ * Receives the resolved request options, a `defaultRequester` function that
4817
+ * executes the request through the normal pipeline, and a `client` instance
4818
+ * without a `_requestHandler` (to avoid recursive interception).
4819
+ *
4820
+ * The consumer can:
4821
+ * - Modify request options before calling `defaultRequester`
4822
+ * - Transform the response stream (e.g. via `pipe`)
4823
+ * - Skip `defaultRequester` entirely and return a custom Observable
4824
+ * - Use `client` to make additional requests (e.g. refresh an auth token on 401)
4825
+ *
4826
+ * When set via `withConfig()`, the new handler **replaces** (not wraps) the previous one.
4827
+ *
4828
+ * Note: This only applies to HTTP requests. Real-time listener connections
4829
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
4830
+ *
4831
+ * @param request - The resolved request options including `url`
4832
+ * @param defaultRequester - Executes the request through the normal pipeline
4833
+ * @param client - A client instance with the same configuration but without a `_requestHandler`,
4834
+ * useful for making side requests (e.g. token refresh) without triggering the handler recursively
4835
+ *
4836
+ * @internal
4837
+ * @deprecated Don't use outside of Sanity internals
4838
+ */
4839
+ export declare type RequestHandler = (
4840
+ request: RequestOptions & {
4841
+ url: string
4842
+ },
4843
+ defaultRequester: (
4844
+ options: RequestOptions & {
4845
+ url: string
4846
+ },
4847
+ ) => Observable<HttpRequestEvent>,
4848
+ client: SanityClient,
4849
+ ) => Observable<HttpRequestEvent>
4850
+
4781
4851
  /** @internal */
4782
4852
  export declare interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
4783
4853
  url?: string
@@ -810,6 +810,32 @@ export declare interface ClientConfig {
810
810
  * Lineage token for recursion control
811
811
  */
812
812
  lineage?: string
813
+ /**
814
+ * A custom request handler that intercepts all HTTP requests made by the client.
815
+ *
816
+ * Useful for logging, adding custom headers, refreshing auth tokens, rate limiting, etc.
817
+ *
818
+ * When using `withConfig()`, the new handler **replaces** the previous one (it does not
819
+ * wrap it). To compose handlers, you can chain them manually:
820
+ *
821
+ * ```ts
822
+ * const parent = createClient({...config, _requestHandler: handlerA})
823
+ * const child = parent.withConfig({
824
+ * _requestHandler: (req, defaultRequester) =>
825
+ * handlerB(req, (opts) => handlerA(opts, defaultRequester)),
826
+ * })
827
+ * ```
828
+ *
829
+ * Setting `_requestHandler` to `undefined` via `withConfig()` removes the handler.
830
+ *
831
+ * Note: This only applies to HTTP requests. Real-time listener connections
832
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
833
+ *
834
+ * @internal
835
+ * @deprecated Don't use outside of Sanity internals
836
+ * @see {@link RequestHandler}
837
+ */
838
+ _requestHandler?: RequestHandler
813
839
  }
814
840
 
815
841
  declare type ClientConfigResource =
@@ -2161,6 +2187,7 @@ export declare class LiveClient {
2161
2187
  events({
2162
2188
  includeDrafts,
2163
2189
  tag: _tag,
2190
+ waitFor,
2164
2191
  }?: {
2165
2192
  includeDrafts?: boolean
2166
2193
  /**
@@ -2169,6 +2196,11 @@ export declare class LiveClient {
2169
2196
  * @defaultValue `undefined`
2170
2197
  */
2171
2198
  tag?: string
2199
+ /**
2200
+ * Delays events until after a Sanity Function has processed them and called the callback endpoint.
2201
+ * When omitted, events are delivered immediately.
2202
+ */
2203
+ waitFor?: 'function'
2172
2204
  }): Observable<LiveEvent>
2173
2205
  }
2174
2206
 
@@ -4778,6 +4810,44 @@ export declare interface ReplaceVersionAction {
4778
4810
  /** @public */
4779
4811
  export declare const requester: Requester
4780
4812
 
4813
+ /**
4814
+ * A function that intercepts HTTP requests made by the client.
4815
+ *
4816
+ * Receives the resolved request options, a `defaultRequester` function that
4817
+ * executes the request through the normal pipeline, and a `client` instance
4818
+ * without a `_requestHandler` (to avoid recursive interception).
4819
+ *
4820
+ * The consumer can:
4821
+ * - Modify request options before calling `defaultRequester`
4822
+ * - Transform the response stream (e.g. via `pipe`)
4823
+ * - Skip `defaultRequester` entirely and return a custom Observable
4824
+ * - Use `client` to make additional requests (e.g. refresh an auth token on 401)
4825
+ *
4826
+ * When set via `withConfig()`, the new handler **replaces** (not wraps) the previous one.
4827
+ *
4828
+ * Note: This only applies to HTTP requests. Real-time listener connections
4829
+ * (`client.listen()`) use EventSource and are not intercepted by this handler.
4830
+ *
4831
+ * @param request - The resolved request options including `url`
4832
+ * @param defaultRequester - Executes the request through the normal pipeline
4833
+ * @param client - A client instance with the same configuration but without a `_requestHandler`,
4834
+ * useful for making side requests (e.g. token refresh) without triggering the handler recursively
4835
+ *
4836
+ * @internal
4837
+ * @deprecated Don't use outside of Sanity internals
4838
+ */
4839
+ export declare type RequestHandler = (
4840
+ request: RequestOptions & {
4841
+ url: string
4842
+ },
4843
+ defaultRequester: (
4844
+ options: RequestOptions & {
4845
+ url: string
4846
+ },
4847
+ ) => Observable<HttpRequestEvent>,
4848
+ client: SanityClient,
4849
+ ) => Observable<HttpRequestEvent>
4850
+
4781
4851
  /** @internal */
4782
4852
  export declare interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
4783
4853
  url?: string
@@ -1451,7 +1451,8 @@ class LiveClient {
1451
1451
  */
1452
1452
  events({
1453
1453
  includeDrafts = !1,
1454
- tag: _tag
1454
+ tag: _tag,
1455
+ waitFor
1455
1456
  } = {}) {
1456
1457
  const {
1457
1458
  projectId: projectId2,
@@ -1470,7 +1471,7 @@ class LiveClient {
1470
1471
  "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."
1471
1472
  );
1472
1473
  const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
1473
- tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
1474
+ tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true"), waitFor && url.searchParams.set("waitFor", waitFor);
1474
1475
  const esOptions = {};
1475
1476
  includeDrafts && withCredentials && (esOptions.withCredentials = !0), (includeDrafts && token || configHeaders) && (esOptions.headers = {}, includeDrafts && token && (esOptions.headers.Authorization = `Bearer ${token}`), configHeaders && Object.assign(esOptions.headers, configHeaders));
1476
1477
  const key = `${url.href}::${JSON.stringify(esOptions)}`, existing = eventsCache.get(key);
@@ -2368,13 +2369,22 @@ class ObservableSanityClient {
2368
2369
  * Private properties
2369
2370
  */
2370
2371
  #clientConfig;
2372
+ #originalHttpRequest;
2371
2373
  #httpRequest;
2372
2374
  /**
2373
2375
  * Instance properties
2374
2376
  */
2375
2377
  listen = _listen;
2376
2378
  constructor(httpRequest, config = defaultConfig) {
2377
- 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 = {
2379
+ this.config(config), this.#originalHttpRequest = httpRequest;
2380
+ const requestHandler = config._requestHandler;
2381
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2382
+ let bareClient;
2383
+ return (options, requester2) => {
2384
+ const opts = options;
2385
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2386
+ };
2387
+ })() : httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2378
2388
  video: new ObservableMediaLibraryVideoClient(this, this.#httpRequest)
2379
2389
  }, this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest), this.agent = {
2380
2390
  action: new ObservableAgentsActionClient(this, this.#httpRequest)
@@ -2384,7 +2394,7 @@ class ObservableSanityClient {
2384
2394
  * Clone the client - returns a new instance
2385
2395
  */
2386
2396
  clone() {
2387
- return new ObservableSanityClient(this.#httpRequest, this.config());
2397
+ return new ObservableSanityClient(this.#originalHttpRequest, this.config());
2388
2398
  }
2389
2399
  config(newConfig) {
2390
2400
  if (newConfig === void 0)
@@ -2402,7 +2412,7 @@ class ObservableSanityClient {
2402
2412
  */
2403
2413
  withConfig(newConfig) {
2404
2414
  const thisConfig = this.config();
2405
- return new ObservableSanityClient(this.#httpRequest, {
2415
+ return new ObservableSanityClient(this.#originalHttpRequest, {
2406
2416
  ...thisConfig,
2407
2417
  ...newConfig,
2408
2418
  stega: {
@@ -2632,13 +2642,22 @@ class SanityClient {
2632
2642
  * Private properties
2633
2643
  */
2634
2644
  #clientConfig;
2645
+ #originalHttpRequest;
2635
2646
  #httpRequest;
2636
2647
  /**
2637
2648
  * Instance properties
2638
2649
  */
2639
2650
  listen = _listen;
2640
2651
  constructor(httpRequest, config = defaultConfig) {
2641
- 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 = {
2652
+ this.config(config), this.#originalHttpRequest = httpRequest;
2653
+ const requestHandler = config._requestHandler;
2654
+ this.#httpRequest = requestHandler ? /* @__PURE__ */ (() => {
2655
+ let bareClient;
2656
+ return (options, requester2) => {
2657
+ const opts = options;
2658
+ return bareClient || (bareClient = new SanityClient(httpRequest, { ...config, _requestHandler: void 0 })), requestHandler(opts, (o) => httpRequest(o, requester2), bareClient);
2659
+ };
2660
+ })() : httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.mediaLibrary = {
2642
2661
  video: new MediaLibraryVideoClient(this, this.#httpRequest)
2643
2662
  }, this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.agent = {
2644
2663
  action: new AgentActionsClient(this, this.#httpRequest)
@@ -2648,7 +2667,7 @@ class SanityClient {
2648
2667
  * Clone the client - returns a new instance
2649
2668
  */
2650
2669
  clone() {
2651
- return new SanityClient(this.#httpRequest, this.config());
2670
+ return new SanityClient(this.#originalHttpRequest, this.config());
2652
2671
  }
2653
2672
  config(newConfig) {
2654
2673
  if (newConfig === void 0)
@@ -2666,7 +2685,7 @@ class SanityClient {
2666
2685
  */
2667
2686
  withConfig(newConfig) {
2668
2687
  const thisConfig = this.config();
2669
- return new SanityClient(this.#httpRequest, {
2688
+ return new SanityClient(this.#originalHttpRequest, {
2670
2689
  ...thisConfig,
2671
2690
  ...newConfig,
2672
2691
  stega: {