UNPKG

unstorage-pinia-plugin

Version:

Pinia store persistence and hydration using Unstorage

1 lines 3.25 kB
{"version":3,"file":"unstorage-pinia-plugin.cjs","sources":["../src/index.ts"],"sourcesContent":["import type { PiniaPluginContext, StateTree, Store } from 'pinia';\nimport { createStorage } from 'unstorage';\nimport type { Driver, Storage } from 'unstorage';\n\nexport interface UnstorageStoreOptions {\n driver: Driver,\n filter?: Array<string>\n storage?: Storage\n}\n\ndeclare module 'pinia' {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n export interface DefineStoreOptionsBase<S extends StateTree, Store> {\n unstorage?: UnstorageStoreOptions\n }\n}\n\nconst configureStore = (store: Store, storage: Storage, filter?: Array<string>) => {\n storage.getItem(store.$id).then((state) => {\n store.$patch(state);\n });\n\n const _filter = (filter) ?? Object.keys(store.$state);\n\n store.$subscribe(() => storage.setItem(store.$id,\n JSON.stringify(Object.fromEntries(\n Object.entries(store.$state).filter(([key]) => (_filter.indexOf(key) ?? 0) > -1)\n ))\n ));\n\n storage.setItem(store.$id,\n JSON.stringify(Object.fromEntries(\n Object.entries(store.$state).filter(([key]) => (_filter.indexOf(key) ?? 0) > -1)\n ))\n );\n};\n\nconst unstorageOptionsBag: Record<string, UnstorageStoreOptions> = {};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const persistStore = (store: any, unstorageOptions: UnstorageStoreOptions) => {\n unstorageOptionsBag[(store as Store).$id] = unstorageOptions;\n return store;\n};\n\nexport interface UnstoragePluginOptions {\n driver?: Driver\n}\n\nexport const createUnstoragePlugin = ({ driver }: UnstoragePluginOptions = {}) => ({ options, store }: PiniaPluginContext) => {\n if(options.unstorage) {\n configureStore(store, createStorage({ driver: options.unstorage.driver }), options.unstorage.filter);\n }\n else if(unstorageOptionsBag[store.$id]) {\n configureStore(store, createStorage({ driver: unstorageOptionsBag[store.$id].driver }), unstorageOptionsBag[store.$id].filter );\n }\n else if(driver) {\n configureStore(store, createStorage({ driver }));\n }\n};\n"],"names":["configureStore","store","storage","filter","state","_filter","key","unstorageOptionsBag","persistStore","unstorageOptions","createUnstoragePlugin","driver","options","createStorage"],"mappings":"6GAiBMA,EAAiB,CAACC,EAAcC,EAAkBC,IAA2B,CACjFD,EAAQ,QAAQD,EAAM,GAAG,EAAE,KAAMG,GAAU,CACzCH,EAAM,OAAOG,CAAK,CAAA,CACnB,EAED,MAAMC,EAAWF,GAAW,OAAO,KAAKF,EAAM,MAAM,EAE9CA,EAAA,WAAW,IAAMC,EAAQ,QAAQD,EAAM,IAC3C,KAAK,UAAU,OAAO,YACpB,OAAO,QAAQA,EAAM,MAAM,EAAE,OAAO,CAAC,CAACK,CAAG,KAAOD,EAAQ,QAAQC,CAAG,GAAK,GAAK,EAAE,CAAA,CAChF,CAAA,CACF,EAEOJ,EAAA,QAAQD,EAAM,IACpB,KAAK,UAAU,OAAO,YACpB,OAAO,QAAQA,EAAM,MAAM,EAAE,OAAO,CAAC,CAACK,CAAG,KAAOD,EAAQ,QAAQC,CAAG,GAAK,GAAK,EAAE,CAAA,CAChF,CAAA,CAEL,EAEMC,EAA6D,CAAA,EAGtDC,EAAe,CAACP,EAAYQ,KAClBF,EAAAN,EAAgB,GAAG,EAAIQ,EACrCR,GAOIS,EAAwB,CAAC,CAAE,OAAAC,CAAO,EAA4B,CAAC,IAAM,CAAC,CAAE,QAAAC,EAAS,MAAAX,KAAgC,CACzHW,EAAQ,UACMZ,EAAAC,EAAOY,gBAAc,CAAE,OAAQD,EAAQ,UAAU,MAAQ,CAAA,EAAGA,EAAQ,UAAU,MAAM,EAE7FL,EAAoBN,EAAM,GAAG,EACnCD,EAAeC,EAAOY,EAAA,cAAc,CAAE,OAAQN,EAAoBN,EAAM,GAAG,EAAE,MAAA,CAAQ,EAAGM,EAAoBN,EAAM,GAAG,EAAE,MAAO,EAExHU,GACNX,EAAeC,EAAOY,EAAAA,cAAc,CAAE,OAAAF,CAAA,CAAQ,CAAC,CAEnD"}