feat: init
This commit is contained in:
185
node_modules/unplugin-vue-router/dist/data-loaders/basic.cjs
generated
vendored
Normal file
185
node_modules/unplugin-vue-router/dist/data-loaders/basic.cjs
generated
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
const require_createDataLoader = require('./createDataLoader-CBwrbo71.cjs');
|
||||
let vue_router = require("vue-router");
|
||||
let unplugin_vue_router_data_loaders = require("unplugin-vue-router/data-loaders");
|
||||
let vue = require("vue");
|
||||
require("scule");
|
||||
|
||||
//#region src/core/utils.ts
|
||||
function warn(msg, type = "warn") {
|
||||
console[type](`⚠️ [unplugin-vue-router]: ${msg}`);
|
||||
}
|
||||
/**
|
||||
* Type safe alternative to Array.isArray
|
||||
* https://github.com/microsoft/TypeScript/pull/48228
|
||||
*/
|
||||
const isArray = Array.isArray;
|
||||
|
||||
//#endregion
|
||||
//#region src/data-loaders/defineLoader.ts
|
||||
function defineBasicLoader(nameOrLoader, _loaderOrOptions, opts) {
|
||||
const loader = typeof nameOrLoader === "function" ? nameOrLoader : _loaderOrOptions;
|
||||
opts = typeof _loaderOrOptions === "object" ? _loaderOrOptions : opts;
|
||||
const options = {
|
||||
...DEFAULT_DEFINE_LOADER_OPTIONS,
|
||||
...opts,
|
||||
commit: opts?.commit || DEFAULT_DEFINE_LOADER_OPTIONS.commit
|
||||
};
|
||||
function load(to, router, from, parent) {
|
||||
const entries = router[unplugin_vue_router_data_loaders.LOADER_ENTRIES_KEY];
|
||||
const isSSR = router[unplugin_vue_router_data_loaders.IS_SSR_KEY];
|
||||
if (!entries.has(loader)) entries.set(loader, {
|
||||
data: (0, vue.shallowRef)(),
|
||||
isLoading: (0, vue.shallowRef)(false),
|
||||
error: (0, vue.shallowRef)(null),
|
||||
to,
|
||||
options,
|
||||
children: /* @__PURE__ */ new Set(),
|
||||
resetPending() {
|
||||
this.pendingLoad = null;
|
||||
this.pendingTo = null;
|
||||
},
|
||||
pendingLoad: null,
|
||||
pendingTo: null,
|
||||
staged: unplugin_vue_router_data_loaders.STAGED_NO_VALUE,
|
||||
stagedError: null,
|
||||
stagedNavigationResult: null,
|
||||
commit
|
||||
});
|
||||
const entry = entries.get(loader);
|
||||
if (entry.pendingTo === to && entry.pendingLoad) return entry.pendingLoad;
|
||||
const { error, isLoading, data } = entry;
|
||||
const initialRootData = router[INITIAL_DATA_KEY];
|
||||
const key = options.key || "";
|
||||
let initialData = unplugin_vue_router_data_loaders.STAGED_NO_VALUE;
|
||||
if (initialRootData && key in initialRootData) {
|
||||
initialData = initialRootData[key];
|
||||
delete initialRootData[key];
|
||||
}
|
||||
if (initialData !== unplugin_vue_router_data_loaders.STAGED_NO_VALUE) {
|
||||
data.value = initialData;
|
||||
return entry.pendingLoad = Promise.resolve();
|
||||
}
|
||||
entry.pendingTo = to;
|
||||
isLoading.value = true;
|
||||
const currentContext = (0, unplugin_vue_router_data_loaders.getCurrentContext)();
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (parent !== currentContext[0]) console.warn(`❌👶 "${options.key}" has a different parent than the current context. This shouldn't be happening. Please report a bug with a reproduction to https://github.com/posva/unplugin-vue-router/`);
|
||||
}
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)([
|
||||
entry,
|
||||
router,
|
||||
to
|
||||
]);
|
||||
entry.staged = unplugin_vue_router_data_loaders.STAGED_NO_VALUE;
|
||||
entry.stagedError = error.value;
|
||||
entry.stagedNavigationResult = null;
|
||||
const currentLoad = Promise.resolve(loader(to, { signal: to.meta[unplugin_vue_router_data_loaders.ABORT_CONTROLLER_KEY]?.signal })).then((d) => {
|
||||
if (entry.pendingLoad === currentLoad) if (d instanceof unplugin_vue_router_data_loaders.NavigationResult) {
|
||||
to.meta[unplugin_vue_router_data_loaders.NAVIGATION_RESULTS_KEY].push(d);
|
||||
entry.stagedNavigationResult = d;
|
||||
if (process.env.NODE_ENV !== "production") warnNonExposedLoader({
|
||||
to,
|
||||
options,
|
||||
useDataLoader
|
||||
});
|
||||
} else {
|
||||
entry.staged = d;
|
||||
entry.stagedError = null;
|
||||
}
|
||||
}).catch((error$1) => {
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (error$1 instanceof unplugin_vue_router_data_loaders.NavigationResult) warnNonExposedLoader({
|
||||
to,
|
||||
options,
|
||||
useDataLoader
|
||||
});
|
||||
}
|
||||
entry.stagedError = error$1;
|
||||
if (!require_createDataLoader.toLazyValue(options.lazy, to, from) || isSSR) throw error$1;
|
||||
}
|
||||
}).finally(() => {
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)(currentContext);
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
isLoading.value = false;
|
||||
if (options.commit === "immediate" || !router[unplugin_vue_router_data_loaders.PENDING_LOCATION_KEY]) entry.commit(to);
|
||||
}
|
||||
});
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)(currentContext);
|
||||
entry.pendingLoad = currentLoad;
|
||||
return currentLoad;
|
||||
}
|
||||
function commit(to) {
|
||||
if (this.pendingTo === to) {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (this.staged === unplugin_vue_router_data_loaders.STAGED_NO_VALUE && this.stagedError === null && this.stagedNavigationResult === null) console.warn(`Loader "${options.key}"'s "commit()" was called but there is no staged data.`);
|
||||
}
|
||||
if (this.staged !== unplugin_vue_router_data_loaders.STAGED_NO_VALUE) this.data.value = this.staged;
|
||||
this.error.value = this.stagedError;
|
||||
this.staged = unplugin_vue_router_data_loaders.STAGED_NO_VALUE;
|
||||
this.stagedError = this.error.value;
|
||||
this.pendingTo = null;
|
||||
this.to = to;
|
||||
for (const childEntry of this.children) childEntry.commit(to);
|
||||
}
|
||||
}
|
||||
const useDataLoader = () => {
|
||||
const currentContext = (0, unplugin_vue_router_data_loaders.getCurrentContext)();
|
||||
const [parentEntry, _router, _route] = currentContext;
|
||||
const router = _router || (0, vue_router.useRouter)();
|
||||
const route = _route || (0, vue_router.useRoute)();
|
||||
const entries = router[unplugin_vue_router_data_loaders.LOADER_ENTRIES_KEY];
|
||||
let entry = entries.get(loader);
|
||||
if (!entry || parentEntry && entry.pendingTo !== route || !entry.pendingLoad) router[unplugin_vue_router_data_loaders.APP_KEY].runWithContext(() => load(route, router, void 0, parentEntry));
|
||||
entry = entries.get(loader);
|
||||
if (parentEntry) {
|
||||
if (parentEntry === entry) console.warn(`👶❌ "${options.key}" has itself as parent. This shouldn't be happening. Please report a bug with a reproduction to https://github.com/posva/unplugin-vue-router/`);
|
||||
parentEntry.children.add(entry);
|
||||
}
|
||||
const { data, error, isLoading } = entry;
|
||||
const useDataLoaderResult = {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
reload: (to = router.currentRoute.value) => router[unplugin_vue_router_data_loaders.APP_KEY].runWithContext(() => load(to, router)).then(() => entry.commit(to))
|
||||
};
|
||||
const promise = entry.pendingLoad.then(() => {
|
||||
return entry.staged === unplugin_vue_router_data_loaders.STAGED_NO_VALUE ? entry.stagedNavigationResult ? Promise.reject(entry.stagedNavigationResult) : data.value : entry.staged;
|
||||
}).catch((e) => parentEntry ? Promise.reject(e) : null);
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)(currentContext);
|
||||
return Object.assign(promise, useDataLoaderResult);
|
||||
};
|
||||
useDataLoader[unplugin_vue_router_data_loaders.IS_USE_DATA_LOADER_KEY] = true;
|
||||
useDataLoader._ = {
|
||||
load,
|
||||
options,
|
||||
getEntry(router) {
|
||||
return router[unplugin_vue_router_data_loaders.LOADER_ENTRIES_KEY].get(loader);
|
||||
}
|
||||
};
|
||||
return useDataLoader;
|
||||
}
|
||||
/**
|
||||
* Dev only warning for loaders that return/throw NavigationResult but are not exposed
|
||||
*
|
||||
* @param to - target location
|
||||
* @param options - options used to define the loader
|
||||
* @param useDataLoader - the data loader composable
|
||||
*/
|
||||
function warnNonExposedLoader({ to, options, useDataLoader }) {
|
||||
const loaders = to.meta[unplugin_vue_router_data_loaders.LOADER_SET_KEY];
|
||||
if (loaders && !loaders.has(useDataLoader)) warn("A loader returned a NavigationResult but is not registered on the route. Did you forget to \"export\" it from the page component?" + (options.key ? ` (loader key: "${options.key}")` : ""));
|
||||
}
|
||||
const DEFAULT_DEFINE_LOADER_OPTIONS = {
|
||||
lazy: false,
|
||||
server: true,
|
||||
commit: "after-load"
|
||||
};
|
||||
/**
|
||||
* Initial data generated on server and consumed on client.
|
||||
* @internal
|
||||
*/
|
||||
const INITIAL_DATA_KEY = Symbol();
|
||||
|
||||
//#endregion
|
||||
exports.defineBasicLoader = defineBasicLoader;
|
||||
79
node_modules/unplugin-vue-router/dist/data-loaders/basic.d.cts
generated
vendored
Normal file
79
node_modules/unplugin-vue-router/dist/data-loaders/basic.d.cts
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
import { d as ErrorDefault$1, i as DefineDataLoaderOptionsBase_DefinedData } from "./createDataLoader-DgP0poyl.cjs";
|
||||
import { RouteLocationNormalizedLoaded, RouteMap } from "vue-router";
|
||||
import { DataLoaderContextBase, DataLoaderEntryBase, DefineDataLoaderOptionsBase_LaxData, DefineLoaderFn, UseDataLoader } from "unplugin-vue-router/data-loaders";
|
||||
|
||||
//#region src/data-loaders/defineLoader.d.ts
|
||||
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version `data` is always defined.
|
||||
*
|
||||
* @param name - name of the route
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Name extends keyof RouteMap, Data>(name: Name, loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded<Name>>, options?: DefineDataLoaderOptions_DefinedData): UseDataLoaderBasic_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version, `data` can be `undefined`.
|
||||
*
|
||||
* @param name - name of the route
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Name extends keyof RouteMap, Data>(name: Name, loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded<Name>>, options: DefineDataLoaderOptions_LaxData): UseDataLoaderBasic_LaxData<Data>;
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version `data` is always defined.
|
||||
*
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Data>(loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded>, options?: DefineDataLoaderOptions_DefinedData): UseDataLoaderBasic_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version, `data` can be `undefined`.
|
||||
*
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Data>(loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded>, options: DefineDataLoaderOptions_LaxData): UseDataLoaderBasic_LaxData<Data>;
|
||||
interface DefineDataLoaderOptions_LaxData extends DefineDataLoaderOptionsBase_LaxData {
|
||||
/**
|
||||
* Key to use for SSR state. This will be used to read the initial data from `initialData`'s object.
|
||||
*/
|
||||
key?: string;
|
||||
}
|
||||
interface DefineDataLoaderOptions_DefinedData extends DefineDataLoaderOptionsBase_DefinedData {
|
||||
key?: string;
|
||||
}
|
||||
/**
|
||||
* @deprecated use {@link DefineDataLoaderOptions_LaxData} instead
|
||||
*/
|
||||
type DefineDataLoaderOptions = DefineDataLoaderOptions_LaxData;
|
||||
interface DataLoaderContext extends DataLoaderContextBase {}
|
||||
/**
|
||||
* Symbol used to store the data in the router so it can be retrieved after the initial navigation.
|
||||
* @internal
|
||||
*/
|
||||
declare const SERVER_INITIAL_DATA_KEY: unique symbol;
|
||||
/**
|
||||
* Initial data generated on server and consumed on client.
|
||||
* @internal
|
||||
*/
|
||||
declare const INITIAL_DATA_KEY: unique symbol;
|
||||
declare module 'vue-router' {
|
||||
interface Router {
|
||||
/**
|
||||
* Gives access to the initial state during rendering. Should be set to `false` once it's consumed.
|
||||
* @internal
|
||||
*/
|
||||
[SERVER_INITIAL_DATA_KEY]?: Record<string, unknown> | false;
|
||||
[INITIAL_DATA_KEY]?: Record<string, unknown> | false;
|
||||
}
|
||||
}
|
||||
interface UseDataLoaderBasic_LaxData<Data> extends UseDataLoader<Data | undefined, ErrorDefault$1> {}
|
||||
/**
|
||||
* @deprecated use {@link UseDataLoaderBasic_LaxData} instead
|
||||
*/
|
||||
type UseDataLoaderBasic<Data> = UseDataLoaderBasic_LaxData<Data>;
|
||||
interface UseDataLoaderBasic_DefinedData<Data> extends UseDataLoader<Data, ErrorDefault$1> {}
|
||||
interface DataLoaderBasicEntry<TData, TError = unknown, TDataInitial extends TData | undefined = TData | undefined> extends DataLoaderEntryBase<TData, TError, TDataInitial> {}
|
||||
//#endregion
|
||||
export { type DataLoaderBasicEntry, type DataLoaderContext, type DefineDataLoaderOptions, type DefineDataLoaderOptions_DefinedData, type DefineDataLoaderOptions_LaxData, type UseDataLoaderBasic, type UseDataLoaderBasic_DefinedData, type UseDataLoaderBasic_LaxData, defineBasicLoader };
|
||||
79
node_modules/unplugin-vue-router/dist/data-loaders/basic.d.mts
generated
vendored
Normal file
79
node_modules/unplugin-vue-router/dist/data-loaders/basic.d.mts
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
import { d as ErrorDefault$1, i as DefineDataLoaderOptionsBase_DefinedData } from "./createDataLoader-C8o8GrdD.mjs";
|
||||
import { RouteLocationNormalizedLoaded, RouteMap } from "vue-router";
|
||||
import { DataLoaderContextBase, DataLoaderEntryBase, DefineDataLoaderOptionsBase_LaxData, DefineLoaderFn, UseDataLoader } from "unplugin-vue-router/data-loaders";
|
||||
|
||||
//#region src/data-loaders/defineLoader.d.ts
|
||||
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version `data` is always defined.
|
||||
*
|
||||
* @param name - name of the route
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Name extends keyof RouteMap, Data>(name: Name, loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded<Name>>, options?: DefineDataLoaderOptions_DefinedData): UseDataLoaderBasic_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version, `data` can be `undefined`.
|
||||
*
|
||||
* @param name - name of the route
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Name extends keyof RouteMap, Data>(name: Name, loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded<Name>>, options: DefineDataLoaderOptions_LaxData): UseDataLoaderBasic_LaxData<Data>;
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version `data` is always defined.
|
||||
*
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Data>(loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded>, options?: DefineDataLoaderOptions_DefinedData): UseDataLoaderBasic_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a data loader composable that can be exported by pages to attach the data loading to a route. In this version, `data` can be `undefined`.
|
||||
*
|
||||
* @param loader - function that returns a promise with the data
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineBasicLoader<Data>(loader: DefineLoaderFn<Data, DataLoaderContext, RouteLocationNormalizedLoaded>, options: DefineDataLoaderOptions_LaxData): UseDataLoaderBasic_LaxData<Data>;
|
||||
interface DefineDataLoaderOptions_LaxData extends DefineDataLoaderOptionsBase_LaxData {
|
||||
/**
|
||||
* Key to use for SSR state. This will be used to read the initial data from `initialData`'s object.
|
||||
*/
|
||||
key?: string;
|
||||
}
|
||||
interface DefineDataLoaderOptions_DefinedData extends DefineDataLoaderOptionsBase_DefinedData {
|
||||
key?: string;
|
||||
}
|
||||
/**
|
||||
* @deprecated use {@link DefineDataLoaderOptions_LaxData} instead
|
||||
*/
|
||||
type DefineDataLoaderOptions = DefineDataLoaderOptions_LaxData;
|
||||
interface DataLoaderContext extends DataLoaderContextBase {}
|
||||
/**
|
||||
* Symbol used to store the data in the router so it can be retrieved after the initial navigation.
|
||||
* @internal
|
||||
*/
|
||||
declare const SERVER_INITIAL_DATA_KEY: unique symbol;
|
||||
/**
|
||||
* Initial data generated on server and consumed on client.
|
||||
* @internal
|
||||
*/
|
||||
declare const INITIAL_DATA_KEY: unique symbol;
|
||||
declare module 'vue-router' {
|
||||
interface Router {
|
||||
/**
|
||||
* Gives access to the initial state during rendering. Should be set to `false` once it's consumed.
|
||||
* @internal
|
||||
*/
|
||||
[SERVER_INITIAL_DATA_KEY]?: Record<string, unknown> | false;
|
||||
[INITIAL_DATA_KEY]?: Record<string, unknown> | false;
|
||||
}
|
||||
}
|
||||
interface UseDataLoaderBasic_LaxData<Data> extends UseDataLoader<Data | undefined, ErrorDefault$1> {}
|
||||
/**
|
||||
* @deprecated use {@link UseDataLoaderBasic_LaxData} instead
|
||||
*/
|
||||
type UseDataLoaderBasic<Data> = UseDataLoaderBasic_LaxData<Data>;
|
||||
interface UseDataLoaderBasic_DefinedData<Data> extends UseDataLoader<Data, ErrorDefault$1> {}
|
||||
interface DataLoaderBasicEntry<TData, TError = unknown, TDataInitial extends TData | undefined = TData | undefined> extends DataLoaderEntryBase<TData, TError, TDataInitial> {}
|
||||
//#endregion
|
||||
export { type DataLoaderBasicEntry, type DataLoaderContext, type DefineDataLoaderOptions, type DefineDataLoaderOptions_DefinedData, type DefineDataLoaderOptions_LaxData, type UseDataLoaderBasic, type UseDataLoaderBasic_DefinedData, type UseDataLoaderBasic_LaxData, defineBasicLoader };
|
||||
185
node_modules/unplugin-vue-router/dist/data-loaders/basic.mjs
generated
vendored
Normal file
185
node_modules/unplugin-vue-router/dist/data-loaders/basic.mjs
generated
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
import { t as toLazyValue } from "./createDataLoader-BK9Gdnky.mjs";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { ABORT_CONTROLLER_KEY, APP_KEY, IS_SSR_KEY, IS_USE_DATA_LOADER_KEY, LOADER_ENTRIES_KEY, LOADER_SET_KEY, NAVIGATION_RESULTS_KEY, NavigationResult, PENDING_LOCATION_KEY, STAGED_NO_VALUE, getCurrentContext, setCurrentContext } from "unplugin-vue-router/data-loaders";
|
||||
import { shallowRef } from "vue";
|
||||
import "scule";
|
||||
|
||||
//#region src/core/utils.ts
|
||||
function warn(msg, type = "warn") {
|
||||
console[type](`⚠️ [unplugin-vue-router]: ${msg}`);
|
||||
}
|
||||
/**
|
||||
* Type safe alternative to Array.isArray
|
||||
* https://github.com/microsoft/TypeScript/pull/48228
|
||||
*/
|
||||
const isArray = Array.isArray;
|
||||
|
||||
//#endregion
|
||||
//#region src/data-loaders/defineLoader.ts
|
||||
function defineBasicLoader(nameOrLoader, _loaderOrOptions, opts) {
|
||||
const loader = typeof nameOrLoader === "function" ? nameOrLoader : _loaderOrOptions;
|
||||
opts = typeof _loaderOrOptions === "object" ? _loaderOrOptions : opts;
|
||||
const options = {
|
||||
...DEFAULT_DEFINE_LOADER_OPTIONS,
|
||||
...opts,
|
||||
commit: opts?.commit || DEFAULT_DEFINE_LOADER_OPTIONS.commit
|
||||
};
|
||||
function load(to, router, from, parent) {
|
||||
const entries = router[LOADER_ENTRIES_KEY];
|
||||
const isSSR = router[IS_SSR_KEY];
|
||||
if (!entries.has(loader)) entries.set(loader, {
|
||||
data: shallowRef(),
|
||||
isLoading: shallowRef(false),
|
||||
error: shallowRef(null),
|
||||
to,
|
||||
options,
|
||||
children: /* @__PURE__ */ new Set(),
|
||||
resetPending() {
|
||||
this.pendingLoad = null;
|
||||
this.pendingTo = null;
|
||||
},
|
||||
pendingLoad: null,
|
||||
pendingTo: null,
|
||||
staged: STAGED_NO_VALUE,
|
||||
stagedError: null,
|
||||
stagedNavigationResult: null,
|
||||
commit
|
||||
});
|
||||
const entry = entries.get(loader);
|
||||
if (entry.pendingTo === to && entry.pendingLoad) return entry.pendingLoad;
|
||||
const { error, isLoading, data } = entry;
|
||||
const initialRootData = router[INITIAL_DATA_KEY];
|
||||
const key = options.key || "";
|
||||
let initialData = STAGED_NO_VALUE;
|
||||
if (initialRootData && key in initialRootData) {
|
||||
initialData = initialRootData[key];
|
||||
delete initialRootData[key];
|
||||
}
|
||||
if (initialData !== STAGED_NO_VALUE) {
|
||||
data.value = initialData;
|
||||
return entry.pendingLoad = Promise.resolve();
|
||||
}
|
||||
entry.pendingTo = to;
|
||||
isLoading.value = true;
|
||||
const currentContext = getCurrentContext();
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (parent !== currentContext[0]) console.warn(`❌👶 "${options.key}" has a different parent than the current context. This shouldn't be happening. Please report a bug with a reproduction to https://github.com/posva/unplugin-vue-router/`);
|
||||
}
|
||||
setCurrentContext([
|
||||
entry,
|
||||
router,
|
||||
to
|
||||
]);
|
||||
entry.staged = STAGED_NO_VALUE;
|
||||
entry.stagedError = error.value;
|
||||
entry.stagedNavigationResult = null;
|
||||
const currentLoad = Promise.resolve(loader(to, { signal: to.meta[ABORT_CONTROLLER_KEY]?.signal })).then((d) => {
|
||||
if (entry.pendingLoad === currentLoad) if (d instanceof NavigationResult) {
|
||||
to.meta[NAVIGATION_RESULTS_KEY].push(d);
|
||||
entry.stagedNavigationResult = d;
|
||||
if (process.env.NODE_ENV !== "production") warnNonExposedLoader({
|
||||
to,
|
||||
options,
|
||||
useDataLoader
|
||||
});
|
||||
} else {
|
||||
entry.staged = d;
|
||||
entry.stagedError = null;
|
||||
}
|
||||
}).catch((error$1) => {
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (error$1 instanceof NavigationResult) warnNonExposedLoader({
|
||||
to,
|
||||
options,
|
||||
useDataLoader
|
||||
});
|
||||
}
|
||||
entry.stagedError = error$1;
|
||||
if (!toLazyValue(options.lazy, to, from) || isSSR) throw error$1;
|
||||
}
|
||||
}).finally(() => {
|
||||
setCurrentContext(currentContext);
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
isLoading.value = false;
|
||||
if (options.commit === "immediate" || !router[PENDING_LOCATION_KEY]) entry.commit(to);
|
||||
}
|
||||
});
|
||||
setCurrentContext(currentContext);
|
||||
entry.pendingLoad = currentLoad;
|
||||
return currentLoad;
|
||||
}
|
||||
function commit(to) {
|
||||
if (this.pendingTo === to) {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (this.staged === STAGED_NO_VALUE && this.stagedError === null && this.stagedNavigationResult === null) console.warn(`Loader "${options.key}"'s "commit()" was called but there is no staged data.`);
|
||||
}
|
||||
if (this.staged !== STAGED_NO_VALUE) this.data.value = this.staged;
|
||||
this.error.value = this.stagedError;
|
||||
this.staged = STAGED_NO_VALUE;
|
||||
this.stagedError = this.error.value;
|
||||
this.pendingTo = null;
|
||||
this.to = to;
|
||||
for (const childEntry of this.children) childEntry.commit(to);
|
||||
}
|
||||
}
|
||||
const useDataLoader = () => {
|
||||
const currentContext = getCurrentContext();
|
||||
const [parentEntry, _router, _route] = currentContext;
|
||||
const router = _router || useRouter();
|
||||
const route = _route || useRoute();
|
||||
const entries = router[LOADER_ENTRIES_KEY];
|
||||
let entry = entries.get(loader);
|
||||
if (!entry || parentEntry && entry.pendingTo !== route || !entry.pendingLoad) router[APP_KEY].runWithContext(() => load(route, router, void 0, parentEntry));
|
||||
entry = entries.get(loader);
|
||||
if (parentEntry) {
|
||||
if (parentEntry === entry) console.warn(`👶❌ "${options.key}" has itself as parent. This shouldn't be happening. Please report a bug with a reproduction to https://github.com/posva/unplugin-vue-router/`);
|
||||
parentEntry.children.add(entry);
|
||||
}
|
||||
const { data, error, isLoading } = entry;
|
||||
const useDataLoaderResult = {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
reload: (to = router.currentRoute.value) => router[APP_KEY].runWithContext(() => load(to, router)).then(() => entry.commit(to))
|
||||
};
|
||||
const promise = entry.pendingLoad.then(() => {
|
||||
return entry.staged === STAGED_NO_VALUE ? entry.stagedNavigationResult ? Promise.reject(entry.stagedNavigationResult) : data.value : entry.staged;
|
||||
}).catch((e) => parentEntry ? Promise.reject(e) : null);
|
||||
setCurrentContext(currentContext);
|
||||
return Object.assign(promise, useDataLoaderResult);
|
||||
};
|
||||
useDataLoader[IS_USE_DATA_LOADER_KEY] = true;
|
||||
useDataLoader._ = {
|
||||
load,
|
||||
options,
|
||||
getEntry(router) {
|
||||
return router[LOADER_ENTRIES_KEY].get(loader);
|
||||
}
|
||||
};
|
||||
return useDataLoader;
|
||||
}
|
||||
/**
|
||||
* Dev only warning for loaders that return/throw NavigationResult but are not exposed
|
||||
*
|
||||
* @param to - target location
|
||||
* @param options - options used to define the loader
|
||||
* @param useDataLoader - the data loader composable
|
||||
*/
|
||||
function warnNonExposedLoader({ to, options, useDataLoader }) {
|
||||
const loaders = to.meta[LOADER_SET_KEY];
|
||||
if (loaders && !loaders.has(useDataLoader)) warn("A loader returned a NavigationResult but is not registered on the route. Did you forget to \"export\" it from the page component?" + (options.key ? ` (loader key: "${options.key}")` : ""));
|
||||
}
|
||||
const DEFAULT_DEFINE_LOADER_OPTIONS = {
|
||||
lazy: false,
|
||||
server: true,
|
||||
commit: "after-load"
|
||||
};
|
||||
/**
|
||||
* Initial data generated on server and consumed on client.
|
||||
* @internal
|
||||
*/
|
||||
const INITIAL_DATA_KEY = Symbol();
|
||||
|
||||
//#endregion
|
||||
export { defineBasicLoader };
|
||||
5
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-BK9Gdnky.mjs
generated
vendored
Normal file
5
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-BK9Gdnky.mjs
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
//#region src/data-loaders/createDataLoader.ts
|
||||
const toLazyValue = (lazy, to, from) => typeof lazy === "function" ? lazy(to, from) : lazy;
|
||||
|
||||
//#endregion
|
||||
export { toLazyValue as t };
|
||||
513
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-C8o8GrdD.d.mts
generated
vendored
Normal file
513
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-C8o8GrdD.d.mts
generated
vendored
Normal file
@@ -0,0 +1,513 @@
|
||||
import * as vue_router0 from "vue-router";
|
||||
import { LocationQuery, NavigationGuard, NavigationGuardReturn, RouteLocationNormalizedLoaded, Router } from "vue-router";
|
||||
import { App, EffectScope, ShallowRef } from "vue";
|
||||
|
||||
//#region src/data-loaders/symbols.d.ts
|
||||
/**
|
||||
* Retrieves the internal version of loaders.
|
||||
* @internal
|
||||
*/
|
||||
declare const LOADER_SET_KEY: unique symbol;
|
||||
/**
|
||||
* Retrieves the internal version of loader entries.
|
||||
* @internal
|
||||
*/
|
||||
declare const LOADER_ENTRIES_KEY: unique symbol;
|
||||
/**
|
||||
* Added to the loaders returned by `defineLoader()` to identify them.
|
||||
* Allows to extract exported useData() from a component.
|
||||
* @internal
|
||||
*/
|
||||
declare const IS_USE_DATA_LOADER_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to save the pending location on the router.
|
||||
* @internal
|
||||
*/
|
||||
declare const PENDING_LOCATION_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to know there is no value staged for the loader and that commit should be skipped.
|
||||
* @internal
|
||||
*/
|
||||
declare const STAGED_NO_VALUE: unique symbol;
|
||||
/**
|
||||
* Gives access to the current app and it's `runWithContext` method.
|
||||
* @internal
|
||||
*/
|
||||
declare const APP_KEY: unique symbol;
|
||||
/**
|
||||
* Gives access to an AbortController that aborts when the navigation is canceled.
|
||||
* @internal
|
||||
*/
|
||||
declare const ABORT_CONTROLLER_KEY: unique symbol;
|
||||
/**
|
||||
* Gives access to the navigation results when the navigation is aborted by the user within a data loader.
|
||||
* @internal
|
||||
*/
|
||||
declare const NAVIGATION_RESULTS_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to save the initial data on the router.
|
||||
* @internal
|
||||
*/
|
||||
declare const IS_SSR_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to get the effect scope used for data loaders.
|
||||
* @internal
|
||||
*/
|
||||
declare const DATA_LOADERS_EFFECT_SCOPE_KEY: unique symbol;
|
||||
//#endregion
|
||||
//#region src/utils/index.d.ts
|
||||
/**
|
||||
* Maybe a promise maybe not
|
||||
* @internal
|
||||
*/
|
||||
type _Awaitable<T> = T | PromiseLike<T>;
|
||||
//#endregion
|
||||
//#region src/data-loaders/navigation-guard.d.ts
|
||||
|
||||
/**
|
||||
* Options to initialize the data loader guard.
|
||||
*/
|
||||
interface SetupLoaderGuardOptions extends DataLoaderPluginOptions {
|
||||
/**
|
||||
* The Vue app instance. Used to access the `provide` and `inject` APIs.
|
||||
*/
|
||||
app: App<unknown>;
|
||||
/**
|
||||
* The effect scope to use for the data loaders.
|
||||
*/
|
||||
effect: EffectScope;
|
||||
}
|
||||
/**
|
||||
* Possible values to change the result of a navigation within a loader
|
||||
* @internal
|
||||
*/
|
||||
type _DataLoaderRedirectResult = Exclude<ReturnType<NavigationGuard>, Promise<unknown> | Function | true | void | undefined>;
|
||||
/**
|
||||
* Possible values to change the result of a navigation within a loader. Can be returned from a data loader and will
|
||||
* appear in `selectNavigationResult`. If thrown, it will immediately cancel the navigation. It can only contain values
|
||||
* that cancel the navigation.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* export const useUserData = defineLoader(async (to) => {
|
||||
* const user = await fetchUser(to.params.id)
|
||||
* if (!user) {
|
||||
* return new NavigationResult('/404')
|
||||
* }
|
||||
* return user
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
declare class NavigationResult {
|
||||
readonly value: _DataLoaderRedirectResult;
|
||||
constructor(value: _DataLoaderRedirectResult);
|
||||
}
|
||||
/**
|
||||
* Data Loader plugin to add data loading support to Vue Router.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const router = createRouter({
|
||||
* routes,
|
||||
* history: createWebHistory(),
|
||||
* })
|
||||
*
|
||||
* const app = createApp({})
|
||||
* app.use(DataLoaderPlugin, { router })
|
||||
* app.use(router)
|
||||
* ```
|
||||
*/
|
||||
declare function DataLoaderPlugin(app: App, options: DataLoaderPluginOptions): void;
|
||||
/**
|
||||
* Options passed to the DataLoaderPlugin.
|
||||
*/
|
||||
interface DataLoaderPluginOptions {
|
||||
/**
|
||||
* The router instance. Adds the guards to it
|
||||
*/
|
||||
router: Router;
|
||||
isSSR?: boolean;
|
||||
/**
|
||||
* Called if any data loader returns a `NavigationResult` with an array of them. Should decide what is the outcome of
|
||||
* the data fetching guard. Note this isn't called if no data loaders return a `NavigationResult` or if an error is thrown.
|
||||
* @defaultValue `(results) => results[0].value`
|
||||
*/
|
||||
selectNavigationResult?: (results: NavigationResult[]) => _Awaitable<Exclude<NavigationGuardReturn, Function | Promise<unknown>>>;
|
||||
/**
|
||||
* List of _expected_ errors that shouldn't abort the navigation (for non-lazy loaders). Provide a list of
|
||||
* constructors that can be checked with `instanceof` or a custom function that returns `true` for expected errors.
|
||||
*/
|
||||
errors?: Array<new (...args: any) => any> | ((reason?: unknown) => boolean);
|
||||
}
|
||||
/**
|
||||
* Return a ref that reflects the global loading state of all loaders within a navigation.
|
||||
* This state doesn't update if `refresh()` is manually called.
|
||||
*/
|
||||
declare function useIsDataLoading(): ShallowRef<boolean>;
|
||||
//#endregion
|
||||
//#region src/data-loaders/meta-extensions.d.ts
|
||||
/**
|
||||
* Map type for the entries used by data loaders.
|
||||
* @internal
|
||||
*/
|
||||
type _DefineLoaderEntryMap<DataLoaderEntry extends DataLoaderEntryBase<unknown> = DataLoaderEntryBase<unknown>> = WeakMap<object, DataLoaderEntry>;
|
||||
declare module 'vue-router' {
|
||||
interface Router {
|
||||
/**
|
||||
* The entries used by data loaders. Put on the router for convenience.
|
||||
* @internal
|
||||
*/
|
||||
[LOADER_ENTRIES_KEY]: _DefineLoaderEntryMap;
|
||||
/**
|
||||
* Pending navigation that is waiting for data loaders to resolve.
|
||||
* @internal
|
||||
*/
|
||||
[PENDING_LOCATION_KEY]: RouteLocationNormalizedLoaded | null;
|
||||
/**
|
||||
* The app instance that is used by the router.
|
||||
* @internal
|
||||
*/
|
||||
[APP_KEY]: App<unknown>;
|
||||
/**
|
||||
* Whether the router is running in server-side rendering mode.
|
||||
* @internal
|
||||
*/
|
||||
[IS_SSR_KEY]: boolean;
|
||||
/**
|
||||
* The effect scope used to run data loaders.
|
||||
* @internal
|
||||
*/
|
||||
[DATA_LOADERS_EFFECT_SCOPE_KEY]: EffectScope;
|
||||
}
|
||||
interface RouteMeta {
|
||||
/**
|
||||
* The data loaders for a route record. Add any data loader to this array to have it called when the route is
|
||||
* navigated to. Note this is only needed when **not** using lazy components (`() => import('./pages/Home.vue')`) or
|
||||
* when not explicitly exporting data loaders from page components.
|
||||
*/
|
||||
loaders?: UseDataLoader[];
|
||||
/**
|
||||
* Set of loaders for the current route. This is built once during navigation and is used to merge the loaders from
|
||||
* the lazy import in components or the `loaders` array in the route record.
|
||||
* @internal
|
||||
*/
|
||||
[LOADER_SET_KEY]?: Set<UseDataLoader>;
|
||||
/**
|
||||
* The signal that is aborted when the navigation is canceled or an error occurs.
|
||||
* @internal
|
||||
*/
|
||||
[ABORT_CONTROLLER_KEY]?: AbortController;
|
||||
/**
|
||||
* The navigation results when the navigation is canceled by the user within a data loader.
|
||||
* @internal
|
||||
*/
|
||||
[NAVIGATION_RESULTS_KEY]?: NavigationResult[];
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
//#region src/data-loaders/utils.d.ts
|
||||
/**
|
||||
* @internal: data loaders authoring only. Use `getCurrentContext` instead.
|
||||
*/
|
||||
declare let currentContext: readonly [entry: DataLoaderEntryBase, router: Router, route: RouteLocationNormalizedLoaded] | undefined | null;
|
||||
declare function getCurrentContext(): readonly [entry: DataLoaderEntryBase<unknown, unknown, unknown>, router: Router, route: vue_router0.RouteLocationNormalizedLoadedGeneric] | readonly [];
|
||||
/**
|
||||
* Sets the current context for data loaders. This allows for nested loaders to be aware of their parent context.
|
||||
* INTERNAL ONLY.
|
||||
*
|
||||
* @param context - the context to set
|
||||
* @internal
|
||||
*/
|
||||
declare function setCurrentContext(context?: typeof currentContext | readonly []): void;
|
||||
/**
|
||||
* Restore the current context after a promise is resolved.
|
||||
* @param promise - promise to wrap
|
||||
*/
|
||||
declare function withLoaderContext<P extends Promise<unknown>>(promise: P): P;
|
||||
/**
|
||||
* Object and promise of the object itself. Used when we can await some of the properties of an object to be loaded.
|
||||
* @internal
|
||||
*/
|
||||
type _PromiseMerged<PromiseType, RawType = PromiseType> = RawType & Promise<PromiseType>;
|
||||
declare const assign: {
|
||||
<T extends {}, U>(target: T, source: U): T & U;
|
||||
<T extends {}, U, V>(target: T, source1: U, source2: V): T & U & V;
|
||||
<T extends {}, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
|
||||
(target: object, ...sources: any[]): any;
|
||||
};
|
||||
/**
|
||||
* Track the reads of a route and its properties
|
||||
* @internal
|
||||
* @param route - route to track
|
||||
*/
|
||||
declare function trackRoute(route: RouteLocationNormalizedLoaded): readonly [{
|
||||
readonly hash: string;
|
||||
readonly params: vue_router0.RouteParamsGeneric;
|
||||
readonly query: LocationQuery;
|
||||
readonly matched: vue_router0.RouteLocationMatched[];
|
||||
readonly name: vue_router0.RouteRecordNameGeneric;
|
||||
readonly fullPath: string;
|
||||
readonly redirectedFrom: vue_router0.RouteLocation | undefined;
|
||||
readonly path: string;
|
||||
readonly meta: vue_router0.RouteMeta;
|
||||
}, Partial<vue_router0.RouteParamsGeneric>, Partial<LocationQuery>, {
|
||||
v: string | null;
|
||||
}];
|
||||
/**
|
||||
* Returns `true` if `inner` is a subset of `outer`. Used to check if a tr
|
||||
*
|
||||
* @internal
|
||||
* @param outer - the bigger params
|
||||
* @param inner - the smaller params
|
||||
*/
|
||||
declare function isSubsetOf(inner: Partial<LocationQuery>, outer: LocationQuery): boolean;
|
||||
//#endregion
|
||||
//#region src/data-loaders/types-config.d.ts
|
||||
/**
|
||||
* Allows you to extend the default types of the library.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // types-extension.d.ts
|
||||
* import 'unplugin-vue-router/data-loaders'
|
||||
* export {}
|
||||
* declare module 'unplugin-vue-router/data-loaders' {
|
||||
* interface TypesConfig {
|
||||
* Error: MyCustomError
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
interface TypesConfig {}
|
||||
/**
|
||||
* The default error type used.
|
||||
* @internal
|
||||
*/
|
||||
type ErrorDefault = TypesConfig extends Record<'Error', infer E> ? E : Error;
|
||||
//#endregion
|
||||
//#region src/data-loaders/createDataLoader.d.ts
|
||||
/**
|
||||
* Base type for a data loader entry. Each Data Loader has its own entry in the `loaderEntries` (accessible via `[LOADER_ENTRIES_KEY]`) map.
|
||||
*/
|
||||
interface DataLoaderEntryBase<TData = unknown, TError = unknown, TDataInitial extends TData | undefined = TData | undefined> {
|
||||
/**
|
||||
* Data stored in the entry.
|
||||
*/
|
||||
data: ShallowRef<TData | TDataInitial>;
|
||||
/**
|
||||
* Error if there was an error.
|
||||
*/
|
||||
error: ShallowRef<TError | null>;
|
||||
/**
|
||||
* Location the data was loaded for or `null` if the data is not loaded.
|
||||
*/
|
||||
to: RouteLocationNormalizedLoaded | null;
|
||||
/**
|
||||
* Whether there is an ongoing request.
|
||||
*/
|
||||
isLoading: ShallowRef<boolean>;
|
||||
options: DefineDataLoaderOptionsBase_LaxData;
|
||||
/**
|
||||
* Called by the navigation guard when the navigation is duplicated. Should be used to reset pendingTo and pendingLoad and any other property that should be reset.
|
||||
*/
|
||||
resetPending: () => void;
|
||||
/**
|
||||
* The latest pending load. Allows to verify if the load is still valid when it resolves.
|
||||
*/
|
||||
pendingLoad: Promise<void> | null;
|
||||
/**
|
||||
* The latest pending navigation's `to` route. Used to verify if the navigation is still valid when it resolves.
|
||||
*/
|
||||
pendingTo: RouteLocationNormalizedLoaded | null;
|
||||
/**
|
||||
* Data that was staged by a loader. This is used to avoid showing the old data while the new data is loading. Calling
|
||||
* the internal `commit()` function will replace the data with the staged data.
|
||||
*/
|
||||
staged: TData | typeof STAGED_NO_VALUE;
|
||||
/**
|
||||
* Error that was staged by a loader. This is used to avoid showing the old error while the new data is loading.
|
||||
* Calling the internal `commit()` function will replace the error with the staged error.
|
||||
*/
|
||||
stagedError: TError | null;
|
||||
/**
|
||||
* NavigationResult that was returned by a loader. Used to avoid treating it as data.
|
||||
*/
|
||||
stagedNavigationResult: NavigationResult | null;
|
||||
/**
|
||||
* Other data loaders that depend on this one. This is used to invalidate the data when a dependency is invalidated.
|
||||
*/
|
||||
children: Set<DataLoaderEntryBase>;
|
||||
/**
|
||||
* Commits the pending data to the entry. This is called by the navigation guard when all non-lazy loaders have
|
||||
* finished loading. It should be implemented by the loader. It **must be called** from the entry itself:
|
||||
* `entry.commit(to)`.
|
||||
*/
|
||||
commit(to: RouteLocationNormalizedLoaded): void;
|
||||
}
|
||||
/**
|
||||
* Common properties for the options of `defineLoader()`. Types are `unknown` to allow for more specific types in the
|
||||
* extended types while having documentation in one single place.
|
||||
* @internal
|
||||
*/
|
||||
interface _DefineDataLoaderOptionsBase_Common {
|
||||
/**
|
||||
* When the data should be committed to the entry. In the case of lazy loaders, the loader will try to commit the data
|
||||
* after all non-lazy loaders have finished loading, but it might not be able to if the lazy loader hasn't been
|
||||
* resolved yet.
|
||||
*
|
||||
* @see {@link DefineDataLoaderCommit}
|
||||
* @defaultValue `'after-load'`
|
||||
*/
|
||||
commit?: DefineDataLoaderCommit;
|
||||
/**
|
||||
* Whether the data should be lazy loaded without blocking the client side navigation or not. When set to true, the loader will no longer block the navigation and the returned composable can be called even
|
||||
* without having the data ready.
|
||||
*
|
||||
* @defaultValue `false`
|
||||
*/
|
||||
lazy?: unknown;
|
||||
/**
|
||||
* Whether this loader should be awaited on the server side or not. Combined with the `lazy` option, this gives full
|
||||
* control over how to await for the data.
|
||||
*
|
||||
* @defaultValue `true`
|
||||
*/
|
||||
server?: unknown;
|
||||
/**
|
||||
* List of _expected_ errors that shouldn't abort the navigation (for non-lazy loaders). Provide a list of
|
||||
* constructors that can be checked with `instanceof` or a custom function that returns `true` for expected errors. Can also be set to `true` to accept all globally defined errors. Defaults to `false` to abort on any error.
|
||||
* @default `false`
|
||||
*/
|
||||
errors?: unknown;
|
||||
}
|
||||
/**
|
||||
* Options for a data loader that returns a data that is possibly `undefined`. Available for data loaders
|
||||
* implementations so they can be used in `defineLoader()` overloads.
|
||||
*/
|
||||
interface DefineDataLoaderOptionsBase_LaxData extends _DefineDataLoaderOptionsBase_Common {
|
||||
lazy?: boolean | ((to: RouteLocationNormalizedLoaded, from?: RouteLocationNormalizedLoaded) => boolean);
|
||||
server?: boolean;
|
||||
errors?: boolean | Array<new (...args: any[]) => any> | ((reason?: unknown) => boolean);
|
||||
}
|
||||
/**
|
||||
* Options for a data loader making the data defined without it being possibly `undefined`. Available for data loaders
|
||||
* implementations so they can be used in `defineLoader()` overloads.
|
||||
*/
|
||||
interface DefineDataLoaderOptionsBase_DefinedData extends _DefineDataLoaderOptionsBase_Common {
|
||||
lazy?: false;
|
||||
server?: true;
|
||||
errors?: false;
|
||||
}
|
||||
declare const toLazyValue: (lazy: undefined | DefineDataLoaderOptionsBase_LaxData["lazy"], to: RouteLocationNormalizedLoaded, from?: RouteLocationNormalizedLoaded) => boolean | undefined;
|
||||
/**
|
||||
* When the data should be committed to the entry.
|
||||
* - `immediate`: the data is committed as soon as it is loaded.
|
||||
* - `after-load`: the data is committed after all non-lazy loaders have finished loading.
|
||||
*/
|
||||
type DefineDataLoaderCommit = 'immediate' | 'after-load';
|
||||
interface DataLoaderContextBase {
|
||||
/**
|
||||
* Signal associated with the current navigation. It is aborted when the navigation is canceled or an error occurs.
|
||||
*/
|
||||
signal: AbortSignal | undefined;
|
||||
}
|
||||
/**
|
||||
* Data Loader composable returned by `defineLoader()`.
|
||||
* @see {@link DefineDataLoader}
|
||||
*/
|
||||
interface UseDataLoader<Data = unknown, TError = unknown> {
|
||||
[IS_USE_DATA_LOADER_KEY]: true;
|
||||
/**
|
||||
* Data Loader composable returned by `defineLoader()`.
|
||||
*
|
||||
* @example
|
||||
* Returns the Data loader data, isLoading, error etc. Meant to be used in `setup()` or `<script setup>` **without `await`**:
|
||||
* ```vue
|
||||
* <script setup>
|
||||
* const { data, isLoading, error } = useUserData()
|
||||
* </script>
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* It also returns a promise of the data when used in nested loaders. Note this `data` is **not a ref**. This is not meant to be used in `setup()` or `<script setup>`.
|
||||
* ```ts
|
||||
* export const useUserConnections = defineLoader(async () => {
|
||||
* const user = await useUserData()
|
||||
* return fetchUserConnections(user.id)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
(): _PromiseMerged<Exclude<Data, NavigationResult | undefined>, UseDataLoaderResult<Exclude<Data, NavigationResult>, TError>>;
|
||||
/**
|
||||
* Internals of the data loader.
|
||||
* @internal
|
||||
*/
|
||||
_: UseDataLoaderInternals<Exclude<Data, NavigationResult | undefined>, TError>;
|
||||
}
|
||||
/**
|
||||
* Internal properties of a data loader composable. Used by the internal implementation of `defineLoader()`. **Should
|
||||
* not be used in application code.**
|
||||
*/
|
||||
interface UseDataLoaderInternals<Data = unknown, TError = unknown> {
|
||||
/**
|
||||
* Loads the data from the cache if possible, otherwise loads it from the loader and awaits it.
|
||||
*
|
||||
* @param to - route location to load the data for
|
||||
* @param router - router instance
|
||||
* @param from - route location we are coming from
|
||||
* @param parent - parent data loader entry
|
||||
*/
|
||||
load: (to: RouteLocationNormalizedLoaded, router: Router, from?: RouteLocationNormalizedLoaded, parent?: DataLoaderEntryBase) => Promise<void>;
|
||||
/**
|
||||
* Resolved options for the loader.
|
||||
*/
|
||||
options: DefineDataLoaderOptionsBase_LaxData;
|
||||
/**
|
||||
* Gets the entry associated with the router instance. Assumes the data loader has been loaded and that the entry
|
||||
* exists.
|
||||
*
|
||||
* @param router - router instance
|
||||
*/
|
||||
getEntry(router: Router): DataLoaderEntryBase<Data, TError>;
|
||||
}
|
||||
/**
|
||||
* Return value of a loader composable defined with `defineLoader()`.
|
||||
*/
|
||||
interface UseDataLoaderResult<TData = unknown, TError = ErrorDefault> {
|
||||
/**
|
||||
* Data returned by the loader. If the data loader is lazy, it will be undefined until the first load.
|
||||
*/
|
||||
data: ShallowRef<TData>;
|
||||
/**
|
||||
* Whether there is an ongoing request.
|
||||
*/
|
||||
isLoading: ShallowRef<boolean>;
|
||||
/**
|
||||
* Error if there was an error.
|
||||
*/
|
||||
error: ShallowRef<TError | null>;
|
||||
/**
|
||||
* Reload the data using the current route location. Returns a promise that resolves when the data is reloaded. This
|
||||
* method should not be called during a navigation as it can conflict with an ongoing load and lead to
|
||||
* inconsistencies.
|
||||
*/
|
||||
reload(): Promise<void>;
|
||||
/**
|
||||
* Reload the data using the route location passed as argument. Returns a promise that resolves when the data is reloaded.
|
||||
*
|
||||
* @param route - route location to load the data for
|
||||
*/
|
||||
reload(route: RouteLocationNormalizedLoaded): Promise<void>;
|
||||
}
|
||||
/**
|
||||
* Loader function that can be passed to `defineLoader()`.
|
||||
*/
|
||||
interface DefineLoaderFn<Data, Context extends DataLoaderContextBase = DataLoaderContextBase, Route = RouteLocationNormalizedLoaded> {
|
||||
(route: Route, context: Context): Promise<Data>;
|
||||
}
|
||||
/**
|
||||
* @deprecated Use `DefineDataLoaderOptionsBase_LaxData` instead.
|
||||
*/
|
||||
type DefineDataLoaderOptionsBase = DefineDataLoaderOptionsBase_LaxData;
|
||||
//#endregion
|
||||
export { DATA_LOADERS_EFFECT_SCOPE_KEY as A, DataLoaderPluginOptions as C, useIsDataLoading as D, _DataLoaderRedirectResult as E, NAVIGATION_RESULTS_KEY as F, PENDING_LOCATION_KEY as I, STAGED_NO_VALUE as L, IS_USE_DATA_LOADER_KEY as M, LOADER_ENTRIES_KEY as N, ABORT_CONTROLLER_KEY as O, LOADER_SET_KEY as P, DataLoaderPlugin as S, SetupLoaderGuardOptions as T, isSubsetOf as _, DefineDataLoaderOptionsBase_LaxData as a, withLoaderContext as b, UseDataLoaderInternals as c, ErrorDefault as d, TypesConfig as f, getCurrentContext as g, currentContext as h, DefineDataLoaderOptionsBase_DefinedData as i, IS_SSR_KEY as j, APP_KEY as k, UseDataLoaderResult as l, assign as m, DataLoaderEntryBase as n, DefineLoaderFn as o, _PromiseMerged as p, DefineDataLoaderOptionsBase as r, UseDataLoader as s, DataLoaderContextBase as t, toLazyValue as u, setCurrentContext as v, NavigationResult as w, _DefineLoaderEntryMap as x, trackRoute as y };
|
||||
11
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-CBwrbo71.cjs
generated
vendored
Normal file
11
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-CBwrbo71.cjs
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
//#region src/data-loaders/createDataLoader.ts
|
||||
const toLazyValue = (lazy, to, from) => typeof lazy === "function" ? lazy(to, from) : lazy;
|
||||
|
||||
//#endregion
|
||||
Object.defineProperty(exports, 'toLazyValue', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return toLazyValue;
|
||||
}
|
||||
});
|
||||
513
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-DgP0poyl.d.cts
generated
vendored
Normal file
513
node_modules/unplugin-vue-router/dist/data-loaders/createDataLoader-DgP0poyl.d.cts
generated
vendored
Normal file
@@ -0,0 +1,513 @@
|
||||
import * as vue_router0 from "vue-router";
|
||||
import { LocationQuery, NavigationGuard, NavigationGuardReturn, RouteLocationNormalizedLoaded, Router } from "vue-router";
|
||||
import { App, EffectScope, ShallowRef } from "vue";
|
||||
|
||||
//#region src/data-loaders/symbols.d.ts
|
||||
/**
|
||||
* Retrieves the internal version of loaders.
|
||||
* @internal
|
||||
*/
|
||||
declare const LOADER_SET_KEY: unique symbol;
|
||||
/**
|
||||
* Retrieves the internal version of loader entries.
|
||||
* @internal
|
||||
*/
|
||||
declare const LOADER_ENTRIES_KEY: unique symbol;
|
||||
/**
|
||||
* Added to the loaders returned by `defineLoader()` to identify them.
|
||||
* Allows to extract exported useData() from a component.
|
||||
* @internal
|
||||
*/
|
||||
declare const IS_USE_DATA_LOADER_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to save the pending location on the router.
|
||||
* @internal
|
||||
*/
|
||||
declare const PENDING_LOCATION_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to know there is no value staged for the loader and that commit should be skipped.
|
||||
* @internal
|
||||
*/
|
||||
declare const STAGED_NO_VALUE: unique symbol;
|
||||
/**
|
||||
* Gives access to the current app and it's `runWithContext` method.
|
||||
* @internal
|
||||
*/
|
||||
declare const APP_KEY: unique symbol;
|
||||
/**
|
||||
* Gives access to an AbortController that aborts when the navigation is canceled.
|
||||
* @internal
|
||||
*/
|
||||
declare const ABORT_CONTROLLER_KEY: unique symbol;
|
||||
/**
|
||||
* Gives access to the navigation results when the navigation is aborted by the user within a data loader.
|
||||
* @internal
|
||||
*/
|
||||
declare const NAVIGATION_RESULTS_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to save the initial data on the router.
|
||||
* @internal
|
||||
*/
|
||||
declare const IS_SSR_KEY: unique symbol;
|
||||
/**
|
||||
* Symbol used to get the effect scope used for data loaders.
|
||||
* @internal
|
||||
*/
|
||||
declare const DATA_LOADERS_EFFECT_SCOPE_KEY: unique symbol;
|
||||
//#endregion
|
||||
//#region src/utils/index.d.ts
|
||||
/**
|
||||
* Maybe a promise maybe not
|
||||
* @internal
|
||||
*/
|
||||
type _Awaitable<T> = T | PromiseLike<T>;
|
||||
//#endregion
|
||||
//#region src/data-loaders/navigation-guard.d.ts
|
||||
|
||||
/**
|
||||
* Options to initialize the data loader guard.
|
||||
*/
|
||||
interface SetupLoaderGuardOptions extends DataLoaderPluginOptions {
|
||||
/**
|
||||
* The Vue app instance. Used to access the `provide` and `inject` APIs.
|
||||
*/
|
||||
app: App<unknown>;
|
||||
/**
|
||||
* The effect scope to use for the data loaders.
|
||||
*/
|
||||
effect: EffectScope;
|
||||
}
|
||||
/**
|
||||
* Possible values to change the result of a navigation within a loader
|
||||
* @internal
|
||||
*/
|
||||
type _DataLoaderRedirectResult = Exclude<ReturnType<NavigationGuard>, Promise<unknown> | Function | true | void | undefined>;
|
||||
/**
|
||||
* Possible values to change the result of a navigation within a loader. Can be returned from a data loader and will
|
||||
* appear in `selectNavigationResult`. If thrown, it will immediately cancel the navigation. It can only contain values
|
||||
* that cancel the navigation.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* export const useUserData = defineLoader(async (to) => {
|
||||
* const user = await fetchUser(to.params.id)
|
||||
* if (!user) {
|
||||
* return new NavigationResult('/404')
|
||||
* }
|
||||
* return user
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
declare class NavigationResult {
|
||||
readonly value: _DataLoaderRedirectResult;
|
||||
constructor(value: _DataLoaderRedirectResult);
|
||||
}
|
||||
/**
|
||||
* Data Loader plugin to add data loading support to Vue Router.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const router = createRouter({
|
||||
* routes,
|
||||
* history: createWebHistory(),
|
||||
* })
|
||||
*
|
||||
* const app = createApp({})
|
||||
* app.use(DataLoaderPlugin, { router })
|
||||
* app.use(router)
|
||||
* ```
|
||||
*/
|
||||
declare function DataLoaderPlugin(app: App, options: DataLoaderPluginOptions): void;
|
||||
/**
|
||||
* Options passed to the DataLoaderPlugin.
|
||||
*/
|
||||
interface DataLoaderPluginOptions {
|
||||
/**
|
||||
* The router instance. Adds the guards to it
|
||||
*/
|
||||
router: Router;
|
||||
isSSR?: boolean;
|
||||
/**
|
||||
* Called if any data loader returns a `NavigationResult` with an array of them. Should decide what is the outcome of
|
||||
* the data fetching guard. Note this isn't called if no data loaders return a `NavigationResult` or if an error is thrown.
|
||||
* @defaultValue `(results) => results[0].value`
|
||||
*/
|
||||
selectNavigationResult?: (results: NavigationResult[]) => _Awaitable<Exclude<NavigationGuardReturn, Function | Promise<unknown>>>;
|
||||
/**
|
||||
* List of _expected_ errors that shouldn't abort the navigation (for non-lazy loaders). Provide a list of
|
||||
* constructors that can be checked with `instanceof` or a custom function that returns `true` for expected errors.
|
||||
*/
|
||||
errors?: Array<new (...args: any) => any> | ((reason?: unknown) => boolean);
|
||||
}
|
||||
/**
|
||||
* Return a ref that reflects the global loading state of all loaders within a navigation.
|
||||
* This state doesn't update if `refresh()` is manually called.
|
||||
*/
|
||||
declare function useIsDataLoading(): ShallowRef<boolean>;
|
||||
//#endregion
|
||||
//#region src/data-loaders/meta-extensions.d.ts
|
||||
/**
|
||||
* Map type for the entries used by data loaders.
|
||||
* @internal
|
||||
*/
|
||||
type _DefineLoaderEntryMap<DataLoaderEntry extends DataLoaderEntryBase<unknown> = DataLoaderEntryBase<unknown>> = WeakMap<object, DataLoaderEntry>;
|
||||
declare module 'vue-router' {
|
||||
interface Router {
|
||||
/**
|
||||
* The entries used by data loaders. Put on the router for convenience.
|
||||
* @internal
|
||||
*/
|
||||
[LOADER_ENTRIES_KEY]: _DefineLoaderEntryMap;
|
||||
/**
|
||||
* Pending navigation that is waiting for data loaders to resolve.
|
||||
* @internal
|
||||
*/
|
||||
[PENDING_LOCATION_KEY]: RouteLocationNormalizedLoaded | null;
|
||||
/**
|
||||
* The app instance that is used by the router.
|
||||
* @internal
|
||||
*/
|
||||
[APP_KEY]: App<unknown>;
|
||||
/**
|
||||
* Whether the router is running in server-side rendering mode.
|
||||
* @internal
|
||||
*/
|
||||
[IS_SSR_KEY]: boolean;
|
||||
/**
|
||||
* The effect scope used to run data loaders.
|
||||
* @internal
|
||||
*/
|
||||
[DATA_LOADERS_EFFECT_SCOPE_KEY]: EffectScope;
|
||||
}
|
||||
interface RouteMeta {
|
||||
/**
|
||||
* The data loaders for a route record. Add any data loader to this array to have it called when the route is
|
||||
* navigated to. Note this is only needed when **not** using lazy components (`() => import('./pages/Home.vue')`) or
|
||||
* when not explicitly exporting data loaders from page components.
|
||||
*/
|
||||
loaders?: UseDataLoader[];
|
||||
/**
|
||||
* Set of loaders for the current route. This is built once during navigation and is used to merge the loaders from
|
||||
* the lazy import in components or the `loaders` array in the route record.
|
||||
* @internal
|
||||
*/
|
||||
[LOADER_SET_KEY]?: Set<UseDataLoader>;
|
||||
/**
|
||||
* The signal that is aborted when the navigation is canceled or an error occurs.
|
||||
* @internal
|
||||
*/
|
||||
[ABORT_CONTROLLER_KEY]?: AbortController;
|
||||
/**
|
||||
* The navigation results when the navigation is canceled by the user within a data loader.
|
||||
* @internal
|
||||
*/
|
||||
[NAVIGATION_RESULTS_KEY]?: NavigationResult[];
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
//#region src/data-loaders/utils.d.ts
|
||||
/**
|
||||
* @internal: data loaders authoring only. Use `getCurrentContext` instead.
|
||||
*/
|
||||
declare let currentContext: readonly [entry: DataLoaderEntryBase, router: Router, route: RouteLocationNormalizedLoaded] | undefined | null;
|
||||
declare function getCurrentContext(): readonly [entry: DataLoaderEntryBase<unknown, unknown, unknown>, router: Router, route: vue_router0.RouteLocationNormalizedLoadedGeneric] | readonly [];
|
||||
/**
|
||||
* Sets the current context for data loaders. This allows for nested loaders to be aware of their parent context.
|
||||
* INTERNAL ONLY.
|
||||
*
|
||||
* @param context - the context to set
|
||||
* @internal
|
||||
*/
|
||||
declare function setCurrentContext(context?: typeof currentContext | readonly []): void;
|
||||
/**
|
||||
* Restore the current context after a promise is resolved.
|
||||
* @param promise - promise to wrap
|
||||
*/
|
||||
declare function withLoaderContext<P extends Promise<unknown>>(promise: P): P;
|
||||
/**
|
||||
* Object and promise of the object itself. Used when we can await some of the properties of an object to be loaded.
|
||||
* @internal
|
||||
*/
|
||||
type _PromiseMerged<PromiseType, RawType = PromiseType> = RawType & Promise<PromiseType>;
|
||||
declare const assign: {
|
||||
<T extends {}, U>(target: T, source: U): T & U;
|
||||
<T extends {}, U, V>(target: T, source1: U, source2: V): T & U & V;
|
||||
<T extends {}, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
|
||||
(target: object, ...sources: any[]): any;
|
||||
};
|
||||
/**
|
||||
* Track the reads of a route and its properties
|
||||
* @internal
|
||||
* @param route - route to track
|
||||
*/
|
||||
declare function trackRoute(route: RouteLocationNormalizedLoaded): readonly [{
|
||||
readonly hash: string;
|
||||
readonly params: vue_router0.RouteParamsGeneric;
|
||||
readonly query: LocationQuery;
|
||||
readonly matched: vue_router0.RouteLocationMatched[];
|
||||
readonly name: vue_router0.RouteRecordNameGeneric;
|
||||
readonly fullPath: string;
|
||||
readonly redirectedFrom: vue_router0.RouteLocation | undefined;
|
||||
readonly path: string;
|
||||
readonly meta: vue_router0.RouteMeta;
|
||||
}, Partial<vue_router0.RouteParamsGeneric>, Partial<LocationQuery>, {
|
||||
v: string | null;
|
||||
}];
|
||||
/**
|
||||
* Returns `true` if `inner` is a subset of `outer`. Used to check if a tr
|
||||
*
|
||||
* @internal
|
||||
* @param outer - the bigger params
|
||||
* @param inner - the smaller params
|
||||
*/
|
||||
declare function isSubsetOf(inner: Partial<LocationQuery>, outer: LocationQuery): boolean;
|
||||
//#endregion
|
||||
//#region src/data-loaders/types-config.d.ts
|
||||
/**
|
||||
* Allows you to extend the default types of the library.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // types-extension.d.ts
|
||||
* import 'unplugin-vue-router/data-loaders'
|
||||
* export {}
|
||||
* declare module 'unplugin-vue-router/data-loaders' {
|
||||
* interface TypesConfig {
|
||||
* Error: MyCustomError
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
interface TypesConfig {}
|
||||
/**
|
||||
* The default error type used.
|
||||
* @internal
|
||||
*/
|
||||
type ErrorDefault = TypesConfig extends Record<'Error', infer E> ? E : Error;
|
||||
//#endregion
|
||||
//#region src/data-loaders/createDataLoader.d.ts
|
||||
/**
|
||||
* Base type for a data loader entry. Each Data Loader has its own entry in the `loaderEntries` (accessible via `[LOADER_ENTRIES_KEY]`) map.
|
||||
*/
|
||||
interface DataLoaderEntryBase<TData = unknown, TError = unknown, TDataInitial extends TData | undefined = TData | undefined> {
|
||||
/**
|
||||
* Data stored in the entry.
|
||||
*/
|
||||
data: ShallowRef<TData | TDataInitial>;
|
||||
/**
|
||||
* Error if there was an error.
|
||||
*/
|
||||
error: ShallowRef<TError | null>;
|
||||
/**
|
||||
* Location the data was loaded for or `null` if the data is not loaded.
|
||||
*/
|
||||
to: RouteLocationNormalizedLoaded | null;
|
||||
/**
|
||||
* Whether there is an ongoing request.
|
||||
*/
|
||||
isLoading: ShallowRef<boolean>;
|
||||
options: DefineDataLoaderOptionsBase_LaxData;
|
||||
/**
|
||||
* Called by the navigation guard when the navigation is duplicated. Should be used to reset pendingTo and pendingLoad and any other property that should be reset.
|
||||
*/
|
||||
resetPending: () => void;
|
||||
/**
|
||||
* The latest pending load. Allows to verify if the load is still valid when it resolves.
|
||||
*/
|
||||
pendingLoad: Promise<void> | null;
|
||||
/**
|
||||
* The latest pending navigation's `to` route. Used to verify if the navigation is still valid when it resolves.
|
||||
*/
|
||||
pendingTo: RouteLocationNormalizedLoaded | null;
|
||||
/**
|
||||
* Data that was staged by a loader. This is used to avoid showing the old data while the new data is loading. Calling
|
||||
* the internal `commit()` function will replace the data with the staged data.
|
||||
*/
|
||||
staged: TData | typeof STAGED_NO_VALUE;
|
||||
/**
|
||||
* Error that was staged by a loader. This is used to avoid showing the old error while the new data is loading.
|
||||
* Calling the internal `commit()` function will replace the error with the staged error.
|
||||
*/
|
||||
stagedError: TError | null;
|
||||
/**
|
||||
* NavigationResult that was returned by a loader. Used to avoid treating it as data.
|
||||
*/
|
||||
stagedNavigationResult: NavigationResult | null;
|
||||
/**
|
||||
* Other data loaders that depend on this one. This is used to invalidate the data when a dependency is invalidated.
|
||||
*/
|
||||
children: Set<DataLoaderEntryBase>;
|
||||
/**
|
||||
* Commits the pending data to the entry. This is called by the navigation guard when all non-lazy loaders have
|
||||
* finished loading. It should be implemented by the loader. It **must be called** from the entry itself:
|
||||
* `entry.commit(to)`.
|
||||
*/
|
||||
commit(to: RouteLocationNormalizedLoaded): void;
|
||||
}
|
||||
/**
|
||||
* Common properties for the options of `defineLoader()`. Types are `unknown` to allow for more specific types in the
|
||||
* extended types while having documentation in one single place.
|
||||
* @internal
|
||||
*/
|
||||
interface _DefineDataLoaderOptionsBase_Common {
|
||||
/**
|
||||
* When the data should be committed to the entry. In the case of lazy loaders, the loader will try to commit the data
|
||||
* after all non-lazy loaders have finished loading, but it might not be able to if the lazy loader hasn't been
|
||||
* resolved yet.
|
||||
*
|
||||
* @see {@link DefineDataLoaderCommit}
|
||||
* @defaultValue `'after-load'`
|
||||
*/
|
||||
commit?: DefineDataLoaderCommit;
|
||||
/**
|
||||
* Whether the data should be lazy loaded without blocking the client side navigation or not. When set to true, the loader will no longer block the navigation and the returned composable can be called even
|
||||
* without having the data ready.
|
||||
*
|
||||
* @defaultValue `false`
|
||||
*/
|
||||
lazy?: unknown;
|
||||
/**
|
||||
* Whether this loader should be awaited on the server side or not. Combined with the `lazy` option, this gives full
|
||||
* control over how to await for the data.
|
||||
*
|
||||
* @defaultValue `true`
|
||||
*/
|
||||
server?: unknown;
|
||||
/**
|
||||
* List of _expected_ errors that shouldn't abort the navigation (for non-lazy loaders). Provide a list of
|
||||
* constructors that can be checked with `instanceof` or a custom function that returns `true` for expected errors. Can also be set to `true` to accept all globally defined errors. Defaults to `false` to abort on any error.
|
||||
* @default `false`
|
||||
*/
|
||||
errors?: unknown;
|
||||
}
|
||||
/**
|
||||
* Options for a data loader that returns a data that is possibly `undefined`. Available for data loaders
|
||||
* implementations so they can be used in `defineLoader()` overloads.
|
||||
*/
|
||||
interface DefineDataLoaderOptionsBase_LaxData extends _DefineDataLoaderOptionsBase_Common {
|
||||
lazy?: boolean | ((to: RouteLocationNormalizedLoaded, from?: RouteLocationNormalizedLoaded) => boolean);
|
||||
server?: boolean;
|
||||
errors?: boolean | Array<new (...args: any[]) => any> | ((reason?: unknown) => boolean);
|
||||
}
|
||||
/**
|
||||
* Options for a data loader making the data defined without it being possibly `undefined`. Available for data loaders
|
||||
* implementations so they can be used in `defineLoader()` overloads.
|
||||
*/
|
||||
interface DefineDataLoaderOptionsBase_DefinedData extends _DefineDataLoaderOptionsBase_Common {
|
||||
lazy?: false;
|
||||
server?: true;
|
||||
errors?: false;
|
||||
}
|
||||
declare const toLazyValue: (lazy: undefined | DefineDataLoaderOptionsBase_LaxData["lazy"], to: RouteLocationNormalizedLoaded, from?: RouteLocationNormalizedLoaded) => boolean | undefined;
|
||||
/**
|
||||
* When the data should be committed to the entry.
|
||||
* - `immediate`: the data is committed as soon as it is loaded.
|
||||
* - `after-load`: the data is committed after all non-lazy loaders have finished loading.
|
||||
*/
|
||||
type DefineDataLoaderCommit = 'immediate' | 'after-load';
|
||||
interface DataLoaderContextBase {
|
||||
/**
|
||||
* Signal associated with the current navigation. It is aborted when the navigation is canceled or an error occurs.
|
||||
*/
|
||||
signal: AbortSignal | undefined;
|
||||
}
|
||||
/**
|
||||
* Data Loader composable returned by `defineLoader()`.
|
||||
* @see {@link DefineDataLoader}
|
||||
*/
|
||||
interface UseDataLoader<Data = unknown, TError = unknown> {
|
||||
[IS_USE_DATA_LOADER_KEY]: true;
|
||||
/**
|
||||
* Data Loader composable returned by `defineLoader()`.
|
||||
*
|
||||
* @example
|
||||
* Returns the Data loader data, isLoading, error etc. Meant to be used in `setup()` or `<script setup>` **without `await`**:
|
||||
* ```vue
|
||||
* <script setup>
|
||||
* const { data, isLoading, error } = useUserData()
|
||||
* </script>
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* It also returns a promise of the data when used in nested loaders. Note this `data` is **not a ref**. This is not meant to be used in `setup()` or `<script setup>`.
|
||||
* ```ts
|
||||
* export const useUserConnections = defineLoader(async () => {
|
||||
* const user = await useUserData()
|
||||
* return fetchUserConnections(user.id)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
(): _PromiseMerged<Exclude<Data, NavigationResult | undefined>, UseDataLoaderResult<Exclude<Data, NavigationResult>, TError>>;
|
||||
/**
|
||||
* Internals of the data loader.
|
||||
* @internal
|
||||
*/
|
||||
_: UseDataLoaderInternals<Exclude<Data, NavigationResult | undefined>, TError>;
|
||||
}
|
||||
/**
|
||||
* Internal properties of a data loader composable. Used by the internal implementation of `defineLoader()`. **Should
|
||||
* not be used in application code.**
|
||||
*/
|
||||
interface UseDataLoaderInternals<Data = unknown, TError = unknown> {
|
||||
/**
|
||||
* Loads the data from the cache if possible, otherwise loads it from the loader and awaits it.
|
||||
*
|
||||
* @param to - route location to load the data for
|
||||
* @param router - router instance
|
||||
* @param from - route location we are coming from
|
||||
* @param parent - parent data loader entry
|
||||
*/
|
||||
load: (to: RouteLocationNormalizedLoaded, router: Router, from?: RouteLocationNormalizedLoaded, parent?: DataLoaderEntryBase) => Promise<void>;
|
||||
/**
|
||||
* Resolved options for the loader.
|
||||
*/
|
||||
options: DefineDataLoaderOptionsBase_LaxData;
|
||||
/**
|
||||
* Gets the entry associated with the router instance. Assumes the data loader has been loaded and that the entry
|
||||
* exists.
|
||||
*
|
||||
* @param router - router instance
|
||||
*/
|
||||
getEntry(router: Router): DataLoaderEntryBase<Data, TError>;
|
||||
}
|
||||
/**
|
||||
* Return value of a loader composable defined with `defineLoader()`.
|
||||
*/
|
||||
interface UseDataLoaderResult<TData = unknown, TError = ErrorDefault> {
|
||||
/**
|
||||
* Data returned by the loader. If the data loader is lazy, it will be undefined until the first load.
|
||||
*/
|
||||
data: ShallowRef<TData>;
|
||||
/**
|
||||
* Whether there is an ongoing request.
|
||||
*/
|
||||
isLoading: ShallowRef<boolean>;
|
||||
/**
|
||||
* Error if there was an error.
|
||||
*/
|
||||
error: ShallowRef<TError | null>;
|
||||
/**
|
||||
* Reload the data using the current route location. Returns a promise that resolves when the data is reloaded. This
|
||||
* method should not be called during a navigation as it can conflict with an ongoing load and lead to
|
||||
* inconsistencies.
|
||||
*/
|
||||
reload(): Promise<void>;
|
||||
/**
|
||||
* Reload the data using the route location passed as argument. Returns a promise that resolves when the data is reloaded.
|
||||
*
|
||||
* @param route - route location to load the data for
|
||||
*/
|
||||
reload(route: RouteLocationNormalizedLoaded): Promise<void>;
|
||||
}
|
||||
/**
|
||||
* Loader function that can be passed to `defineLoader()`.
|
||||
*/
|
||||
interface DefineLoaderFn<Data, Context extends DataLoaderContextBase = DataLoaderContextBase, Route = RouteLocationNormalizedLoaded> {
|
||||
(route: Route, context: Context): Promise<Data>;
|
||||
}
|
||||
/**
|
||||
* @deprecated Use `DefineDataLoaderOptionsBase_LaxData` instead.
|
||||
*/
|
||||
type DefineDataLoaderOptionsBase = DefineDataLoaderOptionsBase_LaxData;
|
||||
//#endregion
|
||||
export { DATA_LOADERS_EFFECT_SCOPE_KEY as A, DataLoaderPluginOptions as C, useIsDataLoading as D, _DataLoaderRedirectResult as E, NAVIGATION_RESULTS_KEY as F, PENDING_LOCATION_KEY as I, STAGED_NO_VALUE as L, IS_USE_DATA_LOADER_KEY as M, LOADER_ENTRIES_KEY as N, ABORT_CONTROLLER_KEY as O, LOADER_SET_KEY as P, DataLoaderPlugin as S, SetupLoaderGuardOptions as T, isSubsetOf as _, DefineDataLoaderOptionsBase_LaxData as a, withLoaderContext as b, UseDataLoaderInternals as c, ErrorDefault as d, TypesConfig as f, getCurrentContext as g, currentContext as h, DefineDataLoaderOptionsBase_DefinedData as i, IS_SSR_KEY as j, APP_KEY as k, UseDataLoaderResult as l, assign as m, DataLoaderEntryBase as n, DefineLoaderFn as o, _PromiseMerged as p, DefineDataLoaderOptionsBase as r, UseDataLoader as s, DataLoaderContextBase as t, toLazyValue as u, setCurrentContext as v, NavigationResult as w, _DefineLoaderEntryMap as x, trackRoute as y };
|
||||
188
node_modules/unplugin-vue-router/dist/data-loaders/index.cjs
generated
vendored
Normal file
188
node_modules/unplugin-vue-router/dist/data-loaders/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,188 @@
|
||||
const require_createDataLoader = require('./createDataLoader-CBwrbo71.cjs');
|
||||
const require_utils = require('./utils-CL409u9D.cjs');
|
||||
let vue_router = require("vue-router");
|
||||
let vue = require("vue");
|
||||
|
||||
//#region src/data-loaders/navigation-guard.ts
|
||||
/**
|
||||
* Key to inject the global loading state for loaders used in `useIsDataLoading`.
|
||||
* @internal
|
||||
*/
|
||||
const IS_DATA_LOADING_KEY = Symbol();
|
||||
/**
|
||||
* TODO: export functions that allow preloading outside of a navigation guard
|
||||
*/
|
||||
/**
|
||||
* Setups the different Navigation Guards to collect the data loaders from the route records and then to execute them.
|
||||
* @internal used by the `DataLoaderPlugin`
|
||||
* @see {@link DataLoaderPlugin}
|
||||
*
|
||||
* @param router - the router instance
|
||||
* @returns
|
||||
*/
|
||||
function setupLoaderGuard({ router, app, effect: scope, isSSR, errors: globalErrors = [], selectNavigationResult = (results) => results[0].value }) {
|
||||
if (router[require_utils.LOADER_ENTRIES_KEY] != null) {
|
||||
if (process.env.NODE_ENV !== "production") console.warn("[vue-router]: Data Loader was setup twice. Make sure to setup only once.");
|
||||
return () => {};
|
||||
}
|
||||
if (process.env.NODE_ENV === "development" && !isSSR) console.warn("[vue-router]: Data Loader is experimental and subject to breaking changes in the future.");
|
||||
router[require_utils.LOADER_ENTRIES_KEY] = /* @__PURE__ */ new WeakMap();
|
||||
router[require_utils.APP_KEY] = app;
|
||||
router[require_utils.DATA_LOADERS_EFFECT_SCOPE_KEY] = scope;
|
||||
router[require_utils.IS_SSR_KEY] = !!isSSR;
|
||||
const isDataLoading = scope.run(() => (0, vue.shallowRef)(false));
|
||||
app.provide(IS_DATA_LOADING_KEY, isDataLoading);
|
||||
const removeLoaderGuard = router.beforeEach((to) => {
|
||||
if (router[require_utils.PENDING_LOCATION_KEY]) router[require_utils.PENDING_LOCATION_KEY].meta[require_utils.ABORT_CONTROLLER_KEY]?.abort();
|
||||
router[require_utils.PENDING_LOCATION_KEY] = to;
|
||||
to.meta[require_utils.LOADER_SET_KEY] = /* @__PURE__ */ new Set();
|
||||
to.meta[require_utils.ABORT_CONTROLLER_KEY] = new AbortController();
|
||||
to.meta[require_utils.NAVIGATION_RESULTS_KEY] = [];
|
||||
for (const record of to.matched) record.meta[require_utils.LOADER_SET_KEY] ??= new Set(record.meta.loaders || []);
|
||||
});
|
||||
const removeDataLoaderGuard = router.beforeResolve((to, from) => {
|
||||
for (const record of to.matched) {
|
||||
for (const loader of record.meta[require_utils.LOADER_SET_KEY]) to.meta[require_utils.LOADER_SET_KEY].add(loader);
|
||||
for (const componentName in record.mods) {
|
||||
const viewModule = record.mods[componentName];
|
||||
for (const exportName in viewModule) {
|
||||
const exportValue = viewModule[exportName];
|
||||
if (require_utils.isDataLoader(exportValue)) to.meta[require_utils.LOADER_SET_KEY].add(exportValue);
|
||||
}
|
||||
const component = record.components?.[componentName];
|
||||
if (component && Array.isArray(component.__loaders)) {
|
||||
for (const loader of component.__loaders) if (require_utils.isDataLoader(loader)) to.meta[require_utils.LOADER_SET_KEY].add(loader);
|
||||
}
|
||||
}
|
||||
}
|
||||
const loaders = Array.from(to.meta[require_utils.LOADER_SET_KEY]);
|
||||
const { signal } = to.meta[require_utils.ABORT_CONTROLLER_KEY];
|
||||
require_utils.setCurrentContext([]);
|
||||
isDataLoading.value = true;
|
||||
return Promise.all(loaders.map((loader) => {
|
||||
const { server, lazy, errors } = loader._.options;
|
||||
if (!server && isSSR) return;
|
||||
const ret = scope.run(() => app.runWithContext(() => loader._.load(to, router, from)));
|
||||
return !isSSR && require_createDataLoader.toLazyValue(lazy, to, from) ? void 0 : ret.catch((reason) => {
|
||||
if (errors === true) {
|
||||
if (Array.isArray(globalErrors) ? globalErrors.some((Err) => reason instanceof Err) : globalErrors(reason)) return;
|
||||
} else if (errors && (Array.isArray(errors) ? errors.some((Err) => reason instanceof Err) : errors(reason))) return;
|
||||
throw reason;
|
||||
});
|
||||
})).then(() => {
|
||||
if (to.meta[require_utils.NAVIGATION_RESULTS_KEY].length) return selectNavigationResult(to.meta[require_utils.NAVIGATION_RESULTS_KEY]);
|
||||
}).catch((error) => error instanceof NavigationResult ? error.value : signal.aborted && error === signal.reason ? false : Promise.reject(error)).finally(() => {
|
||||
require_utils.setCurrentContext([]);
|
||||
isDataLoading.value = false;
|
||||
});
|
||||
});
|
||||
const removeAfterEach = router.afterEach((to, from, failure) => {
|
||||
if (failure) {
|
||||
to.meta[require_utils.ABORT_CONTROLLER_KEY]?.abort(failure);
|
||||
if ((0, vue_router.isNavigationFailure)(failure, 16)) for (const loader of to.meta[require_utils.LOADER_SET_KEY]) loader._.getEntry(router).resetPending();
|
||||
} else for (const loader of to.meta[require_utils.LOADER_SET_KEY]) {
|
||||
const { commit, lazy } = loader._.options;
|
||||
if (commit === "after-load") {
|
||||
const entry = loader._.getEntry(router);
|
||||
if (entry && (!require_createDataLoader.toLazyValue(lazy, to, from) || !entry.isLoading.value)) entry.commit(to);
|
||||
}
|
||||
}
|
||||
if (router[require_utils.PENDING_LOCATION_KEY] === to) router[require_utils.PENDING_LOCATION_KEY] = null;
|
||||
});
|
||||
const removeOnError = router.onError((error, to) => {
|
||||
to.meta[require_utils.ABORT_CONTROLLER_KEY]?.abort(error);
|
||||
if (router[require_utils.PENDING_LOCATION_KEY] === to) router[require_utils.PENDING_LOCATION_KEY] = null;
|
||||
});
|
||||
return () => {
|
||||
delete router[require_utils.LOADER_ENTRIES_KEY];
|
||||
delete router[require_utils.APP_KEY];
|
||||
removeLoaderGuard();
|
||||
removeDataLoaderGuard();
|
||||
removeAfterEach();
|
||||
removeOnError();
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Possible values to change the result of a navigation within a loader. Can be returned from a data loader and will
|
||||
* appear in `selectNavigationResult`. If thrown, it will immediately cancel the navigation. It can only contain values
|
||||
* that cancel the navigation.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* export const useUserData = defineLoader(async (to) => {
|
||||
* const user = await fetchUser(to.params.id)
|
||||
* if (!user) {
|
||||
* return new NavigationResult('/404')
|
||||
* }
|
||||
* return user
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
var NavigationResult = class {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Data Loader plugin to add data loading support to Vue Router.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const router = createRouter({
|
||||
* routes,
|
||||
* history: createWebHistory(),
|
||||
* })
|
||||
*
|
||||
* const app = createApp({})
|
||||
* app.use(DataLoaderPlugin, { router })
|
||||
* app.use(router)
|
||||
* ```
|
||||
*/
|
||||
function DataLoaderPlugin(app, options) {
|
||||
const effect = (0, vue.effectScope)(true);
|
||||
const removeGuards = setupLoaderGuard(require_utils.assign({
|
||||
app,
|
||||
effect
|
||||
}, options));
|
||||
const { unmount } = app;
|
||||
app.unmount = () => {
|
||||
effect.stop();
|
||||
removeGuards();
|
||||
unmount.call(app);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Return a ref that reflects the global loading state of all loaders within a navigation.
|
||||
* This state doesn't update if `refresh()` is manually called.
|
||||
*/
|
||||
function useIsDataLoading() {
|
||||
return (0, vue.inject)(IS_DATA_LOADING_KEY);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
exports.ABORT_CONTROLLER_KEY = require_utils.ABORT_CONTROLLER_KEY;
|
||||
exports.APP_KEY = require_utils.APP_KEY;
|
||||
exports.DATA_LOADERS_EFFECT_SCOPE_KEY = require_utils.DATA_LOADERS_EFFECT_SCOPE_KEY;
|
||||
exports.DataLoaderPlugin = DataLoaderPlugin;
|
||||
exports.IS_SSR_KEY = require_utils.IS_SSR_KEY;
|
||||
exports.IS_USE_DATA_LOADER_KEY = require_utils.IS_USE_DATA_LOADER_KEY;
|
||||
exports.LOADER_ENTRIES_KEY = require_utils.LOADER_ENTRIES_KEY;
|
||||
exports.LOADER_SET_KEY = require_utils.LOADER_SET_KEY;
|
||||
exports.NAVIGATION_RESULTS_KEY = require_utils.NAVIGATION_RESULTS_KEY;
|
||||
exports.NavigationResult = NavigationResult;
|
||||
exports.PENDING_LOCATION_KEY = require_utils.PENDING_LOCATION_KEY;
|
||||
exports.STAGED_NO_VALUE = require_utils.STAGED_NO_VALUE;
|
||||
exports.assign = require_utils.assign;
|
||||
Object.defineProperty(exports, 'currentContext', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return require_utils.currentContext;
|
||||
}
|
||||
});
|
||||
exports.getCurrentContext = require_utils.getCurrentContext;
|
||||
exports.isSubsetOf = require_utils.isSubsetOf;
|
||||
exports.setCurrentContext = require_utils.setCurrentContext;
|
||||
exports.toLazyValue = require_createDataLoader.toLazyValue;
|
||||
exports.trackRoute = require_utils.trackRoute;
|
||||
exports.useIsDataLoading = useIsDataLoading;
|
||||
exports.withLoaderContext = require_utils.withLoaderContext;
|
||||
2
node_modules/unplugin-vue-router/dist/data-loaders/index.d.cts
generated
vendored
Normal file
2
node_modules/unplugin-vue-router/dist/data-loaders/index.d.cts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import { A as DATA_LOADERS_EFFECT_SCOPE_KEY, C as DataLoaderPluginOptions, D as useIsDataLoading, E as _DataLoaderRedirectResult, F as NAVIGATION_RESULTS_KEY, I as PENDING_LOCATION_KEY, L as STAGED_NO_VALUE, M as IS_USE_DATA_LOADER_KEY, N as LOADER_ENTRIES_KEY, O as ABORT_CONTROLLER_KEY, P as LOADER_SET_KEY, S as DataLoaderPlugin, T as SetupLoaderGuardOptions, _ as isSubsetOf, a as DefineDataLoaderOptionsBase_LaxData, b as withLoaderContext, c as UseDataLoaderInternals, d as ErrorDefault, f as TypesConfig, g as getCurrentContext, h as currentContext, i as DefineDataLoaderOptionsBase_DefinedData, j as IS_SSR_KEY, k as APP_KEY, l as UseDataLoaderResult, m as assign, n as DataLoaderEntryBase, o as DefineLoaderFn, p as _PromiseMerged, r as DefineDataLoaderOptionsBase, s as UseDataLoader, t as DataLoaderContextBase, u as toLazyValue, v as setCurrentContext, w as NavigationResult, x as _DefineLoaderEntryMap, y as trackRoute } from "./createDataLoader-DgP0poyl.cjs";
|
||||
export { ABORT_CONTROLLER_KEY, APP_KEY, DATA_LOADERS_EFFECT_SCOPE_KEY, type DataLoaderContextBase, type DataLoaderEntryBase, DataLoaderPlugin, type DataLoaderPluginOptions, type DefineDataLoaderOptionsBase, type DefineDataLoaderOptionsBase_DefinedData, type DefineDataLoaderOptionsBase_LaxData, type DefineLoaderFn, type ErrorDefault, IS_SSR_KEY, IS_USE_DATA_LOADER_KEY, LOADER_ENTRIES_KEY, LOADER_SET_KEY, NAVIGATION_RESULTS_KEY, NavigationResult, PENDING_LOCATION_KEY, STAGED_NO_VALUE, type SetupLoaderGuardOptions, type TypesConfig, type UseDataLoader, type UseDataLoaderInternals, type UseDataLoaderResult, type _DataLoaderRedirectResult, _DefineLoaderEntryMap, type _PromiseMerged, assign, currentContext, getCurrentContext, isSubsetOf, setCurrentContext, toLazyValue, trackRoute, useIsDataLoading, withLoaderContext };
|
||||
2
node_modules/unplugin-vue-router/dist/data-loaders/index.d.mts
generated
vendored
Normal file
2
node_modules/unplugin-vue-router/dist/data-loaders/index.d.mts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import { A as DATA_LOADERS_EFFECT_SCOPE_KEY, C as DataLoaderPluginOptions, D as useIsDataLoading, E as _DataLoaderRedirectResult, F as NAVIGATION_RESULTS_KEY, I as PENDING_LOCATION_KEY, L as STAGED_NO_VALUE, M as IS_USE_DATA_LOADER_KEY, N as LOADER_ENTRIES_KEY, O as ABORT_CONTROLLER_KEY, P as LOADER_SET_KEY, S as DataLoaderPlugin, T as SetupLoaderGuardOptions, _ as isSubsetOf, a as DefineDataLoaderOptionsBase_LaxData, b as withLoaderContext, c as UseDataLoaderInternals, d as ErrorDefault, f as TypesConfig, g as getCurrentContext, h as currentContext, i as DefineDataLoaderOptionsBase_DefinedData, j as IS_SSR_KEY, k as APP_KEY, l as UseDataLoaderResult, m as assign, n as DataLoaderEntryBase, o as DefineLoaderFn, p as _PromiseMerged, r as DefineDataLoaderOptionsBase, s as UseDataLoader, t as DataLoaderContextBase, u as toLazyValue, v as setCurrentContext, w as NavigationResult, x as _DefineLoaderEntryMap, y as trackRoute } from "./createDataLoader-C8o8GrdD.mjs";
|
||||
export { ABORT_CONTROLLER_KEY, APP_KEY, DATA_LOADERS_EFFECT_SCOPE_KEY, type DataLoaderContextBase, type DataLoaderEntryBase, DataLoaderPlugin, type DataLoaderPluginOptions, type DefineDataLoaderOptionsBase, type DefineDataLoaderOptionsBase_DefinedData, type DefineDataLoaderOptionsBase_LaxData, type DefineLoaderFn, type ErrorDefault, IS_SSR_KEY, IS_USE_DATA_LOADER_KEY, LOADER_ENTRIES_KEY, LOADER_SET_KEY, NAVIGATION_RESULTS_KEY, NavigationResult, PENDING_LOCATION_KEY, STAGED_NO_VALUE, type SetupLoaderGuardOptions, type TypesConfig, type UseDataLoader, type UseDataLoaderInternals, type UseDataLoaderResult, type _DataLoaderRedirectResult, _DefineLoaderEntryMap, type _PromiseMerged, assign, currentContext, getCurrentContext, isSubsetOf, setCurrentContext, toLazyValue, trackRoute, useIsDataLoading, withLoaderContext };
|
||||
163
node_modules/unplugin-vue-router/dist/data-loaders/index.mjs
generated
vendored
Normal file
163
node_modules/unplugin-vue-router/dist/data-loaders/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
import { t as toLazyValue } from "./createDataLoader-BK9Gdnky.mjs";
|
||||
import { _ as PENDING_LOCATION_KEY, a as isSubsetOf, c as withLoaderContext, d as DATA_LOADERS_EFFECT_SCOPE_KEY, f as IS_SSR_KEY, g as NAVIGATION_RESULTS_KEY, h as LOADER_SET_KEY, i as isDataLoader, l as ABORT_CONTROLLER_KEY, m as LOADER_ENTRIES_KEY, n as currentContext, o as setCurrentContext, p as IS_USE_DATA_LOADER_KEY, r as getCurrentContext, s as trackRoute, t as assign, u as APP_KEY, v as STAGED_NO_VALUE } from "./utils-C5t8q_dT.mjs";
|
||||
import { isNavigationFailure } from "vue-router";
|
||||
import { effectScope, inject, shallowRef } from "vue";
|
||||
|
||||
//#region src/data-loaders/navigation-guard.ts
|
||||
/**
|
||||
* Key to inject the global loading state for loaders used in `useIsDataLoading`.
|
||||
* @internal
|
||||
*/
|
||||
const IS_DATA_LOADING_KEY = Symbol();
|
||||
/**
|
||||
* TODO: export functions that allow preloading outside of a navigation guard
|
||||
*/
|
||||
/**
|
||||
* Setups the different Navigation Guards to collect the data loaders from the route records and then to execute them.
|
||||
* @internal used by the `DataLoaderPlugin`
|
||||
* @see {@link DataLoaderPlugin}
|
||||
*
|
||||
* @param router - the router instance
|
||||
* @returns
|
||||
*/
|
||||
function setupLoaderGuard({ router, app, effect: scope, isSSR, errors: globalErrors = [], selectNavigationResult = (results) => results[0].value }) {
|
||||
if (router[LOADER_ENTRIES_KEY] != null) {
|
||||
if (process.env.NODE_ENV !== "production") console.warn("[vue-router]: Data Loader was setup twice. Make sure to setup only once.");
|
||||
return () => {};
|
||||
}
|
||||
if (process.env.NODE_ENV === "development" && !isSSR) console.warn("[vue-router]: Data Loader is experimental and subject to breaking changes in the future.");
|
||||
router[LOADER_ENTRIES_KEY] = /* @__PURE__ */ new WeakMap();
|
||||
router[APP_KEY] = app;
|
||||
router[DATA_LOADERS_EFFECT_SCOPE_KEY] = scope;
|
||||
router[IS_SSR_KEY] = !!isSSR;
|
||||
const isDataLoading = scope.run(() => shallowRef(false));
|
||||
app.provide(IS_DATA_LOADING_KEY, isDataLoading);
|
||||
const removeLoaderGuard = router.beforeEach((to) => {
|
||||
if (router[PENDING_LOCATION_KEY]) router[PENDING_LOCATION_KEY].meta[ABORT_CONTROLLER_KEY]?.abort();
|
||||
router[PENDING_LOCATION_KEY] = to;
|
||||
to.meta[LOADER_SET_KEY] = /* @__PURE__ */ new Set();
|
||||
to.meta[ABORT_CONTROLLER_KEY] = new AbortController();
|
||||
to.meta[NAVIGATION_RESULTS_KEY] = [];
|
||||
for (const record of to.matched) record.meta[LOADER_SET_KEY] ??= new Set(record.meta.loaders || []);
|
||||
});
|
||||
const removeDataLoaderGuard = router.beforeResolve((to, from) => {
|
||||
for (const record of to.matched) {
|
||||
for (const loader of record.meta[LOADER_SET_KEY]) to.meta[LOADER_SET_KEY].add(loader);
|
||||
for (const componentName in record.mods) {
|
||||
const viewModule = record.mods[componentName];
|
||||
for (const exportName in viewModule) {
|
||||
const exportValue = viewModule[exportName];
|
||||
if (isDataLoader(exportValue)) to.meta[LOADER_SET_KEY].add(exportValue);
|
||||
}
|
||||
const component = record.components?.[componentName];
|
||||
if (component && Array.isArray(component.__loaders)) {
|
||||
for (const loader of component.__loaders) if (isDataLoader(loader)) to.meta[LOADER_SET_KEY].add(loader);
|
||||
}
|
||||
}
|
||||
}
|
||||
const loaders = Array.from(to.meta[LOADER_SET_KEY]);
|
||||
const { signal } = to.meta[ABORT_CONTROLLER_KEY];
|
||||
setCurrentContext([]);
|
||||
isDataLoading.value = true;
|
||||
return Promise.all(loaders.map((loader) => {
|
||||
const { server, lazy, errors } = loader._.options;
|
||||
if (!server && isSSR) return;
|
||||
const ret = scope.run(() => app.runWithContext(() => loader._.load(to, router, from)));
|
||||
return !isSSR && toLazyValue(lazy, to, from) ? void 0 : ret.catch((reason) => {
|
||||
if (errors === true) {
|
||||
if (Array.isArray(globalErrors) ? globalErrors.some((Err) => reason instanceof Err) : globalErrors(reason)) return;
|
||||
} else if (errors && (Array.isArray(errors) ? errors.some((Err) => reason instanceof Err) : errors(reason))) return;
|
||||
throw reason;
|
||||
});
|
||||
})).then(() => {
|
||||
if (to.meta[NAVIGATION_RESULTS_KEY].length) return selectNavigationResult(to.meta[NAVIGATION_RESULTS_KEY]);
|
||||
}).catch((error) => error instanceof NavigationResult ? error.value : signal.aborted && error === signal.reason ? false : Promise.reject(error)).finally(() => {
|
||||
setCurrentContext([]);
|
||||
isDataLoading.value = false;
|
||||
});
|
||||
});
|
||||
const removeAfterEach = router.afterEach((to, from, failure) => {
|
||||
if (failure) {
|
||||
to.meta[ABORT_CONTROLLER_KEY]?.abort(failure);
|
||||
if (isNavigationFailure(failure, 16)) for (const loader of to.meta[LOADER_SET_KEY]) loader._.getEntry(router).resetPending();
|
||||
} else for (const loader of to.meta[LOADER_SET_KEY]) {
|
||||
const { commit, lazy } = loader._.options;
|
||||
if (commit === "after-load") {
|
||||
const entry = loader._.getEntry(router);
|
||||
if (entry && (!toLazyValue(lazy, to, from) || !entry.isLoading.value)) entry.commit(to);
|
||||
}
|
||||
}
|
||||
if (router[PENDING_LOCATION_KEY] === to) router[PENDING_LOCATION_KEY] = null;
|
||||
});
|
||||
const removeOnError = router.onError((error, to) => {
|
||||
to.meta[ABORT_CONTROLLER_KEY]?.abort(error);
|
||||
if (router[PENDING_LOCATION_KEY] === to) router[PENDING_LOCATION_KEY] = null;
|
||||
});
|
||||
return () => {
|
||||
delete router[LOADER_ENTRIES_KEY];
|
||||
delete router[APP_KEY];
|
||||
removeLoaderGuard();
|
||||
removeDataLoaderGuard();
|
||||
removeAfterEach();
|
||||
removeOnError();
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Possible values to change the result of a navigation within a loader. Can be returned from a data loader and will
|
||||
* appear in `selectNavigationResult`. If thrown, it will immediately cancel the navigation. It can only contain values
|
||||
* that cancel the navigation.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* export const useUserData = defineLoader(async (to) => {
|
||||
* const user = await fetchUser(to.params.id)
|
||||
* if (!user) {
|
||||
* return new NavigationResult('/404')
|
||||
* }
|
||||
* return user
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
var NavigationResult = class {
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Data Loader plugin to add data loading support to Vue Router.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const router = createRouter({
|
||||
* routes,
|
||||
* history: createWebHistory(),
|
||||
* })
|
||||
*
|
||||
* const app = createApp({})
|
||||
* app.use(DataLoaderPlugin, { router })
|
||||
* app.use(router)
|
||||
* ```
|
||||
*/
|
||||
function DataLoaderPlugin(app, options) {
|
||||
const effect = effectScope(true);
|
||||
const removeGuards = setupLoaderGuard(assign({
|
||||
app,
|
||||
effect
|
||||
}, options));
|
||||
const { unmount } = app;
|
||||
app.unmount = () => {
|
||||
effect.stop();
|
||||
removeGuards();
|
||||
unmount.call(app);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Return a ref that reflects the global loading state of all loaders within a navigation.
|
||||
* This state doesn't update if `refresh()` is manually called.
|
||||
*/
|
||||
function useIsDataLoading() {
|
||||
return inject(IS_DATA_LOADING_KEY);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
export { ABORT_CONTROLLER_KEY, APP_KEY, DATA_LOADERS_EFFECT_SCOPE_KEY, DataLoaderPlugin, IS_SSR_KEY, IS_USE_DATA_LOADER_KEY, LOADER_ENTRIES_KEY, LOADER_SET_KEY, NAVIGATION_RESULTS_KEY, NavigationResult, PENDING_LOCATION_KEY, STAGED_NO_VALUE, assign, currentContext, getCurrentContext, isSubsetOf, setCurrentContext, toLazyValue, trackRoute, useIsDataLoading, withLoaderContext };
|
||||
229
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.cjs
generated
vendored
Normal file
229
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.cjs
generated
vendored
Normal file
@@ -0,0 +1,229 @@
|
||||
const require_createDataLoader = require('./createDataLoader-CBwrbo71.cjs');
|
||||
let vue_router = require("vue-router");
|
||||
let unplugin_vue_router_data_loaders = require("unplugin-vue-router/data-loaders");
|
||||
let vue = require("vue");
|
||||
let __pinia_colada = require("@pinia/colada");
|
||||
|
||||
//#region src/data-loaders/defineColadaLoader.ts
|
||||
function defineColadaLoader(nameOrOptions, _options) {
|
||||
_options = _options || nameOrOptions;
|
||||
const loader = _options.query;
|
||||
const options = {
|
||||
...DEFAULT_DEFINE_LOADER_OPTIONS,
|
||||
..._options,
|
||||
commit: _options?.commit || "after-load"
|
||||
};
|
||||
let isInitial = true;
|
||||
const useDefinedQuery = (0, __pinia_colada.defineQuery)(() => {
|
||||
const router = (0, vue_router.useRouter)();
|
||||
const entry = router[unplugin_vue_router_data_loaders.LOADER_ENTRIES_KEY].get(loader);
|
||||
return (0, __pinia_colada.useQuery)({
|
||||
...options,
|
||||
query: () => {
|
||||
const route = entry.route.value;
|
||||
const [trackedRoute, params, query, hash] = (0, unplugin_vue_router_data_loaders.trackRoute)(route);
|
||||
entry.tracked.set(joinKeys(serializeQueryKey(options.key, trackedRoute)), {
|
||||
ready: false,
|
||||
params,
|
||||
query,
|
||||
hash
|
||||
});
|
||||
return router[unplugin_vue_router_data_loaders.APP_KEY].runWithContext(() => loader(trackedRoute, { signal: route.meta[unplugin_vue_router_data_loaders.ABORT_CONTROLLER_KEY]?.signal }));
|
||||
},
|
||||
key: () => toValueWithParameters(options.key, entry.route.value)
|
||||
});
|
||||
});
|
||||
function load(to, router, from, parent, reload) {
|
||||
const entries = router[unplugin_vue_router_data_loaders.LOADER_ENTRIES_KEY];
|
||||
const isSSR = router[unplugin_vue_router_data_loaders.IS_SSR_KEY];
|
||||
const key = serializeQueryKey(options.key, to);
|
||||
if (!entries.has(loader)) {
|
||||
const route = (0, vue.shallowRef)(to);
|
||||
entries.set(loader, {
|
||||
data: (0, vue.shallowRef)(),
|
||||
isLoading: (0, vue.shallowRef)(false),
|
||||
error: (0, vue.shallowRef)(null),
|
||||
to,
|
||||
options,
|
||||
children: /* @__PURE__ */ new Set(),
|
||||
resetPending() {
|
||||
this.pendingTo = null;
|
||||
this.pendingLoad = null;
|
||||
this.isLoading.value = false;
|
||||
},
|
||||
staged: unplugin_vue_router_data_loaders.STAGED_NO_VALUE,
|
||||
stagedError: null,
|
||||
stagedNavigationResult: null,
|
||||
commit,
|
||||
tracked: /* @__PURE__ */ new Map(),
|
||||
ext: null,
|
||||
route,
|
||||
pendingTo: null,
|
||||
pendingLoad: null
|
||||
});
|
||||
}
|
||||
const entry = entries.get(loader);
|
||||
if (entry.pendingTo === to && entry.pendingLoad) return entry.pendingLoad;
|
||||
const currentContext = (0, unplugin_vue_router_data_loaders.getCurrentContext)();
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (parent !== currentContext[0]) console.warn(`❌👶 "${key}" has a different parent than the current context. This shouldn't be happening. Please report a bug with a reproduction to https://github.com/posva/unplugin-vue-router/`);
|
||||
}
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)([
|
||||
entry,
|
||||
router,
|
||||
to
|
||||
]);
|
||||
if (!entry.ext) {
|
||||
entry.ext = useDefinedQuery();
|
||||
(0, __pinia_colada.useQueryCache)().get(toValueWithParameters(options.key, to))?.deps.delete(router[unplugin_vue_router_data_loaders.DATA_LOADERS_EFFECT_SCOPE_KEY]);
|
||||
reload = false;
|
||||
}
|
||||
const { isLoading, data, error, ext } = entry;
|
||||
if (isInitial) {
|
||||
isInitial = false;
|
||||
if (ext.data.value !== void 0) {
|
||||
data.value = ext.data.value;
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)(currentContext);
|
||||
return entry.pendingLoad = Promise.resolve();
|
||||
}
|
||||
}
|
||||
if (entry.route.value !== to) {
|
||||
const tracked = entry.tracked.get(joinKeys(key));
|
||||
reload = !tracked || hasRouteChanged(to, tracked);
|
||||
}
|
||||
entry.route.value = entry.pendingTo = to;
|
||||
isLoading.value = true;
|
||||
entry.staged = unplugin_vue_router_data_loaders.STAGED_NO_VALUE;
|
||||
entry.stagedError = error.value;
|
||||
entry.stagedNavigationResult = null;
|
||||
const currentLoad = ext[reload ? "refetch" : "refresh"]().then(() => {
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
const newError = ext.error.value;
|
||||
if (newError) {
|
||||
entry.stagedError = newError;
|
||||
if (!require_createDataLoader.toLazyValue(options.lazy, to, from) || isSSR) throw newError;
|
||||
} else {
|
||||
const newData = ext.data.value;
|
||||
if (newData instanceof unplugin_vue_router_data_loaders.NavigationResult) {
|
||||
to.meta[unplugin_vue_router_data_loaders.NAVIGATION_RESULTS_KEY].push(newData);
|
||||
entry.stagedNavigationResult = newData;
|
||||
} else {
|
||||
entry.staged = newData;
|
||||
entry.stagedError = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}).finally(() => {
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)(currentContext);
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
isLoading.value = false;
|
||||
if (options.commit === "immediate" || !router[unplugin_vue_router_data_loaders.PENDING_LOCATION_KEY]) entry.commit(to);
|
||||
}
|
||||
});
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)(currentContext);
|
||||
entry.pendingLoad = currentLoad;
|
||||
return currentLoad;
|
||||
}
|
||||
function commit(to) {
|
||||
const key = serializeQueryKey(options.key, to);
|
||||
if (this.pendingTo === to) {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (this.staged === unplugin_vue_router_data_loaders.STAGED_NO_VALUE && this.stagedError === null && this.stagedNavigationResult === null) console.warn(`Loader "${key}"'s "commit()" was called but there is no staged data.`);
|
||||
}
|
||||
if (this.staged !== unplugin_vue_router_data_loaders.STAGED_NO_VALUE) {
|
||||
this.data.value = this.staged;
|
||||
if (process.env.NODE_ENV !== "production" && !this.tracked.has(joinKeys(key))) {
|
||||
console.warn(`A query was defined with the same key as the loader "[${key.join(", ")}]". If the "key" is meant to be the same, you should directly use the data loader instead. If not, change the key of the "useQuery()".\nSee https://pinia-colada.esm.dev/#TODO`);
|
||||
return;
|
||||
}
|
||||
this.tracked.get(joinKeys(key)).ready = true;
|
||||
}
|
||||
this.error.value = this.stagedError;
|
||||
this.staged = unplugin_vue_router_data_loaders.STAGED_NO_VALUE;
|
||||
this.stagedError = this.error.value;
|
||||
this.to = to;
|
||||
this.pendingTo = null;
|
||||
for (const childEntry of this.children) childEntry.commit(to);
|
||||
}
|
||||
}
|
||||
const useDataLoader = () => {
|
||||
const currentEntry = (0, unplugin_vue_router_data_loaders.getCurrentContext)();
|
||||
const [parentEntry, _router, _route] = currentEntry;
|
||||
const router = _router || (0, vue_router.useRouter)();
|
||||
const route = _route || (0, vue_router.useRoute)();
|
||||
const app = router[unplugin_vue_router_data_loaders.APP_KEY];
|
||||
const entries = router[unplugin_vue_router_data_loaders.LOADER_ENTRIES_KEY];
|
||||
let entry = entries.get(loader);
|
||||
if (!entry || parentEntry && entry.pendingTo !== route || !entry.pendingLoad) app.runWithContext(() => load(route, router, void 0, parentEntry, true));
|
||||
entry = entries.get(loader);
|
||||
if (parentEntry) {
|
||||
if (parentEntry !== entry) parentEntry.children.add(entry);
|
||||
}
|
||||
const { data, error, isLoading } = entry;
|
||||
const ext = useDefinedQuery();
|
||||
(0, __pinia_colada.useQueryCache)().get(toValueWithParameters(options.key, route))?.deps.delete(router[unplugin_vue_router_data_loaders.DATA_LOADERS_EFFECT_SCOPE_KEY]);
|
||||
(0, vue.watch)(ext.data, (newData) => {
|
||||
if (!router[unplugin_vue_router_data_loaders.PENDING_LOCATION_KEY]) data.value = newData;
|
||||
});
|
||||
(0, vue.watch)(ext.isLoading, (isFetching) => {
|
||||
if (!router[unplugin_vue_router_data_loaders.PENDING_LOCATION_KEY]) isLoading.value = isFetching;
|
||||
});
|
||||
(0, vue.watch)(ext.error, (newError) => {
|
||||
if (!router[unplugin_vue_router_data_loaders.PENDING_LOCATION_KEY]) error.value = newError;
|
||||
});
|
||||
const useDataLoaderResult = {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
reload: (to = router.currentRoute.value) => app.runWithContext(() => load(to, router, void 0, void 0, true)).then(() => entry.commit(to)),
|
||||
refetch: (to = router.currentRoute.value) => app.runWithContext(() => load(to, router, void 0, void 0, true)).then(() => (entry.commit(to), entry.ext.state.value)),
|
||||
refresh: (to = router.currentRoute.value) => app.runWithContext(() => load(to, router)).then(() => (entry.commit(to), entry.ext.state.value)),
|
||||
status: ext.status,
|
||||
asyncStatus: ext.asyncStatus,
|
||||
state: ext.state,
|
||||
isPending: ext.isPending
|
||||
};
|
||||
const promise = entry.pendingLoad.then(() => {
|
||||
return entry.staged === unplugin_vue_router_data_loaders.STAGED_NO_VALUE ? entry.stagedNavigationResult ? Promise.reject(entry.stagedNavigationResult) : ext.data.value : entry.staged;
|
||||
}).catch((e) => parentEntry ? Promise.reject(e) : null);
|
||||
(0, unplugin_vue_router_data_loaders.setCurrentContext)(currentEntry);
|
||||
return (0, unplugin_vue_router_data_loaders.assign)(promise, useDataLoaderResult);
|
||||
};
|
||||
useDataLoader[unplugin_vue_router_data_loaders.IS_USE_DATA_LOADER_KEY] = true;
|
||||
useDataLoader._ = {
|
||||
load,
|
||||
options,
|
||||
getEntry(router) {
|
||||
return router[unplugin_vue_router_data_loaders.LOADER_ENTRIES_KEY].get(loader);
|
||||
}
|
||||
};
|
||||
return useDataLoader;
|
||||
}
|
||||
const joinKeys = (keys) => keys.join("|");
|
||||
function hasRouteChanged(to, tracked) {
|
||||
return !tracked.ready || !(0, unplugin_vue_router_data_loaders.isSubsetOf)(tracked.params, to.params) || !(0, unplugin_vue_router_data_loaders.isSubsetOf)(tracked.query, to.query) || tracked.hash.v != null && tracked.hash.v !== to.hash;
|
||||
}
|
||||
const DEFAULT_DEFINE_LOADER_OPTIONS = {
|
||||
lazy: false,
|
||||
server: true,
|
||||
commit: "after-load"
|
||||
};
|
||||
const toValueWithParameters = (optionValue, arg) => {
|
||||
return typeof optionValue === "function" ? optionValue(arg) : optionValue;
|
||||
};
|
||||
/**
|
||||
* Transform the key to a string array so it can be used as a key in caches.
|
||||
*
|
||||
* @param key - key to transform
|
||||
* @param to - route to use
|
||||
*/
|
||||
function serializeQueryKey(keyOption, to) {
|
||||
const key = toValueWithParameters(keyOption, to);
|
||||
return (Array.isArray(key) ? key : [key]).map(stringifyFlatObject);
|
||||
}
|
||||
function stringifyFlatObject(obj) {
|
||||
return obj && typeof obj === "object" ? JSON.stringify(obj, Object.keys(obj).sort()) : String(obj);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
exports.defineColadaLoader = defineColadaLoader;
|
||||
150
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.d.cts
generated
vendored
Normal file
150
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.d.cts
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
import { i as DefineDataLoaderOptionsBase_DefinedData } from "./createDataLoader-DgP0poyl.cjs";
|
||||
import { LocationQuery, RouteLocationNormalizedLoaded, RouteMap } from "vue-router";
|
||||
import { DataLoaderContextBase, DataLoaderEntryBase, DefineDataLoaderOptionsBase_LaxData, DefineLoaderFn, ErrorDefault, NavigationResult, UseDataLoader, UseDataLoaderResult, _PromiseMerged } from "unplugin-vue-router/data-loaders";
|
||||
import { ShallowRef } from "vue";
|
||||
import { EntryKey, UseQueryOptions, UseQueryReturn } from "@pinia/colada";
|
||||
|
||||
//#region src/data-loaders/defineColadaLoader.d.ts
|
||||
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is always defined.
|
||||
*
|
||||
* @param name - name of the route to have typed routes
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Name extends keyof RouteMap, Data>(name: Name, options: DefineDataColadaLoaderOptions_DefinedData<Name, Data>): UseDataLoaderColada_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is possibly `undefined`.
|
||||
*
|
||||
* @param name - name of the route to have typed routes
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Name extends keyof RouteMap, Data>(name: Name, options: DefineDataColadaLoaderOptions_LaxData<Name, Data>): UseDataLoaderColada_LaxData<Data>;
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is always defined.
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Data>(options: DefineDataColadaLoaderOptions_DefinedData<keyof RouteMap, Data>): UseDataLoaderColada_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is possibly `undefined`.
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Data>(options: DefineDataColadaLoaderOptions_LaxData<keyof RouteMap, Data>): UseDataLoaderColada_LaxData<Data>;
|
||||
/**
|
||||
* Base type with docs for the options of `defineColadaLoader`.
|
||||
* @internal
|
||||
*/
|
||||
interface _DefineDataColadaLoaderOptions_Common<Name extends keyof RouteMap, Data> extends Omit<UseQueryOptions<Data, ErrorDefault, Data>, 'query' | 'key'> {
|
||||
/**
|
||||
* Key associated with the data and passed to pinia colada
|
||||
* @param to - Route to load the data
|
||||
*/
|
||||
key: EntryKey | ((to: RouteLocationNormalizedLoaded<Name>) => EntryKey);
|
||||
/**
|
||||
* Function that returns a promise with the data.
|
||||
*/
|
||||
query: DefineLoaderFn<Data, DataColadaLoaderContext, RouteLocationNormalizedLoaded<Name>>;
|
||||
}
|
||||
/**
|
||||
* Options for `defineColadaLoader` when the data is possibly `undefined`.
|
||||
*/
|
||||
interface DefineDataColadaLoaderOptions_LaxData<Name extends keyof RouteMap, Data> extends _DefineDataColadaLoaderOptions_Common<Name, Data>, DefineDataLoaderOptionsBase_LaxData {}
|
||||
/**
|
||||
* Options for `defineColadaLoader` when the data is always defined.
|
||||
*/
|
||||
interface DefineDataColadaLoaderOptions_DefinedData<Name extends keyof RouteMap, Data> extends _DefineDataColadaLoaderOptions_Common<Name, Data>, DefineDataLoaderOptionsBase_DefinedData {}
|
||||
/**
|
||||
* @deprecated Use {@link `DefineDataColadaLoaderOptions_LaxData`} instead.
|
||||
*/
|
||||
type DefineDataColadaLoaderOptions<Name extends keyof RouteMap, Data> = DefineDataColadaLoaderOptions_LaxData<Name, Data>;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface DataColadaLoaderContext extends DataLoaderContextBase {}
|
||||
interface UseDataLoaderColadaResult<TData, TError = ErrorDefault, TDataInitial extends TData | undefined = TData | undefined> extends UseDataLoaderResult<TData | TDataInitial, ErrorDefault>, Pick<UseQueryReturn<TData, TError, TDataInitial>, 'isPending' | 'status' | 'asyncStatus' | 'state'> {
|
||||
/**
|
||||
* Equivalent to `useQuery().refetch()`. Refetches the data no matter if its stale or not.
|
||||
* @see reload - It also calls `refetch()` but returns an empty promise
|
||||
*/
|
||||
refetch: (to?: RouteLocationNormalizedLoaded) => ReturnType<UseQueryReturn<TData, TError, TDataInitial>['refetch']>;
|
||||
/**
|
||||
* Equivalent to `useQuery().refresh()`. Refetches the data **only** if it's stale.
|
||||
*/
|
||||
refresh: (to?: RouteLocationNormalizedLoaded) => ReturnType<UseQueryReturn<TData, TError, TDataInitial>['refetch']>;
|
||||
}
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*/
|
||||
interface UseDataLoaderColada_LaxData<Data> extends UseDataLoader<Data | undefined, ErrorDefault> {
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*
|
||||
* @example
|
||||
* Returns the Data loader data, isLoading, error etc. Meant to be used in `setup()` or `<script setup>` **without `await`**:
|
||||
* ```vue
|
||||
* <script setup>
|
||||
* const { data, isLoading, error } = useUserData()
|
||||
* </script>
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* It also returns a promise of the data when used in nested loaders. Note this `data` is **not a ref**. This is not meant to be used in `setup()` or `<script setup>`.
|
||||
* ```ts
|
||||
* export const useUserConnections = defineLoader(async () => {
|
||||
* const user = await useUserData()
|
||||
* return fetchUserConnections(user.id)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
(): _PromiseMerged<Exclude<Data, NavigationResult | undefined>, UseDataLoaderColadaResult<Exclude<Data, NavigationResult> | undefined>>;
|
||||
}
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*/
|
||||
interface UseDataLoaderColada_DefinedData<TData> extends UseDataLoader<TData, ErrorDefault> {
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*
|
||||
* @example
|
||||
* Returns the Data loader data, isLoading, error etc. Meant to be used in `setup()` or `<script setup>` **without `await`**:
|
||||
* ```vue
|
||||
* <script setup>
|
||||
* const { data, isLoading, error } = useUserData()
|
||||
* </script>
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* It also returns a promise of the data when used in nested loaders. Note this `data` is **not a ref**. This is not meant to be used in `setup()` or `<script setup>`.
|
||||
* ```ts
|
||||
* export const useUserConnections = defineLoader(async () => {
|
||||
* const user = await useUserData()
|
||||
* return fetchUserConnections(user.id)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
(): _PromiseMerged<Exclude<TData, NavigationResult | undefined>, UseDataLoaderColadaResult<Exclude<TData, NavigationResult>, ErrorDefault, Exclude<TData, NavigationResult>>>;
|
||||
}
|
||||
interface DataLoaderColadaEntry<TData, TError = unknown, TDataInitial extends TData | undefined = TData | undefined> extends DataLoaderEntryBase<TData, TError, TDataInitial> {
|
||||
/**
|
||||
* Reactive route passed to pinia colada so it automatically refetch
|
||||
*/
|
||||
route: ShallowRef<RouteLocationNormalizedLoaded>;
|
||||
/**
|
||||
* Tracked routes to know when the data should be refreshed. Key is the key of the query.
|
||||
*/
|
||||
tracked: Map<string, TrackedRoute>;
|
||||
/**
|
||||
* Extended options for pinia colada
|
||||
*/
|
||||
ext: UseQueryReturn<TData, TError, TDataInitial> | null;
|
||||
}
|
||||
interface TrackedRoute {
|
||||
ready: boolean;
|
||||
params: Partial<LocationQuery>;
|
||||
query: Partial<LocationQuery>;
|
||||
hash: {
|
||||
v: string | null;
|
||||
};
|
||||
}
|
||||
//#endregion
|
||||
export { type DataColadaLoaderContext, type DataLoaderColadaEntry, type DefineDataColadaLoaderOptions, type DefineDataColadaLoaderOptions_LaxData, type UseDataLoaderColadaResult, type UseDataLoaderColada_DefinedData, type UseDataLoaderColada_LaxData, defineColadaLoader };
|
||||
150
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.d.mts
generated
vendored
Normal file
150
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.d.mts
generated
vendored
Normal file
@@ -0,0 +1,150 @@
|
||||
import { i as DefineDataLoaderOptionsBase_DefinedData } from "./createDataLoader-C8o8GrdD.mjs";
|
||||
import { LocationQuery, RouteLocationNormalizedLoaded, RouteMap } from "vue-router";
|
||||
import { DataLoaderContextBase, DataLoaderEntryBase, DefineDataLoaderOptionsBase_LaxData, DefineLoaderFn, ErrorDefault, NavigationResult, UseDataLoader, UseDataLoaderResult, _PromiseMerged } from "unplugin-vue-router/data-loaders";
|
||||
import { ShallowRef } from "vue";
|
||||
import { EntryKey, UseQueryOptions, UseQueryReturn } from "@pinia/colada";
|
||||
|
||||
//#region src/data-loaders/defineColadaLoader.d.ts
|
||||
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is always defined.
|
||||
*
|
||||
* @param name - name of the route to have typed routes
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Name extends keyof RouteMap, Data>(name: Name, options: DefineDataColadaLoaderOptions_DefinedData<Name, Data>): UseDataLoaderColada_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is possibly `undefined`.
|
||||
*
|
||||
* @param name - name of the route to have typed routes
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Name extends keyof RouteMap, Data>(name: Name, options: DefineDataColadaLoaderOptions_LaxData<Name, Data>): UseDataLoaderColada_LaxData<Data>;
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is always defined.
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Data>(options: DefineDataColadaLoaderOptions_DefinedData<keyof RouteMap, Data>): UseDataLoaderColada_DefinedData<Data>;
|
||||
/**
|
||||
* Creates a Pinia Colada data loader with `data` is possibly `undefined`.
|
||||
* @param options - options to configure the data loader
|
||||
*/
|
||||
declare function defineColadaLoader<Data>(options: DefineDataColadaLoaderOptions_LaxData<keyof RouteMap, Data>): UseDataLoaderColada_LaxData<Data>;
|
||||
/**
|
||||
* Base type with docs for the options of `defineColadaLoader`.
|
||||
* @internal
|
||||
*/
|
||||
interface _DefineDataColadaLoaderOptions_Common<Name extends keyof RouteMap, Data> extends Omit<UseQueryOptions<Data, ErrorDefault, Data>, 'query' | 'key'> {
|
||||
/**
|
||||
* Key associated with the data and passed to pinia colada
|
||||
* @param to - Route to load the data
|
||||
*/
|
||||
key: EntryKey | ((to: RouteLocationNormalizedLoaded<Name>) => EntryKey);
|
||||
/**
|
||||
* Function that returns a promise with the data.
|
||||
*/
|
||||
query: DefineLoaderFn<Data, DataColadaLoaderContext, RouteLocationNormalizedLoaded<Name>>;
|
||||
}
|
||||
/**
|
||||
* Options for `defineColadaLoader` when the data is possibly `undefined`.
|
||||
*/
|
||||
interface DefineDataColadaLoaderOptions_LaxData<Name extends keyof RouteMap, Data> extends _DefineDataColadaLoaderOptions_Common<Name, Data>, DefineDataLoaderOptionsBase_LaxData {}
|
||||
/**
|
||||
* Options for `defineColadaLoader` when the data is always defined.
|
||||
*/
|
||||
interface DefineDataColadaLoaderOptions_DefinedData<Name extends keyof RouteMap, Data> extends _DefineDataColadaLoaderOptions_Common<Name, Data>, DefineDataLoaderOptionsBase_DefinedData {}
|
||||
/**
|
||||
* @deprecated Use {@link `DefineDataColadaLoaderOptions_LaxData`} instead.
|
||||
*/
|
||||
type DefineDataColadaLoaderOptions<Name extends keyof RouteMap, Data> = DefineDataColadaLoaderOptions_LaxData<Name, Data>;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface DataColadaLoaderContext extends DataLoaderContextBase {}
|
||||
interface UseDataLoaderColadaResult<TData, TError = ErrorDefault, TDataInitial extends TData | undefined = TData | undefined> extends UseDataLoaderResult<TData | TDataInitial, ErrorDefault>, Pick<UseQueryReturn<TData, TError, TDataInitial>, 'isPending' | 'status' | 'asyncStatus' | 'state'> {
|
||||
/**
|
||||
* Equivalent to `useQuery().refetch()`. Refetches the data no matter if its stale or not.
|
||||
* @see reload - It also calls `refetch()` but returns an empty promise
|
||||
*/
|
||||
refetch: (to?: RouteLocationNormalizedLoaded) => ReturnType<UseQueryReturn<TData, TError, TDataInitial>['refetch']>;
|
||||
/**
|
||||
* Equivalent to `useQuery().refresh()`. Refetches the data **only** if it's stale.
|
||||
*/
|
||||
refresh: (to?: RouteLocationNormalizedLoaded) => ReturnType<UseQueryReturn<TData, TError, TDataInitial>['refetch']>;
|
||||
}
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*/
|
||||
interface UseDataLoaderColada_LaxData<Data> extends UseDataLoader<Data | undefined, ErrorDefault> {
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*
|
||||
* @example
|
||||
* Returns the Data loader data, isLoading, error etc. Meant to be used in `setup()` or `<script setup>` **without `await`**:
|
||||
* ```vue
|
||||
* <script setup>
|
||||
* const { data, isLoading, error } = useUserData()
|
||||
* </script>
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* It also returns a promise of the data when used in nested loaders. Note this `data` is **not a ref**. This is not meant to be used in `setup()` or `<script setup>`.
|
||||
* ```ts
|
||||
* export const useUserConnections = defineLoader(async () => {
|
||||
* const user = await useUserData()
|
||||
* return fetchUserConnections(user.id)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
(): _PromiseMerged<Exclude<Data, NavigationResult | undefined>, UseDataLoaderColadaResult<Exclude<Data, NavigationResult> | undefined>>;
|
||||
}
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*/
|
||||
interface UseDataLoaderColada_DefinedData<TData> extends UseDataLoader<TData, ErrorDefault> {
|
||||
/**
|
||||
* Data Loader composable returned by `defineColadaLoader()`.
|
||||
*
|
||||
* @example
|
||||
* Returns the Data loader data, isLoading, error etc. Meant to be used in `setup()` or `<script setup>` **without `await`**:
|
||||
* ```vue
|
||||
* <script setup>
|
||||
* const { data, isLoading, error } = useUserData()
|
||||
* </script>
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* It also returns a promise of the data when used in nested loaders. Note this `data` is **not a ref**. This is not meant to be used in `setup()` or `<script setup>`.
|
||||
* ```ts
|
||||
* export const useUserConnections = defineLoader(async () => {
|
||||
* const user = await useUserData()
|
||||
* return fetchUserConnections(user.id)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
(): _PromiseMerged<Exclude<TData, NavigationResult | undefined>, UseDataLoaderColadaResult<Exclude<TData, NavigationResult>, ErrorDefault, Exclude<TData, NavigationResult>>>;
|
||||
}
|
||||
interface DataLoaderColadaEntry<TData, TError = unknown, TDataInitial extends TData | undefined = TData | undefined> extends DataLoaderEntryBase<TData, TError, TDataInitial> {
|
||||
/**
|
||||
* Reactive route passed to pinia colada so it automatically refetch
|
||||
*/
|
||||
route: ShallowRef<RouteLocationNormalizedLoaded>;
|
||||
/**
|
||||
* Tracked routes to know when the data should be refreshed. Key is the key of the query.
|
||||
*/
|
||||
tracked: Map<string, TrackedRoute>;
|
||||
/**
|
||||
* Extended options for pinia colada
|
||||
*/
|
||||
ext: UseQueryReturn<TData, TError, TDataInitial> | null;
|
||||
}
|
||||
interface TrackedRoute {
|
||||
ready: boolean;
|
||||
params: Partial<LocationQuery>;
|
||||
query: Partial<LocationQuery>;
|
||||
hash: {
|
||||
v: string | null;
|
||||
};
|
||||
}
|
||||
//#endregion
|
||||
export { type DataColadaLoaderContext, type DataLoaderColadaEntry, type DefineDataColadaLoaderOptions, type DefineDataColadaLoaderOptions_LaxData, type UseDataLoaderColadaResult, type UseDataLoaderColada_DefinedData, type UseDataLoaderColada_LaxData, defineColadaLoader };
|
||||
229
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.mjs
generated
vendored
Normal file
229
node_modules/unplugin-vue-router/dist/data-loaders/pinia-colada.mjs
generated
vendored
Normal file
@@ -0,0 +1,229 @@
|
||||
import { t as toLazyValue } from "./createDataLoader-BK9Gdnky.mjs";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { ABORT_CONTROLLER_KEY, APP_KEY, DATA_LOADERS_EFFECT_SCOPE_KEY, IS_SSR_KEY, IS_USE_DATA_LOADER_KEY, LOADER_ENTRIES_KEY, NAVIGATION_RESULTS_KEY, NavigationResult, PENDING_LOCATION_KEY, STAGED_NO_VALUE, assign, getCurrentContext, isSubsetOf, setCurrentContext, trackRoute } from "unplugin-vue-router/data-loaders";
|
||||
import { shallowRef, watch } from "vue";
|
||||
import { defineQuery, useQuery, useQueryCache } from "@pinia/colada";
|
||||
|
||||
//#region src/data-loaders/defineColadaLoader.ts
|
||||
function defineColadaLoader(nameOrOptions, _options) {
|
||||
_options = _options || nameOrOptions;
|
||||
const loader = _options.query;
|
||||
const options = {
|
||||
...DEFAULT_DEFINE_LOADER_OPTIONS,
|
||||
..._options,
|
||||
commit: _options?.commit || "after-load"
|
||||
};
|
||||
let isInitial = true;
|
||||
const useDefinedQuery = defineQuery(() => {
|
||||
const router = useRouter();
|
||||
const entry = router[LOADER_ENTRIES_KEY].get(loader);
|
||||
return useQuery({
|
||||
...options,
|
||||
query: () => {
|
||||
const route = entry.route.value;
|
||||
const [trackedRoute, params, query, hash] = trackRoute(route);
|
||||
entry.tracked.set(joinKeys(serializeQueryKey(options.key, trackedRoute)), {
|
||||
ready: false,
|
||||
params,
|
||||
query,
|
||||
hash
|
||||
});
|
||||
return router[APP_KEY].runWithContext(() => loader(trackedRoute, { signal: route.meta[ABORT_CONTROLLER_KEY]?.signal }));
|
||||
},
|
||||
key: () => toValueWithParameters(options.key, entry.route.value)
|
||||
});
|
||||
});
|
||||
function load(to, router, from, parent, reload) {
|
||||
const entries = router[LOADER_ENTRIES_KEY];
|
||||
const isSSR = router[IS_SSR_KEY];
|
||||
const key = serializeQueryKey(options.key, to);
|
||||
if (!entries.has(loader)) {
|
||||
const route = shallowRef(to);
|
||||
entries.set(loader, {
|
||||
data: shallowRef(),
|
||||
isLoading: shallowRef(false),
|
||||
error: shallowRef(null),
|
||||
to,
|
||||
options,
|
||||
children: /* @__PURE__ */ new Set(),
|
||||
resetPending() {
|
||||
this.pendingTo = null;
|
||||
this.pendingLoad = null;
|
||||
this.isLoading.value = false;
|
||||
},
|
||||
staged: STAGED_NO_VALUE,
|
||||
stagedError: null,
|
||||
stagedNavigationResult: null,
|
||||
commit,
|
||||
tracked: /* @__PURE__ */ new Map(),
|
||||
ext: null,
|
||||
route,
|
||||
pendingTo: null,
|
||||
pendingLoad: null
|
||||
});
|
||||
}
|
||||
const entry = entries.get(loader);
|
||||
if (entry.pendingTo === to && entry.pendingLoad) return entry.pendingLoad;
|
||||
const currentContext = getCurrentContext();
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (parent !== currentContext[0]) console.warn(`❌👶 "${key}" has a different parent than the current context. This shouldn't be happening. Please report a bug with a reproduction to https://github.com/posva/unplugin-vue-router/`);
|
||||
}
|
||||
setCurrentContext([
|
||||
entry,
|
||||
router,
|
||||
to
|
||||
]);
|
||||
if (!entry.ext) {
|
||||
entry.ext = useDefinedQuery();
|
||||
useQueryCache().get(toValueWithParameters(options.key, to))?.deps.delete(router[DATA_LOADERS_EFFECT_SCOPE_KEY]);
|
||||
reload = false;
|
||||
}
|
||||
const { isLoading, data, error, ext } = entry;
|
||||
if (isInitial) {
|
||||
isInitial = false;
|
||||
if (ext.data.value !== void 0) {
|
||||
data.value = ext.data.value;
|
||||
setCurrentContext(currentContext);
|
||||
return entry.pendingLoad = Promise.resolve();
|
||||
}
|
||||
}
|
||||
if (entry.route.value !== to) {
|
||||
const tracked = entry.tracked.get(joinKeys(key));
|
||||
reload = !tracked || hasRouteChanged(to, tracked);
|
||||
}
|
||||
entry.route.value = entry.pendingTo = to;
|
||||
isLoading.value = true;
|
||||
entry.staged = STAGED_NO_VALUE;
|
||||
entry.stagedError = error.value;
|
||||
entry.stagedNavigationResult = null;
|
||||
const currentLoad = ext[reload ? "refetch" : "refresh"]().then(() => {
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
const newError = ext.error.value;
|
||||
if (newError) {
|
||||
entry.stagedError = newError;
|
||||
if (!toLazyValue(options.lazy, to, from) || isSSR) throw newError;
|
||||
} else {
|
||||
const newData = ext.data.value;
|
||||
if (newData instanceof NavigationResult) {
|
||||
to.meta[NAVIGATION_RESULTS_KEY].push(newData);
|
||||
entry.stagedNavigationResult = newData;
|
||||
} else {
|
||||
entry.staged = newData;
|
||||
entry.stagedError = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}).finally(() => {
|
||||
setCurrentContext(currentContext);
|
||||
if (entry.pendingLoad === currentLoad) {
|
||||
isLoading.value = false;
|
||||
if (options.commit === "immediate" || !router[PENDING_LOCATION_KEY]) entry.commit(to);
|
||||
}
|
||||
});
|
||||
setCurrentContext(currentContext);
|
||||
entry.pendingLoad = currentLoad;
|
||||
return currentLoad;
|
||||
}
|
||||
function commit(to) {
|
||||
const key = serializeQueryKey(options.key, to);
|
||||
if (this.pendingTo === to) {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
if (this.staged === STAGED_NO_VALUE && this.stagedError === null && this.stagedNavigationResult === null) console.warn(`Loader "${key}"'s "commit()" was called but there is no staged data.`);
|
||||
}
|
||||
if (this.staged !== STAGED_NO_VALUE) {
|
||||
this.data.value = this.staged;
|
||||
if (process.env.NODE_ENV !== "production" && !this.tracked.has(joinKeys(key))) {
|
||||
console.warn(`A query was defined with the same key as the loader "[${key.join(", ")}]". If the "key" is meant to be the same, you should directly use the data loader instead. If not, change the key of the "useQuery()".\nSee https://pinia-colada.esm.dev/#TODO`);
|
||||
return;
|
||||
}
|
||||
this.tracked.get(joinKeys(key)).ready = true;
|
||||
}
|
||||
this.error.value = this.stagedError;
|
||||
this.staged = STAGED_NO_VALUE;
|
||||
this.stagedError = this.error.value;
|
||||
this.to = to;
|
||||
this.pendingTo = null;
|
||||
for (const childEntry of this.children) childEntry.commit(to);
|
||||
}
|
||||
}
|
||||
const useDataLoader = () => {
|
||||
const currentEntry = getCurrentContext();
|
||||
const [parentEntry, _router, _route] = currentEntry;
|
||||
const router = _router || useRouter();
|
||||
const route = _route || useRoute();
|
||||
const app = router[APP_KEY];
|
||||
const entries = router[LOADER_ENTRIES_KEY];
|
||||
let entry = entries.get(loader);
|
||||
if (!entry || parentEntry && entry.pendingTo !== route || !entry.pendingLoad) app.runWithContext(() => load(route, router, void 0, parentEntry, true));
|
||||
entry = entries.get(loader);
|
||||
if (parentEntry) {
|
||||
if (parentEntry !== entry) parentEntry.children.add(entry);
|
||||
}
|
||||
const { data, error, isLoading } = entry;
|
||||
const ext = useDefinedQuery();
|
||||
useQueryCache().get(toValueWithParameters(options.key, route))?.deps.delete(router[DATA_LOADERS_EFFECT_SCOPE_KEY]);
|
||||
watch(ext.data, (newData) => {
|
||||
if (!router[PENDING_LOCATION_KEY]) data.value = newData;
|
||||
});
|
||||
watch(ext.isLoading, (isFetching) => {
|
||||
if (!router[PENDING_LOCATION_KEY]) isLoading.value = isFetching;
|
||||
});
|
||||
watch(ext.error, (newError) => {
|
||||
if (!router[PENDING_LOCATION_KEY]) error.value = newError;
|
||||
});
|
||||
const useDataLoaderResult = {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
reload: (to = router.currentRoute.value) => app.runWithContext(() => load(to, router, void 0, void 0, true)).then(() => entry.commit(to)),
|
||||
refetch: (to = router.currentRoute.value) => app.runWithContext(() => load(to, router, void 0, void 0, true)).then(() => (entry.commit(to), entry.ext.state.value)),
|
||||
refresh: (to = router.currentRoute.value) => app.runWithContext(() => load(to, router)).then(() => (entry.commit(to), entry.ext.state.value)),
|
||||
status: ext.status,
|
||||
asyncStatus: ext.asyncStatus,
|
||||
state: ext.state,
|
||||
isPending: ext.isPending
|
||||
};
|
||||
const promise = entry.pendingLoad.then(() => {
|
||||
return entry.staged === STAGED_NO_VALUE ? entry.stagedNavigationResult ? Promise.reject(entry.stagedNavigationResult) : ext.data.value : entry.staged;
|
||||
}).catch((e) => parentEntry ? Promise.reject(e) : null);
|
||||
setCurrentContext(currentEntry);
|
||||
return assign(promise, useDataLoaderResult);
|
||||
};
|
||||
useDataLoader[IS_USE_DATA_LOADER_KEY] = true;
|
||||
useDataLoader._ = {
|
||||
load,
|
||||
options,
|
||||
getEntry(router) {
|
||||
return router[LOADER_ENTRIES_KEY].get(loader);
|
||||
}
|
||||
};
|
||||
return useDataLoader;
|
||||
}
|
||||
const joinKeys = (keys) => keys.join("|");
|
||||
function hasRouteChanged(to, tracked) {
|
||||
return !tracked.ready || !isSubsetOf(tracked.params, to.params) || !isSubsetOf(tracked.query, to.query) || tracked.hash.v != null && tracked.hash.v !== to.hash;
|
||||
}
|
||||
const DEFAULT_DEFINE_LOADER_OPTIONS = {
|
||||
lazy: false,
|
||||
server: true,
|
||||
commit: "after-load"
|
||||
};
|
||||
const toValueWithParameters = (optionValue, arg) => {
|
||||
return typeof optionValue === "function" ? optionValue(arg) : optionValue;
|
||||
};
|
||||
/**
|
||||
* Transform the key to a string array so it can be used as a key in caches.
|
||||
*
|
||||
* @param key - key to transform
|
||||
* @param to - route to use
|
||||
*/
|
||||
function serializeQueryKey(keyOption, to) {
|
||||
const key = toValueWithParameters(keyOption, to);
|
||||
return (Array.isArray(key) ? key : [key]).map(stringifyFlatObject);
|
||||
}
|
||||
function stringifyFlatObject(obj) {
|
||||
return obj && typeof obj === "object" ? JSON.stringify(obj, Object.keys(obj).sort()) : String(obj);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
export { defineColadaLoader };
|
||||
147
node_modules/unplugin-vue-router/dist/data-loaders/utils-C5t8q_dT.mjs
generated
vendored
Normal file
147
node_modules/unplugin-vue-router/dist/data-loaders/utils-C5t8q_dT.mjs
generated
vendored
Normal file
@@ -0,0 +1,147 @@
|
||||
//#region src/data-loaders/symbols.ts
|
||||
/**
|
||||
* Retrieves the internal version of loaders.
|
||||
* @internal
|
||||
*/
|
||||
const LOADER_SET_KEY = Symbol("loaders");
|
||||
/**
|
||||
* Retrieves the internal version of loader entries.
|
||||
* @internal
|
||||
*/
|
||||
const LOADER_ENTRIES_KEY = Symbol("loaderEntries");
|
||||
/**
|
||||
* Added to the loaders returned by `defineLoader()` to identify them.
|
||||
* Allows to extract exported useData() from a component.
|
||||
* @internal
|
||||
*/
|
||||
const IS_USE_DATA_LOADER_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to save the pending location on the router.
|
||||
* @internal
|
||||
*/
|
||||
const PENDING_LOCATION_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to know there is no value staged for the loader and that commit should be skipped.
|
||||
* @internal
|
||||
*/
|
||||
const STAGED_NO_VALUE = Symbol();
|
||||
/**
|
||||
* Gives access to the current app and it's `runWithContext` method.
|
||||
* @internal
|
||||
*/
|
||||
const APP_KEY = Symbol();
|
||||
/**
|
||||
* Gives access to an AbortController that aborts when the navigation is canceled.
|
||||
* @internal
|
||||
*/
|
||||
const ABORT_CONTROLLER_KEY = Symbol();
|
||||
/**
|
||||
* Gives access to the navigation results when the navigation is aborted by the user within a data loader.
|
||||
* @internal
|
||||
*/
|
||||
const NAVIGATION_RESULTS_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to save the initial data on the router.
|
||||
* @internal
|
||||
*/
|
||||
const IS_SSR_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to get the effect scope used for data loaders.
|
||||
* @internal
|
||||
*/
|
||||
const DATA_LOADERS_EFFECT_SCOPE_KEY = Symbol();
|
||||
|
||||
//#endregion
|
||||
//#region src/data-loaders/utils.ts
|
||||
/**
|
||||
* Check if a value is a `DataLoader`.
|
||||
*
|
||||
* @param loader - the object to check
|
||||
*/
|
||||
function isDataLoader(loader) {
|
||||
return loader && loader[IS_USE_DATA_LOADER_KEY];
|
||||
}
|
||||
/**
|
||||
* @internal: data loaders authoring only. Use `getCurrentContext` instead.
|
||||
*/
|
||||
let currentContext;
|
||||
function getCurrentContext() {
|
||||
return currentContext || [];
|
||||
}
|
||||
/**
|
||||
* Sets the current context for data loaders. This allows for nested loaders to be aware of their parent context.
|
||||
* INTERNAL ONLY.
|
||||
*
|
||||
* @param context - the context to set
|
||||
* @internal
|
||||
*/
|
||||
function setCurrentContext(context) {
|
||||
currentContext = context ? context.length ? context : null : null;
|
||||
}
|
||||
/**
|
||||
* Restore the current context after a promise is resolved.
|
||||
* @param promise - promise to wrap
|
||||
*/
|
||||
function withLoaderContext(promise) {
|
||||
const context = currentContext;
|
||||
return promise.finally(() => currentContext = context);
|
||||
}
|
||||
const assign = Object.assign;
|
||||
/**
|
||||
* Track the reads of a route and its properties
|
||||
* @internal
|
||||
* @param route - route to track
|
||||
*/
|
||||
function trackRoute(route) {
|
||||
const [params, paramReads] = trackObjectReads(route.params);
|
||||
const [query, queryReads] = trackObjectReads(route.query);
|
||||
let hash = { v: null };
|
||||
return [
|
||||
{
|
||||
...route,
|
||||
get hash() {
|
||||
return hash.v = route.hash;
|
||||
},
|
||||
params,
|
||||
query
|
||||
},
|
||||
paramReads,
|
||||
queryReads,
|
||||
hash
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Track the reads of an object (that doesn't change) and add the read properties to an object
|
||||
* @internal
|
||||
* @param obj - object to track
|
||||
*/
|
||||
function trackObjectReads(obj) {
|
||||
const reads = {};
|
||||
return [new Proxy(obj, { get(target, p, receiver) {
|
||||
const value = Reflect.get(target, p, receiver);
|
||||
reads[p] = value;
|
||||
return value;
|
||||
} }), reads];
|
||||
}
|
||||
/**
|
||||
* Returns `true` if `inner` is a subset of `outer`. Used to check if a tr
|
||||
*
|
||||
* @internal
|
||||
* @param outer - the bigger params
|
||||
* @param inner - the smaller params
|
||||
*/
|
||||
function isSubsetOf(inner, outer) {
|
||||
for (const key in inner) {
|
||||
const innerValue = inner[key];
|
||||
const outerValue = outer[key];
|
||||
if (typeof innerValue === "string") {
|
||||
if (innerValue !== outerValue) return false;
|
||||
} else if (!innerValue || !outerValue) {
|
||||
if (innerValue !== outerValue) return false;
|
||||
} else if (!Array.isArray(outerValue) || outerValue.length !== innerValue.length || innerValue.some((value, i) => value !== outerValue[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
export { PENDING_LOCATION_KEY as _, isSubsetOf as a, withLoaderContext as c, DATA_LOADERS_EFFECT_SCOPE_KEY as d, IS_SSR_KEY as f, NAVIGATION_RESULTS_KEY as g, LOADER_SET_KEY as h, isDataLoader as i, ABORT_CONTROLLER_KEY as l, LOADER_ENTRIES_KEY as m, currentContext as n, setCurrentContext as o, IS_USE_DATA_LOADER_KEY as p, getCurrentContext as r, trackRoute as s, assign as t, APP_KEY as u, STAGED_NO_VALUE as v };
|
||||
255
node_modules/unplugin-vue-router/dist/data-loaders/utils-CL409u9D.cjs
generated
vendored
Normal file
255
node_modules/unplugin-vue-router/dist/data-loaders/utils-CL409u9D.cjs
generated
vendored
Normal file
@@ -0,0 +1,255 @@
|
||||
|
||||
//#region src/data-loaders/symbols.ts
|
||||
/**
|
||||
* Retrieves the internal version of loaders.
|
||||
* @internal
|
||||
*/
|
||||
const LOADER_SET_KEY = Symbol("loaders");
|
||||
/**
|
||||
* Retrieves the internal version of loader entries.
|
||||
* @internal
|
||||
*/
|
||||
const LOADER_ENTRIES_KEY = Symbol("loaderEntries");
|
||||
/**
|
||||
* Added to the loaders returned by `defineLoader()` to identify them.
|
||||
* Allows to extract exported useData() from a component.
|
||||
* @internal
|
||||
*/
|
||||
const IS_USE_DATA_LOADER_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to save the pending location on the router.
|
||||
* @internal
|
||||
*/
|
||||
const PENDING_LOCATION_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to know there is no value staged for the loader and that commit should be skipped.
|
||||
* @internal
|
||||
*/
|
||||
const STAGED_NO_VALUE = Symbol();
|
||||
/**
|
||||
* Gives access to the current app and it's `runWithContext` method.
|
||||
* @internal
|
||||
*/
|
||||
const APP_KEY = Symbol();
|
||||
/**
|
||||
* Gives access to an AbortController that aborts when the navigation is canceled.
|
||||
* @internal
|
||||
*/
|
||||
const ABORT_CONTROLLER_KEY = Symbol();
|
||||
/**
|
||||
* Gives access to the navigation results when the navigation is aborted by the user within a data loader.
|
||||
* @internal
|
||||
*/
|
||||
const NAVIGATION_RESULTS_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to save the initial data on the router.
|
||||
* @internal
|
||||
*/
|
||||
const IS_SSR_KEY = Symbol();
|
||||
/**
|
||||
* Symbol used to get the effect scope used for data loaders.
|
||||
* @internal
|
||||
*/
|
||||
const DATA_LOADERS_EFFECT_SCOPE_KEY = Symbol();
|
||||
|
||||
//#endregion
|
||||
//#region src/data-loaders/utils.ts
|
||||
/**
|
||||
* Check if a value is a `DataLoader`.
|
||||
*
|
||||
* @param loader - the object to check
|
||||
*/
|
||||
function isDataLoader(loader) {
|
||||
return loader && loader[IS_USE_DATA_LOADER_KEY];
|
||||
}
|
||||
/**
|
||||
* @internal: data loaders authoring only. Use `getCurrentContext` instead.
|
||||
*/
|
||||
let currentContext;
|
||||
function getCurrentContext() {
|
||||
return currentContext || [];
|
||||
}
|
||||
/**
|
||||
* Sets the current context for data loaders. This allows for nested loaders to be aware of their parent context.
|
||||
* INTERNAL ONLY.
|
||||
*
|
||||
* @param context - the context to set
|
||||
* @internal
|
||||
*/
|
||||
function setCurrentContext(context) {
|
||||
currentContext = context ? context.length ? context : null : null;
|
||||
}
|
||||
/**
|
||||
* Restore the current context after a promise is resolved.
|
||||
* @param promise - promise to wrap
|
||||
*/
|
||||
function withLoaderContext(promise) {
|
||||
const context = currentContext;
|
||||
return promise.finally(() => currentContext = context);
|
||||
}
|
||||
const assign = Object.assign;
|
||||
/**
|
||||
* Track the reads of a route and its properties
|
||||
* @internal
|
||||
* @param route - route to track
|
||||
*/
|
||||
function trackRoute(route) {
|
||||
const [params, paramReads] = trackObjectReads(route.params);
|
||||
const [query, queryReads] = trackObjectReads(route.query);
|
||||
let hash = { v: null };
|
||||
return [
|
||||
{
|
||||
...route,
|
||||
get hash() {
|
||||
return hash.v = route.hash;
|
||||
},
|
||||
params,
|
||||
query
|
||||
},
|
||||
paramReads,
|
||||
queryReads,
|
||||
hash
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Track the reads of an object (that doesn't change) and add the read properties to an object
|
||||
* @internal
|
||||
* @param obj - object to track
|
||||
*/
|
||||
function trackObjectReads(obj) {
|
||||
const reads = {};
|
||||
return [new Proxy(obj, { get(target, p, receiver) {
|
||||
const value = Reflect.get(target, p, receiver);
|
||||
reads[p] = value;
|
||||
return value;
|
||||
} }), reads];
|
||||
}
|
||||
/**
|
||||
* Returns `true` if `inner` is a subset of `outer`. Used to check if a tr
|
||||
*
|
||||
* @internal
|
||||
* @param outer - the bigger params
|
||||
* @param inner - the smaller params
|
||||
*/
|
||||
function isSubsetOf(inner, outer) {
|
||||
for (const key in inner) {
|
||||
const innerValue = inner[key];
|
||||
const outerValue = outer[key];
|
||||
if (typeof innerValue === "string") {
|
||||
if (innerValue !== outerValue) return false;
|
||||
} else if (!innerValue || !outerValue) {
|
||||
if (innerValue !== outerValue) return false;
|
||||
} else if (!Array.isArray(outerValue) || outerValue.length !== innerValue.length || innerValue.some((value, i) => value !== outerValue[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
Object.defineProperty(exports, 'ABORT_CONTROLLER_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return ABORT_CONTROLLER_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'APP_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return APP_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'DATA_LOADERS_EFFECT_SCOPE_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return DATA_LOADERS_EFFECT_SCOPE_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'IS_SSR_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return IS_SSR_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'IS_USE_DATA_LOADER_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return IS_USE_DATA_LOADER_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'LOADER_ENTRIES_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return LOADER_ENTRIES_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'LOADER_SET_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return LOADER_SET_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'NAVIGATION_RESULTS_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return NAVIGATION_RESULTS_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'PENDING_LOCATION_KEY', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return PENDING_LOCATION_KEY;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'STAGED_NO_VALUE', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return STAGED_NO_VALUE;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'assign', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return assign;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'currentContext', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return currentContext;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'getCurrentContext', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return getCurrentContext;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'isDataLoader', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return isDataLoader;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'isSubsetOf', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return isSubsetOf;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'setCurrentContext', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return setCurrentContext;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'trackRoute', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return trackRoute;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, 'withLoaderContext', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return withLoaderContext;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user