Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
vv13 committed Nov 10, 2018
0 parents commit 6ae3996
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@babel/env",
{
"modules": false
}
]
]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
dist
.DS_Store
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"singleQuote": true,
"semi": false
}
32 changes: 32 additions & 0 deletions README.md
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 })
},
})
```
19 changes: 19 additions & 0 deletions index.js
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
37 changes: 37 additions & 0 deletions package.json
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"
}
}
24 changes: 24 additions & 0 deletions rollup.config.js
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
}),
]
};

0 comments on commit 6ae3996

Please sign in to comment.