dcl-npc-toolkit-ai-version
Version:
A collection of tools for creating Non-Player-Characters (NPCs). These are capable of having conversations with the player, and play different animations. AI usage is added atop of it
74 lines (61 loc) • 1.82 kB
JavaScript
function apply(src, tar) {
tar.headers = src.headers || {};
tar.statusMessage = src.statusText;
tar.statusCode = src.status;
tar.data = src.response;
}
function send(method, uri, opts) {
return new Promise(function (res, rej) {
opts = opts || {};
var req = new XMLHttpRequest;
var k, tmp, arr, str=opts.body;
var headers = opts.headers || {};
// IE compatible
if (opts.timeout) req.timeout = opts.timeout;
req.ontimeout = req.onerror = function (err) {
err.timeout = err.type == 'timeout';
rej(err);
}
req.open(method, uri.href || uri);
req.onload = function () {
arr = req.getAllResponseHeaders().trim().split(/[\r\n]+/);
apply(req, req); //=> req.headers
while (tmp = arr.shift()) {
tmp = tmp.split(': ');
req.headers[tmp.shift().toLowerCase()] = tmp.join(': ');
}
tmp = req.headers['content-type'];
if (tmp && !!~tmp.indexOf('application/json')) {
try {
req.data = JSON.parse(req.data, opts.reviver);
} catch (err) {
apply(req, err);
return rej(err);
}
}
(req.status >= 400 ? rej : res)(req);
};
if (typeof FormData < 'u' && str instanceof FormData) {
// str = opts.body
} else if (str && typeof str == 'object') {
headers['content-type'] = 'application/json';
str = JSON.stringify(str);
}
req.withCredentials = !!opts.withCredentials;
for (k in headers) {
req.setRequestHeader(k, headers[k]);
}
req.send(str);
});
}
var get = /*#__PURE__*/ send.bind(send, 'GET');
var post = /*#__PURE__*/ send.bind(send, 'POST');
var patch = /*#__PURE__*/ send.bind(send, 'PATCH');
var del = /*#__PURE__*/ send.bind(send, 'DELETE');
var put = /*#__PURE__*/ send.bind(send, 'PUT');
exports.del = del;
exports.get = get;
exports.patch = patch;
exports.post = post;
exports.put = put;
exports.send = send;