Skip to content

Commit

Permalink
fix: type config in storage packages and remove unnecessary use callb…
Browse files Browse the repository at this point in the history
…acks
  • Loading branch information
psteinroe committed Sep 9, 2023
1 parent 7e90878 commit 6b1f00c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 41 deletions.
8 changes: 8 additions & 0 deletions .changeset/chilly-tigers-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@supabase-cache-helpers/postgrest-react-query": patch
"@supabase-cache-helpers/storage-react-query": patch
"@supabase-cache-helpers/postgrest-swr": patch
"@supabase-cache-helpers/storage-swr": patch
---

fix: type configuration parameter and add tests for fallbackData
11 changes: 5 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 @@ -9,7 +9,6 @@ import {
UseQueryResult as UseReactQueryResult,
UseQueryOptions as UseReactQueryOptions,
} from '@tanstack/react-query';
import { useCallback } from 'react';

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

Expand Down Expand Up @@ -39,14 +38,14 @@ function useDirectoryFileUrls(
(FileObject & { url: string })[] | undefined,
StorageError
> {
const fetcher = useCallback(createDirectoryUrlsFetcher(mode, config), [
mode,
config,
]);
return useReactQuery<
(FileObject & { url: string })[] | undefined,
StorageError
>(encode([fileApi, path]), () => fetcher(fileApi, path), config);
>(
encode([fileApi, path]),
() => createDirectoryUrlsFetcher(mode, config)(fileApi, path),
config
);
}

export { useDirectoryFileUrls };
7 changes: 3 additions & 4 deletions packages/storage-react-query/src/query/use-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
UseQueryResult as UseReactQueryResult,
UseQueryOptions as UseReactQueryOptions,
} from '@tanstack/react-query';
import { useCallback } from 'react';

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

Expand All @@ -25,11 +24,11 @@ function useDirectory(
'queryKey' | 'queryFn'
>
): UseReactQueryResult<FileObject[] | undefined, StorageError> {
const fetcher = useCallback(
return useReactQuery(
encode([fileApi, path]),
() => fetchDirectory(fileApi, path),
[fileApi, path]
config
);
return useReactQuery(encode([fileApi, path]), fetcher, config);
}

export { useDirectory };
9 changes: 1 addition & 8 deletions packages/storage-react-query/src/query/use-file-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
UseQueryResult as UseReactQueryResult,
UseQueryOptions as UseReactQueryOptions,
} from '@tanstack/react-query';
import { useCallback } from 'react';

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

Expand All @@ -32,15 +31,9 @@ function useFileUrl(
> &
URLFetcherConfig
): UseReactQueryResult<string | undefined, StorageError> {
const fetcher = useCallback(createUrlFetcher(mode, config), [
config,
mode,
fileApi,
path,
]);
return useReactQuery<string | undefined, StorageError>(
encode([fileApi, path]),
() => fetcher(fileApi, path),
() => createUrlFetcher(mode, config)(fileApi, path),
config
);
}
Expand Down
15 changes: 7 additions & 8 deletions packages/storage-swr/src/query/use-directory-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
URLFetcherConfig,
} from '@supabase-cache-helpers/storage-core';
import { FileObject, StorageError } from '@supabase/storage-js';
import { useCallback } from 'react';
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';

import { StorageKeyInput, middleware, StorageFileApi } from '../lib';
Expand All @@ -23,16 +22,16 @@ function useDirectoryFileUrls(
fileApi: StorageFileApi,
path: string | null,
mode: StoragePrivacy,
config?: SWRConfiguration & Pick<URLFetcherConfig, 'expiresIn'>
config?: SWRConfiguration<
(FileObject & { url: string })[] | undefined,
StorageError
> &
Pick<URLFetcherConfig, 'expiresIn'>
): SWRResponse<(FileObject & { url: string })[] | undefined, StorageError> {
const fetcher = useCallback(
([fileApi, path]: StorageKeyInput) =>
createDirectoryUrlsFetcher(mode, config)(fileApi, path),
[mode, config]
);
return useSWR<(FileObject & { url: string })[] | undefined, StorageError>(
path ? [fileApi, path] : null,
fetcher,
([fileApi, path]: StorageKeyInput) =>
createDirectoryUrlsFetcher(mode, config)(fileApi, path),
{
...config,
use: [...(config?.use ?? []), middleware],
Expand Down
9 changes: 2 additions & 7 deletions packages/storage-swr/src/query/use-directory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { fetchDirectory } from '@supabase-cache-helpers/storage-core';
import { FileObject, StorageError } from '@supabase/storage-js';
import { useCallback } from 'react';
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';

import { middleware, StorageKeyInput, StorageFileApi } from '../lib';
Expand All @@ -16,15 +15,11 @@ import { middleware, StorageKeyInput, StorageFileApi } from '../lib';
function useDirectory(
fileApi: StorageFileApi,
path: string | null,
config?: SWRConfiguration
config?: SWRConfiguration<FileObject[] | undefined, StorageError>
): SWRResponse<FileObject[] | undefined, StorageError> {
const fetcher = useCallback(
([fileApi, path]: StorageKeyInput) => fetchDirectory(fileApi, path),
[]
);
return useSWR<FileObject[] | undefined, StorageError>(
path ? [fileApi, path] : null,
fetcher,
([fileApi, path]: StorageKeyInput) => fetchDirectory(fileApi, path),
{
...config,
use: [...(config?.use ?? []), middleware],
Expand Down
15 changes: 7 additions & 8 deletions packages/storage-swr/src/query/use-file-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
URLFetcherConfig,
} from '@supabase-cache-helpers/storage-core';
import { StorageError } from '@supabase/storage-js';
import { useCallback } from 'react';
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';

import { StorageKeyInput, middleware, StorageFileApi } from '../lib';
Expand All @@ -22,17 +21,17 @@ function useFileUrl(
fileApi: StorageFileApi,
path: string | null,
mode: StoragePrivacy,
config?: SWRConfiguration & URLFetcherConfig
config?: SWRConfiguration<string | undefined, StorageError> & URLFetcherConfig
): SWRResponse<string | undefined, StorageError> {
const fetcher = useCallback(
return useSWR<string | undefined, StorageError>(
path ? [fileApi, path] : null,
([fileApi, path]: StorageKeyInput) =>
createUrlFetcher(mode, config)(fileApi, path),
[config, mode]
{
...config,
use: [...(config?.use ?? []), middleware],
}
);
return useSWR(path ? [fileApi, path] : null, fetcher, {
...config,
use: [...(config?.use ?? []), middleware],
});
}

export { useFileUrl };

0 comments on commit 6b1f00c

Please sign in to comment.