Skip to content

Commit a8ad31d

Browse files
authored
feat(vue-query, vue-colada)!: disallow using refs in .key (#137)
1 parent fca153f commit a8ad31d

File tree

8 files changed

+48
-65
lines changed

8 files changed

+48
-65
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1+
import type { GeneralUtils } from './general-utils'
12
import { ref } from 'vue'
2-
import { createGeneralUtils } from './general-utils'
33

4-
describe('key', () => {
5-
const utils = createGeneralUtils<{ a: { b: { c: number } } }>([])
4+
describe('GeneralUtils', () => {
5+
describe('.key', () => {
6+
const utils = {} as GeneralUtils<{ a: { b: { c: number } } }>
67

7-
it('infer correct input type & input', () => {
8-
utils.key()
9-
utils.key({})
10-
utils.key({ input: { a: { b: { c: 1 } } } })
8+
it('infer correct input type & input', () => {
9+
utils.key()
10+
utils.key({})
11+
utils.key({ input: { a: { b: { c: 1 } } } })
1112

12-
utils.key({ input: { a: ref({ b: ref({ c: 1 }) }) } })
13+
// @ts-expect-error invalid input
14+
utils.key({ input: 123 })
15+
// @ts-expect-error invalid input
16+
utils.key({ input: { a: { b: { c: '1' } } } })
1317

14-
// @ts-expect-error invalid input
15-
utils.key({ input: 123 })
16-
// @ts-expect-error invalid input
17-
utils.key({ input: { a: { b: { c: '1' } } } })
18+
// @ts-expect-error not allow ref
19+
utils.key({ input: { a: { b: ref({ c: 1 }) } } })
1820

19-
// @ts-expect-error invalid input
20-
utils.key({ input: ref(123) })
21-
22-
// @ts-expect-error invalid input
23-
utils.key({ type: 'ddd' })
21+
// @ts-expect-error invalid input
22+
utils.key({ type: 'ddd' })
23+
})
2424
})
2525
})

packages/vue-colada/src/general-utils.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { computed, ref } from 'vue'
21
import { createGeneralUtils } from './general-utils'
32
import * as keyModule from './key'
43

@@ -13,8 +12,11 @@ describe('createGeneralUtils', () => {
1312

1413
it('.key', () => {
1514
expect(
16-
utils.key({ input: computed(() => ({ search: ref('__search__') })) }),
17-
).toEqual(['path', { input: '{"json":{"search":"__search__"},"meta":[]}' }])
15+
utils.key({ input: { search: '__search__' } }),
16+
).toEqual(
17+
buildKeySpy.mock.results[0]!.value,
18+
)
19+
1820
expect(buildKeySpy).toHaveBeenCalledTimes(1)
1921
expect(buildKeySpy).toHaveBeenCalledWith(['path'], { input: { search: '__search__' } })
2022
})
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import type { EntryKey } from '@pinia/colada'
22
import type { BuildKeyOptions } from './key'
3-
import type { MaybeRefDeep } from './types'
43
import { buildKey } from './key'
5-
import { unrefDeep } from './utils'
64

75
export interface GeneralUtils<TInput> {
8-
key(options?: MaybeRefDeep<BuildKeyOptions<TInput>>): EntryKey
6+
key(options?: BuildKeyOptions<TInput>): EntryKey
97
}
108

119
export function createGeneralUtils<TInput>(
1210
path: string[],
1311
): GeneralUtils<TInput> {
1412
return {
1513
key(options) {
16-
return buildKey(path, unrefDeep(options))
14+
return buildKey(path, options)
1715
},
1816
}
1917
}

packages/vue-colada/tests/e2e.test-d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ it('.key', () => {
1111
})
1212

1313
orpc.ping.key({})
14-
orpc.ping.key({ input: computed(() => ({ input: ref(123) })) })
1514
// @ts-expect-error --- input is invalid
1615
orpc.ping.key({ input: { input: 'INVALID' } })
1716
})
Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
1+
import type { GeneralUtils } from './general-utils'
12
import { ref } from 'vue'
2-
import { createGeneralUtils } from './general-utils'
33

4-
describe('key', () => {
5-
const utils = createGeneralUtils<{ a: { b: { c: number } } }>([])
4+
describe('GeneralUtils', () => {
5+
describe('.key', () => {
6+
const utils = {} as GeneralUtils<{ a: { b: { c: number } } }>
67

7-
it('infer correct input type & partial input', () => {
8-
utils.key()
9-
utils.key({})
10-
utils.key({ type: 'infinite' })
11-
utils.key({ input: {}, type: 'query' })
12-
utils.key({ input: {} })
13-
utils.key({ input: { a: {} } })
14-
utils.key({ input: { a: { b: {} } } })
15-
utils.key({ input: { a: { b: { c: 1 } } } })
8+
it('infer correct input type & input', () => {
9+
utils.key()
10+
utils.key({})
11+
utils.key({ input: { a: { b: { c: 1 } } } })
1612

17-
utils.key({ type: ref('infinite'), input: { a: ref({ b: ref({ c: 1 }) }) } })
13+
// @ts-expect-error invalid input
14+
utils.key({ input: 123 })
15+
// @ts-expect-error invalid input
16+
utils.key({ input: { a: { b: { c: '1' } } } })
1817

19-
// @ts-expect-error invalid input
20-
utils.key({ input: 123 })
21-
// @ts-expect-error invalid input
22-
utils.key({ input: { a: { b: { c: '1' } } } })
18+
// @ts-expect-error not allow ref
19+
utils.key({ input: { a: { b: ref({ c: 1 }) } } })
2320

24-
// @ts-expect-error invalid input
25-
utils.key({ input: ref(123) })
26-
// @ts-expect-error invalid type
27-
utils.key({ type: ref(123) })
28-
29-
// @ts-expect-error invalid input
30-
utils.key({ type: 'ddd' })
31-
})
32-
33-
it('it prevent pass input when type is mutation', () => {
34-
utils.key({ type: 'mutation' })
35-
// @ts-expect-error input is not allowed when type is mutation
36-
utils.key({ input: {}, type: 'mutation' })
37-
// @ts-expect-error input is not allowed when type is mutation
38-
utils.key({ input: ref({}), type: 'mutation' })
21+
// @ts-expect-error invalid input
22+
utils.key({ type: 'ddd' })
23+
})
3924
})
4025
})

packages/vue-query/src/general-utils.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { computed, ref } from 'vue'
21
import { createGeneralUtils } from './general-utils'
32
import * as keyModule from './key'
43

@@ -13,8 +12,11 @@ describe('createGeneralUtils', () => {
1312

1413
it('.key', () => {
1514
expect(
16-
utils.key({ input: computed(() => ({ search: ref('__search__') })), type: 'infinite' }),
17-
).toEqual([['path'], { input: { search: '__search__' }, type: 'infinite' }])
15+
utils.key({ input: { search: '__search__' }, type: 'infinite' }),
16+
).toEqual(
17+
buildKeySpy.mock.results[0]!.value,
18+
)
19+
1820
expect(buildKeySpy).toHaveBeenCalledTimes(1)
1921
expect(buildKeySpy).toHaveBeenCalledWith(['path'], { input: { search: '__search__' }, type: 'infinite' })
2022
})
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import type { QueryKey } from '@tanstack/vue-query'
22
import type { BuildKeyOptions, KeyType } from './key'
3-
import type { MaybeRefDeep } from './types'
43
import { buildKey } from './key'
5-
import { unrefDeep } from './utils'
64

75
export interface GeneralUtils<TInput> {
8-
key<UType extends KeyType = undefined>(options?: MaybeRefDeep<BuildKeyOptions<UType, TInput>>): QueryKey
6+
key<UType extends KeyType = undefined>(options?: BuildKeyOptions<UType, TInput>): QueryKey
97
}
108

119
export function createGeneralUtils<TInput>(
1210
path: string[],
1311
): GeneralUtils<TInput> {
1412
return {
1513
key(options) {
16-
return buildKey(path, unrefDeep(options as any))
14+
return buildKey(path, options)
1715
},
1816
}
1917
}

packages/vue-query/tests/e2e.test-d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ it('.key', () => {
1010
})
1111

1212
orpc.ping.key({})
13-
orpc.ping.key({ input: computed(() => ({ input: ref(123) })) })
1413
// @ts-expect-error --- input is invalid
1514
orpc.ping.key({ input: { input: 'INVALID' } })
1615
})

0 commit comments

Comments
 (0)