-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
243 lines (216 loc) · 7.02 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
var request = require('./lib/node/request'),
qs = require('qs'),
crypto = require('./lib/node/crypto'),
printf = require('util').format,
path = require('path'),
join = path.posix ? path.posix.join : path.join;
/**
* Extend `target` with properties from `source`.
* @param {Object} target
* @param {Object} source
* @return {Object}
*/
function extend(target, source) {
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
target[prop] = source[prop];
}
}
return target;
}
/**
* Create ASS API instance.
* @param {Object} opts
* @param {String} opts.httpUrl
* @param {String} opts.httpsUrl
* @param {String} opts.accessToken
* @param {String} opts.username
*/
function ASS(opts) {
this.httpBaseUrl = opts.httpUrl;
this.httpsBaseUrl = opts.httpsUrl;
this.accessToken = opts.accessToken;
this.username = opts.username;
}
/**
* Get default headers.
* @param {Object} headers Add headers
* @return {Object}
*/
ASS.prototype.getDefaultHeaders = function (headers) {
var h = {
'Authorization': 'bearer ' + this.accessToken
};
if (headers) {
Object.keys(headers).forEach(function (key) {
h[key] = headers[key];
});
}
return h;
};
/**
* Upload file to endpoint.
* @param {String} endpoint
* @param {File|String|stream.Readable} file File object, full path to the file or stream.Readable
* @param {Object} headers Add additional headers
* @return {Promise} Resolves with response object
*/
ASS.prototype.upload = function (endpoint, file, headers) {
return request.postFile(this.getUrl(endpoint), file, this.getDefaultHeaders(headers));
};
/**
* Upload a file to /files endpoint.
* @param {File|String|stream.Readable} file File object, full path to the file or stream.Readable
* @param {String} path Additional path to append to /files
* @param {Object} headers Add additional headers
* @return {Promise} Resolves with response object
*/
ASS.prototype.uploadFile = function (file, path, headers) {
var endpoint = '/files';
if (path) {
endpoint += path.charAt(0) === '/' ? path : '/' + path;
}
return this.upload(endpoint, file, headers);
};
/**
* Upload an image.
* @param {File|String|stream.Readable} file File object, full path to the file or stream.Readable
* @param {Object} headers Add additional headers
* @return {Promise} Resolves with response object
*/
ASS.prototype.uploadImage = function (file, headers) {
return this.upload('/images', file, headers);
};
/**
* Make a request.
* @param {String} endpoint
* @param {Object} [opts] Options to request
* @param {Boolean} opts.http Set to true if request should use HTTP instead of HTTPS URL
* @param {String} opts.method Request method (GET/HEAD/POST/PUT/PATCH/DELETE)
* @param {Object} opts.headers Additional headers to add
* @param {Number} opts.timeout Number of ms to wait before timing out
* @param {String|Object} opts.body Data to send
* @return {Promise} Resolves with response object
*/
ASS.prototype.request = function (endpoint, opts) {
opts = extend({}, opts);
opts.url = this.getUrl(endpoint, opts.http);
opts.headers = this.getRequestHeaders(opts.headers);
delete opts.http;
return request(opts);
};
/**
* Get headers for convenient request methods.
* @param {Object} headers Add additional headers
* @return {Object}
*/
ASS.prototype.getRequestHeaders = function (headers) {
return this.getDefaultHeaders(extend({'Content-Type': 'application/json'}, headers));
};
/**
* Make a GET request to an endpoint.
* @param {String} endpoint
* @param {Object} [opts] Options to request
* @return {Promise} Resolves with response object
*/
ASS.prototype.get = function (endpoint, opts) {
return this.request(endpoint, extend({method: 'GET'}, opts));
};
/**
* Make a DELETE request to an endpoint.
* @param {String} endpoint
* @param {Object} [opts] Options to request
* @return {Promise} Resolves with response object
*/
ASS.prototype.delete = function (endpoint, opts) {
return this.request(endpoint, extend({method: 'DELETE'}, opts));
};
/**
* Make a HEAD request to an endpoint.
* @param {String} endpoint
* @param {Object} [opts] Options to request
* @return {Promise} Resolves with response object
*/
ASS.prototype.head = function (endpoint, opts) {
return this.request(endpoint, extend({method: 'HEAD'}, opts));
};
/**
* Make a POST request to an endpoint.
* @param {String} endpoint
* @param {String|Object} data Data to send as body
* @param {Object} [opts] Options to request
* @return {Promise} Resolves with response object
*/
ASS.prototype.post = function (endpoint, data, opts) {
return this.request(endpoint, extend({method: 'POST', body: data}, opts));
};
/**
* Make a PUT request to an endpoint.
* @param {String} endpoint
* @param {String|Object} data Data to send as body
* @param {Object} [opts] Options to request
* @return {Promise} Resolves with response object
*/
ASS.prototype.put = function (endpoint, data, opts) {
return this.request(endpoint, extend({method: 'PUT', body: data}, opts));
};
/**
* Make a PATCH request to an endpoint.
* @param {String} endpoint
* @param {String|Object} data Data to send as body
* @param {Object} [opts] Options to request
* @return {Promise} Resolves with response object
*/
ASS.prototype.patch = function (endpoint, data, opts) {
return this.request(endpoint, extend({method: 'PATCH', body: data}, opts));
};
/**
* Get full URL to ASS endpoint, defaults to https URL.
* @param {String} endpoint
* @param {Boolean} [http] If we should return http URL
* @return {String}
*/
ASS.prototype.getUrl = function (endpoint, http) {
return (http ? this.httpBaseUrl : this.httpsBaseUrl) + join('/', endpoint);
};
/**
* Create a signed URL from image id and actions.
* @param {Integer} id - image id
* @param {Object} [actions]
* @param {Object} [options]
* @param {Boolean} [options.https] if we should return https
* @param {Boolean} [options.encode] if we should url encode the actions
* @param {String} [options.format] image format to use
* @return {String}
*/
ASS.prototype.createImageUrl = function (id, actions, options) {
options = options ? options : {};
options.https = typeof options.https === 'boolean' ? options.https : false;
options.encode = typeof options.encode === 'boolean' ? options.encode : false;
var url = this.getUrl(printf('/users/%s/images/%s.%s', this.username, id, options.format || 'jpg'), !options.https);
if (actions) {
url += '?' + qs.stringify({ t: actions }, { encode: false });
}
return (options.encode ? encodeURI(url) : url) + (actions ? '&' : '?') + 'accessToken=' + this.createSignature(url);
};
/**
* Create signature for an image or file URL.
* @param {String} URL
* @return {String}
*/
ASS.prototype.createSignature = function (url) {
return crypto.sha256(this.accessToken, url);
};
/**
* Create ASS instance.
* @param {Object} opts
* @param {String} opts.httpUrl
* @param {String} opts.httpsUrl
* @param {String} opts.accessToken
* @param {String} opts.username
* @return {ASS}
*/
module.exports = function (opts) {
return new ASS(opts);
};