Skip to content

Commit 137e258

Browse files
committed
修正部分接口 promise 支持;使用 ES6 Promise 替代 when
1 parent fec8187 commit 137e258

File tree

13 files changed

+104
-80
lines changed

13 files changed

+104
-80
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
4+
## 2.1.2 (2018-05-14)
5+
#### 修正
6+
- 修正部分接口 promise 支持
7+
8+
#### 更改
9+
- 使用 ES6 Promise 替代 when
10+
311
## 2.1.1 (2018-01-10)
412
#### 更改
513
- 去除 transfer 取消接口

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ example 文件夹里面是一个简单的接入示例,该示例仅供参考。
66
docs 接口文档
77

88
## 版本要求
9-
nodejs 版本 v0.8.0 及以上
9+
nodejs 版本 4 及以上
1010

1111
## 安装
1212
`npm install pingpp`

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.1
1+
2.1.2

example/promise.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const API_KEY = 'sk_test_ibbTe5jLGCi5rzfH4OqPW9KC';
4+
5+
var pingpp = require('../lib/pingpp')(API_KEY);
6+
var path = require('path');
7+
8+
pingpp.setPrivateKeyPath(path.join(__dirname, 'your_rsa_private_key.pem'));
9+
10+
pingpp.charges.retrieve(
11+
'ch_bLWP80Ci9S4ODaXLSKLOGe5S'
12+
).then(ch => {
13+
console.log(ch);
14+
}).catch(e => {
15+
console.log(e);
16+
});

