UNPKG

@hot-updater/plugin-core

Version:

React Native OTA solution for self-hosted

57 lines (55 loc) 1.7 kB
//#region src/createStoragePlugin.ts /** * Creates a storage plugin with lazy initialization and automatic hook execution. * * This factory function abstracts the double currying pattern used by all storage plugins, * ensuring consistent lazy initialization behavior across different storage providers. * Hooks are automatically executed at appropriate times without requiring manual invocation. * * @param options - Configuration options for the storage plugin * @returns A double-curried function that lazily initializes the storage plugin * * @example * ```typescript * export const s3Storage = createStoragePlugin<S3StorageConfig>({ * name: "s3Storage", * supportedProtocol: "s3", * factory: (config) => { * const client = new S3Client(config); * return { * async upload(key, filePath) { ... }, * async delete(storageUri) { ... }, * async getDownloadUrl(storageUri) { ... } * }; * } * }); * ``` */ const createStoragePlugin = (options) => { return (config, hooks) => { return () => { let cachedMethods = null; const getMethods = () => { if (!cachedMethods) cachedMethods = options.factory(config); return cachedMethods; }; return { name: options.name, supportedProtocol: options.supportedProtocol, async upload(key, filePath) { const result = await getMethods().upload(key, filePath); await hooks?.onStorageUploaded?.(); return result; }, async delete(storageUri) { return getMethods().delete(storageUri); }, async getDownloadUrl(storageUri) { return getMethods().getDownloadUrl(storageUri); } }; }; }; }; //#endregion exports.createStoragePlugin = createStoragePlugin;