Coder Social home page Coder Social logo

better-storage's Introduction

Welcome to better-storage 👋

Version License: MIT

安装

npm install bt-storage

示例

import BetterStorage from "bt-storage";

const stg = new BetterStorage({
  storage: localStorage,         // localStorage or sessionStorage
  prefix: 'BIU',
  expires: 0,
  secret: false,
  AES: {
    secret: '30D17839695CB24C',    // AES secret key
    iv: '8BCAB84D2148034C'         // AES IV
  },
})

stg.set('test', 'some value', {
  expires: 1000, // 1秒后过期
  secret: true,  // 加密存储
})

参数说明

instanceConfig:

属性 说明 类型 是否必填 默认值
storage storage类型,为localStorage或sessionStorage Storage --
prefix 存储的key的前缀 string --
expires 存储持续时间,为0则不过期 number 0
secret 存储时是否加密 boolean false
AES.secret 加密存储时的AES secret key,为8n位的16进制字符 string 30D17839695CB24C
AES.iv 加密存储时的AES IV,为8n位的16进制字符 string 8BCAB84D2148034C

setConfig:

属性 说明 类型 是否必填 默认值
expires 存储持续时间,为0则不过期 number 取自instance config
secret 存储时是否加密 boolean 取自instance config

storageValue:

属性 说明 类型
expires 过期的时间戳 number
secret 是否被加密 boolean
value 被存储的数据 any
signature 签名,用于读取时校验数据是否被更改 string

API

创建实例

constructor(options: instanceConfig): storageInstance

const stg = new BetterStorage({
  storage: localStorage,
  prefix: 'BIU',
  expires: 0,
  secret: false,
  AES: {
    secret: '01234567',
    iv: '0123456789abcdef'
  },
})

set(key: string, value: any, config: setConfig): void

向storage中存数据

stg.set('hello', 'world')

/* 存储结果
'@BIU/hello'
{
  "secret":false,
  "value":"world",
  "expires":0,
  "signature":"8E162389DE1AB8B27CB8D41C17AC94AD08721FC7"
}
*/

stg.set('hello', 'world', {
  secret: true,
  expires: 2000,
})

/* 存储结果
'@BIU/hello'
{
  "secret":true,
  "value":"e1FcAN/aPkJR/dCAWIl10Q==",
  "expires":1573025822648,
  "signature":"1A867EFC666516B7B0E7CC4552EFA969106A907F"
}
*/

get(key: string): any

从storage中取数据,未取到时返回null

const result = stg.get('hello') // "world"

remove(key: string): void

从storage中移除相应数据

stg.remove('hello')

stg.get('hello') // null

clear(): void

移除所有由该实例存入的数据

localStorage.setItem('hi', 'hi from origin');
stg.set('hi', 'hi from stg');
stg.set('hello', 'hello from stg');

localStorage.getItem('hi') // "hi from origin"
stg.get('hi') // "hi from stg"
stg.get('hello') // "hello from stg"

stg.clear();
localStorage.getItem('hi') // "hi from origin"
stg.get('hi') // null
stg.get('hello') // null

readable(): void

允许调用Api get

unreadable(): void

禁止调用Api get

stg.get('hello') // "world"

stg.unreadable()
stg.get('hello') // Error: this storage is unreadable for now!

keys: string[]

stg.set('a', 1)
stg.set('b', 1)
stg.set('c', 1)

console.log(stg.keys) // ["a", "b", "c"]

length: number

stg.set('a', 1)
stg.set('b', 1)
stg.set('c', 1)

console.log(stg.length) // 3

Author

👤 render

better-storage's People

Contributors

zjcrender avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.