UNPKG

autumn-js

Version:
1,178 lines (1,155 loc) 38.9 kB
"use client"; "use strict"; "use client"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/react/hooks/useReferrals.ts var useReferrals_exports = {}; __export(useReferrals_exports, { useReferrals: () => useReferrals }); module.exports = __toCommonJS(useReferrals_exports); // ../../node_modules/@tanstack/query-core/build/modern/subscribable.js var Subscribable = class { constructor() { this.listeners = /* @__PURE__ */ new Set(); this.subscribe = this.subscribe.bind(this); } subscribe(listener) { this.listeners.add(listener); this.onSubscribe(); return () => { this.listeners.delete(listener); this.onUnsubscribe(); }; } hasListeners() { return this.listeners.size > 0; } onSubscribe() { } onUnsubscribe() { } }; // ../../node_modules/@tanstack/query-core/build/modern/focusManager.js var FocusManager = class extends Subscribable { #focused; #cleanup; #setup; constructor() { super(); this.#setup = (onFocus) => { if (typeof window !== "undefined" && window.addEventListener) { const listener = () => onFocus(); window.addEventListener("visibilitychange", listener, false); return () => { window.removeEventListener("visibilitychange", listener); }; } return; }; } onSubscribe() { if (!this.#cleanup) { this.setEventListener(this.#setup); } } onUnsubscribe() { if (!this.hasListeners()) { this.#cleanup?.(); this.#cleanup = void 0; } } setEventListener(setup) { this.#setup = setup; this.#cleanup?.(); this.#cleanup = setup((focused) => { if (typeof focused === "boolean") { this.setFocused(focused); } else { this.onFocus(); } }); } setFocused(focused) { const changed = this.#focused !== focused; if (changed) { this.#focused = focused; this.onFocus(); } } onFocus() { const isFocused = this.isFocused(); this.listeners.forEach((listener) => { listener(isFocused); }); } isFocused() { if (typeof this.#focused === "boolean") { return this.#focused; } return globalThis.document?.visibilityState !== "hidden"; } }; var focusManager = new FocusManager(); // ../../node_modules/@tanstack/query-core/build/modern/timeoutManager.js var defaultTimeoutProvider = { // We need the wrapper function syntax below instead of direct references to // global setTimeout etc. // // BAD: `setTimeout: setTimeout` // GOOD: `setTimeout: (cb, delay) => setTimeout(cb, delay)` // // If we use direct references here, then anything that wants to spy on or // replace the global setTimeout (like tests) won't work since we'll already // have a hard reference to the original implementation at the time when this // file was imported. setTimeout: (callback, delay) => setTimeout(callback, delay), clearTimeout: (timeoutId) => clearTimeout(timeoutId), setInterval: (callback, delay) => setInterval(callback, delay), clearInterval: (intervalId) => clearInterval(intervalId) }; var TimeoutManager = class { // We cannot have TimeoutManager<T> as we must instantiate it with a concrete // type at app boot; and if we leave that type, then any new timer provider // would need to support the default provider's concrete timer ID, which is // infeasible across environments. // // We settle for type safety for the TimeoutProvider type, and accept that // this class is unsafe internally to allow for extension. #provider = defaultTimeoutProvider; #providerCalled = false; setTimeoutProvider(provider) { if (process.env.NODE_ENV !== "production") { if (this.#providerCalled && provider !== this.#provider) { console.error( `[timeoutManager]: Switching provider after calls to previous provider might result in unexpected behavior.`, { previous: this.#provider, provider } ); } } this.#provider = provider; if (process.env.NODE_ENV !== "production") { this.#providerCalled = false; } } setTimeout(callback, delay) { if (process.env.NODE_ENV !== "production") { this.#providerCalled = true; } return this.#provider.setTimeout(callback, delay); } clearTimeout(timeoutId) { this.#provider.clearTimeout(timeoutId); } setInterval(callback, delay) { if (process.env.NODE_ENV !== "production") { this.#providerCalled = true; } return this.#provider.setInterval(callback, delay); } clearInterval(intervalId) { this.#provider.clearInterval(intervalId); } }; var timeoutManager = new TimeoutManager(); function systemSetTimeoutZero(callback) { setTimeout(callback, 0); } // ../../node_modules/@tanstack/query-core/build/modern/utils.js var isServer = typeof window === "undefined" || "Deno" in globalThis; function noop() { } function isValidTimeout(value) { return typeof value === "number" && value >= 0 && value !== Infinity; } function timeUntilStale(updatedAt, staleTime) { return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0); } function resolveStaleTime(staleTime, query) { return typeof staleTime === "function" ? staleTime(query) : staleTime; } function resolveQueryBoolean(option, query) { return typeof option === "function" ? option(query) : option; } var hasOwn = Object.prototype.hasOwnProperty; function replaceEqualDeep(a, b, depth = 0) { if (a === b) { return a; } if (depth > 500) return b; const array = isPlainArray(a) && isPlainArray(b); if (!array && !(isPlainObject(a) && isPlainObject(b))) return b; const aItems = array ? a : Object.keys(a); const aSize = aItems.length; const bItems = array ? b : Object.keys(b); const bSize = bItems.length; const copy = array ? new Array(bSize) : {}; let equalItems = 0; for (let i = 0; i < bSize; i++) { const key = array ? i : bItems[i]; const aItem = a[key]; const bItem = b[key]; if (aItem === bItem) { copy[key] = aItem; if (array ? i < aSize : hasOwn.call(a, key)) equalItems++; continue; } if (aItem === null || bItem === null || typeof aItem !== "object" || typeof bItem !== "object") { copy[key] = bItem; continue; } const v = replaceEqualDeep(aItem, bItem, depth + 1); copy[key] = v; if (v === aItem) equalItems++; } return aSize === bSize && equalItems === aSize ? a : copy; } function shallowEqualObjects(a, b) { if (!b || Object.keys(a).length !== Object.keys(b).length) { return false; } for (const key in a) { if (a[key] !== b[key]) { return false; } } return true; } function isPlainArray(value) { return Array.isArray(value) && value.length === Object.keys(value).length; } function isPlainObject(o) { if (!hasObjectPrototype(o)) { return false; } const ctor = o.constructor; if (ctor === void 0) { return true; } const prot = ctor.prototype; if (!hasObjectPrototype(prot)) { return false; } if (!prot.hasOwnProperty("isPrototypeOf")) { return false; } if (Object.getPrototypeOf(o) !== Object.prototype) { return false; } return true; } function hasObjectPrototype(o) { return Object.prototype.toString.call(o) === "[object Object]"; } function replaceData(prevData, data, options) { if (typeof options.structuralSharing === "function") { return options.structuralSharing(prevData, data); } else if (options.structuralSharing !== false) { if (process.env.NODE_ENV !== "production") { try { return replaceEqualDeep(prevData, data); } catch (error) { console.error( `Structural sharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}` ); throw error; } } return replaceEqualDeep(prevData, data); } return data; } function shouldThrowError(throwOnError, params) { if (typeof throwOnError === "function") { return throwOnError(...params); } return !!throwOnError; } // ../../node_modules/@tanstack/query-core/build/modern/environmentManager.js var environmentManager = /* @__PURE__ */ (() => { let isServerFn = () => isServer; return { /** * Returns whether the current runtime should be treated as a server environment. */ isServer() { return isServerFn(); }, /** * Overrides the server check globally. */ setIsServer(isServerValue) { isServerFn = isServerValue; } }; })(); // ../../node_modules/@tanstack/query-core/build/modern/thenable.js function pendingThenable() { let resolve; let reject; const thenable = new Promise((_resolve, _reject) => { resolve = _resolve; reject = _reject; }); thenable.status = "pending"; thenable.catch(() => { }); function finalize(data) { Object.assign(thenable, data); delete thenable.resolve; delete thenable.reject; } thenable.resolve = (value) => { finalize({ status: "fulfilled", value }); resolve(value); }; thenable.reject = (reason) => { finalize({ status: "rejected", reason }); reject(reason); }; return thenable; } // ../../node_modules/@tanstack/query-core/build/modern/notifyManager.js var defaultScheduler = systemSetTimeoutZero; function createNotifyManager() { let queue = []; let transactions = 0; let notifyFn = (callback) => { callback(); }; let batchNotifyFn = (callback) => { callback(); }; let scheduleFn = defaultScheduler; const schedule = (callback) => { if (transactions) { queue.push(callback); } else { scheduleFn(() => { notifyFn(callback); }); } }; const flush = () => { const originalQueue = queue; queue = []; if (originalQueue.length) { scheduleFn(() => { batchNotifyFn(() => { originalQueue.forEach((callback) => { notifyFn(callback); }); }); }); } }; return { batch: (callback) => { let result; transactions++; try { result = callback(); } finally { transactions--; if (!transactions) { flush(); } } return result; }, /** * All calls to the wrapped function will be batched. */ batchCalls: (callback) => { return (...args) => { schedule(() => { callback(...args); }); }; }, schedule, /** * Use this method to set a custom notify function. * This can be used to for example wrap notifications with `React.act` while running tests. */ setNotifyFunction: (fn) => { notifyFn = fn; }, /** * Use this method to set a custom function to batch notifications together into a single tick. * By default React Query will use the batch function provided by ReactDOM or React Native. */ setBatchNotifyFunction: (fn) => { batchNotifyFn = fn; }, setScheduler: (fn) => { scheduleFn = fn; } }; } var notifyManager = createNotifyManager(); // ../../node_modules/@tanstack/query-core/build/modern/onlineManager.js var OnlineManager = class extends Subscribable { #online = true; #cleanup; #setup; constructor() { super(); this.#setup = (onOnline) => { if (typeof window !== "undefined" && window.addEventListener) { const onlineListener = () => onOnline(true); const offlineListener = () => onOnline(false); window.addEventListener("online", onlineListener, false); window.addEventListener("offline", offlineListener, false); return () => { window.removeEventListener("online", onlineListener); window.removeEventListener("offline", offlineListener); }; } return; }; } onSubscribe() { if (!this.#cleanup) { this.setEventListener(this.#setup); } } onUnsubscribe() { if (!this.hasListeners()) { this.#cleanup?.(); this.#cleanup = void 0; } } setEventListener(setup) { this.#setup = setup; this.#cleanup?.(); this.#cleanup = setup(this.setOnline.bind(this)); } setOnline(online) { const changed = this.#online !== online; if (changed) { this.#online = online; this.listeners.forEach((listener) => { listener(online); }); } } isOnline() { return this.#online; } }; var onlineManager = new OnlineManager(); // ../../node_modules/@tanstack/query-core/build/modern/retryer.js function canFetch(networkMode) { return (networkMode ?? "online") === "online" ? onlineManager.isOnline() : true; } // ../../node_modules/@tanstack/query-core/build/modern/query.js function fetchState(data, options) { return { fetchFailureCount: 0, fetchFailureReason: null, fetchStatus: canFetch(options.networkMode) ? "fetching" : "paused", ...data === void 0 && { error: null, status: "pending" } }; } // ../../node_modules/@tanstack/query-core/build/modern/queryObserver.js var QueryObserver = class extends Subscribable { constructor(client, options) { super(); this.options = options; this.#client = client; this.#selectError = null; this.#currentThenable = pendingThenable(); this.bindMethods(); this.setOptions(options); } #client; #currentQuery = void 0; #currentQueryInitialState = void 0; #currentResult = void 0; #currentResultState; #currentResultOptions; #currentThenable; #selectError; #selectFn; #selectResult; // This property keeps track of the last query with defined data. // It will be used to pass the previous data and query to the placeholder function between renders. #lastQueryWithDefinedData; #staleTimeoutId; #refetchIntervalId; #currentRefetchInterval; #trackedProps = /* @__PURE__ */ new Set(); bindMethods() { this.refetch = this.refetch.bind(this); } onSubscribe() { if (this.listeners.size === 1) { this.#currentQuery.addObserver(this); if (shouldFetchOnMount(this.#currentQuery, this.options)) { this.#executeFetch(); } else { this.updateResult(); } this.#updateTimers(); } } onUnsubscribe() { if (!this.hasListeners()) { this.destroy(); } } shouldFetchOnReconnect() { return shouldFetchOn( this.#currentQuery, this.options, this.options.refetchOnReconnect ); } shouldFetchOnWindowFocus() { return shouldFetchOn( this.#currentQuery, this.options, this.options.refetchOnWindowFocus ); } destroy() { this.listeners = /* @__PURE__ */ new Set(); this.#clearStaleTimeout(); this.#clearRefetchInterval(); this.#currentQuery.removeObserver(this); } setOptions(options) { const prevOptions = this.options; const prevQuery = this.#currentQuery; this.options = this.#client.defaultQueryOptions(options); if (this.options.enabled !== void 0 && typeof this.options.enabled !== "boolean" && typeof this.options.enabled !== "function" && typeof resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== "boolean") { throw new Error( "Expected enabled to be a boolean or a callback that returns a boolean" ); } this.#updateQuery(); this.#currentQuery.setOptions(this.options); if (prevOptions._defaulted && !shallowEqualObjects(this.options, prevOptions)) { this.#client.getQueryCache().notify({ type: "observerOptionsUpdated", query: this.#currentQuery, observer: this }); } const mounted = this.hasListeners(); if (mounted && shouldFetchOptionally( this.#currentQuery, prevQuery, this.options, prevOptions )) { this.#executeFetch(); } this.updateResult(); if (mounted && (this.#currentQuery !== prevQuery || resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== resolveQueryBoolean(prevOptions.enabled, this.#currentQuery) || resolveStaleTime(this.options.staleTime, this.#currentQuery) !== resolveStaleTime(prevOptions.staleTime, this.#currentQuery))) { this.#updateStaleTimeout(); } const nextRefetchInterval = this.#computeRefetchInterval(); if (mounted && (this.#currentQuery !== prevQuery || resolveQueryBoolean(this.options.enabled, this.#currentQuery) !== resolveQueryBoolean(prevOptions.enabled, this.#currentQuery) || nextRefetchInterval !== this.#currentRefetchInterval)) { this.#updateRefetchInterval(nextRefetchInterval); } } getOptimisticResult(options) { const query = this.#client.getQueryCache().build(this.#client, options); const result = this.createResult(query, options); if (shouldAssignObserverCurrentProperties(this, result)) { this.#currentResult = result; this.#currentResultOptions = this.options; this.#currentResultState = this.#currentQuery.state; } return result; } getCurrentResult() { return this.#currentResult; } trackResult(result, onPropTracked) { return new Proxy(result, { get: (target, key) => { this.trackProp(key); onPropTracked?.(key); if (key === "promise") { this.trackProp("data"); if (!this.options.experimental_prefetchInRender && this.#currentThenable.status === "pending") { this.#currentThenable.reject( new Error( "experimental_prefetchInRender feature flag is not enabled" ) ); } } return Reflect.get(target, key); } }); } trackProp(key) { this.#trackedProps.add(key); } getCurrentQuery() { return this.#currentQuery; } refetch({ ...options } = {}) { return this.fetch({ ...options }); } fetchOptimistic(options) { const defaultedOptions = this.#client.defaultQueryOptions(options); const query = this.#client.getQueryCache().build(this.#client, defaultedOptions); return query.fetch().then(() => this.createResult(query, defaultedOptions)); } fetch(fetchOptions) { return this.#executeFetch({ ...fetchOptions, cancelRefetch: fetchOptions.cancelRefetch ?? true }).then(() => { this.updateResult(); return this.#currentResult; }); } #executeFetch(fetchOptions) { this.#updateQuery(); let promise = this.#currentQuery.fetch( this.options, fetchOptions ); if (!fetchOptions?.throwOnError) { promise = promise.catch(noop); } return promise; } #updateStaleTimeout() { this.#clearStaleTimeout(); const staleTime = resolveStaleTime( this.options.staleTime, this.#currentQuery ); if (environmentManager.isServer() || this.#currentResult.isStale || !isValidTimeout(staleTime)) { return; } const time = timeUntilStale(this.#currentResult.dataUpdatedAt, staleTime); const timeout = time + 1; this.#staleTimeoutId = timeoutManager.setTimeout(() => { if (!this.#currentResult.isStale) { this.updateResult(); } }, timeout); } #computeRefetchInterval() { return (typeof this.options.refetchInterval === "function" ? this.options.refetchInterval(this.#currentQuery) : this.options.refetchInterval) ?? false; } #updateRefetchInterval(nextInterval) { this.#clearRefetchInterval(); this.#currentRefetchInterval = nextInterval; if (environmentManager.isServer() || resolveQueryBoolean(this.options.enabled, this.#currentQuery) === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) { return; } this.#refetchIntervalId = timeoutManager.setInterval(() => { if (this.options.refetchIntervalInBackground || focusManager.isFocused()) { this.#executeFetch(); } }, this.#currentRefetchInterval); } #updateTimers() { this.#updateStaleTimeout(); this.#updateRefetchInterval(this.#computeRefetchInterval()); } #clearStaleTimeout() { if (this.#staleTimeoutId !== void 0) { timeoutManager.clearTimeout(this.#staleTimeoutId); this.#staleTimeoutId = void 0; } } #clearRefetchInterval() { if (this.#refetchIntervalId !== void 0) { timeoutManager.clearInterval(this.#refetchIntervalId); this.#refetchIntervalId = void 0; } } createResult(query, options) { const prevQuery = this.#currentQuery; const prevOptions = this.options; const prevResult = this.#currentResult; const prevResultState = this.#currentResultState; const prevResultOptions = this.#currentResultOptions; const queryChange = query !== prevQuery; const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState; const { state } = query; let newState = { ...state }; let isPlaceholderData = false; let data; if (options._optimisticResults) { const mounted = this.hasListeners(); const fetchOnMount = !mounted && shouldFetchOnMount(query, options); const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions); if (fetchOnMount || fetchOptionally) { newState = { ...newState, ...fetchState(state.data, query.options) }; } if (options._optimisticResults === "isRestoring") { newState.fetchStatus = "idle"; } } let { error, errorUpdatedAt, status } = newState; data = newState.data; let skipSelect = false; if (options.placeholderData !== void 0 && data === void 0 && status === "pending") { let placeholderData; if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) { placeholderData = prevResult.data; skipSelect = true; } else { placeholderData = typeof options.placeholderData === "function" ? options.placeholderData( this.#lastQueryWithDefinedData?.state.data, this.#lastQueryWithDefinedData ) : options.placeholderData; } if (placeholderData !== void 0) { status = "success"; data = replaceData( prevResult?.data, placeholderData, options ); isPlaceholderData = true; } } if (options.select && data !== void 0 && !skipSelect) { if (prevResult && data === prevResultState?.data && options.select === this.#selectFn) { data = this.#selectResult; } else { try { this.#selectFn = options.select; data = options.select(data); data = replaceData(prevResult?.data, data, options); this.#selectResult = data; this.#selectError = null; } catch (selectError) { this.#selectError = selectError; } } } if (this.#selectError) { error = this.#selectError; data = this.#selectResult; errorUpdatedAt = Date.now(); status = "error"; } const isFetching = newState.fetchStatus === "fetching"; const isPending = status === "pending"; const isError = status === "error"; const isLoading = isPending && isFetching; const hasData = data !== void 0; const result = { status, fetchStatus: newState.fetchStatus, isPending, isSuccess: status === "success", isError, isInitialLoading: isLoading, isLoading, data, dataUpdatedAt: newState.dataUpdatedAt, error, errorUpdatedAt, failureCount: newState.fetchFailureCount, failureReason: newState.fetchFailureReason, errorUpdateCount: newState.errorUpdateCount, isFetched: query.isFetched(), isFetchedAfterMount: newState.dataUpdateCount > queryInitialState.dataUpdateCount || newState.errorUpdateCount > queryInitialState.errorUpdateCount, isFetching, isRefetching: isFetching && !isPending, isLoadingError: isError && !hasData, isPaused: newState.fetchStatus === "paused", isPlaceholderData, isRefetchError: isError && hasData, isStale: isStale(query, options), refetch: this.refetch, promise: this.#currentThenable, isEnabled: resolveQueryBoolean(options.enabled, query) !== false }; const nextResult = result; if (this.options.experimental_prefetchInRender) { const hasResultData = nextResult.data !== void 0; const isErrorWithoutData = nextResult.status === "error" && !hasResultData; const finalizeThenableIfPossible = (thenable) => { if (isErrorWithoutData) { thenable.reject(nextResult.error); } else if (hasResultData) { thenable.resolve(nextResult.data); } }; const recreateThenable = () => { const pending = this.#currentThenable = nextResult.promise = pendingThenable(); finalizeThenableIfPossible(pending); }; const prevThenable = this.#currentThenable; switch (prevThenable.status) { case "pending": if (query.queryHash === prevQuery.queryHash) { finalizeThenableIfPossible(prevThenable); } break; case "fulfilled": if (isErrorWithoutData || nextResult.data !== prevThenable.value) { recreateThenable(); } break; case "rejected": if (!isErrorWithoutData || nextResult.error !== prevThenable.reason) { recreateThenable(); } break; } } return nextResult; } updateResult() { const prevResult = this.#currentResult; const nextResult = this.createResult(this.#currentQuery, this.options); this.#currentResultState = this.#currentQuery.state; this.#currentResultOptions = this.options; if (this.#currentResultState.data !== void 0) { this.#lastQueryWithDefinedData = this.#currentQuery; } if (shallowEqualObjects(nextResult, prevResult)) { return; } this.#currentResult = nextResult; const shouldNotifyListeners = () => { if (!prevResult) { return true; } const { notifyOnChangeProps } = this.options; const notifyOnChangePropsValue = typeof notifyOnChangeProps === "function" ? notifyOnChangeProps() : notifyOnChangeProps; if (notifyOnChangePropsValue === "all" || !notifyOnChangePropsValue && !this.#trackedProps.size) { return true; } const includedProps = new Set( notifyOnChangePropsValue ?? this.#trackedProps ); if (this.options.throwOnError) { includedProps.add("error"); } return Object.keys(this.#currentResult).some((key) => { const typedKey = key; const changed = this.#currentResult[typedKey] !== prevResult[typedKey]; return changed && includedProps.has(typedKey); }); }; this.#notify({ listeners: shouldNotifyListeners() }); } #updateQuery() { const query = this.#client.getQueryCache().build(this.#client, this.options); if (query === this.#currentQuery) { return; } const prevQuery = this.#currentQuery; this.#currentQuery = query; this.#currentQueryInitialState = query.state; if (this.hasListeners()) { prevQuery?.removeObserver(this); query.addObserver(this); } } onQueryUpdate() { this.updateResult(); if (this.hasListeners()) { this.#updateTimers(); } } #notify(notifyOptions) { notifyManager.batch(() => { if (notifyOptions.listeners) { this.listeners.forEach((listener) => { listener(this.#currentResult); }); } this.#client.getQueryCache().notify({ query: this.#currentQuery, type: "observerResultsUpdated" }); }); } }; function shouldLoadOnMount(query, options) { return resolveQueryBoolean(options.enabled, query) !== false && query.state.data === void 0 && !(query.state.status === "error" && resolveQueryBoolean(options.retryOnMount, query) === false); } function shouldFetchOnMount(query, options) { return shouldLoadOnMount(query, options) || query.state.data !== void 0 && shouldFetchOn(query, options, options.refetchOnMount); } function shouldFetchOn(query, options, field) { if (resolveQueryBoolean(options.enabled, query) !== false && resolveStaleTime(options.staleTime, query) !== "static") { const value = typeof field === "function" ? field(query) : field; return value === "always" || value !== false && isStale(query, options); } return false; } function shouldFetchOptionally(query, prevQuery, options, prevOptions) { return (query !== prevQuery || resolveQueryBoolean(prevOptions.enabled, query) === false) && (!options.suspense || query.state.status !== "error") && isStale(query, options); } function isStale(query, options) { return resolveQueryBoolean(options.enabled, query) !== false && query.isStaleByTime(resolveStaleTime(options.staleTime, query)); } function shouldAssignObserverCurrentProperties(observer, optimisticResult) { if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) { return true; } return false; } // ../../node_modules/@tanstack/react-query/build/modern/QueryClientProvider.js var React = __toESM(require("react"), 1); var import_jsx_runtime = require("react/jsx-runtime"); var QueryClientContext = React.createContext( void 0 ); var useQueryClient = (queryClient) => { const client = React.useContext(QueryClientContext); if (queryClient) { return queryClient; } if (!client) { throw new Error("No QueryClient set, use QueryClientProvider to set one"); } return client; }; // ../../node_modules/@tanstack/react-query/build/modern/IsRestoringProvider.js var React2 = __toESM(require("react"), 1); var IsRestoringContext = React2.createContext(false); var useIsRestoring = () => React2.useContext(IsRestoringContext); var IsRestoringProvider = IsRestoringContext.Provider; // ../../node_modules/@tanstack/react-query/build/modern/QueryErrorResetBoundary.js var React3 = __toESM(require("react"), 1); var import_jsx_runtime2 = require("react/jsx-runtime"); function createValue() { let isReset = false; return { clearReset: () => { isReset = false; }, reset: () => { isReset = true; }, isReset: () => { return isReset; } }; } var QueryErrorResetBoundaryContext = React3.createContext(createValue()); var useQueryErrorResetBoundary = () => React3.useContext(QueryErrorResetBoundaryContext); // ../../node_modules/@tanstack/react-query/build/modern/errorBoundaryUtils.js var React4 = __toESM(require("react"), 1); var ensurePreventErrorBoundaryRetry = (options, errorResetBoundary, query) => { const throwOnError = query?.state.error && typeof options.throwOnError === "function" ? shouldThrowError(options.throwOnError, [query.state.error, query]) : options.throwOnError; if (options.suspense || options.experimental_prefetchInRender || throwOnError) { if (!errorResetBoundary.isReset()) { options.retryOnMount = false; } } }; var useClearResetErrorBoundary = (errorResetBoundary) => { React4.useEffect(() => { errorResetBoundary.clearReset(); }, [errorResetBoundary]); }; var getHasError = ({ result, errorResetBoundary, throwOnError, query, suspense }) => { return result.isError && !errorResetBoundary.isReset() && !result.isFetching && query && (suspense && result.data === void 0 || shouldThrowError(throwOnError, [result.error, query])); }; // ../../node_modules/@tanstack/react-query/build/modern/suspense.js var ensureSuspenseTimers = (defaultedOptions) => { if (defaultedOptions.suspense) { const MIN_SUSPENSE_TIME_MS = 1e3; const clamp = (value) => value === "static" ? value : Math.max(value ?? MIN_SUSPENSE_TIME_MS, MIN_SUSPENSE_TIME_MS); const originalStaleTime = defaultedOptions.staleTime; defaultedOptions.staleTime = typeof originalStaleTime === "function" ? (...args) => clamp(originalStaleTime(...args)) : clamp(originalStaleTime); if (typeof defaultedOptions.gcTime === "number") { defaultedOptions.gcTime = Math.max( defaultedOptions.gcTime, MIN_SUSPENSE_TIME_MS ); } } }; var willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring; var shouldSuspend = (defaultedOptions, result) => defaultedOptions?.suspense && result.isPending; var fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer.fetchOptimistic(defaultedOptions).catch(() => { errorResetBoundary.clearReset(); }); // ../../node_modules/@tanstack/react-query/build/modern/useBaseQuery.js var React5 = __toESM(require("react"), 1); function useBaseQuery(options, Observer, queryClient) { if (process.env.NODE_ENV !== "production") { if (typeof options !== "object" || Array.isArray(options)) { throw new Error( 'Bad argument type. Starting with v5, only the "Object" form is allowed when calling query related functions. Please use the error stack to find the culprit call. More info here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#supports-a-single-signature-one-object' ); } } const isRestoring = useIsRestoring(); const errorResetBoundary = useQueryErrorResetBoundary(); const client = useQueryClient(queryClient); const defaultedOptions = client.defaultQueryOptions(options); client.getDefaultOptions().queries?._experimental_beforeQuery?.( defaultedOptions ); const query = client.getQueryCache().get(defaultedOptions.queryHash); if (process.env.NODE_ENV !== "production") { if (!defaultedOptions.queryFn) { console.error( `[${defaultedOptions.queryHash}]: No queryFn was passed as an option, and no default queryFn was found. The queryFn parameter is only optional when using a default queryFn. More info here: https://tanstack.com/query/latest/docs/framework/react/guides/default-query-function` ); } } const subscribed = options.subscribed !== false; defaultedOptions._optimisticResults = isRestoring ? "isRestoring" : subscribed ? "optimistic" : void 0; ensureSuspenseTimers(defaultedOptions); ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary, query); useClearResetErrorBoundary(errorResetBoundary); const isNewCacheEntry = !client.getQueryCache().get(defaultedOptions.queryHash); const [observer] = React5.useState( () => new Observer( client, defaultedOptions ) ); const result = observer.getOptimisticResult(defaultedOptions); const shouldSubscribe = !isRestoring && subscribed; React5.useSyncExternalStore( React5.useCallback( (onStoreChange) => { const unsubscribe = shouldSubscribe ? observer.subscribe(notifyManager.batchCalls(onStoreChange)) : noop; observer.updateResult(); return unsubscribe; }, [observer, shouldSubscribe] ), () => observer.getCurrentResult(), () => observer.getCurrentResult() ); React5.useEffect(() => { observer.setOptions(defaultedOptions); }, [defaultedOptions, observer]); if (shouldSuspend(defaultedOptions, result)) { throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary); } if (getHasError({ result, errorResetBoundary, throwOnError: defaultedOptions.throwOnError, query, suspense: defaultedOptions.suspense })) { throw result.error; } ; client.getDefaultOptions().queries?._experimental_afterQuery?.( defaultedOptions, result ); if (defaultedOptions.experimental_prefetchInRender && !environmentManager.isServer() && willFetch(result, isRestoring)) { const promise = isNewCacheEntry ? ( // Fetch immediately on render in order to ensure `.promise` is resolved even if the component is unmounted fetchOptimistic(defaultedOptions, observer, errorResetBoundary) ) : ( // subscribe to the "cache promise" so that we can finalize the currentThenable once data comes in query?.promise ); promise?.catch(noop).finally(() => { observer.updateResult(); }); } return !defaultedOptions.notifyOnChangeProps ? observer.trackResult(result) : result; } // ../../node_modules/@tanstack/react-query/build/modern/useQuery.js function useQuery(options, queryClient) { return useBaseQuery(options, QueryObserver, queryClient); } // src/react/hooks/useReferrals.ts var import_react2 = require("react"); // src/react/AutumnContext.tsx var import_react = require("react"); var AutumnContext = (0, import_react.createContext)(null); var useAutumnClient = ({ caller }) => { const context = (0, import_react.useContext)(AutumnContext); if (!context) { throw new Error(`${caller} must be used within <AutumnProvider/>`); } return context.client; }; // src/react/hooks/useReferrals.ts var useReferrals = (params) => { const client = useAutumnClient({ caller: "useReferrals" }); const { programId, queryOptions } = params; const queryResult = useQuery({ queryKey: ["autumn", "referrals", "create", programId], queryFn: () => client.createReferralCode({ programId }), enabled: false, ...queryOptions }); const redeemCode = (0, import_react2.useCallback)( async (redeemParams) => { return client.redeemReferralCode(redeemParams); }, [client] ); return { ...queryResult, redeemCode }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useReferrals });