@mpxjs/api-proxy
Advanced tools
Comparing version 1.0.8 to 1.1.0
{ | ||
"name": "@mpxjs/api-proxy", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "convert miniprogram API at each end", | ||
@@ -5,0 +5,0 @@ "module": "src/index.js", |
@@ -28,3 +28,3 @@ # mpx-api-proxy | ||
|---|---|---|---|---|---| | ||
|platform|Object|各平台之间的转换|否|{from:'', to:'' }|使用 mpx 脚手架配置会自动进行转换,无需配置| | ||
|platform|Object|各平台之间的转换|否|{ from:'', to:'' }|使用 mpx 脚手架配置会自动进行转换,无需配置| | ||
|exclude|Array(String)|跨平台时不需要转换的 api|-| | ||
@@ -31,0 +31,0 @@ |usePromise|Boolean|是否将 api 转化为 promise 格式使用|否|false|-| |
@@ -1,15 +0,13 @@ | ||
import { proxyWxToAliApi } from './platform/index' | ||
import proxyAll from './proxy' | ||
import getPromisifyList from './promisify' | ||
import proxyAll from './proxy-all' | ||
import transformApi from './platform/index' | ||
export default function install (target, options = {}) { | ||
const { | ||
platform = {}, // 转换的平台 | ||
exclude = [], // 转换平台时不转换的 Api | ||
usePromise = false, // 是否转为 promise 格式 | ||
whiteList = [] // 不变成 promise 格式的 api | ||
whiteList = [], // 不变成 promise 格式的 api | ||
platform = {}, | ||
exclude = [] // 转换平台时不转换的 Api | ||
} = options | ||
let { from = '', to = '' } = platform | ||
/* eslint-disable camelcase, no-undef */ | ||
@@ -25,13 +23,12 @@ if (typeof __mpx_src_mode__ !== 'undefined') { | ||
// 代理所有 api | ||
proxyAll(target) | ||
proxyAll(target, usePromise, whiteList) | ||
// 转换各端 api | ||
if (from === 'wx' && to === 'ali') { | ||
proxyWxToAliApi(target, exclude) | ||
} | ||
// 变为 promise 格式 | ||
if (usePromise) { | ||
Object.assign(target, getPromisifyList(whiteList, from, to, exclude)) | ||
} | ||
transformApi(target, { | ||
usePromise, | ||
whiteList, | ||
exclude, | ||
from, | ||
to | ||
}) | ||
} |
@@ -1,1 +0,60 @@ | ||
export { wxToAliApi, proxyWxToAliApi } from './wxToAli' | ||
import { error } from '../utils' | ||
import wxToAliApi from './wxToAli' | ||
import promisify from '../promisify' | ||
function transformApi (target, options) { | ||
const platformMap = { | ||
'wx_ali': wxToAliApi | ||
} | ||
const platforms = ['wx', 'ali', 'swan', 'qq', 'tt'] | ||
const cacheTarget = {} | ||
Object.keys(target).forEach(key => { | ||
cacheTarget[key] = target[key] | ||
}) | ||
function joinName (from = '', to = '') { | ||
return `${from}_${to}` | ||
} | ||
Object.keys(wxToAliApi).forEach(api => { | ||
if (options.exclude.includes(api)) { | ||
return | ||
} | ||
const descriptor = { | ||
enumerable: true, | ||
configurable: true | ||
} | ||
descriptor.get = () => { | ||
return (...args) => { | ||
const to = options.to | ||
let from = args.splice(args.length - 1)[0] | ||
if (typeof from !== 'string' || !platforms.includes(from)) { | ||
args.push(from) | ||
from = options.from | ||
} | ||
if (platformMap[joinName(from, to)] && platformMap[joinName(from, to)][api]) { | ||
const result = promisify({ | ||
[api]: platformMap[joinName(from, to)][api] | ||
}, options.usePromise, options.whiteList) | ||
return result[api].apply(target, args) | ||
} | ||
if (cacheTarget[api]) { | ||
return cacheTarget[api].apply(target, args) | ||
} else { | ||
error(`当前环境不存在 ${api} 方法`) | ||
} | ||
} | ||
} | ||
Object.defineProperty(target, api, descriptor) | ||
}) | ||
} | ||
export default transformApi |
import { changeOpts, handleSuccess, getEnvObj, error, warn, noop } from '../utils' | ||
const ALI_OBJ = getEnvObj() | ||
const TIPS_NAME = 'mpx' | ||
const TIPS_NAME = '支付宝环境 mpx' | ||
@@ -643,17 +643,2 @@ // canvas api 用 | ||
/** | ||
* @param {Object} target 要代理的对象 | ||
*/ | ||
const proxyWxToAliApi = (target, exclude) => { | ||
Object.keys(wxToAliApi).forEach(api => { | ||
if (~exclude.indexOf(api)) { | ||
return | ||
} | ||
target[api] = wxToAliApi[api] | ||
}) | ||
} | ||
export { | ||
wxToAliApi, | ||
proxyWxToAliApi | ||
} | ||
export default wxToAliApi |
@@ -1,2 +0,1 @@ | ||
import { wxToAliApi } from './platform/index' | ||
import { getEnvObj, noop } from './utils' | ||
@@ -54,12 +53,12 @@ | ||
function promisify (listObj, whiteList, exclude = []) { | ||
const promisifyList = {} | ||
function promisify (listObj, usePromise, whiteList) { | ||
const result = {} | ||
Object.keys(listObj).forEach(key => { | ||
if (typeof listObj[key] !== 'function' || ~exclude.indexOf(key)) { | ||
if (typeof listObj[key] !== 'function') { | ||
return | ||
} | ||
promisifyList[key] = function (...args) { | ||
if (promisifyFilter(key, whiteList)) { | ||
result[key] = function (...args) { | ||
if (usePromise && promisifyFilter(key, whiteList)) { | ||
if (!args[0]) { | ||
@@ -88,13 +87,5 @@ args[0] = { success: noop, fail: noop } | ||
return promisifyList | ||
return result | ||
} | ||
export default function getPromisifyList (whiteList, from, to, exclude) { | ||
const promisifyObj = promisify(envObj, whiteList) | ||
let promisifyTrans = {} | ||
if (from === 'wx' && to === 'ali') { | ||
promisifyTrans = promisify(wxToAliApi, whiteList, exclude) | ||
} | ||
return Object.assign({}, promisifyObj, promisifyTrans) | ||
} | ||
export default promisify |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28637
735