Skip to content

Commit

Permalink
refactor!: rename type 'UseQueryKey' to 'UseEntryKey'
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the exported type 'UseQueryKey' is replaced by the more generic type 'UseEntryKey', which will be also used to type mutations
  • Loading branch information
ElisePatrikainen committed Apr 5, 2024
1 parent 7fd32b4 commit 6a32d89
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
1 change: 1 addition & 0 deletions playground/src/pages/ecom/item/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const itemAvailability = ref()
watch(() => item.value?.availability, (value) => itemAvailability.value = value)
const { mutate: bookProduct } = useMutation({
key: ['book-item'],
keys: (product) => [['items'], ['items', product.id]],
mutation: async (product: ProductListItem) => {
await delay(Math.random() * 1000 + 200)
Expand Down
7 changes: 7 additions & 0 deletions src/entry-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { EntryNodeKey } from './tree-map'
import type { _ObjectFlat } from './utils'

/**
* Key used to identify a query. Always an array.
*/
export type UseEntryKey = Array<EntryNodeKey | _ObjectFlat>
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export { defineQuery } from './define-query'
// export { type UseQueryKeyList } from './query-keys'

export {
type UseQueryKey,
type UseEntryKey,
} from './entry-options'

export {
type UseQueryOptions,
type UseQueryOptionsWithDefaults,
} from './query-options'
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/invalidate-queries.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* @module @pinia/colada/plugins/invalidate-queries
*/
import type { UseQueryKey } from '@pinia/colada'
import type { UseEntryKey } from '@pinia/colada'

export interface UseMutationOptionsInvalidateQueries<TResult, TVars> {
/**
* Keys to invalidate if the mutation succeeds so that `useQuery()` refetch if used. Only available if
* `@pinia/colada/plugins/invalidate-queries` is installed.
*/
invalidateKeys?:
| UseQueryKey[]
| ((data: TResult, vars: TVars) => UseQueryKey[])
| UseEntryKey[]
| ((data: TResult, vars: TVars) => UseEntryKey[])
}

declare module '@pinia/colada' {
Expand Down
10 changes: 2 additions & 8 deletions src/query-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type InjectionKey, type MaybeRefOrGetter, inject } from 'vue'
import type { EntryNodeKey } from './tree-map'
import type { _ObjectFlat } from './utils'
import type { UseEntryKey } from './entry-options'
import type { QueryPluginOptions } from './query-plugin'
import type { ErrorDefault } from './types-extension'

Expand All @@ -9,11 +8,6 @@ import type { ErrorDefault } from './types-extension'
*/
export type _RefetchOnControl = boolean | 'always'

/**
* Key used to identify a query. Always an array.
*/
export type UseQueryKey = Array<EntryNodeKey | _ObjectFlat>

/**
* Context object passed to the `query` function of `useQuery()`.
* @see {@link UseQueryOptions}
Expand Down Expand Up @@ -59,7 +53,7 @@ export interface UseQueryOptions<TResult = unknown, TError = ErrorDefault> {
* })
* ```
*/
key: MaybeRefOrGetter<UseQueryKey>
key: MaybeRefOrGetter<UseEntryKey>

/**
* The function that will be called to fetch the data. It **must** be async.
Expand Down
11 changes: 6 additions & 5 deletions src/query-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
} from 'vue'
import { stringifyFlatObject } from './utils'
import { type EntryNodeKey, TreeMapNode } from './tree-map'
import type { UseQueryKey, UseQueryOptionsWithDefaults } from './query-options'
import type { UseEntryKey } from './entry-options'
import type { UseQueryOptionsWithDefaults } from './query-options'
import type { ErrorDefault } from './types-extension'
import type { defineQuery } from './define-query'

Expand Down Expand Up @@ -152,7 +153,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
}

function ensureEntry<TResult = unknown, TError = ErrorDefault>(
keyRaw: UseQueryKey,
keyRaw: UseEntryKey,
options: UseQueryOptionsWithDefaults<TResult, TError>,
): UseQueryEntry<TResult, TError> {
if (process.env.NODE_ENV !== 'production' && keyRaw.length === 0) {
Expand Down Expand Up @@ -187,7 +188,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
* @param options.refetch - if true, it will refetch the data
*/
function invalidateEntry(
key: UseQueryKey,
key: UseEntryKey,
{
refetch: shouldRefetch = true,
exact = false,
Expand Down Expand Up @@ -310,7 +311,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {

// TODO: tests, remove function version
function setQueryData<TResult = unknown>(
key: UseQueryKey,
key: UseEntryKey,
data: TResult | ((data: Ref<TResult | undefined>) => void),
) {
const entry = caches.get(key.map(stringifyFlatObject)) as
Expand All @@ -331,7 +332,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
}

function getQueryData<TResult = unknown>(
key: UseQueryKey,
key: UseEntryKey,
): TResult | undefined {
const entry = caches.get(key.map(stringifyFlatObject)) as
| UseQueryEntry<TResult>
Expand Down
10 changes: 6 additions & 4 deletions src/use-mutation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { computed, shallowRef } from 'vue'
import type { ComputedRef, ShallowRef } from 'vue'
import type { ComputedRef, MaybeRefOrGetter, ShallowRef } from 'vue'
import { useQueryCache } from './query-store'
import type { UseQueryKey } from './query-options'
import type { UseEntryKey } from './entry-options'
import type { ErrorDefault } from './types-extension'
import { type _Awaitable, noop } from './utils'

Expand All @@ -11,8 +11,8 @@ import { type _Awaitable, noop } from './utils'
* @internal
*/
type _MutationKeys<TVars, TResult> =
| UseQueryKey[]
| ((data: TResult, vars: TVars) => UseQueryKey[])
| UseEntryKey[]
| ((data: TResult, vars: TVars) => UseEntryKey[])

/**
* The status of the mutation.
Expand Down Expand Up @@ -69,6 +69,8 @@ export interface UseMutationOptions<
*/
mutation: (vars: TVars, context: NoInfer<TContext>) => Promise<TResult>

key?: MaybeRefOrGetter<UseEntryKey>

// TODO: move this to a plugin that calls invalidateEntry()
/**
* Keys to invalidate if the mutation succeeds so that `useQuery()` refetch if used.
Expand Down
6 changes: 3 additions & 3 deletions src/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
import { IS_CLIENT, computedRef, useEventListener } from './utils'
import { type _UseQueryEntry_State, useQueryCache } from './query-store'
import { useQueryOptions } from './query-options'
import type { UseEntryKey } from './entry-options'
import type {
UseQueryKey,
UseQueryOptions,
UseQueryOptionsWithDefaults,
} from './query-options'
Expand Down Expand Up @@ -176,9 +176,9 @@ export function useQuery<TResult, TError = ErrorDefault>(
* @returns - the computed key
*/
function _computedKeyWithWarnings(
key: MaybeRefOrGetter<UseQueryKey>,
key: MaybeRefOrGetter<UseEntryKey>,
warnChecksMap: WeakMap<object, boolean>,
): () => UseQueryKey {
): () => UseEntryKey {
const componentInstance = getCurrentInstance()
// probably correct scope, no need to warn
if (!componentInstance) return () => toValue(key)
Expand Down

0 comments on commit 6a32d89

Please sign in to comment.