Skip to content

Commit

Permalink
0.3.0: 升级sdk依赖,支持直接调用底层api
Browse files Browse the repository at this point in the history
- 包装 form 与 qs,使用快捷方法可以不指定 form 与 qs
  • Loading branch information
guo-yu committed Jul 8, 2014
1 parent 399f409 commit 0f41838
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions libs/duoshuo.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,33 @@ Duoshuo.Client = function(sdk, access_token) {

Duoshuo.Client.prototype.init = function(sdk) {
var self = this;
// init build-in method
['get','post','put','delete'].forEach(function(buildInMethod){
self[buildInMethod] = function(url, params, callback) {
var data = params;
if (buildInMethod === 'post') {
if (!data.form) data.form = {};
data.form.access_token = self.access_token;
}
if (buildInMethod === 'get') {
if (!data.qs) data.qs = {};
data.qs.access_token = self.access_token;
}
return sdk[buildInMethod](url, data, callback);
};
});
// init custom api and inject `access_token`
Object.keys(apis).forEach(function(key) {
if (key === 'token') return false;
self[key] = function(params, callback) {
var method = apis[key].method;
var data = params || {};
var data = {};
if (method === 'post') {
if (!data.form) data.form = {}
data.form = params.form || params;
data.form.access_token = self.access_token;
}
if (method === 'get') {
if (!data.qs) data.qs = {}
data.qs = params.qs || params;
data.qs.access_token = self.access_token;
}
return sdk[key](data, callback);
Expand Down

0 comments on commit 0f41838

Please sign in to comment.