Skip to content

Commit

Permalink
Merge pull request #385 from psteinroe/feat/build-query-opts-storage
Browse files Browse the repository at this point in the history
feat:  add query opt builder fns for storage
  • Loading branch information
psteinroe authored Feb 14, 2024
2 parents 6ddf52a + bfbb039 commit a1c4848
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-tools-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/storage-react-query": minor
---

feat: add query opt builder fns for storage
31 changes: 25 additions & 6 deletions packages/storage-react-query/src/query/use-directory-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ import {

import { encode, StorageFileApi } from '../lib';

function buildDirectoryUrlsQueryOpts(
fileApi: StorageFileApi,
path: string,
mode: StoragePrivacy,
config?: Omit<
UseReactQueryOptions<
(FileObject & { url: string })[] | undefined,
StorageError
>,
'queryKey' | 'queryFn'
> &
Pick<URLFetcherConfig, 'expiresIn'>,
): UseReactQueryOptions<
(FileObject & { url: string })[] | undefined,
StorageError
> {
return {
queryKey: encode([fileApi, path]),
queryFn: () => createDirectoryUrlsFetcher(mode, config)(fileApi, path),
...config,
};
}

/**
* Convenience hook to fetch all files in a directory, and their corresponding URLs, from Supabase Storage using React Query.
*
Expand Down Expand Up @@ -41,11 +64,7 @@ function useDirectoryFileUrls(
return useReactQuery<
(FileObject & { url: string })[] | undefined,
StorageError
>({
queryKey: encode([fileApi, path]),
queryFn: () => createDirectoryUrlsFetcher(mode, config)(fileApi, path),
...config,
});
>(buildDirectoryUrlsQueryOpts(fileApi, path, mode, config));
}

export { useDirectoryFileUrls };
export { useDirectoryFileUrls, buildDirectoryUrlsQueryOpts };
21 changes: 16 additions & 5 deletions packages/storage-react-query/src/query/use-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import {

import { StorageFileApi, encode } from '../lib';

function buildDirectoryQueryOpts(
fileApi: StorageFileApi,
path: string,
config?: Omit<
UseReactQueryOptions<FileObject[] | undefined, StorageError>,
'queryKey' | 'queryFn'
>,
): UseReactQueryOptions<FileObject[] | undefined, StorageError> {
return {
queryKey: encode([fileApi, path]),
queryFn: () => fetchDirectory(fileApi, path),
...config,
};
}

/**
* Convenience hook to fetch a directory from Supabase Storage using React Query.
*
Expand All @@ -24,11 +39,7 @@ function useDirectory(
'queryKey' | 'queryFn'
>,
): UseReactQueryResult<FileObject[] | undefined, StorageError> {
return useReactQuery({
queryKey: encode([fileApi, path]),
queryFn: () => fetchDirectory(fileApi, path),
...config,
});
return useReactQuery(buildDirectoryQueryOpts(fileApi, path, config));
}

export { useDirectory };
25 changes: 20 additions & 5 deletions packages/storage-react-query/src/query/use-file-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ import {

import { StorageFileApi, encode } from '../lib';

function buildFileUrlQueryOpts(
fileApi: StorageFileApi,
path: string,
mode: StoragePrivacy,
config?: Omit<
UseReactQueryOptions<string | undefined, StorageError>,
'queryKey' | 'queryFn'
> &
URLFetcherConfig,
): UseReactQueryOptions<string | undefined, StorageError> {
return {
queryKey: encode([fileApi, path]),
queryFn: () => createUrlFetcher(mode, config)(fileApi, path),
...config,
};
}

/**
* A hook to fetch the URL for a file in the Storage.
*
Expand All @@ -31,11 +48,9 @@ function useFileUrl(
> &
URLFetcherConfig,
): UseReactQueryResult<string | undefined, StorageError> {
return useReactQuery<string | undefined, StorageError>({
queryKey: encode([fileApi, path]),
queryFn: () => createUrlFetcher(mode, config)(fileApi, path),
...config,
});
return useReactQuery<string | undefined, StorageError>(
buildFileUrlQueryOpts(fileApi, path, mode, config),
);
}

export { useFileUrl };

0 comments on commit a1c4848

Please sign in to comment.