diff --git a/package-lock.json b/package-lock.json index 24634f3..dd2a75d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5348,6 +5348,18 @@ } } }, + "mobx": { + "version": "5.11.0", + "resolved": "https://registry.npmjs.org/mobx/-/mobx-5.11.0.tgz", + "integrity": "sha512-WMZCgjuW9vo2MhumrhJa0byTGdlZQcTKzLW9LoqMdmqVqWnG74HOSXWJ0YpxBngsc/rAUMwvdy+CVnw+/a4yeg==", + "dev": true + }, + "mobx-state-tree": { + "version": "3.14.0", + "resolved": "https://registry.npmjs.org/mobx-state-tree/-/mobx-state-tree-3.14.0.tgz", + "integrity": "sha512-obQ+LPCff4LqX86ghQpYIVAtCmps7ewCxIds/EmSxBULj9+7Qs2v4YEbvUeWymp/Sj3dPcvDjIMMdIRz6sUE5Q==", + "dev": true + }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", diff --git a/package.json b/package.json index 0e8e153..1450e66 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "mst-persist", "version": "0.0.2", "description": "Persist and hydrate MobX-state-tree stores", - "main": "src/index.js", + "main": "dist/mst-persist.esm.js", "files": [ - "src/" + "dist/" ], "scripts": { "start": "tsdx watch", @@ -33,6 +33,8 @@ "mobx-state-tree": "^3.2.1" }, "devDependencies": { + "mobx": "^5.11.0", + "mobx-state-tree": "^3.14.0", "tsdx": "^0.7.2", "typescript": "^3.5.2" } diff --git a/src/asyncLocalStorage.js b/src/asyncLocalStorage.ts similarity index 60% rename from src/asyncLocalStorage.js rename to src/asyncLocalStorage.ts index 7072aa2..4a91d52 100644 --- a/src/asyncLocalStorage.js +++ b/src/asyncLocalStorage.ts @@ -1,4 +1,11 @@ -export const AsyncLocalStorage = { +interface IAsyncLocalStorage { + clear(): Promise + getItem(key: string): Promise + removeItem(key: string): Promise + setItem(key: string, value: string): Promise +} + +export const AsyncLocalStorage: IAsyncLocalStorage = { clear () { return callWithPromise(window.localStorage.clear) }, @@ -13,7 +20,7 @@ export const AsyncLocalStorage = { } } -function callWithPromise (func, ...args) { +function callWithPromise (func: Function, ...args: any[]): Promise { try { return Promise.resolve(func(...args)) } catch (err) { diff --git a/src/index.js b/src/index.ts similarity index 73% rename from src/index.js rename to src/index.ts index df9f34a..71abbf5 100644 --- a/src/index.js +++ b/src/index.ts @@ -1,8 +1,18 @@ import { onSnapshot, applySnapshot } from 'mobx-state-tree' -import AsyncLocalStorage from './asyncLocalStorage.js' +import AsyncLocalStorage from './asyncLocalStorage' -export const persist = (name, store, options = {}) => { +export interface IArgs { + (name: string, store: object, options?: IOptions): Promise +} +export interface IOptions { + storage?: any, + jsonify?: boolean, + readonly whitelist?: Array, + readonly blacklist?: Array +} + +export const persist: IArgs = (name, store, options = {}) => { let {storage, jsonify, whitelist, blacklist} = options if (typeof window.localStorage !== 'undefined' && (!storage || storage === window.localStorage)) { @@ -28,7 +38,7 @@ export const persist = (name, store, options = {}) => { }) return storage.getItem(name) - .then((data) => { + .then((data: any) => { const snapshot = !jsonify ? data : JSON.parse(data) // don't apply falsey (which will error), leave store in initial state if (!snapshot) { return } @@ -36,7 +46,7 @@ export const persist = (name, store, options = {}) => { }) } -function arrToDict (arr) { +function arrToDict (arr?: Array): object { if (!arr) { return {} } return arr.reduce((dict, elem) => { dict[elem] = true