UNPKG

init-storage

Version:

为应用中的storage提供丝滑的typescript支持

73 lines (56 loc) 3.12 kB
## 为何而生 你是不是受够了`Storage.getItem(key: string): string | null`没有类型提示的日子 你是不是需要一个在应用中集成一个清晰可见的存储空间列表 * { * token: number * info: { id: string, pwd: string} * version: string * } 那请你试用下`init-storage`吧! --- ## 特性 - 📦包大小仅为1kb - ✅无任何生产依赖。 - 💪🏼使用原生 Javascript Api,支持任意框架使用 - 💯100%测试场景覆盖 - 🚀SSR支持 ## 快速开始 ```bash // npm npm install init-storage --save ``` ```typescript import { setInitStorage } from "init-storage"; const { getLocalStorageItem } = setInitStorage( local: { local: { token: "1" }, session: {}, }, session: {} ) console.log(getLocalStorageItem('token')) // null console.log(getLocalStorageItem('token', true)) // "1" ``` **内部自动通过关键字`typeof`进行类型推断,当你认为自动推断的类型有误时,使用`as`关键字可以帮助进行类型拓展** 下面是可以调用的方法: | 方法 | 说明 | 类 型 | |--------------------------|--------------------|--------------------------------------------------------------------| | getLocalStorageItem | 获取本地存储空间 | (item: 'aa' \| 'bb' \| '...', ifNullIsGetInit: boolean) => void | | setLocalStorageItem | 存储本地存储空间(某键) | (item: 'aa' \| 'bb' \| '...', value: ValueType) => void | | removeLocalStorageItem | 移除本地存储空间(某键) | (item: 'aa' \| 'bb' \| '...') => void | | clearLocalStorageItem | 清空本地存储空间 | (item: 'aa' \| 'bb' \| '...') => void | | getSessionStorageItem | 获取会话存储空间 | (item: 'aa' \| 'bb' \| '...', ifNullIsGetInit: boolean) => void | | setSessionStorageItem | 存储会话存储空间(某键) | (item: 'aa' \| 'bb' \| '...', value: ValueType) => void | | removeSessionStorageItem | 移除会话存储空间(某键) | (item: 'aa' \| 'bb' \| '...') => void | | clearSessionStorageIte | 清空会话存储空间 | (item: 'aa' \| 'bb' \| '...') => void | 同时`setInitStorage(INIT, option)`还有用可配置项,下面是一些有关配置项: | 参数 | 说明 | 类 型 | 默认值 | |----------------|-------------------------------------|------------|---------------------------------| | defaultUseInit | 返回的方法中ifNullIsGetInit参数的默认值 | boolean | false (如果存储中没有就返回null) | ## 示例 * [React](https://codesandbox.io/s/init-storage-react-fgkfhp?file=/src/App.tsx) * [Angular](https://codesandbox.io/s/init-storage-angular-e7ul73?file=/src/app/app.component.ts) * [Vue](https://codesandbox.io/s/init-storage-vue-3sq17x?file=/src/components/MyComponent.vue) --- ## 许可证 MIT