diff --git a/packages/asyncstorage/README-zh_CN.md b/packages/asyncstorage/README-zh_CN.md index 1c1e73b6..1cf0e393 100644 --- a/packages/asyncstorage/README-zh_CN.md +++ b/packages/asyncstorage/README-zh_CN.md @@ -3,7 +3,7 @@ > AsyncStorage ## 支持 -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp ## 安装 diff --git a/packages/asyncstorage/README.md b/packages/asyncstorage/README.md index f030a27f..c171549c 100644 --- a/packages/asyncstorage/README.md +++ b/packages/asyncstorage/README.md @@ -3,7 +3,7 @@ > AsyncStorage ## Support -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp ## Install diff --git a/packages/asyncstorage/docs-template/README-zh_CN.md b/packages/asyncstorage/docs-template/README-zh_CN.md index 86fcd80a..e8ce65a9 100644 --- a/packages/asyncstorage/docs-template/README-zh_CN.md +++ b/packages/asyncstorage/docs-template/README-zh_CN.md @@ -3,7 +3,7 @@ > AsyncStorage ## 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ ## 安装 diff --git a/packages/asyncstorage/docs-template/README.md b/packages/asyncstorage/docs-template/README.md index 73c5808e..3c1fff38 100644 --- a/packages/asyncstorage/docs-template/README.md +++ b/packages/asyncstorage/docs-template/README.md @@ -3,7 +3,7 @@ > AsyncStorage ## Support -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ ## Install diff --git a/packages/asyncstorage/package.json b/packages/asyncstorage/package.json index 17266c1d..6684b898 100644 --- a/packages/asyncstorage/package.json +++ b/packages/asyncstorage/package.json @@ -1,6 +1,6 @@ { "name": "universal-asyncstorage", - "version": "1.1.0", + "version": "1.2.0", "description": "A universal AsyncStorage API.", "main": "lib/index.js", "module": "src/index.js", diff --git a/packages/asyncstorage/src/index.ts b/packages/asyncstorage/src/index.ts index 18f56bc7..c51c87bd 100644 --- a/packages/asyncstorage/src/index.ts +++ b/packages/asyncstorage/src/index.ts @@ -1,15 +1,15 @@ -import { isWeb, isWeex, isMiniApp, isWeChatMiniProgram, isQuickApp } from 'universal-env'; +import { isWeb, isWeex, isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env'; + import webModule from './web/index'; import weexModule from './weex/index'; import miniAppModule from './miniapp/ali/index'; import weChatModule from './miniapp/wechat/index'; -import quickModule from './quickapp/index'; +import bytedanceModule from './miniapp/bytedance/index'; + import { AsyncStorage } from './types'; let AsyncStorage: AsyncStorage; -if (isQuickApp) { - AsyncStorage = quickModule; -} else if (isWeb) { +if (isWeb) { AsyncStorage = webModule; } else if (isWeex) { AsyncStorage = weexModule; @@ -17,6 +17,8 @@ if (isQuickApp) { AsyncStorage = miniAppModule; } else if (isWeChatMiniProgram) { AsyncStorage = weChatModule; +} else if (isByteDanceMicroApp) { + AsyncStorage = bytedanceModule; } export default AsyncStorage; diff --git a/packages/asyncstorage/src/miniapp/bytedance/index.ts b/packages/asyncstorage/src/miniapp/bytedance/index.ts new file mode 100644 index 00000000..896da678 --- /dev/null +++ b/packages/asyncstorage/src/miniapp/bytedance/index.ts @@ -0,0 +1,84 @@ +import { AsyncStorage } from '../../types'; + +declare const tt: any; + +const AsyncStorage: AsyncStorage = { + getItem: (key: string): Promise => { + return new Promise((resolve, reject): void => { + tt.getStorage({ + key, + success: (res): void => { + resolve(res.data); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + setItem: (key: string, value: object | string): Promise => { + return new Promise((resolve, reject): void => { + tt.setStorage({ + key, + data: value, + success: (): void => { + resolve(null); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + removeItem: (key: string): Promise => { + return new Promise((resolve, reject): void => { + tt.removeStorage({ + key, + success: (): void => { + resolve(null); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + getAllKeys: (): Promise => { + return new Promise((resolve, reject): void => { + tt.getStorageInfo({ + success: (res): void => { + resolve(res.keys); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + clear: (): Promise => { + return new Promise((resolve, reject): void => { + tt.clearStorage({ + success: (): void => { + resolve(null); + }, + fail: (e): void => { + reject(e); + } + }); + }); + }, + length: (): Promise => { + return new Promise((resolve, reject): void => { + tt.getStorageInfo({ + success: (res): void => { + resolve(res.keys.length); + }, + fail: (e): void => { + reject(e); + } + }); + }); + } +}; + +export default AsyncStorage; diff --git a/packages/asyncstorage/src/quickapp/index.js b/packages/asyncstorage/src/quickapp/index.js deleted file mode 100644 index d4d18fff..00000000 --- a/packages/asyncstorage/src/quickapp/index.js +++ /dev/null @@ -1,70 +0,0 @@ -function setItem(key, value) { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.set({ - key, - value, - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -function getItem(key) { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.get({ - key, - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -function removeItem(key) { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.delete({ - key, - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -function clear() { - return new Promise(function(resolve, reject) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const storage = require('@system.storage'); - storage.clear({ - success: function(data) { - resolve(data); - }, - fail: function(data, code) { - reject(`handling fail, code = ${code}`); - } - }); - }); -} - -module.exports = { - setItem, - getItem, - removeItem, - clear -}; diff --git a/packages/request/README-zh_CN.md b/packages/request/README-zh_CN.md index ec27ca6c..051df8fa 100644 --- a/packages/request/README-zh_CN.md +++ b/packages/request/README-zh_CN.md @@ -31,7 +31,7 @@ request({ #### 支持 -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp #### 参数 diff --git a/packages/request/README.md b/packages/request/README.md index 817fbd58..e241ec63 100644 --- a/packages/request/README.md +++ b/packages/request/README.md @@ -31,7 +31,7 @@ request({ #### Supported -browser weex miniApp wechatMiniprogram quickApp +browser weex miniApp wechatMiniprogram bytedanceMicroApp #### Arguments diff --git a/packages/request/docs-template/README-zh_CN.md b/packages/request/docs-template/README-zh_CN.md index ae6d3835..5fb1098b 100644 --- a/packages/request/docs-template/README-zh_CN.md +++ b/packages/request/docs-template/README-zh_CN.md @@ -31,7 +31,7 @@ request({ #### 支持 -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ #### 参数 diff --git a/packages/request/docs-template/README.md b/packages/request/docs-template/README.md index e267bfd6..5415735d 100644 --- a/packages/request/docs-template/README.md +++ b/packages/request/docs-template/README.md @@ -31,7 +31,7 @@ request({ #### Supported -__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_quick_app__ +__icon_web__ __icon_weex__ __icon_miniapp_mp__ __icon_miniapp_wx__ __icon_microapp_tt__ #### Arguments diff --git a/packages/request/package.json b/packages/request/package.json index 5de409a8..d321f93e 100644 --- a/packages/request/package.json +++ b/packages/request/package.json @@ -1,6 +1,6 @@ { "name": "universal-request", - "version": "2.0.0", + "version": "2.1.0", "description": "", "main": "lib/index.js", "files": [ diff --git a/packages/request/src/index.ts b/packages/request/src/index.ts index 87a54ec7..633e4b0b 100644 --- a/packages/request/src/index.ts +++ b/packages/request/src/index.ts @@ -1,4 +1,4 @@ -import { isWeex, isMiniApp, isWeChatMiniProgram, isWeb } from 'universal-env'; +import { isWeex, isMiniApp, isWeChatMiniProgram, isWeb, isByteDanceMicroApp } from 'universal-env'; import { RequestOptions, DEFAULT_REQUEST_OPTIONS @@ -8,6 +8,7 @@ import webModule from './web/index'; import weexModule from './weex/index'; import miniAppModule from './miniapp/index'; import weChatModule from './wechat-miniprogram/index'; +import bytedanceModule from './miniapp-bytedance/index'; function dutyChain(...fns) { for (let i = 0; i < fns.length; i++) { @@ -49,7 +50,12 @@ function handleWeChatMiniprogram(afterOptions) { } return null; } - +function handleBytedanceMiniprogram(afterOptions) { + if (isByteDanceMicroApp) { + const request = bytedanceModule; + return request(afterOptions); + } +} export default function(options: RequestOptions) { let afterOptions: RequestOptions = Object.assign({}, DEFAULT_REQUEST_OPTIONS, @@ -63,5 +69,6 @@ export default function(options: RequestOptions) { handleWeex.bind(null, afterOptions), handleWeb.bind(null, afterOptions), handleMiniApp.bind(null, afterOptions), + handleBytedanceMiniprogram.bind(null, afterOptions), handleWeChatMiniprogram.bind(null, afterOptions)); } diff --git a/packages/request/src/miniapp-bytedance/index.ts b/packages/request/src/miniapp-bytedance/index.ts new file mode 100644 index 00000000..d273fbda --- /dev/null +++ b/packages/request/src/miniapp-bytedance/index.ts @@ -0,0 +1,45 @@ +import { + RequestOptions, + ResponseData, + ERROR_REQUEST_TIMEOUT, + ERROR_REQUEST_ABORT +} from '../types'; +import { + object2json +} from '../utils'; +declare const tt: any; + +export default function(options: RequestOptions): Promise { + return new Promise((resolve, reject) => { + let { url, method, data, dataType, headers, timeout } = options; + let timeoutTimer; + let requestTask = tt.request({ + url, + headers, + method, + data, + timeout, + dataType, + success: function(res: ResponseData) { + resolve(res); + }, + fail: function(res) { + reject({ + code: ERROR_REQUEST_ABORT.code, + message: object2json(res) + }); + }, + complete() { + if (timeoutTimer) { + clearTimeout(timeoutTimer); + } + } + }); + if (timeout) { + timeoutTimer = setTimeout(() => { + requestTask.abort(); + reject(ERROR_REQUEST_TIMEOUT); + }, timeout); + } + }); +}