strata-storage
Version:
Zero-dependency universal storage plugin providing a unified API for all storage operations across web, Android, and iOS platforms
94 lines (93 loc) • 2.87 kB
JavaScript
/**
* Strata Storage Capacitor Plugin
* Main plugin registration and web implementation
* This is now optional and only loaded when Capacitor adapters are used
*/
// Mock implementation for when Capacitor is not available
const mockPlugin = {
isAvailable: async () => ({ available: false }),
get: async () => ({ value: null }),
set: async () => { },
remove: async () => { },
clear: async () => { },
keys: async () => ({ keys: [] }),
size: async () => ({ total: 0, count: 0 }),
};
// Create a lazy-loading wrapper that only attempts to load Capacitor when actually used
class LazyStrataStoragePlugin {
plugin;
attempted = false;
getPlugin() {
if (this.attempted) {
return this.plugin || mockPlugin;
}
this.attempted = true;
if (typeof window !== 'undefined') {
const cap = window.Capacitor;
if (cap && cap.registerPlugin) {
try {
this.plugin = cap.registerPlugin('StrataStorage', {
web: () => import("./web.js").then((m) => new m.StrataStorageWeb()),
});
}
catch (error) {
console.warn('Failed to register StrataStorage plugin:', error);
this.plugin = mockPlugin;
}
}
else {
this.plugin = mockPlugin;
}
}
else {
this.plugin = mockPlugin;
}
return this.plugin;
}
async isAvailable(options) {
return this.getPlugin().isAvailable(options);
}
async get(options) {
return this.getPlugin().get(options);
}
async set(options) {
return this.getPlugin().set(options);
}
async remove(options) {
return this.getPlugin().remove(options);
}
async clear(options) {
return this.getPlugin().clear(options);
}
async keys(options) {
return this.getPlugin().keys(options);
}
async size(options) {
return this.getPlugin().size(options);
}
// Optional methods delegated to the underlying plugin
get setKeychain() {
return this.getPlugin().setKeychain;
}
get getKeychain() {
return this.getPlugin().getKeychain;
}
get setEncryptedPreference() {
return this.getPlugin().setEncryptedPreference;
}
get getEncryptedPreference() {
return this.getPlugin().getEncryptedPreference;
}
get query() {
return this.getPlugin().query;
}
get getUserDefaults() {
return this.getPlugin().getUserDefaults;
}
get setUserDefaults() {
return this.getPlugin().setUserDefaults;
}
}
// Export the lazy-loading plugin
export const StrataStorage = new LazyStrataStoragePlugin();
export * from "./definitions.js";