Skip to content

Commit

Permalink
First Commit ⚡🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
eeprojectsee committed Oct 10, 2022
0 parents commit 8fff94a
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
.DS_Store
dist-ssr
coverage
*.local

/cypress/videos/
/cypress/screenshots/

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

demo
test
/index.html
jsconfig.json
33 changes: 33 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
.DS_Store
dist-ssr
coverage
*.local

/cypress/videos/
/cypress/screenshots/

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

demo
dist
test
index.html
jsconfig.json
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Pinia Storage

![npm license](https://img.shields.io/npm/l/@ctechhindi/Pinia-Storage.svg)
![npm download](https://img.shields.io/npm/dm/@ctechhindi/Pinia-Storage.svg)
![GitHub top language](https://img.shields.io/github/languages/top/ctechhindi/Pinia-Storage.svg)
![GitHub issues](https://img.shields.io/github/issues/ctechhindi/Pinia-Storage.svg)
[![npm package](https://img.shields.io/npm/v/@ctechhindi/Pinia-Storage.svg)](https://www.npmjs.com/package/@ctechhindi/Pinia-Storage)

> A infinite progress bar for vue 3
- [GitHub](https://github.com/ctechhindi/Pinia-Storage)

## Requirements

- Vue.js `^3.0.0`

## Installation

### npm

```
npm i @ctechhindi/Pinia-Storage
```

## Usage

**`main.js`**

```js
import piniaStorage from "@ctechhindi/pinia-storage";
const pinia = createPinia();
pinia.use(piniaStorage);
```

**`\stores\{sites}-store.js`**

```js
import { defineStore } from "pinia";

export const useUsersStore = defineStore({
id: "users-store",
state: () => ({
name: {
first: "",
last: "",
},
}),
conserve: {
enabled: true,
strategies: [
{ storage: localStorage, states: ["name"] }, // Save custom state
// { storage: localStorage, }, // Save all state
// { storage: sessionStorage, states: ['name'] },
],
},
getters: {},
actions: {},
});
```

### Custom LocalStorage

**`\stores\{sites}-store.js`**

```js
import { defineStore } from "pinia";

// Custom
const secureStorage = {
setItem(key, state) {
localStorage.setItem(key, state)
},
getItem(key) {
localStorage.getItem(key, state)
},
}

export const useUsersStore = defineStore({
id: "users-store",
state: () => ({
name: {
first: "",
last: "",
},
}),
conserve: {
enabled: true,
strategies: [
{ storage: secureStorage, states: ["name"] }, // Save custom state
],
},
getters: {},
actions: {},
});
```

# License

[MIT License](https://opensource.org/licenses/MIT)
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@ctechhindi/pinia-storage",
"version": "1.0.0",
"description": "Store Vue 3 Store data in the browser LocalStorage",
"homepage": "https://github.com/ctechhindi/Pinia-Storage",
"bugs": "https://github.com/ctechhindi/Pinia-Storage/issues",
"email": "[email protected]",
"main": "src/index.js",
"module": "src/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/ctechhindi/Pinia-Storage.git"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview --port 4173",
"publish": "npm publish --access public"
},
"keywords": [
"vue3",
"pinia",
"pinia-storage",
"storage",
"vue",
"ctechhindi"
],
"author": "Jeevan Lal",
"license": "MIT",
"devDependencies": {
"@vitejs/plugin-vue": "^3.0.3",
"vite": "^3.0.9",
"vue": "^3.2.38",
"vue-router": "^4.1.5"
}
}
46 changes: 46 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Update Storage
* @param {Object} conserve
* @param {Object} store
*/
export const updateStorage = (conserve, store) => {
const storage = conserve.storage || sessionStorage
const storeKey = conserve.key || store.$id

if (conserve.states) {
const partialState = conserve.states.reduce((finalObj, key) => {
finalObj[key] = store.$state[key];
return finalObj;
}, {})

storage.setItem(storeKey, JSON.stringify(partialState))
} else {
storage.setItem(storeKey, JSON.stringify(store.$state))
}
}

// Default
function piniaStorage({ options, store }) {
if (options.conserve && options.conserve.enabled && options.conserve.strategies && options.conserve.strategies.length > 0) {

// Get Storage and Set in the Store
options.conserve.strategies.forEach((s) => {
const storage = s.storage || sessionStorage
const storeKey = s.key || store.$id
const storageResult = storage.getItem(storeKey)

if (storageResult) {
store.$patch(JSON.parse(storageResult))
updateStorage(s, store)
}
});

store.$subscribe((mutations, state) => {
options.conserve.strategies.forEach((s) => {
updateStorage(s, store)
})
}, { detached: true });
}
}

export default piniaStorage;
13 changes: 13 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fileURLToPath, URL } from 'url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./test', import.meta.url))
}
}
})

0 comments on commit 8fff94a

Please sign in to comment.