@supabase-cache-helpers/storage-swr
Version:
A collection of SWR utilities for working with Supabase.
100 lines (84 loc) • 5.77 kB
text/typescript
import { DecodedStorageKey, UploadFetcherConfig, UploadFileResponse, FileInput, StoragePrivacy, URLFetcherConfig } from '@supabase-cache-helpers/storage-core';
export { FileInput, UploadFetcherConfig, UploadFileResponse } from '@supabase-cache-helpers/storage-core';
import { Key, Middleware, SWRConfiguration, SWRResponse } from 'swr';
import { SupabaseClient } from '@supabase/supabase-js';
import { FileObject, StorageError } from '@supabase/storage-js';
import { SWRMutationConfiguration, SWRMutationResponse } from 'swr/mutation';
declare const KEY_PREFIX = "storage";
declare const KEY_SEPARATOR = "$";
declare const decode: (key: Key) => DecodedStorageKey | null;
declare const encode: (key: Key | null) => Key;
type StorageFileApi = ReturnType<SupabaseClient['storage']['from']>;
declare const getBucketId: (fileApi: StorageFileApi) => string;
declare const middleware: Middleware;
declare const isStorageKeyInput: (key: Key) => key is StorageKeyInput;
declare const assertStorageKeyInput: (key: Key) => StorageKeyInput;
type StorageKeyInput = [StorageFileApi, string];
type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
declare function truthy<T>(value: T): value is Truthy<T>;
/**
* A hook that provides a mutation function to remove a directory and all its contents.
* @param fileApi The `StorageFileApi` instance to use for the removal.
* @param config Optional configuration options for the SWR mutation.
* @returns An object containing the mutation function, loading state, and error state.
*/
declare function useRemoveDirectory(fileApi: StorageFileApi, config?: SWRMutationConfiguration<FileObject[], StorageError, string, string>): SWRMutationResponse<FileObject[], StorageError, string, string>;
/**
* Hook for removing files from storage using SWR mutation
* @param {StorageFileApi} fileApi - The Supabase Storage API
* @param {SWRMutationConfiguration<FileObject[], StorageError, string[], string>} [config] - The SWR mutation configuration
* @returns {SWRMutationResponse<FileObject[], StorageError, string[], string>} - The SWR mutation response object
*/
declare function useRemoveFiles(fileApi: StorageFileApi, config?: SWRMutationConfiguration<FileObject[], StorageError, string, string[]>): SWRMutationResponse<FileObject[], StorageError, string, string[]>;
/**
* The input object for the useUpload mutation function.
* @typedef {Object} UseUploadInput
* @property {FileList|(File|FileInput)[]} files - The file(s) to be uploaded
* @property {string} [path] - The path in the storage bucket to upload the file(s) to
*/
type UseUploadInput = {
files: FileList | (File | FileInput)[];
path?: string;
};
/**
* Hook for uploading files to storage using SWR mutation
* @param {StorageFileApi} fileApi - The Supabase Storage API
* @param {UploadFetcherConfig & SWRMutationConfiguration<UploadFileResponse[], StorageError, UseUploadInput, string>} [config] - The SWR mutation configuration
* @returns {SWRMutationResponse<UploadFileResponse[], StorageError, UseUploadInput, string>} - The SWR mutation response object
*/
declare function useUpload(fileApi: StorageFileApi, config?: UploadFetcherConfig & SWRMutationConfiguration<UploadFileResponse[], StorageError, string, UseUploadInput>): SWRMutationResponse<UploadFileResponse[], StorageError, string, UseUploadInput>;
/**
* Convenience hook to fetch all files in a directory, and their corresponding URLs, from Supabase Storage using SWR.
*
* @param {StorageFileApi} fileApi - The file API of the storage bucket.
* @param {string|null} path - The path of the directory to fetch files from.
* @param {StoragePrivacy} mode - The privacy mode of the bucket to fetch files from.
* @param {SWRConfiguration & Pick<URLFetcherConfig, 'expiresIn'>} [config] - Optional SWR configuration and `expiresIn` value to pass to the `createDirectoryUrlsFetcher` function.
*
* @returns {SWRResponse<(FileObject & { url: string })[] | undefined, StorageError>} An SWR response containing an array of file objects with their corresponding URLs.
*/
declare function useDirectoryFileUrls(fileApi: StorageFileApi, path: string | null, mode: StoragePrivacy, config?: SWRConfiguration<(FileObject & {
url: string;
})[] | undefined, StorageError> & Pick<URLFetcherConfig, 'expiresIn'>): SWRResponse<(FileObject & {
url: string;
})[] | undefined, StorageError>;
/**
* Convenience hook to fetch a directory from Supabase Storage using SWR.
*
* @param fileApi The StorageFileApi instance.
* @param path The path to the directory.
* @param config The SWR configuration.
* @returns An SWRResponse containing an array of FileObjects
*/
declare function useDirectory(fileApi: StorageFileApi, path: string | null, config?: SWRConfiguration<FileObject[] | undefined, StorageError>): SWRResponse<FileObject[] | undefined, StorageError>;
/**
* A hook to fetch the URL for a file in the Storage.
*
* @param fileApi - the file API instance from the Supabase client.
* @param path - the path of the file to fetch the URL for.
* @param mode - the privacy mode of the bucket the file is in.
* @param config - the SWR configuration options and URL fetcher configuration.
* @returns the SWR response for the URL of the file
*/
declare function useFileUrl(fileApi: StorageFileApi, path: string | null, mode: StoragePrivacy, config?: SWRConfiguration<string | undefined, StorageError> & URLFetcherConfig): SWRResponse<string | undefined, StorageError>;
export { KEY_PREFIX, KEY_SEPARATOR, type StorageFileApi, type StorageKeyInput, type UseUploadInput, assertStorageKeyInput, decode, encode, getBucketId, isStorageKeyInput, middleware, truthy, useDirectory, useDirectoryFileUrls, useFileUrl, useRemoveDirectory, useRemoveFiles, useUpload };