# Storage

封装了对应用数据（业务数据、配置数据）的统一操作

Storage 工具，可以自动分析 Url，localStorage，UC SDK 中关于当前应用的信息，例如组织id、组织名称、sdp-app-id、语言环境等

使用方通过使用 Storage 工具，方便的获取到这些信息

以下为内置的查找字段及其别名（不在此表内的数据原封不动存入 storage）

```js
// Storage.ALIAS_KEYS_MAP
'sdp-app-id': ['appId', '__app_id'],
'sdp-migrated': ['SDP-MIGRATED', 'migrated'],
'sdp-user-id': ['user-id'],
'vorg': ['vorgName', 'vOrgName'],
'vOrgID': ['vOrgId', 'vorgId'],
'Orgname': ['orgName', 'org_name'],
'language': ['lang', 'locale']
```

## 安装

```bash
npm i @sdp.nd/storage --save
```

## 使用

### 初始化
```js
/**
 * 创建 Storage
 * @export
 * @param {object} options 配置项
 *  {
 *    strategy    {Strategy[]}        数据初始化策略，默认值：[], 目前提供的 Strategy 如下：
 *                                        @sdp.nd/storage/lib/strategy-hash      从 url hash 解析数据
 *                                        @sdp.nd/storage/lib/strategy-search    从 url search 解析数据
 *                                        @sdp.nd/storage/lib/strategy-db        从 localStorage.SDP_APP_DATA 解析数据
 *                                        @sdp.nd/storage/lib/strategy-remote    从 SDK_UC 解析数据（异步）
 *                                        @sdp.nd/storage/lib/strategy-global    从 window.SDP_APP_DATA 解析数据
 *    data        {object}            初始化数据
 *  }
 * @returns {Storage} Storage 实例
 */
createStorage (options: Object = {})


// Example 不使用默认策略，有业务方自己定义数据
import createStorage from '@sdp.nd/storage' // commonjs 使用 const createStorage = require('@sdp.nd/storage')
const storage = createStorage({data: {Orgname: 'nd'}})
console.log(storage.getOrgName()) // 输出 nd


// Example 使用 hash 策略，从 url hash 解析数据
import createStorage from '@sdp.nd/storage'
import hash from '@sdp.nd/storage/lib/strategy-hash'
window.location.hash = `#/?sdp-app-id=1-2-3-4`
const storage = createStorage({strategy: [hash]})
console.log(storage.getAppId()) // 输出 1-2-3-4


// Example 同时使用 hash 和 remote 策略
import createStorage from '@sdp.nd/storage'
import hash from '@sdp.nd/storage/lib/strategy-hash'
import remote from '@sdp.nd/storage/lib/strategy-remote'
window.location.hash = `#/?sdp-app-id=1-2-3-4`
// 由于 strategy-remote 策略是异步的，所以初始化使用异步方法
(async () => {
  const storage = await createStorage({strategy: [remote, hash]})
  console.log(storage.getAppId()) // 如果 ucsdk 有返回 app-id，就输出 ucsdk 的值，否则输出 hash 的值 1-2-3-4
})()
```

### 自定义数据接口
```js
/**
 * 获取数据
 * @param {string} [key] 键
 * @param {string} [scope='_dft_'] 存储空间
 * @returns {any}
 */
get (key?: string, scope?: string = '_dft_'): any

/**
 * 设置数据
 * @param {string} key 键
 * @param {*} [value] 值
 * @param {string} [scope='_dft_'] 存储空间
 */
set (key: string, value?: any, scope?: string = '_dft_'): void


// Example 不使用默认策略，有业务方自己定义数据
import createStorage from '@sdp.nd/storage' // commonjs 使用 const createStorage = require('@sdp.nd/storage')
const storage = createStorage({})
storage.set('Orgname', 'nd')
console.log(storage.get('Orgname')) // 输出 nd
console.log(storage.getOrgName()) // 输出 nd
```

### 应用数据接口
```js
getAppId (): any
getPkgId (): any
getOrgId (): any
getOrgName (): any
setVorgId (): any
getVorg (): any
getMigrated (): any

setAppId (value: any): void
setPkgId (value: any): void
setOrgId (value: any): void
setOrgName (value: any): void
setVorgId (value: any): void
setVorg (value: any): void
setMigrated (value: any): void
```

### 更多

请访问 [接口文档]( http://docs.dev.web.nd/utilities/class/packages/storage/src/storage.js~Storage.html)

## 本地开发

```bash
# 安装依赖
npm install

# 测试
npm test

# 打包
npm run build

# Flow
npm run flow

# ESLint
npm run lint
```
