marshallswain / feathers-pinia

Connect your Feathers API to the elegant data store for Vue
52 stars 22 forks source link

custom storage option #146

Open ericuldall opened 10 months ago

ericuldall commented 10 months ago

I use feathers pinia in a chrome extension and would be great if I could sync with chrome.storage.sync or chrome.storage.local which does not conform to the standard browser Storage {} interface.

Is there a way to do this in v3?

ericuldall commented 10 months ago

Just for an example, I have to do all sorts of trickery to get auth working:

const authStore = useAuthStore();
chrome.storage.onChanged.addListener(async (changes, area) => {
    if (area == 'local' && changes['feathers-jwt']) {
        await authStore.authenticate({
            strategy: "jwt",
            accessToken: changes['feathers-jwt'].newValue
        })
        router.push('/');
    }
});
(async () => {
    let token;
    if (chrome?.storage?.local) {
        token = await chrome.storage.local.get(['feathers-jwt']);
    }
    let preAuth;
    if (token?.['feathers-jwt']) {
        preAuth = authStore.authenticate({
            strategy: "jwt",
            accessToken: token['feathers-jwt']
        })
    } else {
        preAuth = Promise.resolve();
    }
    await preAuth.catch((e) => {});
    app.mount('#app')
})();

If feather-pinia already understood how to work with chrome.storage I could probably cut out a lot of this hacky stuff.

marshallswain commented 10 months ago

Yep. I think you can do the following in a PR:

  1. Make your own replacement utilities for the localstorage plugin found here: https://github.com/marshallswain/feathers-pinia/tree/main/src/localstorage
  2. Add clearStorage and storagePlugin options to the PiniaClientConfig: https://github.com/marshallswain/feathers-pinia/blob/main/src/create-pinia-client.ts#L35
  3. If the options.storagePlugin is provided, use it instead of __sync here: https://github.com/marshallswain/feathers-pinia/blob/main/src/create-pinia-client.ts#L137C9-L137C15
  4. If options.clearStorage is provided, use it here: https://github.com/marshallswain/feathers-pinia/blob/main/src/create-pinia-client.ts#L176

I think that should do the trick.

ericuldall commented 10 months ago

I've started a draft PR here: https://github.com/marshallswain/feathers-pinia/pull/147/files Any comments are appreciated. I'll be testing and updating this week.