-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreq.xapi.js
More file actions
305 lines (247 loc) · 7.97 KB
/
req.xapi.js
File metadata and controls
305 lines (247 loc) · 7.97 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* @author jboeselt
* simple xapi http requests via XHR or NODE (request module)
*/
if ((typeof module !== 'undefined' && module.exports)) {
var req = require('./req.js');
}
(function(req) {
'use strict';
////
// aggregation
////
// aggregate helper (callback mode only, see search())
var aggregate = function(api, config, options) {
if (typeof options.cap !== 'number') {
options.cap = -1; // TODO
}
var response = null;
var prev = null;
var next = null;
var resolve = config.success || function() {};
var mergeData = function(res) {
if (!response) {
response = res;
return;
}
var oldData = options.dataFn(response, config);
var newData = options.dataFn(res, config);
for (var i = 0; i < newData.length; i++) {
oldData.push(newData[i]);
}
return oldData.length;
};
var success = function(res, ins) {
next = options.nextFn(res);
var length = mergeData(res);
if (!next || next === prev) {
// replace data with merged data in current response
res.data = response.data;
resolve(res, ins);
return;
}
if (options.cap > -1 && length >= options.cap) {
// replace data with merged data in current response
res.data = response.data;
resolve(res, ins);
return;
}
prev = next;
config.query = {};// clear query object for follow-up calls as they should be part of the "more" url
req.xapi.get(next, config);
};
config.success = success;
req.xapi.get(api, config);
};
var search = function(api, config, options) {
var p = (config.promise === true || req.ASYNC === 'promise');
if (p) {
config.promise = false;
return new req.Promise(function(resolve, reject) {
config.success = function(data) {
resolve(data);
};
config.error = function(data) {
reject(data);
};
return aggregate(api, config, options);
});
}
return aggregate(api, config, options);
};
////
// xapi legacy mode (CORS)
////
// for details @see ./test/xapi/legacy.js
var beforeSendLegacy = function(config) {
// responseType
var method = config.method || 'GET';
var headers = config.headers || null;
var query = config.query || null;
var data = config.data || null;
var legacyData = {};
if (query) {
var flatQ = {};
for (var key in query) {
if (!query.hasOwnProperty(key)) {
continue;
}
flatQ[key] = (req.isScalar(query[key])) ? query[key] : JSON.stringify(query[key]);
}
req.extend(legacyData, flatQ);
}
// req.xapi() works with preset 'json' which inserts json header later on. We need to insert it manually since we will change the preset to form
headers['Content-Type'] = 'application/json; charset=utf-8';
if (headers) {
req.extend(legacyData, headers);
}
if (data) {
legacyData.content = JSON.stringify(data);
}
// transform config
config.preset = '';
config.query = {
method: method
};
config.method = 'POST';
config.transformResponse = function(response) {
if (!response.data) {
return;
}
try {
response.data = JSON.parse(response.data);
} catch (e) {
//@TODO
console.error(e);
}
};
// add legacyData
config.data = legacyData;
return config;
};
////
// config
////
var lrs = function(config) {
var xapi = config.xapi || {};
return {
lrs: xapi.lrs || req.xapi.LRS,
auth: xapi.auth || req.xapi.AUTH,
version: xapi.version || req.xapi.VERSION,
legacy : (typeof xapi.legacy !== 'undefined') ? xapi.legacy : req.xapi.LEGACY
};
};
var defaults = function(xapi) {
return {
headers: {
'Authorization' : xapi.auth,
'X-Experience-API-Version' : xapi.version
},
preset: 'json'
};
};
var endpoint = function(lrs, api) {
return lrs + api;
};
////
// default xapi request
////
req.xapi = function(api, config) {
// xapi config
var xapi = lrs(config);
config.xapi = undefined; // overwrite temporarily
// merge config and re-attach xapi for debug
// - note it's still possible to overwrite default headers
// - TODO req.DEBUG
config = req.extendDefaults(defaults(xapi), config);
config.xapi = xapi;
// build url
var url = endpoint(xapi.lrs, api);
if (config.xapi.legacy) {
config = beforeSendLegacy(config);
return req.form(url, config);
}
return req.request(url, config); // note the order of merge. default overwrites are allowed
};
////
// shortcut methods
////
req.xapi.get = function(api, config) {
return req.xapi(api, config); // note the order of merge. default overwrites are allowed
};
req.xapi.head = function(api, config) {
config = config || {};
config.method = 'HEAD';
return req.xapi(api, config);
};
req.xapi['delete'] = function(api, config) {
config = config || {};
config.method = 'HEAD';
return req.xapi(api, config);
};
req.xapi.post = function(api, data, config) {
config = config || {};
config.method = 'POST';
config.data = data;
return req.xapi(api, config);
};
req.xapi.put = function(api, data, config) {
config = config || {};
config.method = 'PUT';
config.data = data;
return req.xapi(api, config);
};
req.xapi.statements = function(config, options) {
// TODO: decide on default cap
options = req.extend({
// callback for fetching data
dataFn: function(res, conf) {
return res.data.statements;
},
// callback for building parse more url
nextFn: function(res) {
var more = res.data.more || null;
if (!more) {
return null;
}
var parts = more.split('/statements');
return '/statements' + parts[parts.length - 1];
}
}, options);
return search('/statements', config, options);
};
////
// helper methods
////
req.xapi.uuid = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
// eslint-disable-next-line no-bitwise, eqeqeq
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
req.xapi.toBase64 = function(str) {
// eslint-disable-next-line no-undef
return (typeof btoa === 'function') ? btoa(str) : new Buffer(str).toString('base64');
};
// get headers
req.xapi.getHeaders = function() {
return defaults().headers;
};
// get headers
req.xapi.getEndpoint = function(api) {
return endpoint(api);
};
////
// vars
////
req.xapi.LEGACY = false;
req.xapi.LRS = '';
req.xapi.AUTH = '';
req.xapi.VERSION = '';
return req;
})(req);
// node
if (typeof module !== 'undefined' && module.exports) {
module.exports = req;
}