Skip to content

Commit

Permalink
fix(data-loaders): fix types references
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Feb 22, 2024
1 parent 9cad5b0 commit 6558fa8
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 165 deletions.
6 changes: 0 additions & 6 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import type { RouteRecordOverride, TreeRouteParam } from './treeNodeValue'
import { pascalCase } from 'scule'
import { ResolvedOptions, RoutesFolderOption } from '../options'

/**
* Maybe a promise maybe not
* @internal
*/
export type _Awaitable<T> = T | PromiseLike<T>

export type LiteralStringUnion<LiteralType, BaseType extends string = string> =
| LiteralType
| (BaseType & Record<never, never>)
Expand Down
38 changes: 5 additions & 33 deletions src/data-fetching/createDataLoader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { TypeEqual, expectType } from 'ts-expect'
import { type ShallowRef, effectScope, shallowRef } from 'vue'
import { IS_USE_DATA_LOADER_KEY, STAGED_NO_VALUE } from './meta-extensions'
import { _Awaitable } from '../core/utils'
import { _PromiseMerged } from './utils'
import { NavigationResult } from './navigation-guard'
import { _RouteLocationNormalizedLoaded } from '../type-extensions/routeLocation'
import { _Router } from '../type-extensions/router'
import { type _PromiseMerged } from './utils'
import { type NavigationResult } from './navigation-guard'
import { type _RouteLocationNormalizedLoaded } from '../type-extensions/routeLocation'
import { type _Router } from '../type-extensions/router'
import { type _Awaitable } from '../utils'

/**
* Base type for a data loader entry. Each Data Loader has its own entry in the `loaderEntries` (accessible via `[LOADER_ENTRIES_KEY]`) map.
Expand Down Expand Up @@ -314,33 +313,6 @@ export interface UseDataLoaderResult<
reload(route: _RouteLocationNormalizedLoaded): Promise<void>
}

function _testing() {
const defineBasicLoader = createDataLoader<DataLoaderContextBase>({
before: (context) => {
// do nothing, always reexecute
return context
},
// no caching or anything
after: (data) => {},
})

const useUserData = defineBasicLoader(
async (context) => {
return {
user: {
name: 'Eduardo',
},
}
},
{
// lazy: true
}
)

const { data } = useUserData()
expectType<TypeEqual<{ user: { name: string } }, typeof data.value>>(true)
}

/**
* Loader function that can be passed to `defineLoader()`.
*/
Expand Down
27 changes: 17 additions & 10 deletions src/data-fetching/defineColadaLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,34 @@ export function defineColadaLoader<
isLazy extends boolean,
>(
name: Name,
options: DefineDataLoaderOptions<isLazy, Name, Data>
options: DefineDataColadaLoaderOptions<isLazy, Name, Data>
): UseDataLoaderColada<isLazy, Data>
export function defineColadaLoader<Data, isLazy extends boolean>(
options: DefineDataLoaderOptions<isLazy, _RouteRecordName, Data>
options: DefineDataColadaLoaderOptions<isLazy, _RouteRecordName, Data>
): UseDataLoaderColada<isLazy, Data>

export function defineColadaLoader<Data, isLazy extends boolean>(
nameOrOptions:
| _RouteRecordName
| DefineDataLoaderOptions<isLazy, _RouteRecordName, Data>,
_options?: DefineDataLoaderOptions<isLazy, _RouteRecordName, Data>
| DefineDataColadaLoaderOptions<isLazy, _RouteRecordName, Data>,
_options?: DefineDataColadaLoaderOptions<isLazy, _RouteRecordName, Data>
): UseDataLoaderColada<isLazy, Data> {
// TODO: make it DEV only and remove the first argument in production mode
// resolve option overrides
_options =
_options ||
(nameOrOptions as DefineDataLoaderOptions<isLazy, _RouteRecordName, Data>)
(nameOrOptions as DefineDataColadaLoaderOptions<
isLazy,
_RouteRecordName,
Data
>)
const loader = _options.query

const options = {
...DEFAULT_DEFINE_LOADER_OPTIONS,
..._options,
commit: _options?.commit || 'after-load',
} as DefineDataLoaderOptions<isLazy, _RouteRecordName, Data>
} as DefineDataColadaLoaderOptions<isLazy, _RouteRecordName, Data>

let isInitial = true

Expand Down Expand Up @@ -449,7 +453,7 @@ export function defineColadaLoader<Data, isLazy extends boolean>(
return useDataLoader
}

