-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6ae3996
Showing
7 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/env", | ||
{ | ||
"modules": false | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
package-lock.json | ||
dist | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"tabWidth": 2, | ||
"singleQuote": true, | ||
"semi": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
可能是设计最优雅的微信小程序API的Promise转化方式,支持所有wx对象中以下形式的API接口: | ||
``` | ||
| 属性 | 类型 | 默认值 | 是否必填 | 说明 | | ||
| -------- | -------- | ------ | -------- | ------------------------------------------------ | | ||
| success | function | | 否 | 接口调用成功的回调函数 | | ||
| fail | function | | 否 | 接口调用失败的回调函数 | | ||
| complete | function | | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | | ||
| ... | ... | | 是 | ... | | ||
``` | ||
|
||
使用Proxy + ES Promise实现,核心代码只有10行 | ||
|
||
## 安装 | ||
``` | ||
$ npm i wx-api-promisify | ||
``` | ||
|
||
## 示例 | ||
若您的工程支持,推荐使用async/await。以下为获取用户信息->下载文件->保存到相册的Promise示例: | ||
``` | ||
import wxPromise from 'wx-api-promisify' | ||
Page({ | ||
... | ||
getUserInfo() { | ||
wxPromise.getUserInfo() | ||
.then(({ userInfo: { avatarUrl } }) => wxPromise.downloadFile({ url: avatarUrl })) | ||
.then(({ tempFilePath }) => wxPromise.saveImageToPhotosAlbum({ filePath: tempFilePath }) | ||
}, | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const proxy = new Proxy(wx || {}, { | ||
get: (target, property) => { | ||
if (!Object.prototype.hasOwnProperty.call(target, property)) { | ||
if (property === '__esModule') return | ||
throw new ReferenceError(`Property ${property} not exists.`) | ||
} | ||
return options => | ||
new Promise((resolve, reject) => { | ||
target[property]( | ||
Object.assign({}, options, { | ||
success: resolve, | ||
fail: reject | ||
}) | ||
) | ||
}) | ||
} | ||
}) | ||
|
||
export default proxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "wx-api-promisify", | ||
"version": "0.1.2", | ||
"description": "promisify微信小程序API", | ||
"main": "dist/wx-api-promisify.umd.js", | ||
"module": "dist/wx-api-promisify.esm.js", | ||
"scripts": { | ||
"dev": "rollup -c -w", | ||
"build": "rollup -c", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vv13/wx-api-promisify.git" | ||
}, | ||
"keywords": [ | ||
"promise" | ||
], | ||
"author": "vv13", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/vv13/wx-api-promisify/issues" | ||
}, | ||
"homepage": "https://github.com/vv13/wx-api-promisify#readme", | ||
"files": [ | ||
"dist" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/core": "^7.1.5", | ||
"@babel/plugin-external-helpers": "^7.0.0", | ||
"@babel/preset-env": "^7.1.5", | ||
"rollup": "^0.67.0", | ||
"rollup-plugin-commonjs": "^9.2.0", | ||
"rollup-plugin-node-resolve": "^3.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import resolve from 'rollup-plugin-node-resolve'; | ||
import pkg from './package.json'; | ||
|
||
export default { | ||
input: 'index.js', | ||
output: [ | ||
{ | ||
name: 'wxApiPromisify', | ||
file: pkg.main, | ||
format: 'umd' | ||
}, | ||
{ | ||
name: 'wxApiPromisify', | ||
file: pkg.module, | ||
format: 'es' | ||
} | ||
], | ||
plugins: [ | ||
resolve({ | ||
main: true, | ||
browser: true | ||
}), | ||
] | ||
}; |