Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat api proxy promisify #1247

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/api-proxy/__tests__/web/toast.spec.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/api-proxy/src/common/js/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './ToPromise'
export * from './web'
export * from './utils'
export * from './promisify'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { genFromMap, getEnvObj, noop } from '../common/js'
import { genFromMap, getEnvObj, noop } from './utils'

const envObj = getEnvObj()

Expand All @@ -21,6 +21,7 @@ const blackList = [
'createSelectorQuery',
'createIntersectionObserver',
'hideKeyboard',
// todo 小程序支持 promise 风格调用
'stopPullDownRefresh',
'createWorker',
'pageScrollTo',
Expand Down Expand Up @@ -96,4 +97,6 @@ function promisify (listObj, whiteList, customBlackList) {
return result
}

export default promisify
export {
promisify
}
2 changes: 1 addition & 1 deletion packages/api-proxy/src/index.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions packages/api-proxy/src/index.web.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions packages/api-proxy/src/web/api/action-sheet/ActionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export default class ActionSheet extends ToPromise {
tapIndex: index
}
webHandleSuccess(res, opts.success, opts.complete)
this.toPromiseResolve(res)
}
list.appendChild(sheet)
})
Expand All @@ -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 () {
Expand Down
9 changes: 2 additions & 7 deletions packages/api-proxy/src/web/api/modal/Modal.js
Original file line number Diff line number Diff line change
@@ -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'
// 汉字为两个字符,字母/数字为一个字符
Expand All @@ -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: '',
Expand Down Expand Up @@ -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()
Expand All @@ -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 () {
Expand Down
36 changes: 14 additions & 22 deletions packages/api-proxy/src/web/api/pull-down/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions packages/api-proxy/src/web/api/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -70,9 +70,7 @@ function request (options = { url: '' }) {
return Promise.reject(res)
}
})

promise.__returned = requestTask
return promise
return requestTask
}

export {
Expand Down
127 changes: 54 additions & 73 deletions packages/api-proxy/src/web/api/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
}
}

Expand All @@ -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)
}
)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
Loading
Loading