Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(drivers): add session storage driver #179

Merged
merged 1 commit into from
Mar 27, 2023
Merged

feat(drivers): add session storage driver #179

merged 1 commit into from
Mar 27, 2023

Conversation

CorentinTh
Copy link
Contributor

Hi !

This PR adds the session storage driver

  • Driver implementation
  • Unit test
  • Updated doc

@nuxt-studio
Copy link

nuxt-studio bot commented Mar 8, 2023

Live Preview ready!

Name Edit Preview Latest Commit
unstorage Edit on Studio ↗︎ View Live Preview 813c8f9

@CorentinTh
Copy link
Contributor Author

It has a similar api to the local storage driver, so one could have use :

const storage = createStorage({
  driver: localStorageDriver({ base: "app:", localStorage: window.sessionStorage }),
});

So what do you think of this driver ?

@CorentinTh
Copy link
Contributor Author

Since they have a similar API with localstorage, it can also be written like :

import { defineDriver } from "./utils";
import localStorageDriver from './localstorage'

export interface SessionStorageOptions {
  base?: string;
  window?: typeof window;
  sessionStorage?: typeof window.sessionStorage;
}

export default defineDriver((opts: SessionStorageOptions = {}) => {
  if (!opts.window) {
    opts.window = typeof window !== "undefined" ? window : undefined;
  }
  if (!opts.sessionStorage) {
    opts.sessionStorage = opts.window?.sessionStorage;
  }
  if (!opts.sessionStorage) {
    throw new Error("localStorage not available");
  }

 return localStorageDriver({...opts, localStorage: opts.sessionStorage})
});

But it induces coupling between drivers, and decrease maintainability 🤷🏻

@CorentinTh
Copy link
Contributor Author

Hi @pi0 !
Let me know if I have something to change 🙏🏻

Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 💯

Regarding refactor to extend local storage, let's see how frequently we need to update both and if makes sense to extend for easier maintenance.

@pi0 pi0 merged commit 6ab28f7 into unjs:main Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants