Skip to content

Commit

Permalink
Merge pull request #1474 from didi/feat-react-api-proxy
Browse files Browse the repository at this point in the history
Feat react api proxy
  • Loading branch information
hiyuki authored Jun 3, 2024
2 parents 161a240 + dfe9c6a commit 08b657b
Show file tree
Hide file tree
Showing 31 changed files with 516 additions and 13 deletions.
7 changes: 7 additions & 0 deletions packages/api-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@
"homepage": "https://github.com/didi/mpx#readme",
"dependencies": {
"axios": "^1.6.7"
},
"peerDependencies": {
"@react-native-async-storage/async-storage": "^1.23.1",
"@react-native-clipboard/clipboard": "^1.14.0",
"react-native-device-info": "^10.13.2",
"react-native-safe-area-context": "^4.10.1",
"@react-native-community/netinfo": "^11.2.1"
}
}
23 changes: 22 additions & 1 deletion packages/api-proxy/src/common/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
*/
const hasOwnProperty = Object.prototype.hasOwnProperty

function type (n) {
return Object.prototype.toString.call(n).slice(8, -1)
}

function hasOwn (obj, key) {
return hasOwnProperty.call(obj, key)
}
Expand Down Expand Up @@ -68,6 +72,8 @@ function getEnvObj () {
case 'dd':
return dd
case 'web':
case 'ios':
case 'android':
return {}
}
}
Expand Down Expand Up @@ -95,6 +101,19 @@ function makeMap (arr) {
}, {})
}

function defineUnsupportedProps (resObj, props) {
const defineProps = {}
props.forEach((item) => {
defineProps[item] = {
get () {
warn(`The ${item} attribute is not supported in ${__mpx_mode__} environment`)
return null
}
}
})
Object.defineProperties(resObj, defineProps)
}

const isBrowser = typeof window !== 'undefined'

function throwSSRWarning (info) {
Expand All @@ -111,5 +130,7 @@ export {
makeMap,
isBrowser,
hasOwn,
throwSSRWarning
throwSSRWarning,
type,
defineUnsupportedProps
}
1 change: 1 addition & 0 deletions packages/api-proxy/src/platform/api/base/index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.web'
1 change: 1 addition & 0 deletions packages/api-proxy/src/platform/api/base/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.web'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnClipboard'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnClipboard'
41 changes: 41 additions & 0 deletions packages/api-proxy/src/platform/api/clipboard-data/rnClipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Clipboard from '@react-native-clipboard/clipboard'
import { webHandleSuccess, webHandleFail } from '../../../common/js/web'
import { type } from '@mpxjs/utils'
const setClipboardData = function (options) {
const { data, success, fail, complete } = options
if (!data || type(data) !== 'String') {
const errStr = !data ? 'parameter.data should be String instead of Undefined;' : `parameter.data should be String instead of ${type(data)};`
const result = {
errno: 1001,
errMsg: errStr
}
webHandleFail(result, fail, complete)
return
}
Clipboard.setString(data)
const result = {
errMsg: 'setClipboardData:ok'
}
webHandleSuccess(result, success, complete)
}

const getClipboardData = function (options) {
const { success, fail, complete } = options
Clipboard.getString().then((data) => {
const result = {
data,
errMsg: 'getClipboardData:ok'
}
webHandleSuccess(result, success, complete)
}).catch(() => {
const result = {
errMsg: 'setClipboardData:fail'
}
webHandleFail(result, fail, complete)
})
}

export {
setClipboardData,
getClipboardData
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnNetwork'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnNetwork'
59 changes: 59 additions & 0 deletions packages/api-proxy/src/platform/api/device/network/rnNetwork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { webHandleSuccess, webHandleFail, defineUnsupportedProps } from '../../../../common/js'
import NetInfo, { NetInfoStateType } from '@react-native-community/netinfo'

let _unsubscribe = null
const _callbacks = new Set()
const getConnectionType = function (connectionInfo) {
let type = 'unknown'
if (connectionInfo.type === NetInfoStateType.cellular && connectionInfo.details.cellularGeneration) {
type = connectionInfo.details.cellularGeneration
} else if (connectionInfo.type === NetInfoStateType.wifi || connectionInfo.type === NetInfoStateType.none) {
type = connectionInfo.type
}
return type
}

const getNetworkType = function (options) {
const { success, fail, complete } = options
NetInfo.fetch().then((connectionInfo) => {
const result = {
networkType: getConnectionType(connectionInfo),
errMsg: 'getNetworkType:ok'
}
defineUnsupportedProps(result, ['signalStrength', 'hasSystemProxy'])
webHandleSuccess(result, success, complete)
}).catch((err) => {
const result = {
errMsg: err.message
}
webHandleFail(result, fail, complete)
})
}

const onNetworkStatusChange = function (callback) {
_callbacks.add(callback)
if (!_unsubscribe) {
_unsubscribe = NetInfo.addEventListener((connectionInfo) => {
_callbacks.forEach(cb => {
const { isConnected } = connectionInfo
// eslint-disable-next-line node/no-callback-literal
cb && cb({ isConnected, networkType: getConnectionType(connectionInfo) })
})
})
}
}
const offNetworkStatusChange = function (callback) {
if (callback && typeof callback === 'function') {
_callbacks.delete(callback)
} else if (callback === undefined) {
_callbacks.clear()
_unsubscribe && _unsubscribe()
_unsubscribe = null
}
}

export {
getNetworkType,
offNetworkStatusChange,
onNetworkStatusChange
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnMakePhone'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnMakePhone'
27 changes: 27 additions & 0 deletions packages/api-proxy/src/platform/api/make-phone-call/rnMakePhone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { webHandleSuccess, webHandleFail } from '../../../common/js'
import { Linking } from 'react-native'

const makePhoneCall = function (options) {
const {
phoneNumber = '',
success = null,
fail = null,
complete = null
} = options

Linking.openURL(`tel:${phoneNumber}`).then(() => {
const result = {
errMsg: 'makePhoneCall:ok'
}
webHandleSuccess(result, success, complete)
}).catch(() => {
const result = {
errMsg: 'makePhoneCall:fail cancel'
}
webHandleFail(result, fail, complete)
})
}

export {
makePhoneCall
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.web'
1 change: 1 addition & 0 deletions packages/api-proxy/src/platform/api/request/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.web'
3 changes: 2 additions & 1 deletion packages/api-proxy/src/platform/api/request/index.web.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { webHandleSuccess, webHandleFail } from '../../../common/js'
import { webHandleSuccess, webHandleFail, defineUnsupportedProps } from '../../../common/js'
import RequestTask from './RequestTask'

function request (options = { url: '' }) {
Expand Down Expand Up @@ -69,6 +69,7 @@ function request (options = { url: '' }) {
statusCode: res.status,
header: res.headers
}
defineUnsupportedProps(result, ['cookies', 'profile', 'exception'])
webHandleSuccess(result, success, complete)
return result
}).catch(err => {
Expand Down
20 changes: 15 additions & 5 deletions packages/api-proxy/src/platform/api/socket/SocketTask.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { webHandleSuccess, webHandleFail } from '../../../common/js'
import { webHandleSuccess, webHandleFail, type } from '../../../common/js'

const socketTasks = new Set()

Expand Down Expand Up @@ -41,7 +41,11 @@ class SocketTask {

send (options) {
const { data = '', success, fail, complete } = options

if (typeof data !== 'string' || type(data) !== 'ArrayBuffer') {
const res = { errMsg: 'sendSocketMessage:fail Unsupported data type' }
webHandleFail(res, fail, complete)
return
}
if (this._socket.readyState === 1) {
this._socket.send(data)
const res = { errMsg: 'sendSocketMessage:ok' }
Expand Down Expand Up @@ -77,8 +81,14 @@ class SocketTask {
}

addListener (socket) {
socket.onOpen = event => { typeof this._openCb === 'function' && this._openCb(event) }
socket.onmessage = event => { typeof this._messageCb === 'function' && this._messageCb(event) }
socket.onopen = event => {
typeof this._openCb === 'function' && this._openCb(event)
}
socket.onmessage = event => {
typeof this._messageCb === 'function' && this._messageCb({
data: event.data
})
}
socket.onerror = event => {
socketTasks.delete(this._socket)
typeof this._errorCb === 'function' && this._errorCb(event)
Expand All @@ -91,7 +101,7 @@ class SocketTask {
if (this._closeData) {
this._closeCb(event)
} else {
this._closeCb({ code: 2000, reason: `${event}` })
this._closeCb({ code: event.code, reason: event.reason })
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.web'
1 change: 1 addition & 0 deletions packages/api-proxy/src/platform/api/socket/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.web'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnStorage'
1 change: 1 addition & 0 deletions packages/api-proxy/src/platform/api/storage/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './rnStorage'
Loading

0 comments on commit 08b657b

Please sign in to comment.