├── .gitignore ├── webpack.config.js ├── package.json ├── src ├── index.js └── index.d.ts ├── dist └── vlf.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | /* 2 | * @Description: webpack config 3 | * @Author: dmlzj 4 | * @Github: https://github.com/dmlzj 5 | * @Email: 284832506@qq.com 6 | * @Date: 2020-01-10 15:32:32 7 | * @LastEditors: dmlzj 8 | * @LastEditTime: 2020-04-23 14:45:14 9 | * @如果有bug,那肯定不是我的锅,嘤嘤嘤 10 | */ 11 | const path = require('path'); 12 | 13 | module.exports = { 14 | mode: 'production', 15 | entry: './src/index.js', 16 | output: { 17 | libraryTarget: 'commonjs2', 18 | filename: 'vlf.js', 19 | path: path.resolve(__dirname, 'dist') 20 | } 21 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vlf", 3 | "version": "1.1.0", 4 | "description": "vue-localforage", 5 | "main": "dist/vlf.js", 6 | "types": "src/index.d.ts", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1", 9 | "build": "webpack --config webpack.config.js" 10 | }, 11 | "typing": "src/index.d.ts", 12 | "keywords": [ 13 | "vue-localforage" 14 | ], 15 | "author": "dmlzj", 16 | "license": "ISC", 17 | "devDependencies": { 18 | "webpack": "^4.41.5", 19 | "webpack-cli": "^3.3.10" 20 | }, 21 | "dependencies": { 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "git+https://github.com/dmlzj/vlf.git" 26 | }, 27 | "bugs": { 28 | "url": "https://github.com/dmlzj/vlf/issues" 29 | }, 30 | "homepage": "https://github.com/dmlzj/vlf#readme" 31 | } 32 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | /** 4 | * Install plugin 5 | * @param Vue 6 | * @param localForage 7 | */ 8 | 9 | function plugin(Vue, localForage) { 10 | 11 | if (plugin.installed) { 12 | return 13 | } 14 | plugin.installed = true 15 | 16 | if (!localForage) { 17 | console.error('You have to install localForage') 18 | return 19 | } 20 | 21 | Vue.$vlf = localForage 22 | 23 | Object.defineProperties(Vue.prototype, { 24 | 25 | $vlf: { 26 | get() { 27 | return localForage 28 | } 29 | } 30 | 31 | }) 32 | } 33 | 34 | if (typeof exports == "object") { 35 | module.exports = plugin 36 | } else if (typeof define == "function" && define.amd) { 37 | define([], function(){ return plugin }) 38 | } else if (window.Vue && window.localForage) { 39 | Vue.use(plugin, window.localForage) 40 | } 41 | 42 | })(); 43 | -------------------------------------------------------------------------------- /dist/vlf.js: -------------------------------------------------------------------------------- 1 | module.exports=function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){e.exports=function e(t,r){e.installed||(e.installed=!0,r?(t.$vlf=r,Object.defineProperties(t.prototype,{$vlf:{get:()=>r}})):console.error("You have to install localForage"))}}]); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 11 | # vlf 12 | 13 | vue-localforage and support typescript 14 | 15 | > localForage is a fast and simple storage library for JavaScript. localForage improves the offline experience of your web app by using asynchronous storage (IndexedDB or WebSQL) with a simple, `localStorage`-like API. 16 | 17 | > localForage uses localStorage in browsers with no IndexedDB or WebSQL support. See [the wiki for detailed compatibility info][supported browsers]. 18 | 19 | - [localforage repository](https://github.com/localForage/localForage) 20 | - [localforage documentation](https://localforage.github.io/localForage/) 21 | 22 | ## Install 23 | 24 | ```bash 25 | npm install --save localforage vlf 26 | ``` 27 | 28 | ## How to use vlf 29 | 30 | ```javascript 31 | import Vlf from 'vlf' 32 | import localforage from 'localforage' 33 | Vue.use(Vlf, localforage) 34 | ``` 35 | 36 | ## In your code 37 | 38 | ```javascript 39 | // 创建实例 40 | this.$vlf.createInstance({ 41 | storeName: 'user' 42 | }) 43 | // 迭代 44 | this.$vlf.iterate((value, key, num) => { 45 | console.log(key); 46 | }); 47 | // 设置值 48 | this.$vlf.setItem('test', 'hello').then(v => { 49 | console.log(v); 50 | }); 51 | 52 | // ...和官方调用一致 53 | // The other methods of use are the same as the official website, just add a this.$vlf in front, the same behind! 54 | 55 | ``` 56 | --- 57 | -------------------------------------------------------------------------------- /src/index.d.ts: -------------------------------------------------------------------------------- 1 | import Vue, {PluginFunction, PluginObject} from "vue"; 2 | 3 | interface LocalForageDbInstanceOptions { 4 | name?: string; 5 | 6 | storeName?: string; 7 | } 8 | 9 | interface LocalForageOptions extends LocalForageDbInstanceOptions { 10 | driver?: string | string[]; 11 | 12 | size?: number; 13 | 14 | version?: number; 15 | 16 | description?: string; 17 | } 18 | 19 | interface LocalForageDbMethodsCore { 20 | getItem(key: string, callback?: (err: any, value: T) => void): Promise; 21 | 22 | setItem(key: string, value: T, callback?: (err: any, value: T) => void): Promise; 23 | 24 | removeItem(key: string, callback?: (err: any) => void): Promise; 25 | 26 | clear(callback?: (err: any) => void): Promise; 27 | 28 | length(callback?: (err: any, numberOfKeys: number) => void): Promise; 29 | 30 | key(keyIndex: number, callback?: (err: any, key: string) => void): Promise; 31 | 32 | keys(callback?: (err: any, keys: string[]) => void): Promise; 33 | 34 | iterate(iteratee: (value: T, key: string, iterationNumber: number) => U, 35 | callback?: (err: any, result: U) => void): Promise; 36 | } 37 | 38 | interface LocalForageDropInstanceFn { 39 | (dbInstanceOptions?: LocalForageDbInstanceOptions, callback?: (err: any) => void): Promise; 40 | } 41 | 42 | interface LocalForageDriverMethodsOptional { 43 | dropInstance?: LocalForageDropInstanceFn; 44 | } 45 | 46 | // duplicating LocalForageDriverMethodsOptional to preserve TS v2.0 support, 47 | // since Partial<> isn't supported there 48 | interface LocalForageDbMethodsOptional { 49 | dropInstance: LocalForageDropInstanceFn; 50 | } 51 | 52 | interface LocalForageDriverDbMethods extends LocalForageDbMethodsCore, LocalForageDriverMethodsOptional {} 53 | 54 | interface LocalForageDriverSupportFunc { 55 | (): Promise; 56 | } 57 | 58 | interface LocalForageDriver extends LocalForageDriverDbMethods { 59 | _driver: string; 60 | 61 | _initStorage(options: LocalForageOptions): void; 62 | 63 | _support?: boolean | LocalForageDriverSupportFunc; 64 | } 65 | 66 | interface LocalForageSerializer { 67 | serialize(value: T | ArrayBuffer | Blob, callback: (value: string, error: any) => void): void; 68 | 69 | deserialize(value: string): T | ArrayBuffer | Blob; 70 | 71 | stringToBuffer(serializedString: string): ArrayBuffer; 72 | 73 | bufferToString(buffer: ArrayBuffer): string; 74 | } 75 | 76 | interface LocalForageDbMethods extends LocalForageDbMethodsCore, LocalForageDbMethodsOptional {} 77 | 78 | interface LocalForage extends LocalForageDbMethods { 79 | LOCALSTORAGE: string; 80 | WEBSQL: string; 81 | INDEXEDDB: string; 82 | 83 | /** 84 | * Set and persist localForage options. This must be called before any other calls to localForage are made, but can be called after localForage is loaded. 85 | * If you set any config values with this method they will persist after driver changes, so you can call config() then setDriver() 86 | * @param {LocalForageOptions} options? 87 | */ 88 | config(options: LocalForageOptions): boolean; 89 | config(options: string): any; 90 | config(): LocalForageOptions; 91 | 92 | /** 93 | * Create a new instance of localForage to point to a different store. 94 | * All the configuration options used by config are supported. 95 | * @param {LocalForageOptions} options 96 | */ 97 | createInstance(options: LocalForageOptions): LocalForage; 98 | 99 | driver(): string; 100 | 101 | /** 102 | * Force usage of a particular driver or drivers, if available. 103 | * @param {string} driver 104 | */ 105 | setDriver(driver: string | string[], callback?: () => void, errorCallback?: (error: any) => void): Promise; 106 | 107 | defineDriver(driver: LocalForageDriver, callback?: () => void, errorCallback?: (error: any) => void): Promise; 108 | 109 | /** 110 | * Return a particular driver 111 | * @param {string} driver 112 | */ 113 | getDriver(driver: string): Promise; 114 | 115 | getSerializer(callback?: (serializer: LocalForageSerializer) => void): Promise; 116 | 117 | supports(driverName: string): boolean; 118 | 119 | ready(callback?: (error: any) => void): Promise; 120 | } 121 | 122 | 123 | declare module "vue/types/vue" { 124 | 125 | interface Vue { 126 | $vlf: LocalForage; 127 | } 128 | 129 | interface VueConstructor { 130 | $vlf: LocalForage; 131 | } 132 | } 133 | 134 | declare class vlf { 135 | static install: PluginFunction; 136 | } 137 | 138 | export default vlf; 139 | --------------------------------------------------------------------------------