-
Notifications
You must be signed in to change notification settings - Fork 9
/
FetchApp.js
130 lines (118 loc) · 3.66 KB
/
FetchApp.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
/**
* GitHub https://github.com/tanaikech/FetchApp<br>
* Library name
* @type {string}
* @const {string}
* @readonly
*/
var appName = "FetchApp";
/**
* Fetch single request.<br>
* @param {string} url URL for fetching.
* @param {object} params Parameters for fetching.
* @return {object} Response
*/
function fetch(url, params) {
return new FetchApp().Fetch(url, params);
}
/**
* Fetch multiple requests.<br>
* @param {object[]} requests requests for fetching.
* @return {object[]} Responses[]
*/
function fetchAll(requests) {
return new FetchApp().FetchAll(requests);
}
/**
* Create instance for creating form data.<br>
* @return {FetchApp}
*/
function createFormData() {
return new FetchApp();
}
/**
* Append form data.<br>
* @param {string} key key of formData
* @param {object} blob value of formData. Give this as a blob.
* @return {FetchApp}
*/
function append(key, blob) {
return this.append(key, blob);
}
;
(function(r) {
var FetchApp;
FetchApp = (function() {
var createPayload;
FetchApp.name = appName;
function FetchApp() {
this.name = appName;
this.objectForFetchApp = {};
}
FetchApp.prototype.Fetch = function(url_, params_) {
var e;
if (!("payload" in params_)) {
try {
createPayload.call(this, params_);
} catch (error) {
e = error;
throw new Error("Object for request is wrong. Please confirm formData again.");
}
}
return UrlFetchApp.fetch(url_, params_);
};
FetchApp.prototype.FetchAll = function(requests_) {
var e, len, reqs;
try {
len = requests_.length;
reqs = requests_.map((function(_this) {
return function(e) {
if (!("payload" in e)) {
return createPayload.call(_this, e);
}
return e;
};
})(this));
} catch (error) {
e = error;
throw new Error("Object for request is wrong. Please confirm formData again.");
}
return UrlFetchApp.fetchAll(reqs);
};
FetchApp.prototype.append = function(key_, blob_) {
if (!key_ || !blob_) {
throw new Error("Wrong values. 'key' is a string. 'blob' is a blob.");
}
if (this.objectForFetchApp[key_]) {
throw new Error("Duplicated key.");
}
this.objectForFetchApp[key_] = blob_;
return this;
};
createPayload = function(params_) {
var boundary, obj, object, payload;
if ("body" in params_) {
boundary = "xxxxxxxxxx" + this.name + "xxxxxxxxxx";
object = params_.body.objectForFetchApp;
obj = Object.keys(object);
payload = obj.reduce(function(ar, e, i) {
var data;
if (!(object[e].toString() === "Blob" && typeof object[e] === "object")) {
throw new Error("Value of formData is not a Blob.");
}
data = "Content-Disposition: form-data; name=\"" + (e || "sample" + i) + "\"; filename=\"" + (e || "sample" + i) + "\"\r\n";
data += "Content-Type: " + (object[e].getContentType() || "application/octet-stream") + "; charset=UTF-8\r\n\r\n";
Array.prototype.push.apply(ar, Utilities.newBlob(data).getBytes());
ar = ar.concat(object[e].getBytes());
Array.prototype.push.apply(ar, Utilities.newBlob("\r\n--" + boundary + (i === obj.length - 1 ? "--" : "\r\n")).getBytes());
return ar;
}, Utilities.newBlob("--" + boundary + "\r\n").getBytes());
params_.payload = payload;
params_.contentType = "multipart/form-data; boundary=" + boundary;
}
return params_;
};
return FetchApp;
})();
return r.FetchApp = FetchApp;
})(this);