-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
223 lines (195 loc) · 4.97 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
'use strict';
const debug = require('diagnostics')('appetizer');
const request = require('request');
/**
* API client for Appetize.io
*
* @constructor
* @param {Object} options API configuration.
* @public
*/
class Appetizer {
constructor(options = {}) {
this.endpoint = options.endpoint || 'https://$key@api.appetize.io/$version/$path';
this.version = options.version || 'v1';
this.key = options.key;
}
/**
* Figure out the type of things.
*
* @param {Mixed} what Thing we want the type of.
* @returns {String} Internal type.
* @private
*/
type(what) {
return Object.prototype.toString.call(what).slice(8, -1).toLowerCase();
}
/**
* Flatten an object to a dot notation keys as required for multi/part
* uploads. JSON is only one level deep, so we don't need to apply this
* recursively.
*
* @param {Object} obj The JSON structure that needs to be flat.
* @returns {Object} New object.
* @private
*/
flatten(obj) {
return Object.keys(obj).reduce((memo, key) => {
if (this.type(obj[key]) !== 'object' || key === 'file') {
let value = obj[key];
if (typeof value !== 'object') value = value.toString();
memo[key] = value;
return memo;
}
Object.keys(obj[key]).forEach((name) => {
let value = obj[key][name];
if (typeof value !== 'object') value = value.toString();
memo[key + '.' + name] = value;
});
return memo;
}, {});
}
/**
* Compile a appetize.io compatible URL.
*
* @param {String} pathname Path.
* @returns {String} Full URL to the appetize API.
* @private
*/
url(pathname) {
return this.endpoint
.replace('$path', pathname)
.replace('$version', this.version)
.replace('$key', this.key);
}
/**
* Send an API request to the server.
*
* @param {Object} opts HTTP request options.
* @param {Object} payload JSON payload.
* @param {Function} fn Completion callback.
* @returns {Appertize} Selfie.
* @private
*/
send(opts = {}, payload = {}, fn) {
opts = Object.assign({ json: true }, opts);
//
// So there's a difference when uploading/updating/creating apps. You can
// point to a file or you can upload it with the API call. If we're
// uploading it we need to change the response type and also how JSON is
// processed.
//
const file = payload && 'file' in payload && !('url' in payload);
if (file) {
debug('detected direct upload, flatting payload');
opts.formData = this.flatten(payload);
payload = null;
}
/**
* Process the API response.
*
* @param {Error} err Optional error.
* @param {Response} res Response headers etc..
* @param {Object} body API response.
* @returns {Undefined} Void 0.
* @private
*/
function process(err, res, body) {
if (err) return fn(err);
fn(err, body);
}
opts.url = this.url(opts.pathname);
delete opts.pathname;
debug('requesting', opts);
if (file || !payload) request(opts, process);
else request(opts, payload, process);
return this;
}
/**
* Create a new application.
*
* @param {Object} data Data to upload.
* @param {Function} fn Completion callback.
* @returns {Appertize} Chaining.
* @public
*/
create(data = {}, fn) {
return this.send({
method: 'POST',
pathname: 'apps'
}, data, fn);
}
/**
* Update an application.
*
* @param {String} id id of the application we need to update.
* @param {Object} data New application details.
* @param {Function} fn Completion callback.
* @returns {Appertize} Chaining.
* @public
*/
update(id, data = {}, fn) {
return this.send({
method: 'POST',
pathname: `apps/${id}`
}, data, fn);
}
/**
* Remove an application.
*
* @param {String} id id of the application we need to update.
* @param {Function} fn Completion callback.
* @returns {Appertize} Chaining.
* @public
*/
remove(id, fn) {
return this.send({
method: 'DELETE',
pathname: `apps/${id}`
}, null, fn);
}
/**
* List all created applications.
*
* @param {Function} fn Completion callback.
* @returns {Appertize} Chaining.
* @public
*/
list(fn) {
return this.send({
method: 'GET',
pathname: 'apps'
}, null, fn);
}
/**
* Same as list, but for more apps.
*
* @param {String} nextKey Next Key for more apps.
* @param {Function} fn Completion callback.
* @returns {Appertize} Chaining.
* @public
*/
more(nextKey, fn) {
return this.send({
method: 'GET',
pathname: 'apps?' + nextKey
}, null, fn);
}
/**
* Usage statistics.
*
* @param {Function} fn Completion callback.
* @returns {Appertize} Chaining.
* @public
*/
usage(fn) {
return this.send({
method: 'GET',
pathname: 'usageSummary'
}, null, fn);
}
}
//
// Expose the module.
//
module.exports = Appetizer;