forked from undoZen/promisingagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (132 loc) · 3.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
var Promise = require('bluebird');
var superagent = require('superagent');
var qs = require('qs');
var extend = require('extend');
var methods = [
"checkout",
"connect",
"copy",
"delete",
"get",
"head",
"lock",
"m-search",
"merge",
"mkactivity",
"mkcol",
"move",
"notify",
"options",
"patch",
"post",
"propfind",
"proppatch",
"purge",
"put",
"report",
"search",
"subscribe",
"trace",
"unlock",
"unsubscribe"
].map(function (method) {
return method.toUpperCase();
});
exports = module.exports = promisingagent;
exports.Promise = Promise;
exports.superagent = superagent;
var Request = promisingagent.Request = superagent.Request;
exports.querySerializer = qs.stringify;
var serialize = exports.bodySerializer = superagent.serialize;
serialize['application/x-www-form-urlencoded'] = qs.stringify;
Request.prototype.end = (function(origEnd) {
return function (fn) {
var self = this;
this.promise = this.promise || new Promise(function (resolve, reject) {
origEnd.call(self, function (err, response) {
if (err && !err.status) {
reject(err);
} else {
resolve(response);
}
});
});
if (typeof fn === 'function') {
this.promise.nodeify(fn);
}
return this.promise;
};
}(Request.prototype.end));
// delegate promise methods so you can do something like promisingagent(url).then(...)
// also delegate some handy utility methods from bluebird
'then spread catch caught finnaly lastly bind tap call get return throw reflect'.split(' ').forEach(function (m) {
Request.prototype[m] = function () {
var promise = this.promise || this.end();
return promise[m].apply(promise, arguments);
}
});
function promisingagent() {
var method, url, query;
var args = Array.prototype.slice.call(arguments);
var strs = [];
for (var i = 0; i < args.length;) {
if (typeof args[i] === 'string') {
var part = args.splice(i, 1)[0]
if (!method && methods.indexOf(part) > -1) {
method = part;
} else {
strs.push(part);
}
} else {
i += 1;
}
}
url = strs.join('');
var opts = extend.apply(null, [true, method ? {method: method} : {}].concat(args));
method = (opts.method||'').toUpperCase() || 'GET';
url = url || opts.url;
if (opts.query) {
query = exports.querySerializer(opts.query);
url += ~url.indexOf('?')
? '&' + query
: '?' + query;
}
var request = new Request(method, url);
if (method !== 'GET' && method !== 'HEAD') {
request.type(opts.type || 'form');
}
if (opts.body) {
request.send(opts.body);
}
if (opts.headers) {
Object.keys(opts.headers).forEach(function (key) {
var v = opts.headers[key];
if (typeof v === 'function') {
v = v.call(request);
}
if (typeof v === 'string') {
request.set(key.toLowerCase(), v);
}
});
}
return request;
}
function addMethods(fn) {
methods.forEach(function (method) {
var ml = method.toLowerCase();
fn[method] = fn[ml] = fn.bind(null, method);
if (method === 'DELETE') {
fn.del = fn.DEL = fn['delete'];
}
});
}
addMethods(promisingagent);
promisingagent.extend = function extend () {
var args = Array.prototype.slice.call(arguments);
args.unshift(null);
var fn = this.bind.apply(this, args);
addMethods(fn);
fn.extend = promisingagent.extend;
return fn;
};