-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from psteinroe/feat/build-query-opts
feat: export buildQueryOptions and fetchQuery wrapper
- Loading branch information
Showing
6 changed files
with
90 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@supabase-cache-helpers/postgrest-react-query": minor | ||
--- | ||
|
||
feat: export buildQueryOptions and fetchQuery wrapper |
27 changes: 27 additions & 0 deletions
27
packages/postgrest-react-query/src/query/build-query-opts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PostgrestError } from '@supabase/postgrest-js'; | ||
import { | ||
AnyPostgrestResponse, | ||
isPostgrestBuilder, | ||
} from '@supabase-cache-helpers/postgrest-core'; | ||
import { UseQueryOptions as UseReactQueryOptions } from '@tanstack/react-query'; | ||
|
||
import { encode } from '../lib/key'; | ||
|
||
export function buildQueryOpts<Result>( | ||
query: PromiseLike<AnyPostgrestResponse<Result>>, | ||
config?: Omit< | ||
UseReactQueryOptions<AnyPostgrestResponse<Result>, PostgrestError>, | ||
'queryKey' | 'queryFn' | ||
>, | ||
): UseReactQueryOptions<AnyPostgrestResponse<Result>, PostgrestError> { | ||
return { | ||
queryKey: encode<Result>(query, false), | ||
queryFn: async () => { | ||
if (isPostgrestBuilder(query)) { | ||
query = query.throwOnError(); | ||
} | ||
return await query; | ||
}, | ||
...config, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
PostgrestError, | ||
PostgrestMaybeSingleResponse, | ||
PostgrestResponse, | ||
PostgrestSingleResponse, | ||
} from '@supabase/postgrest-js'; | ||
import { AnyPostgrestResponse } from '@supabase-cache-helpers/postgrest-core'; | ||
import { FetchQueryOptions, QueryClient } from '@tanstack/react-query'; | ||
|
||
import { buildQueryOpts } from './build-query-opts'; | ||
|
||
function fetchQuery<Result>( | ||
queryClient: QueryClient, | ||
query: PromiseLike<PostgrestSingleResponse<Result>>, | ||
config?: Omit< | ||
FetchQueryOptions<PostgrestSingleResponse<Result>, PostgrestError>, | ||
'queryKey' | 'queryFn' | ||
>, | ||
): Promise<void>; | ||
function fetchQuery<Result>( | ||
queryClient: QueryClient, | ||
query: PromiseLike<PostgrestMaybeSingleResponse<Result>>, | ||
config?: Omit< | ||
FetchQueryOptions<PostgrestMaybeSingleResponse<Result>, PostgrestError>, | ||
'queryKey' | 'queryFn' | ||
>, | ||
): Promise<void>; | ||
function fetchQuery<Result>( | ||
queryClient: QueryClient, | ||
query: PromiseLike<PostgrestResponse<Result>>, | ||
config?: Omit< | ||
FetchQueryOptions<PostgrestResponse<Result>, PostgrestError>, | ||
'queryKey' | 'queryFn' | ||
>, | ||
): Promise<void>; | ||
|
||
async function fetchQuery<Result>( | ||
queryClient: QueryClient, | ||
query: PromiseLike<AnyPostgrestResponse<Result>>, | ||
config?: Omit< | ||
FetchQueryOptions<AnyPostgrestResponse<Result>, PostgrestError>, | ||
'queryKey' | 'queryFn' | ||
>, | ||
) { | ||
await queryClient.fetchQuery<AnyPostgrestResponse<Result>, PostgrestError>( | ||
buildQueryOpts(query, config), | ||
); | ||
} | ||
|
||
export { fetchQuery }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './build-query-opts'; | ||
export * from './fetch'; | ||
export * from './prefetch'; | ||
export * from './use-query'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters