forked from psteinroe/supabase-cache-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-query.ts
148 lines (139 loc) · 5.29 KB
/
use-query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
PostgrestError,
PostgrestResponse,
PostgrestSingleResponse,
PostgrestMaybeSingleResponse,
} from '@supabase/postgrest-js';
import { AnyPostgrestResponse } from '@supabase-cache-helpers/postgrest-core';
import {
useQuery as useVueQuery,
UseQueryReturnType as UseVueQueryResult,
UseQueryOptions as UseVueQueryOptions,
} from '@tanstack/vue-query';
import { buildQueryOpts } from './build-query-opts';
/**
* Represents the return value of the `useQuery` hook when `query` is expected to return
* a single row.
*/
export type UseQuerySingleReturn<Result> = Omit<
UseVueQueryResult<PostgrestSingleResponse<Result>['data'], PostgrestError>,
'refetch'
> &
Pick<
UseVueQueryResult<PostgrestSingleResponse<Result>, PostgrestError>,
'refetch'
> &
Pick<PostgrestSingleResponse<Result>, 'count'>;
/**
* Represents the return value of the `useQuery` hook when `query` is expected to return
* either a single row or an empty response.
*/
export type UseQueryMaybeSingleReturn<Result> = Omit<
UseVueQueryResult<
PostgrestMaybeSingleResponse<Result>['data'],
PostgrestError
>,
'refetch'
> &
Pick<
UseVueQueryResult<PostgrestMaybeSingleResponse<Result>, PostgrestError>,
'refetch'
> &
Pick<PostgrestMaybeSingleResponse<Result>, 'count'>;
/**
* Represents the return value of the `useQuery` hook when `query` is expected to return
* one or more rows.
*/
export type UseQueryReturn<Result> = Omit<
UseVueQueryResult<PostgrestResponse<Result>['data'], PostgrestError>,
'refetch'
> &
Pick<
UseVueQueryResult<PostgrestResponse<Result>, PostgrestError>,
'refetch'
> &
Pick<PostgrestResponse<Result>, 'count'>;
/**
* Represents the return value of the `useQuery` hook when the type of the query response
* is not known.
*/
export type UseQueryAnyReturn<Result> = Omit<
UseVueQueryResult<AnyPostgrestResponse<Result>['data'], PostgrestError>,
'refetch'
> &
Pick<
UseVueQueryResult<AnyPostgrestResponse<Result>, PostgrestError>,
'refetch'
> &
Pick<AnyPostgrestResponse<Result>, 'count'>;
/**
* Vue hook to execute a PostgREST query and return a single item response.
*
* @param {PromiseLike<PostgrestSingleResponse<Result>>} query A promise that resolves to a PostgREST single item response.
* @param {Omit<UseVueQueryOptions<PostgrestSingleResponse<Result>, PostgrestError>, 'queryKey' | 'queryFn'>} [config] The Vue Query options.
* @returns {UseQuerySingleReturn<Result>} The hook result containing the single item response data.
*/
function useQuery<Result>(
query: PromiseLike<PostgrestSingleResponse<Result>>,
config?: Omit<
UseVueQueryOptions<PostgrestSingleResponse<Result>, PostgrestError>,
'queryKey' | 'queryFn'
>,
): UseQuerySingleReturn<Result>;
/**
* Vue hook to execute a PostgREST query and return a maybe single item response.
*
* @param {PromiseLike<PostgrestMaybeSingleResponse<Result>>} query A promise that resolves to a PostgREST maybe single item response.
* @param {Omit<UseVueQueryOptions<PostgrestMaybeSingleResponse<Result>, PostgrestError>, 'queryKey' | 'queryFn'>} [config] The Vue Query options.
* @returns {UseQueryMaybeSingleReturn<Result>} The hook result containing the maybe single item response data.
*/
function useQuery<Result>(
query: PromiseLike<PostgrestMaybeSingleResponse<Result>>,
config?: Omit<
UseVueQueryOptions<PostgrestMaybeSingleResponse<Result>, PostgrestError>,
'queryKey' | 'queryFn'
>,
): UseQueryMaybeSingleReturn<Result>;
/**
* Vue hook to execute a PostgREST query.
*
* @template Result The expected response data type.
* @param {PromiseLike<PostgrestResponse<Result>>} query A promise that resolves to a PostgREST response.
* @param {Omit<UseVueQueryOptions<PostgrestResponse<Result>, PostgrestError>, 'queryKey' | 'queryFn'>} [config] The Vue Query options.
* @returns {UseQueryReturn<Result>} The hook result containing the response data.
*/
function useQuery<Result>(
query: PromiseLike<PostgrestResponse<Result>>,
config?: Omit<
UseVueQueryOptions<PostgrestResponse<Result>, PostgrestError>,
'queryKey' | 'queryFn'
>,
): UseQueryReturn<Result>;
/**
* Vue hook to execute a PostgREST query.
*
* @template Result The expected response data type.
* @param {PromiseLike<AnyPostgrestResponse<Result>>} query A promise that resolves to a PostgREST response of any kind.
* @param {Omit<UseVueQueryOptions<AnyPostgrestResponse<Result>, PostgrestError>, 'queryKey' | 'queryFn'>} [config] The Vue Query options.
* @returns {UseQueryAnyReturn<Result>} The hook result containing the response data.
*/
function useQuery<Result>(
query: PromiseLike<AnyPostgrestResponse<Result>>,
config?: Omit<
UseVueQueryOptions<AnyPostgrestResponse<Result>, PostgrestError>,
'queryKey' | 'queryFn'
>,
): UseQueryAnyReturn<Result> {
const { data, ...rest } = useVueQuery<
AnyPostgrestResponse<Result>,
PostgrestError
>(buildQueryOpts<Result>(query, config));
// TODO: data: data.value || Type 'AnyPostgrestResponse<Result> | undefined' is not assignable to type 'Ref<undefined> | Ref<Result | Result[] | null>'
// TODO: data: data.value?.data || Type 'Result | Result[] | null | undefined' is not assignable to type 'Ref<undefined> | Ref<Result | Result[] | null>'.
return {
data: data.value?.data,
count: data.value?.count ?? null,
...rest,
};
}
export { useQuery };