@supabase/postgrest-js 2.118.0 → 2.119.0-canary.1

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.cjs CHANGED
@@ -3637,6 +3637,36 @@ function toTransportFailure(cause, status, statusText) {
3637
3637
  };
3638
3638
  }
3639
3639
  /**
3640
+ * Bounds every call of `fetch` to `timeout` milliseconds by aborting it through
3641
+ * an `AbortController`. A signal already present on the request keeps working:
3642
+ * its abort also aborts the timed request. A missing or non-positive timeout
3643
+ * returns `fetch` unchanged.
3644
+ */
3645
+ function withTimeout(fetch$1, timeout) {
3646
+ if (timeout === void 0 || timeout <= 0) return fetch$1;
3647
+ return (input, init) => {
3648
+ const controller = new AbortController();
3649
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
3650
+ const existingSignal = init === null || init === void 0 ? void 0 : init.signal;
3651
+ if (existingSignal) {
3652
+ if (existingSignal.aborted) {
3653
+ clearTimeout(timeoutId);
3654
+ return fetch$1(input, init);
3655
+ }
3656
+ const abortHandler = () => {
3657
+ clearTimeout(timeoutId);
3658
+ controller.abort();
3659
+ };
3660
+ existingSignal.addEventListener("abort", abortHandler, { once: true });
3661
+ return fetch$1(input, _objectSpread2(_objectSpread2({}, init), {}, { signal: controller.signal })).finally(() => {
3662
+ clearTimeout(timeoutId);
3663
+ existingSignal.removeEventListener("abort", abortHandler);
3664
+ });
3665
+ }
3666
+ return fetch$1(input, _objectSpread2(_objectSpread2({}, init), {}, { signal: controller.signal })).finally(() => clearTimeout(timeoutId));
3667
+ };
3668
+ }
3669
+ /**
3640
3670
  * PostgREST client.
3641
3671
  *
3642
3672
  * @typeParam Database - Types for the schema from the [type
@@ -3661,6 +3691,10 @@ var PostgrestClient = class PostgrestClient {
3661
3691
  * When enabled, idempotent requests (GET, HEAD, OPTIONS) that fail with network
3662
3692
  * errors or HTTP 503/520 responses will be automatically retried up to 3 times
3663
3693
  * with exponential backoff (1s, 2s, 4s). Defaults to `true`.
3694
+ * @param options.wrapFetch - Wraps every fetch this client uses, including a per-request
3695
+ * `fetch` passed to `from()`, and is carried over to clients returned by `schema()`.
3696
+ * Use it for headers or instrumentation that must reach every request regardless of
3697
+ * which fetch performs it.
3664
3698
  * @example Using supabase-js (recommended)
3665
3699
  * ```ts
3666
3700
  * import { createClient } from '@supabase/supabase-js'
@@ -3686,44 +3720,35 @@ var PostgrestClient = class PostgrestClient {
3686
3720
  * })
3687
3721
  * ```
3688
3722
  */
3689
- constructor(url, { headers = {}, schema, fetch: fetch$1, timeout, urlLengthLimit = 8e3, retry } = {}) {
3723
+ constructor(url, { headers = {}, schema, fetch: fetch$1, timeout, urlLengthLimit = 8e3, retry, wrapFetch } = {}) {
3690
3724
  this.url = url;
3691
3725
  this.headers = new Headers(headers);
3692
3726
  this.schemaName = schema;
3693
3727
  this.urlLengthLimit = urlLengthLimit;
3694
- const originalFetch = fetch$1 !== null && fetch$1 !== void 0 ? fetch$1 : globalThis.fetch;
3695
- if (timeout !== void 0 && timeout > 0) this.fetch = (input, init) => {
3696
- const controller = new AbortController();
3697
- const timeoutId = setTimeout(() => controller.abort(), timeout);
3698
- const existingSignal = init === null || init === void 0 ? void 0 : init.signal;
3699
- if (existingSignal) {
3700
- if (existingSignal.aborted) {
3701
- clearTimeout(timeoutId);
3702
- return originalFetch(input, init);
3703
- }
3704
- const abortHandler = () => {
3705
- clearTimeout(timeoutId);
3706
- controller.abort();
3707
- };
3708
- existingSignal.addEventListener("abort", abortHandler, { once: true });
3709
- return originalFetch(input, _objectSpread2(_objectSpread2({}, init), {}, { signal: controller.signal })).finally(() => {
3710
- clearTimeout(timeoutId);
3711
- existingSignal.removeEventListener("abort", abortHandler);
3712
- });
3713
- }
3714
- return originalFetch(input, _objectSpread2(_objectSpread2({}, init), {}, { signal: controller.signal })).finally(() => clearTimeout(timeoutId));
3715
- };
3716
- else this.fetch = originalFetch;
3728
+ this.timeout = timeout;
3729
+ this.wrapFetch = wrapFetch;
3730
+ this.baseFetch = fetch$1 !== null && fetch$1 !== void 0 ? fetch$1 : globalThis.fetch;
3731
+ this.fetch = this.baseFetch ? this.decorateFetch(this.baseFetch) : void 0;
3717
3732
  this.retry = retry;
3718
3733
  }
3719
- from(relation) {
3734
+ /**
3735
+ * Applies the client's decorations to a fetch implementation: `wrapFetch`
3736
+ * first, then the timeout guard, so the timeout bounds the whole wrapped call.
3737
+ * The client-level fetch and every per-request `fetch` passed to `from()` go
3738
+ * through this.
3739
+ */
3740
+ decorateFetch(fetch$1) {
3741
+ return withTimeout(this.wrapFetch ? this.wrapFetch(fetch$1) : fetch$1, this.timeout);
3742
+ }
3743
+ from(relation, options) {
3744
+ var _options$urlLengthLim, _options$retry;
3720
3745
  if (!relation || typeof relation !== "string" || relation.trim() === "") throw new Error("Invalid relation name: relation must be a non-empty string.");
3721
3746
  return new PostgrestQueryBuilder(new URL(`${this.url}/${relation}`), {
3722
3747
  headers: new Headers(this.headers),
3723
3748
  schema: this.schemaName,
3724
- fetch: this.fetch,
3725
- urlLengthLimit: this.urlLengthLimit,
3726
- retry: this.retry
3749
+ fetch: (options === null || options === void 0 ? void 0 : options.fetch) ? this.decorateFetch(options.fetch) : this.fetch,
3750
+ urlLengthLimit: (_options$urlLengthLim = options === null || options === void 0 ? void 0 : options.urlLengthLimit) !== null && _options$urlLengthLim !== void 0 ? _options$urlLengthLim : this.urlLengthLimit,
3751
+ retry: (_options$retry = options === null || options === void 0 ? void 0 : options.retry) !== null && _options$retry !== void 0 ? _options$retry : this.retry
3727
3752
  });
3728
3753
  }
3729
3754
  /**
@@ -3739,7 +3764,9 @@ var PostgrestClient = class PostgrestClient {
3739
3764
  return new PostgrestClient(this.url, {
3740
3765
  headers: this.headers,
3741
3766
  schema,
3742
- fetch: this.fetch,
3767
+ fetch: this.baseFetch,
3768
+ timeout: this.timeout,
3769
+ wrapFetch: this.wrapFetch,
3743
3770
  urlLengthLimit: this.urlLengthLimit,
3744
3771
  retry: this.retry
3745
3772
  });