import { ref, toRef, getCurrentInstance, onServerPrefetch, unref } from 'vue'; import { m as useNuxtApp, c as createError } from '../server.mjs'; const getDefault = () => null; function useAsyncData(...args) { var _a; const autoKey = typeof args[args.length - 1] === "string" ? args.pop() : void 0; if (typeof args[0] !== "string") { args.unshift(autoKey); } let [key, handler, options = {}] = args; if (typeof key !== "string") { throw new TypeError("[nuxt] [asyncData] key must be a string."); } if (typeof handler !== "function") { throw new TypeError("[nuxt] [asyncData] handler must be a function."); } options.server = options.server ?? true; options.default = options.default ?? getDefault; options.lazy = options.lazy ?? false; options.immediate = options.immediate ?? true; const nuxt = useNuxtApp(); const getCachedData = () => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]; const hasCachedData = () => getCachedData() !== void 0; if (!nuxt._asyncData[key]) { nuxt._asyncData[key] = { data: ref(getCachedData() ?? ((_a = options.default) == null ? void 0 : _a.call(options)) ?? null), pending: ref(!hasCachedData()), error: toRef(nuxt.payload._errors, key) }; } const asyncData = { ...nuxt._asyncData[key] }; asyncData.refresh = asyncData.execute = (opts = {}) => { if (nuxt._asyncDataPromises[key]) { if (opts.dedupe === false) { return nuxt._asyncDataPromises[key]; } nuxt._asyncDataPromises[key].cancelled = true; } if (opts._initial && hasCachedData()) { return getCachedData(); } asyncData.pending.value = true; const promise = new Promise( (resolve, reject) => { try { resolve(handler(nuxt)); } catch (err) { reject(err); } } ).then((_result) => { if (promise.cancelled) { return nuxt._asyncDataPromises[key]; } let result = _result; if (options.transform) { result = options.transform(_result); } if (options.pick) { result = pick(result, options.pick); } asyncData.data.value = result; asyncData.error.value = null; }).catch((error) => { var _a2; if (promise.cancelled) { return nuxt._asyncDataPromises[key]; } asyncData.error.value = error; asyncData.data.value = unref(((_a2 = options.default) == null ? void 0 : _a2.call(options)) ?? null); }).finally(() => { if (promise.cancelled) { return; } asyncData.pending.value = false; nuxt.payload.data[key] = asyncData.data.value; if (asyncData.error.value) { nuxt.payload._errors[key] = createError(asyncData.error.value); } delete nuxt._asyncDataPromises[key]; }); nuxt._asyncDataPromises[key] = promise; return nuxt._asyncDataPromises[key]; }; const initialFetch = () => asyncData.refresh({ _initial: true }); const fetchOnServer = options.server !== false && nuxt.payload.serverRendered; if (fetchOnServer && options.immediate) { const promise = initialFetch(); if (getCurrentInstance()) { onServerPrefetch(() => promise); } else { nuxt.hook("app:created", () => promise); } } const asyncDataPromise = Promise.resolve(nuxt._asyncDataPromises[key]).then(() => asyncData); Object.assign(asyncDataPromise, asyncData); return asyncDataPromise; } function pick(obj, keys) { const newObj = {}; for (const key of keys) { newObj[key] = obj[key]; } return newObj; } export { useAsyncData as u }; //# sourceMappingURL=asyncData-e8a8bfb6.mjs.map