export interface DefineDataLoaderOptions<
export interface DefineDataColadaLoaderOptions<
isLazy extends boolean,
Name extends _RouteRecordName,
Data,
Expand All @@ -466,14 +470,17 @@ export interface DefineDataLoaderOptions<
*/
query: DefineLoaderFn<
Data,
DataLoaderContext,
DataColadaLoaderContext,
_RouteLocationNormalizedLoaded<Name>
>

// TODO: option to skip refresh if the used properties of the route haven't changed
}

export interface DataLoaderContext extends DataLoaderContextBase {}
/**
* @internal
*/
export interface DataColadaLoaderContext extends DataLoaderContextBase {}

export interface UseDataLoaderColadaResult<isLazy extends boolean, Data>
extends UseDataLoaderResult<isLazy, Data>,
Expand Down Expand Up @@ -559,7 +566,7 @@ const DEFAULT_DEFINE_LOADER_OPTIONS = {
server: true,
commit: 'after-load',
} satisfies Omit<
DefineDataLoaderOptions<boolean, _RouteRecordName, unknown>,
DefineDataColadaLoaderOptions<boolean, _RouteRecordName, unknown>,
'key' | 'query'
>

Expand Down
1 change: 1 addition & 0 deletions src/data-fetching/defineQueryLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe.skip('defineQueryLoader', () => {
// dts testing
function dts(_fn: () => unknown) {}

// FIXME: move to a test-d.ts file
dts(async () => {
interface UserData {
id: string
Expand Down
2 changes: 1 addition & 1 deletion src/data-fetching/navigation-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
PENDING_LOCATION_KEY,
} from './meta-extensions'
import { IS_CLIENT, assign, isDataLoader, setCurrentContext } from './utils'
import type { _Awaitable } from '../core/utils'
import type { _RouteLocationNormalizedLoaded } from '../type-extensions/routeLocation'
import type { _Router } from '../type-extensions/router'
import { type _Awaitable } from '../utils'

/**
* TODO: export functions that allow preloading outside of a navigation guard
Expand Down
5 changes: 0 additions & 5 deletions src/data-fetching/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ export const IS_CLIENT = typeof window !== 'undefined'

export const assign = Object.assign

/**
* @internal
*/
export type _MaybePromise<T> = T | Promise<T>

/**
* Track the reads of a route and its properties
* @internal
Expand Down
3 changes: 2 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { isPackageExists as isPackageInstalled } from 'local-pkg'
import { _Awaitable, getFileBasedRouteName, isArray, warn } from './core/utils'
import { getFileBasedRouteName, isArray, warn } from './core/utils'
import type { TreeNode } from './core/tree'
import { resolve } from 'pathe'
import { EditableTreeNode } from './core/extendRoutes'
import { type ParseSegmentOptions } from './core/treeNodeValue'
import { type _Awaitable } from './utils'

/**
* Options for a routes folder.
Expand Down
31 changes: 29 additions & 2 deletions src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { type RouteRecordRaw } from 'vue-router'
import { type _Router } from './type-extensions/router'

export type {
UseDataLoader,
UseDataLoaderInternals,
UseDataLoaderResult,
DataLoaderContextBase,
DataLoaderEntryBase,
DefineDataLoaderOptionsBase,
DefineLoaderFn,
_DataMaybeLazy,
} from './data-fetching/createDataLoader'

// new data fetching
export { defineBasicLoader } from './data-fetching/defineLoader'
export type {
DataLoaderContext,
DefineDataLoaderOptions,
} from './data-fetching/defineLoader'
export {
DataLoaderPlugin,
NavigationResult,
} from './data-fetching/navigation-guard'
export type { DataLoaderPluginOptions } from './data-fetching/navigation-guard'
export type {
DataLoaderPluginOptions,
SetupLoaderGuardOptions,
_DataLoaderRedirectResult,
} from './data-fetching/navigation-guard'

export type {
DataLoaderColadaEntry,
DataColadaLoaderContext,
DefineDataColadaLoaderOptions,
UseDataLoaderColada,
UseDataLoaderColadaResult,
} from './data-fetching/defineColadaLoader'
export { defineColadaLoader } from './data-fetching/defineColadaLoader'

// NOTE: for tests only
// export * from './data-fetching/defineQueryLoader'
export { defineColadaLoader } from './data-fetching/defineColadaLoader'

/**
* Defines properties of the route for the current page component.
Expand Down

0 comments on commit 6558fa8

Please sign in to comment.