diff --git a/packages/api-proxy/__tests__/web/toast.spec.js b/packages/api-proxy/__tests__/web/toast.spec.js index a7a88a2c10..6b6ecba7d3 100644 --- a/packages/api-proxy/__tests__/web/toast.spec.js +++ b/packages/api-proxy/__tests__/web/toast.spec.js @@ -1,7 +1,7 @@ import '@testing-library/jest-dom/extend-expect' import { showToast, hideToast -} from '../../src/web/api//toast/index' +} from '../../src/web/api/toast/index' describe('test toast', () => { afterAll(() => { diff --git a/packages/api-proxy/src/common/js/index.js b/packages/api-proxy/src/common/js/index.js index bf7227ce40..b1b579d20d 100644 --- a/packages/api-proxy/src/common/js/index.js +++ b/packages/api-proxy/src/common/js/index.js @@ -1,3 +1,4 @@ export * from './ToPromise' export * from './web' export * from './utils' +export * from './promisify' diff --git a/packages/api-proxy/src/mini/promisify.js b/packages/api-proxy/src/common/js/promisify.js similarity index 95% rename from packages/api-proxy/src/mini/promisify.js rename to packages/api-proxy/src/common/js/promisify.js index 3f909812fd..29816ef4e8 100644 --- a/packages/api-proxy/src/mini/promisify.js +++ b/packages/api-proxy/src/common/js/promisify.js @@ -1,4 +1,4 @@ -import { genFromMap, getEnvObj, noop } from '../common/js' +import { genFromMap, getEnvObj, noop } from './utils' const envObj = getEnvObj() @@ -21,6 +21,7 @@ const blackList = [ 'createSelectorQuery', 'createIntersectionObserver', 'hideKeyboard', + // todo 小程序支持 promise 风格调用 'stopPullDownRefresh', 'createWorker', 'pageScrollTo', @@ -96,4 +97,6 @@ function promisify (listObj, whiteList, customBlackList) { return result } -export default promisify +export { + promisify +} diff --git a/packages/api-proxy/src/index.js b/packages/api-proxy/src/index.js index b7cc5ea817..22f6c90173 100644 --- a/packages/api-proxy/src/index.js +++ b/packages/api-proxy/src/index.js @@ -1,5 +1,5 @@ import transformApi from './mini/transform' -import promisify from './mini/promisify' +import { promisify } from './common/js' export default function install (target, options = {}) { const { diff --git a/packages/api-proxy/src/index.web.js b/packages/api-proxy/src/index.web.js index 837b69ca35..1fdbd73414 100644 --- a/packages/api-proxy/src/index.web.js +++ b/packages/api-proxy/src/index.web.js @@ -1,9 +1,15 @@ -import * as allApi from './web/api' -import { genFromMap } from './common/js' +import * as transedApi from './web/api' +import { promisify, genFromMap } from './common/js' -export default function install (target) { +export default function install (target, options = {}) { + const { + usePromise = false, // 是否转为 promise 格式 + whiteList = [], // 强制变成 promise 格式的 api + blackList = [] // 强制不变成 promise 格式的 api + } = options const fromMap = genFromMap() - + const promisedApi = usePromise ? promisify(transedApi, whiteList, blackList) : {} + const allApi = Object.assign({}, transedApi, promisedApi) Object.keys(allApi).forEach(api => { target[api] = function (...args) { if (args.length > 0) { diff --git a/packages/api-proxy/src/web/api/action-sheet/ActionSheet.js b/packages/api-proxy/src/web/api/action-sheet/ActionSheet.js index 478d4f36d8..e32e614463 100644 --- a/packages/api-proxy/src/web/api/action-sheet/ActionSheet.js +++ b/packages/api-proxy/src/web/api/action-sheet/ActionSheet.js @@ -64,7 +64,6 @@ export default class ActionSheet extends ToPromise { tapIndex: index } webHandleSuccess(res, opts.success, opts.complete) - this.toPromiseResolve(res) } list.appendChild(sheet) }) @@ -77,13 +76,10 @@ export default class ActionSheet extends ToPromise { this.hide() const err = { errMsg: 'showActionSheet:fail cancel' } webHandleFail(err, opts.fail, opts.complete) - !opts.fail && this.toPromiseReject(err) } this.box.classList.add('show') this.actionSheet.classList.add('show') - - return this.toPromiseInitPromise() } hide () { diff --git a/packages/api-proxy/src/web/api/modal/Modal.js b/packages/api-proxy/src/web/api/modal/Modal.js index cccf033829..0128420552 100644 --- a/packages/api-proxy/src/web/api/modal/Modal.js +++ b/packages/api-proxy/src/web/api/modal/Modal.js @@ -1,4 +1,4 @@ -import { ToPromise, webHandleSuccess } from '../../../common/js' +import { webHandleSuccess } from '../../../common/js' import '../../../common/stylus/Modal.styl' // import { forEach } from '@didi/mpx-fetch/src/util' // 汉字为两个字符,字母/数字为一个字符 @@ -13,9 +13,8 @@ const _getLength = (t) => { } return len } -export default class Modal extends ToPromise { +export default class Modal { constructor () { - super() this.defaultOpts = { title: '', content: '', @@ -110,7 +109,6 @@ export default class Modal extends ToPromise { confirm: false } webHandleSuccess(result, opts.success, opts.complete) - this.toPromiseResolve(result) } this.confirmBtn.onclick = () => { this.hide() @@ -120,12 +118,9 @@ export default class Modal extends ToPromise { confirm: true } webHandleSuccess(result, opts.success, opts.complete) - this.toPromiseResolve(result) } this.modal.classList.add('show') - - return this.toPromiseInitPromise() } hide () { diff --git a/packages/api-proxy/src/web/api/pull-down/index.js b/packages/api-proxy/src/web/api/pull-down/index.js index 4605bccaf0..98f7a79cd2 100644 --- a/packages/api-proxy/src/web/api/pull-down/index.js +++ b/packages/api-proxy/src/web/api/pull-down/index.js @@ -20,17 +20,13 @@ function stopPullDownRefresh (options = {}) { } } } - return new Promise((resolve, reject) => { - if (err) { - const res = { errMsg: `stopPullDownRefresh:fail ${err}` } - webHandleFail(res, options.fail, options.complete) - reject(res) - } else { - const res = { errMsg: 'stopPullDownRefresh:ok' } - webHandleSuccess(res, options.success, options.complete) - resolve(res) - } - }) + if (err) { + const res = { errMsg: `stopPullDownRefresh:fail ${err}` } + webHandleFail(res, options.fail, options.complete) + } else { + const res = { errMsg: 'stopPullDownRefresh:ok' } + webHandleSuccess(res, options.success, options.complete) + } } } @@ -54,17 +50,13 @@ function startPullDownRefresh (options = {}) { } } } - return new Promise((resolve, reject) => { - if (err) { - const res = { errMsg: `startPullDownRefresh:fail ${err}` } - webHandleFail(res, options.fail, options.complete) - reject(res) - } else { - const res = { errMsg: 'startPullDownRefresh:ok' } - webHandleSuccess(res, options.success, options.complete) - resolve(res) - } - }) + if (err) { + const res = { errMsg: `startPullDownRefresh:fail ${err}` } + webHandleFail(res, options.fail, options.complete) + } else { + const res = { errMsg: 'startPullDownRefresh:ok' } + webHandleSuccess(res, options.success, options.complete) + } } } diff --git a/packages/api-proxy/src/web/api/request/index.js b/packages/api-proxy/src/web/api/request/index.js index 28f3c43cfc..a91a265a2d 100644 --- a/packages/api-proxy/src/web/api/request/index.js +++ b/packages/api-proxy/src/web/api/request/index.js @@ -47,7 +47,7 @@ function request (options = { url: '' }) { delete rOptions.data } - const promise = axios(rOptions).then(res => { + axios(rOptions).then(res => { let data = res.data if (responseType === 'text' && dataType === 'json') { try { @@ -70,9 +70,7 @@ function request (options = { url: '' }) { return Promise.reject(res) } }) - - promise.__returned = requestTask - return promise + return requestTask } export { diff --git a/packages/api-proxy/src/web/api/route/index.js b/packages/api-proxy/src/web/api/route/index.js index b85a636036..b9a1c64cfa 100644 --- a/packages/api-proxy/src/web/api/route/index.js +++ b/packages/api-proxy/src/web/api/route/index.js @@ -13,23 +13,19 @@ function redirectTo (options = {}) { return Promise.reject(res) } router.__mpxAction = { type: 'redirect' } - return new Promise((resolve, reject) => { - router.replace( - { - path: options.url - }, - () => { - const res = { errMsg: 'redirectTo:ok' } - webHandleSuccess(res, options.success, options.complete) - resolve(res) - }, - err => { - const res = { errMsg: `redirectTo:fail ${err}` } - webHandleFail(res, options.fail, options.complete) - reject(res) - } - ) - }) + router.replace( + { + path: options.url + }, + () => { + const res = { errMsg: 'redirectTo:ok' } + webHandleSuccess(res, options.success, options.complete) + }, + err => { + const res = { errMsg: `redirectTo:fail ${err}` } + webHandleFail(res, options.fail, options.complete) + } + ) } } @@ -53,23 +49,19 @@ function navigateTo (options = {}) { if (options.events) { eventChannel._addListeners(options.events) } - return new Promise((resolve, reject) => { - router.push( - { - path: options.url - }, - () => { - const res = { errMsg: 'navigateTo:ok', eventChannel } - webHandleSuccess(res, options.success, options.complete) - resolve(res) - }, - err => { - const res = { errMsg: err } - webHandleFail(res, options.fail, options.complete) - reject(res) - } - ) - }) + router.push( + { + path: options.url + }, + () => { + const res = { errMsg: 'navigateTo:ok', eventChannel } + webHandleSuccess(res, options.success, options.complete) + }, + err => { + const res = { errMsg: err } + webHandleFail(res, options.fail, options.complete) + } + ) } } @@ -88,7 +80,6 @@ function navigateBack (options = {}) { router.go(-delta) const res = { errMsg: 'navigateBack:ok' } webHandleSuccess(res, options.success, options.complete) - return Promise.resolve(res) } } @@ -114,30 +105,25 @@ function reLaunch (options = {}) { router.go(-delta) } else { router.__mpxAction.replaced = true - return new Promise((resolve, reject) => { - router.replace( - { - path: options.url, - query: { - reLaunchCount - } - }, - () => { - const res = { errMsg: 'reLaunch:ok' } - webHandleSuccess(res, options.success, options.complete) - resolve(res) - }, - err => { - const res = { errMsg: err } - webHandleFail(res, options.fail, options.complete) - !options.fail && reject(res) + router.replace( + { + path: options.url, + query: { + reLaunchCount } - ) - }) + }, + () => { + const res = { errMsg: 'reLaunch:ok' } + webHandleSuccess(res, options.success, options.complete) + }, + err => { + const res = { errMsg: err } + webHandleFail(res, options.fail, options.complete) + } + ) } const res = { errMsg: 'reLaunch:ok' } webHandleSuccess(res, options.success, options.complete) - return Promise.resolve(res) } } @@ -166,28 +152,23 @@ function switchTab (options = {}) { router.go(-delta) } else { router.__mpxAction.replaced = true - return new Promise((resolve, reject) => { - router.replace( - { - path: options.url - }, - () => { - const res = { errMsg: 'switchTab:ok' } - webHandleSuccess(res, options.success, options.complete) - resolve(res) - }, - err => { - const res = { errMsg: err } - webHandleFail(res, options.fail, options.complete) - !options.fail && reject(res) - } - ) - }) + router.replace( + { + path: options.url + }, + () => { + const res = { errMsg: 'switchTab:ok' } + webHandleSuccess(res, options.success, options.complete) + }, + err => { + const res = { errMsg: err } + webHandleFail(res, options.fail, options.complete) + } + ) } } const res = { errMsg: 'switchTab:ok' } webHandleSuccess(res, options.success, options.complete) - return Promise.resolve(res) } } diff --git a/packages/api-proxy/src/web/api/storage/index.js b/packages/api-proxy/src/web/api/storage/index.js index 66d4b09eaa..1c136ae88d 100644 --- a/packages/api-proxy/src/web/api/storage/index.js +++ b/packages/api-proxy/src/web/api/storage/index.js @@ -46,11 +46,9 @@ function getStorage (options = {}) { if (result) { const res = { errMsg: 'getStorage:ok', data: data } webHandleSuccess(res, success, complete) - return Promise.resolve(res) } else { const res = { errMsg: 'getStorage:fail', data: null } webHandleFail(res, fail, complete) - return Promise.reject(res) } } @@ -95,7 +93,6 @@ function getStorageInfo (options = {}) { } catch (err) { const res = { errMsg: `getStorageInfo:fail ${err}` } webHandleFail(res, fail, complete) - return Promise.reject(res) } } @@ -123,11 +120,9 @@ function removeStorage (options = { key: '' }) { const res = { errMsg: 'removeStorage:ok' } webHandleSuccess(res, success, complete) - return Promise.resolve(res) } catch (err) { const res = { errMsg: `removeStorage:fail ${err}` } webHandleFail(res, fail, complete) - return Promise.reject(res) } } @@ -151,11 +146,9 @@ function clearStorage (options = {}) { const res = { errMsg: 'clearStorage:ok' } webHandleSuccess(res, success, complete) - return Promise.resolve(res) } catch (err) { const res = { errMsg: `clearStorage:fail ${err}` } webHandleFail(res, fail, complete) - return Promise.reject(res) } } diff --git a/packages/api-proxy/src/web/api/system/index.js b/packages/api-proxy/src/web/api/system/index.js index eab97bc4fd..a7b708550f 100644 --- a/packages/api-proxy/src/web/api/system/index.js +++ b/packages/api-proxy/src/web/api/system/index.js @@ -77,7 +77,6 @@ function getSystemInfo (options = {}) { const info = getSystemInfoSync() const res = Object.assign({ errMsg: 'getSystemInfo:ok' }, info) webHandleSuccess(res, options.success, options.complete) - return Promise.resolve(res) } export { diff --git a/packages/api-proxy/src/web/api/tab-bar/index.js b/packages/api-proxy/src/web/api/tab-bar/index.js index 199912f269..3e9db8b221 100644 --- a/packages/api-proxy/src/web/api/tab-bar/index.js +++ b/packages/api-proxy/src/web/api/tab-bar/index.js @@ -21,10 +21,9 @@ function setTabBarStyle (options = {}) { if (resolved) { webHandleSuccess(resolved, options.success, options.complete) - return Promise.resolve(resolved) + } else { + webHandleFail(rejected, options.fail, options.complete) } - webHandleFail(rejected, options.fail, options.complete) - return Promise.reject(rejected) } function setTabBarItem (options = {}) { @@ -52,10 +51,9 @@ function setTabBarItem (options = {}) { if (resolved) { webHandleSuccess(resolved, options.success, options.complete) - return Promise.resolve(resolved) + } else { + webHandleFail(rejected, options.fail, options.complete) } - webHandleFail(rejected, options.fail, options.complete) - return Promise.reject(rejected) } function showTabBar (options = {}) { @@ -70,10 +68,9 @@ function showTabBar (options = {}) { if (resolved) { webHandleSuccess(resolved, options.success, options.complete) - return Promise.resolve(resolved) + } else { + webHandleFail(rejected, options.fail, options.complete) } - webHandleFail(rejected, options.fail, options.complete) - return Promise.reject(rejected) } function hideTabBar (options = {}) { @@ -88,10 +85,9 @@ function hideTabBar (options = {}) { if (resolved) { webHandleSuccess(resolved, options.success, options.complete) - return Promise.resolve(resolved) + } else { + webHandleFail(rejected, options.fail, options.complete) } - webHandleFail(rejected, options.fail, options.complete) - return Promise.reject(rejected) } export { diff --git a/packages/api-proxy/src/web/api/toast/Toast.js b/packages/api-proxy/src/web/api/toast/Toast.js index 988d5863dd..66a712572d 100644 --- a/packages/api-proxy/src/web/api/toast/Toast.js +++ b/packages/api-proxy/src/web/api/toast/Toast.js @@ -84,7 +84,6 @@ export default class Toast { const errMsg = type === 'loading' ? 'showLoading:ok' : 'showToast:ok' webHandleSuccess({ errMsg }, opts.success, opts.complete) - return Promise.resolve({ errMsg }) } hide (options = {}, type) { @@ -100,6 +99,5 @@ export default class Toast { } this.hideTimer = setTimeout(() => { this.toast.classList.remove('show') }, duration) - return Promise.resolve({ errMsg }) } }