UNPKG

@supabase-cache-helpers/storage-swr

Version:

A collection of SWR utilities for working with Supabase.

277 lines (261 loc) 9.51 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; 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 __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); 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/index.ts var index_exports = {}; __export(index_exports, { KEY_PREFIX: () => KEY_PREFIX, KEY_SEPARATOR: () => KEY_SEPARATOR, assertStorageKeyInput: () => assertStorageKeyInput, decode: () => decode, encode: () => encode, getBucketId: () => getBucketId, isStorageKeyInput: () => isStorageKeyInput, middleware: () => middleware, truthy: () => truthy, useDirectory: () => useDirectory, useDirectoryFileUrls: () => useDirectoryFileUrls, useFileUrl: () => useFileUrl, useRemoveDirectory: () => useRemoveDirectory, useRemoveFiles: () => useRemoveFiles, useUpload: () => useUpload }); module.exports = __toCommonJS(index_exports); // 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 var import_storage_core = require("@supabase-cache-helpers/storage-core"); var import_react2 = require("react"); var import_swr = require("swr"); var import_mutation = __toESM(require("swr/mutation"), 1); // src/mutate/use-random-key.ts var import_react = require("react"); var PREFIX = "random-mutation-key"; var useRandomKey = () => { const key = (0, import_react.useId)(); return [PREFIX, key].join(KEY_SEPARATOR); }; // src/mutate/use-remove-directory.ts function useRemoveDirectory(fileApi, config) { const key = useRandomKey(); const { cache, mutate } = (0, import_swr.useSWRConfig)(); const fetcher = (0, import_react2.useCallback)((0, import_storage_core.createRemoveDirectoryFetcher)(fileApi), [fileApi]); return (0, import_mutation.default)( key, (_0, _1) => __async(this, [_0, _1], function* (_, { arg }) { const result = fetcher(arg); yield (0, import_storage_core.mutatePaths)(getBucketId(fileApi), [arg], { cacheKeys: Array.from(cache.keys()), decode, mutate }); return result; }), config ); } // src/mutate/use-remove-files.ts var import_storage_core2 = require("@supabase-cache-helpers/storage-core"); var import_react3 = require("react"); var import_swr2 = require("swr"); var import_mutation2 = __toESM(require("swr/mutation"), 1); function useRemoveFiles(fileApi, config) { const key = useRandomKey(); const { cache, mutate } = (0, import_swr2.useSWRConfig)(); const fetcher = (0, import_react3.useCallback)((0, import_storage_core2.createRemoveFilesFetcher)(fileApi), [fileApi]); return (0, import_mutation2.default)( key, (_0, _1) => __async(this, [_0, _1], function* (_, { arg: paths }) { const res = yield fetcher(paths); yield (0, import_storage_core2.mutatePaths)(getBucketId(fileApi), paths, { cacheKeys: Array.from(cache.keys()), decode, mutate }); return res; }), config ); } // src/mutate/use-upload.ts var import_storage_core3 = require("@supabase-cache-helpers/storage-core"); var import_react4 = require("react"); var import_swr3 = require("swr"); var import_mutation3 = __toESM(require("swr/mutation"), 1); function useUpload(fileApi, config) { const key = useRandomKey(); const { cache, mutate } = (0, import_swr3.useSWRConfig)(); const fetcher = (0, import_react4.useCallback)((0, import_storage_core3.createUploadFetcher)(fileApi, config), [ config, fileApi ]); return (0, import_mutation3.default)( key, (_0, _1) => __async(this, [_0, _1], function* (_, { arg: { files, path } }) { const result = yield fetcher(files, path); yield (0, import_storage_core3.mutatePaths)( 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 var import_storage_core4 = require("@supabase-cache-helpers/storage-core"); var import_swr4 = __toESM(require("swr"), 1); function useDirectoryFileUrls(fileApi, path, mode, config) { var _a; return (0, import_swr4.default)( path ? [fileApi, path] : null, ([fileApi2, path2]) => (0, import_storage_core4.createDirectoryUrlsFetcher)(mode, config)(fileApi2, path2), __spreadProps(__spreadValues({}, config), { use: [...(_a = config == null ? void 0 : config.use) != null ? _a : [], middleware] }) ); } // src/query/use-directory.ts var import_storage_core5 = require("@supabase-cache-helpers/storage-core"); var import_swr5 = __toESM(require("swr"), 1); function useDirectory(fileApi, path, config) { var _a; return (0, import_swr5.default)( path ? [fileApi, path] : null, ([fileApi2, path2]) => (0, import_storage_core5.fetchDirectory)(fileApi2, path2), __spreadProps(__spreadValues({}, config), { use: [...(_a = config == null ? void 0 : config.use) != null ? _a : [], middleware] }) ); } // src/query/use-file-url.ts var import_storage_core6 = require("@supabase-cache-helpers/storage-core"); var import_swr6 = __toESM(require("swr"), 1); function useFileUrl(fileApi, path, mode, config) { var _a; return (0, import_swr6.default)( path ? [fileApi, path] : null, ([fileApi2, path2]) => (0, import_storage_core6.createUrlFetcher)(mode, config)(fileApi2, path2), __spreadProps(__spreadValues({}, config), { use: [...(_a = config == null ? void 0 : config.use) != null ? _a : [], middleware] }) ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { KEY_PREFIX, KEY_SEPARATOR, assertStorageKeyInput, decode, encode, getBucketId, isStorageKeyInput, middleware, truthy, useDirectory, useDirectoryFileUrls, useFileUrl, useRemoveDirectory, useRemoveFiles, useUpload }); //# sourceMappingURL=index.cjs.map