UNPKG

@supabase-cache-helpers/storage-swr

Version:

A collection of SWR utilities for working with Supabase.

242 lines (227 loc) 7.07 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/lib/constants.ts var KEY_PREFIX = "storage"; var KEY_SEPARATOR = "$"; // src/lib/decode.ts var decode = (key) => { if (typeof key !== "string" || !key.startsWith(KEY_PREFIX)) return null; const [_, bucketId, path] = key.split(KEY_SEPARATOR); return { bucketId, path }; }; // src/lib/get-bucket-id.ts var getBucketId = (fileApi) => fileApi["bucketId"]; // src/lib/key.ts var isStorageKeyInput = (key) => Array.isArray(key) && key.length === 2 && typeof key[1] === "string" && Boolean(key[0]["bucketId"]); var assertStorageKeyInput = (key) => { if (!isStorageKeyInput(key)) throw new Error("Invalid key"); return key; }; // src/lib/encode.ts var encode = (key) => { if (key === null) return null; const [fileApi, path] = assertStorageKeyInput(key); return [KEY_PREFIX, getBucketId(fileApi), path].join(KEY_SEPARATOR); }; // src/lib/middleware.ts var middleware = (useSWRNext) => { return (key, fetcher, config) => { if (!fetcher) throw new Error("No fetcher provided"); return useSWRNext(encode(key), () => fetcher(key), config); }; }; // src/lib/truthy.ts function truthy(value) { return !!value; } // src/mutate/use-remove-directory.ts import { createRemoveDirectoryFetcher, mutatePaths } from "@supabase-cache-helpers/storage-core"; import { useCallback } from "react"; import { useSWRConfig } from "swr"; import useSWRMutation from "swr/mutation"; // src/mutate/use-random-key.ts import { useId } from "react"; var PREFIX = "random-mutation-key"; var useRandomKey = () => { const key = useId(); return [PREFIX, key].join(KEY_SEPARATOR); }; // src/mutate/use-remove-directory.ts function useRemoveDirectory(fileApi, config) { const key = useRandomKey(); const { cache, mutate } = useSWRConfig(); const fetcher = useCallback(createRemoveDirectoryFetcher(fileApi), [fileApi]); return useSWRMutation( key, (_0, _1) => __async(this, [_0, _1], function* (_, { arg }) { const result = fetcher(arg); yield mutatePaths(getBucketId(fileApi), [arg], { cacheKeys: Array.from(cache.keys()), decode, mutate }); return result; }), config ); } // src/mutate/use-remove-files.ts import { createRemoveFilesFetcher, mutatePaths as mutatePaths2 } from "@supabase-cache-helpers/storage-core"; import { useCallback as useCallback2 } from "react"; import { useSWRConfig as useSWRConfig2 } from "swr"; import useSWRMutation2 from "swr/mutation"; function useRemoveFiles(fileApi, config) { const key = useRandomKey(); const { cache, mutate } = useSWRConfig2(); const fetcher = useCallback2(createRemoveFilesFetcher(fileApi), [fileApi]); return useSWRMutation2( key, (_0, _1) => __async(this, [_0, _1], function* (_, { arg: paths }) { const res = yield fetcher(paths); yield mutatePaths2(getBucketId(fileApi), paths, { cacheKeys: Array.from(cache.keys()), decode, mutate }); return res; }), config ); } // src/mutate/use-upload.ts import { createUploadFetcher, mutatePaths as mutatePaths3 } from "@supabase-cache-helpers/storage-core"; import { useCallback as useCallback3 } from "react"; import { useSWRConfig as useSWRConfig3 } from "swr"; import useSWRMutation3 from "swr/mutation"; function useUpload(fileApi, config) { const key = useRandomKey(); const { cache, mutate } = useSWRConfig3(); const fetcher = useCallback3(createUploadFetcher(fileApi, config), [ config, fileApi ]); return useSWRMutation3( key, (_0, _1) => __async(this, [_0, _1], function* (_, { arg: { files, path } }) { const result = yield fetcher(files, path); yield mutatePaths3( getBucketId(fileApi), result.map(({ data }) => data == null ? void 0 : data.path).filter(truthy), { cacheKeys: Array.from(cache.keys()), decode, mutate } ); return result; }), config ); } // src/query/use-directory-urls.ts import { createDirectoryUrlsFetcher } from "@supabase-cache-helpers/storage-core"; import useSWR from "swr"; function useDirectoryFileUrls(fileApi, path, mode, config) { var _a; return useSWR( path ? [fileApi, path] : null, ([fileApi2, path2]) => createDirectoryUrlsFetcher(mode, config)(fileApi2, path2), __spreadProps(__spreadValues({}, config), { use: [...(_a = config == null ? void 0 : config.use) != null ? _a : [], middleware] }) ); } // src/query/use-directory.ts import { fetchDirectory } from "@supabase-cache-helpers/storage-core"; import useSWR2 from "swr"; function useDirectory(fileApi, path, config) { var _a; return useSWR2( path ? [fileApi, path] : null, ([fileApi2, path2]) => fetchDirectory(fileApi2, path2), __spreadProps(__spreadValues({}, config), { use: [...(_a = config == null ? void 0 : config.use) != null ? _a : [], middleware] }) ); } // src/query/use-file-url.ts import { createUrlFetcher } from "@supabase-cache-helpers/storage-core"; import useSWR3 from "swr"; function useFileUrl(fileApi, path, mode, config) { var _a; return useSWR3( path ? [fileApi, path] : null, ([fileApi2, path2]) => createUrlFetcher(mode, config)(fileApi2, path2), __spreadProps(__spreadValues({}, config), { use: [...(_a = config == null ? void 0 : config.use) != null ? _a : [], middleware] }) ); } export { KEY_PREFIX, KEY_SEPARATOR, assertStorageKeyInput, decode, encode, getBucketId, isStorageKeyInput, middleware, truthy, useDirectory, useDirectoryFileUrls, useFileUrl, useRemoveDirectory, useRemoveFiles, useUpload }; //# sourceMappingURL=index.js.map