lib/PingppMethod.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var OPTIONAL_REGEX = /^optional!/;
1616
*/
1717
module.exports = function pingppMethod(spec) {
1818
var commandPath = typeof spec.path == 'function' ? spec.path
19-
: utils.makeURLInterpolator(spec.path || '');
19+
: utils.makeURLInterpolator(spec.path || '');
2020
var requestMethod = (spec.method || 'GET').toUpperCase();
2121
var urlParams = spec.urlParams || [];
2222

@@ -29,47 +29,52 @@ module.exports = function pingppMethod(spec) {
2929
var data = utils.isObject(args[args.length - 1]) ? args.pop() : {};
3030
var urlData = this.createUrlData();
3131

32-
var deferred = this.createDeferred(callback);
32+
return this.wrapTimeout(new Promise((function (resolve, reject) {
33+
for (var i = 0, l = urlParams.length; i < l; ++i) {
34+
var err;
3335

34-
for (var i = 0, l = urlParams.length; i < l; ++i) {
36+
var arg = args[0];
37+
var param = urlParams[i];
3538

36-
var arg = args[0];
37-
var param = urlParams[i];
39+
var isOptional = OPTIONAL_REGEX.test(param);
40+
param = param.replace(OPTIONAL_REGEX, '');
3841

39-
var isOptional = OPTIONAL_REGEX.test(param);
40-
param = param.replace(OPTIONAL_REGEX, '');
41-
42-
if (!arg) {
43-
if (isOptional) {
44-
urlData[param] = '';
45-
continue;
42+
if (!arg) {
43+
if (isOptional) {
44+
urlData[param] = '';
45+
continue;
46+
}
47+
err = new Error('Pingpp: I require argument "' + urlParams[i] + '", but I got: ' + arg);
48+
reject(err);
49+
return;
4650
}
47-
throw new Error('Pingpp: I require argument "' + urlParams[i] + '", but I got: ' + arg);
48-
}
4951

50-
urlData[param] = args.shift();
51-
}
52+
urlData[param] = args.shift();
53+
}
5254

53-
if (args.length) {
54-
throw new Error(
55-
'Pingpp: Unknown arguments (' + args + '). Did you mean to pass an options object?'
56-
);
57-
}
55+
if (args.length) {
56+
err = new Error(
57+
'Pingpp: Unknown arguments (' + args + '). Did you mean to pass an options object?'
58+
);
59+
reject(err);
60+
return;
61+
}
5862

59-
var requestPath = this.createFullPath(commandPath, urlData);
63+
var requestPath = this.createFullPath(commandPath, urlData);
6064

61-
self._request(requestMethod, requestPath, data, auth, function(err, response) {
62-
if (err) {
63-
deferred.reject(err);
64-
} else {
65-
deferred.resolve(
66-
spec.transformResponseData ?
67-
spec.transformResponseData(response) :
68-
response
69-
);
65+
function requestCallback(err, response) {
66+
if (err) {
67+
reject(err);
68+
} else {
69+
resolve(
70+
spec.transformResponseData ?
71+
spec.transformResponseData(response) :
72+
response
73+
);
74+
}
7075
}
71-
});
7276

73-
return deferred.promise;
77+
self._request(requestMethod, requestPath, data, auth, requestCallback);
78+
}).bind(this)), callback);
7479
};
7580
};

lib/PingppResource.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var http = require('http');
44
var https = require('https');
55
var path = require('path');
6-
var when = require('when');
76
var _ = require('lodash');
87

98
var utils = require('./utils');
@@ -62,20 +61,17 @@ PingppResource.prototype = {
6261
return urlData;
6362
},
6463

65-
createDeferred: function(callback) {
66-
var deferred = when.defer();
67-
64+
wrapTimeout: function(promise, callback) {
6865
if (callback) {
69-
// Callback, if provided, is a simply translated to Promise'esque:
70-
// (Ensure callback is called outside of promise stack)
71-
deferred.promise.then(function(res) {
72-
setTimeout(function(){ callback(null, res); }, 0);
66+
// Ensure callback is called outside of promise stack.
67+
return promise.then(function(res) {
68+
setTimeout(function() { callback(null, res); }, 0);
7369
}, function(err) {
74-
setTimeout(function(){ callback(err, null); }, 0);
70+
setTimeout(function() { callback(err, null); }, 0);
7571
});
7672
}
7773

78-
return deferred;
74+
return promise;
7975
},
8076

8177
_timeoutHandler: function(timeout, req, callback) {

lib/pingpp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Pingpp.prototype = {
118118
if (id) {
119119
this._setApiField(
120120
'appID',
121-
id
121+
id
122122
);
123123
}
124124
},

lib/resources/BatchTransfers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ module.exports = PingppResource.extend({
1818
}),
1919

2020
cancel: function(id, callback){
21-
this.update(id, {
21+
return this.wrapTimeout(this.update(id, {
2222
'status': 'canceled'
23-
}, callback);
23+
}), callback);
2424
}
2525

2626
});

lib/resources/Charges.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ module.exports = PingppResource.extend({
1313
'create', 'retrieve'
1414
],
1515

16-
_listCharges: pingppMethod({
17-
method: 'GET'
18-
}),
19-
20-
list: function(params, cb) {
16+
list: function(params, callback) {
2117
if (!hasOwn.call(params, 'app')
2218
|| typeof params.app != 'object'
2319
|| !hasOwn.call(params.app, 'id')
2420
) {
25-
cb.call(null, new Error.PingppInvalidRequestError({
26-
message: 'Please pass app[id] as parameter.'
27-
}));
21+
return this.wrapTimeout(new Promise(function (resolve, reject) {
22+
reject(new Error.PingppInvalidRequestError({
23+
message: 'Please pass app[id] as parameter.'
24+
}));
25+
}), callback);
2826
} else {
29-
this._listCharges(params, cb);
27+
return pingppMethod({
28+
method: 'GET'
29+
});
3030
}
3131
},
3232

lib/resources/Orders.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = PingppResource.extend({
1717
urlParams: ['id']
1818
}),
1919

20-
cancel: function(id,callback){
21-
this.update(id,{
20+
cancel: function(id, callback) {
21+
return this.wrapTimeout(this.update(id, {
2222
'status': 'canceled'
23-
},callback);
23+
}), callback);
2424
},
2525

2626
pay: pingppMethod({

0 commit comments

Comments
 (0)