Skip to content

Commit 5fb1508

Browse files
committed
feat(api): support api promisify in 2.10.2
1 parent f57fc7a commit 5fb1508

File tree

9 files changed

+437
-139
lines changed

9 files changed

+437
-139
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2020-03-18 v2.10.2-1
2+
- 支持 API Promise 化调用
3+
14
## 2020-02-26 v2.10.2
25
- 同步 API 定义到基础库 2.10.2
36
- 将命名空间从 `wx` 更改为更正式的 `WechatMinigame`,这是一个 **破坏性改动**,原本字面上引用了 `wx` 命名空间的代码可能失效

VERSIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
基础库版本|npm 版本|命令
44
-|-|-
5-
[v2.10.2](https://developers.weixin.qq.com/miniprogram/dev/framework/release/#v2-10-2-2020-02-20) | [2.10.2](https://www.npmjs.com/package/miniprogram-api-typings/v/2.10.2) | `npm install [email protected]`
5+
[v2.10.2](https://developers.weixin.qq.com/miniprogram/dev/framework/release/#v2-10-2-2020-02-20) | [2.10.2-1](https://www.npmjs.com/package/miniprogram-api-typings/v/2.10.2-1) | `npm install [email protected]-1`
66
[v2.6.5](https://developers.weixin.qq.com/miniprogram/dev/framework/release/#v2-6-5-2019-04-02) | [2.6.5](https://www.npmjs.com/package/minigame-api-typings/v/2.6.5) | `npm install [email protected]`

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "minigame-api-typings",
3-
"version": "2.10.2",
3+
"version": "2.10.2-1",
44
"description": "Type definitions for APIs of Wechat MiniGame in TypeScript",
55
"main": "./index.d.ts",
66
"types": "./index.d.ts",
File renamed without changes.

test/api-promisify.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { expectType } from 'tsd'
2+
3+
// call with callback
4+
wx.getFriendCloudStorage({
5+
keyList: [],
6+
success(res) {
7+
expectType<WechatMinigame.GetFriendCloudStorageSuccessCallbackResult>(res)
8+
},
9+
})
10+
wx.requestMidasPayment({
11+
currencyType: 'CNY',
12+
mode: 'game',
13+
offerId: '',
14+
success(res) {
15+
expectType<WechatMinigame.MidasPaymentError>(res)
16+
},
17+
})
18+
wx.stopAccelerometer({
19+
fail(res) {
20+
expectType<WechatMinigame.GeneralCallbackResult>(res)
21+
},
22+
})
23+
24+
wx.stopCompass({
25+
complete(res) {
26+
expectType<WechatMinigame.GeneralCallbackResult>(res)
27+
},
28+
})
29+
30+
// call with Promise.prototype.then
31+
wx.getFriendCloudStorage({
32+
keyList: [],
33+
}).then(res => {
34+
expectType<WechatMinigame.GetFriendCloudStorageSuccessCallbackResult>(res)
35+
})
36+
wx.requestMidasPayment({
37+
currencyType: 'CNY',
38+
mode: 'game',
39+
offerId: '',
40+
}).then(res => {
41+
expectType<WechatMinigame.MidasPaymentError>(res)
42+
})
43+
wx.stopAccelerometer().then(res => {
44+
expectType<WechatMinigame.GeneralCallbackResult>(res)
45+
})
46+
wx.stopCompass().then(res => {
47+
expectType<WechatMinigame.GeneralCallbackResult>(res)
48+
})
49+
50+
// call with await
51+
async () => {
52+
expectType<WechatMinigame.GetFriendCloudStorageSuccessCallbackResult>(
53+
await wx.getFriendCloudStorage({
54+
keyList: [],
55+
}),
56+
)
57+
expectType<WechatMinigame.MidasPaymentError>(
58+
await wx.requestMidasPayment({
59+
currencyType: 'CNY',
60+
mode: 'game',
61+
offerId: '',
62+
}),
63+
)
64+
expectType<WechatMinigame.GeneralCallbackResult>(
65+
await wx.stopAccelerometer(),
66+
)
67+
expectType<WechatMinigame.GeneralCallbackResult>(
68+
await wx.stopCompass(),
69+
)
70+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"experimentalDecorators": true,
66
"inlineSourceMap": true,
77
"inlineSources": true,
8-
"lib": ["es5"],
8+
"lib": ["es6"],
99
"module": "CommonJS",
1010
"noFallthroughCasesInSwitch": true,
1111
"noImplicitAny": true,

types/wx/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,17 @@ declare namespace WechatMinigame {
2727
type IAnyObject = Record<string, any>
2828
type Optional<F> = F extends (arg: infer P) => infer R ? (arg?: P) => R : F
2929
type OptionalInterface<T> = { [K in keyof T]: Optional<T[K]> }
30+
interface AsyncMethodOptionLike {
31+
success?: (...args: any[]) => void
32+
}
33+
type PromisifySuccessResult<
34+
P,
35+
T extends AsyncMethodOptionLike
36+
> = P extends { success: any }
37+
? void
38+
: P extends { fail: any }
39+
? void
40+
: P extends { complete: any }
41+
? void
42+
: Promise<Parameters<Exclude<T['success'], undefined>>[0]>
3043
}

types/wx/lib.wx.api.d.ts

Lines changed: 347 additions & 135 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)