Skip to content

Commit

Permalink
feat: #17 add index.d.ts and refactor code by @ts-check (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve authored May 5, 2019
1 parent c8d79d7 commit b8e4edf
Show file tree
Hide file tree
Showing 21 changed files with 285 additions and 108 deletions.
12 changes: 6 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
"extends": "standard",
"parser": "babel-eslint",
"rules": {
"indent": [2, 4],
"promise/param-names": 0,
"comma-dangle": [2, "always-multiline"],
extends: 'standard',
parser: 'babel-eslint',
rules: {
'indent': [2, 4],
'promise/param-names': 0,
'comma-dangle': [2, 'always-multiline'],
},
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "tua-storage",
"version": "1.7.1",
"version": "1.7.2",
"description": "🏗 A common storage for web(localStorage), for RN(AsyncStorage), for mini-program(wx) or just memory cache(Node.js)",
"main": "dist/TuaStorage.cjs.js",
"module": "dist/TuaStorage.esm.js",
"unpkg": "dist/TuaStorage.umd.js",
"jsdelivr": "dist/TuaStorage.umd.js",
"typings": "src/index.d.ts",
"files": [
"src/",
"dist/"
Expand Down Expand Up @@ -51,7 +52,8 @@
},
"collectCoverage": true,
"collectCoverageFrom": [
"src/**"
"src/**",
"!src/index.d.ts"
],
"setupFiles": [
"jest-localstorage-mock"
Expand All @@ -76,6 +78,7 @@
"@babel/preset-env": "^7.3.1",
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@types/jest": "^24.0.12",
"@vue/test-utils": "^1.0.0-beta.29",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
Expand All @@ -101,6 +104,7 @@
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-uglify": "^6.0.2",
"typescript": "^3.4.5",
"vuepress": "^1.0.0-alpha.39"
},
"keywords": [
Expand Down
4 changes: 3 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// @ts-check

import json from 'rollup-plugin-json'
import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace'
import { eslint } from 'rollup-plugin-eslint'
import { uglify } from 'rollup-plugin-uglify'

import pkg from './package.json'
import * as pkg from './package.json'

const input = `src/index.js`
const banner = `/* ${pkg.name} version ${pkg.version} */`
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

export const ERROR_MSGS = {
key: 'Please input key or fullKey!',
promise: 'SyncFn MUST return a Promise!',
Expand Down
11 changes: 8 additions & 3 deletions src/decorators.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

import { ERROR_MSGS } from './constants'

/**
Expand Down Expand Up @@ -53,7 +55,7 @@ const getFullKey = getDecorator((method) =>
/**
* 让函数支持数组参数的装饰器
*/
const supportArrayParam = (isAsync = true) => getDecorator((method) =>
const supportArrayParams = (isAsync) => getDecorator((method) =>
/**
* 参数是数组,则使用 Promise.all 并发调用原函数
*/
Expand All @@ -65,6 +67,8 @@ const supportArrayParam = (isAsync = true) => getDecorator((method) =>
return isAsync ? Promise.all(mapResult) : mapResult
}
)
const syncArrayParams = supportArrayParams(false)
const asyncArrayParams = supportArrayParams(true)

/**
* 保存数据时获取将被保存的数据
Expand All @@ -85,7 +89,7 @@ const getDataToSave = getDecorator((method) =>
const realExpires = isNeverExpired
// 永不超时
? this.neverExpireMark
: parseInt(Date.now() / 1000) + expires
: Math.floor(Date.now() / 1000) + expires
const dataToSave = { rawData, expires: realExpires }

return method.call(this, { ...rest, dataToSave })
Expand All @@ -96,5 +100,6 @@ export {
checkKey,
getFullKey,
getDataToSave,
supportArrayParam,
syncArrayParams,
asyncArrayParams,
}
108 changes: 108 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { PluginFunction } from 'vue'

/* -- types -- */

export type AnyFunction = (...args: any[]) => any
export type AnyPromiseFunction<T = any> = (...args: any[]) => Promise<T>

export type SaveParamsType = SaveParamsWithKey | SaveParamsWithFullKey
export type LoadParamsType = LoadParamsWithKey | LoadParamsWithFullKey
export type RemoveParamsType = string | RemoveParams
export type LoadSyncParamsType = LoadSyncParamsWithKey | LoadSyncParamsWithFullKey

/* -- interfaces -- */

export interface KeyAndSyncParams {
key: string
syncParams?: object
}
export interface FullKey {
fullKey: string
}

export interface SaveParams {
data?: any
expires?: number
isEnableCache?: boolean
[k: string]: any
}
export interface SaveParamsWithKey extends KeyAndSyncParams, SaveParams {}
export interface SaveParamsWithFullKey extends FullKey, SaveParams {}

export interface LoadSyncParams {
isEnableCache?: boolean
[k: string]: any
}
export interface LoadParams extends LoadSyncParams {
syncFn?: AnyPromiseFunction
expires?: number
isAutoSave?: boolean
syncOptions?: object | any[]
isForceUpdate?: boolean
[k: string]: any
}
export interface LoadParamsWithKey extends KeyAndSyncParams, LoadParams {}
export interface LoadParamsWithFullKey extends FullKey, LoadParams {}
export interface LoadSyncParamsWithKey extends KeyAndSyncParams, LoadSyncParams {}
export interface LoadSyncParamsWithFullKey extends FullKey, LoadSyncParams {}

export interface RemoveParams {
prefix: string
}

export interface TuaStorageOptions {
whiteList?: string[],
syncFnMap?: object,
autoClearTime?: number,
storageEngine?: null | object,
defaultExpires?: number,
neverExpireMark?: null | string,
storageKeyPrefix?: string,
isEnableAutoClear?: boolean,
}

export interface TuaStorageClass {
/**
* https://tuateam.github.io/tua-storage/config-methods/default.html
*/
new (args?: TuaStorageOptions): TuaStorageInstance

/**
* https://tuateam.github.io/tua-storage/config-methods/default.html
*/
install: PluginFunction<TuaStorageOptions>
}

export interface TuaStorageInstance {
// public
save: <T = any>(item: SaveParamsType | SaveParamsType[]) => Promise<T>
load: <T = any>(item: LoadParamsType | LoadParamsType[]) => Promise<T>
clear: <T = any>(whiteList?: string[]) => Promise<T>
remove: <T = any>(prefix: RemoveParamsType | RemoveParamsType[]) => Promise<T>
getInfo: <T = any>() => Promise<T>
saveSync: (item: SaveParamsType | SaveParamsType[]) => any
loadSync: (item: LoadSyncParamsType | LoadSyncParamsType[]) => any
clearSync: (whiteList?: string[]) => any
removeSync: (prefix: RemoveParamsType | RemoveParamsType[]) => any
getInfoSync: () => any

// private
_cache: object
_clearExpiredData: <T = any>() => Promise<T>
}

/* -- export default -- */

declare const TuaStorage: TuaStorageClass
export default TuaStorage

/* -- vue plugin -- */

declare module 'vue/types/vue' {
interface Vue {
/**
* https://tuateam.github.io/tua-storage/config-methods/methods.html
*/
$tuaStorage: TuaStorageInstance
}
}
Loading

0 comments on commit b8e4edf

Please sign in to comment.