Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vue-query)!: make .key return reactive value #132

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vue-query/src/general-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('createGeneralUtils', () => {

it('.key', () => {
expect(
utils.key({ input: computed(() => ({ search: ref('__search__') })), type: 'infinite' }),
utils.key({ input: computed(() => ({ search: ref('__search__') })), type: 'infinite' }).value,
).toEqual([['path'], { input: { search: '__search__' }, type: 'infinite' }])
expect(buildKeySpy).toHaveBeenCalledTimes(1)
expect(buildKeySpy).toHaveBeenCalledWith(['path'], { input: { search: '__search__' }, type: 'infinite' })
Expand Down
5 changes: 3 additions & 2 deletions packages/vue-query/src/general-utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { QueryKey } from '@tanstack/vue-query'
import type { BuildKeyOptions, KeyType } from './key'
import type { MaybeRefDeep } from './types'
import { computed, type ComputedRef } from 'vue'
import { buildKey } from './key'
import { unrefDeep } from './utils'

export interface GeneralUtils<TInput> {
key<UType extends KeyType = undefined>(options?: MaybeRefDeep<BuildKeyOptions<UType, TInput>>): QueryKey
key<UType extends KeyType = undefined>(options?: MaybeRefDeep<BuildKeyOptions<UType, TInput>>): ComputedRef<QueryKey>
}

export function createGeneralUtils<TInput>(
path: string[],
): GeneralUtils<TInput> {
return {
key(options) {
return buildKey(path, unrefDeep(options as any))
return computed(() => buildKey(path, unrefDeep(options as any)))
},
}
}
6 changes: 3 additions & 3 deletions packages/vue-query/src/router-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('createRouterUtils', () => {
expect(procedureUtilsSpy).toHaveBeenCalledTimes(1)
expect(procedureUtilsSpy).toHaveBeenCalledWith(client, ['__base__'])

expect(utils.key()).toEqual(generalUtilsSpy.mock.results[0]!.value.key())
expect(utils.key().value).toEqual(generalUtilsSpy.mock.results[0]!.value.key().value)
expect(utils.queryOptions().queryKey.value).toEqual(procedureUtilsSpy.mock.results[0]!.value.queryOptions().queryKey.value)

vi.clearAllMocks()
Expand All @@ -33,7 +33,7 @@ describe('createRouterUtils', () => {
expect(procedureUtilsSpy).toHaveBeenCalledTimes(1)
expect(procedureUtilsSpy).toHaveBeenCalledWith(client.key, ['__base__', 'key'])

expect(keyUtils.key()).toEqual(generalUtilsSpy.mock.results[0]!.value.key())
expect(keyUtils.key().value).toEqual(generalUtilsSpy.mock.results[0]!.value.key().value)
expect(keyUtils.queryOptions().queryKey.value).toEqual(procedureUtilsSpy.mock.results[0]!.value.queryOptions().queryKey.value)

vi.clearAllMocks()
Expand All @@ -47,7 +47,7 @@ describe('createRouterUtils', () => {
expect(procedureUtilsSpy).toHaveBeenNthCalledWith(1, client.key, ['__base__', 'key'])
expect(procedureUtilsSpy).toHaveBeenNthCalledWith(2, client.key.pong, ['__base__', 'key', 'pong'])

expect(pongUtils.key()).toEqual(generalUtilsSpy.mock.results[1]!.value.key())
expect(pongUtils.key().value).toEqual(generalUtilsSpy.mock.results[1]!.value.key().value)
expect(pongUtils.queryOptions().queryKey.value).toEqual(procedureUtilsSpy.mock.results[1]!.value.queryOptions().queryKey.value)
})

Expand Down