Skip to content

Commit 8cdaff3

Browse files
committed
switch to react-native-voice
1 parent d3033ed commit 8cdaff3

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

js/Errors.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class ApiAiBaseError extends Error {
2+
constructor(message) {
3+
super(message);
4+
this.message = message;
5+
this.stack = new Error().stack;
6+
}
7+
}
8+
export class ApiAiClientConfigurationError extends ApiAiBaseError {
9+
constructor(message) {
10+
super(message);
11+
this.name = "ApiAiClientConfigurationError";
12+
}
13+
}
14+
export class ApiAiRequestError extends ApiAiBaseError {
15+
constructor(message, code = null) {
16+
super(message);
17+
this.message = message;
18+
this.code = code;
19+
this.name = "ApiAiRequestError";
20+
}
21+
}

js/XhrRequest.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* quick ts implementation of example from
3+
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
4+
* with some minor improvements
5+
* @todo: test (?)
6+
* @todo: add node.js implementation with node's http inside. Just to make SDK cross-platform
7+
*/
8+
class XhrRequest {
9+
// Method that performs the ajax request
10+
static ajax(method, url, args = null, headers = null, options = {}) {
11+
// Creating a promise
12+
return new Promise((resolve, reject) => {
13+
// Instantiates the XMLHttpRequest
14+
const client = XhrRequest.createXMLHTTPObject();
15+
let uri = url;
16+
let payload = null;
17+
// Add given payload to get request
18+
if (args && (method === XhrRequest.Method.GET)) {
19+
uri += "?";
20+
let argcount = 0;
21+
for (const key in args) {
22+
if (args.hasOwnProperty(key)) {
23+
if (argcount++) {
24+
uri += "&";
25+
}
26+
uri += encodeURIComponent(key) + "=" + encodeURIComponent(args[key]);
27+
}
28+
}
29+
}
30+
else if (args) {
31+
if (!headers) {
32+
headers = {};
33+
}
34+
headers["Content-Type"] = "application/json; charset=utf-8";
35+
payload = JSON.stringify(args);
36+
}
37+
for (const key in options) {
38+
if (key in client) {
39+
client[key] = options[key];
40+
}
41+
}
42+
// hack: method[method] is somewhat like .toString for enum Method
43+
// should be made in normal way
44+
client.open(XhrRequest.Method[method], uri, true);
45+
// Add given headers
46+
if (headers) {
47+
for (const key in headers) {
48+
if (headers.hasOwnProperty(key)) {
49+
client.setRequestHeader(key, headers[key]);
50+
}
51+
}
52+
}
53+
payload ? client.send(payload) : client.send();
54+
client.onload = () => {
55+
if (client.status >= 200 && client.status < 300) {
56+
// Performs the function "resolve" when this.status is equal to 2xx
57+
resolve(client);
58+
}
59+
else {
60+
// Performs the function "reject" when this.status is different than 2xx
61+
reject(client);
62+
}
63+
};
64+
client.onerror = () => {
65+
reject(client);
66+
};
67+
});
68+
}
69+
static get(url, payload = null, headers = null, options = {}) {
70+
return XhrRequest.ajax(XhrRequest.Method.GET, url, payload, headers, options);
71+
}
72+
static post(url, payload = null, headers = null, options = {}) {
73+
return XhrRequest.ajax(XhrRequest.Method.POST, url, payload, headers, options);
74+
}
75+
static put(url, payload = null, headers = null, options = {}) {
76+
return XhrRequest.ajax(XhrRequest.Method.PUT, url, payload, headers, options);
77+
}
78+
static delete(url, payload = null, headers = null, options = {}) {
79+
return XhrRequest.ajax(XhrRequest.Method.DELETE, url, payload, headers, options);
80+
}
81+
static createXMLHTTPObject() {
82+
let xmlhttp = null;
83+
for (const i of XhrRequest.XMLHttpFactories) {
84+
try {
85+
xmlhttp = i();
86+
}
87+
catch (e) {
88+
continue;
89+
}
90+
break;
91+
}
92+
return xmlhttp;
93+
}
94+
}
95+
XhrRequest.XMLHttpFactories = [
96+
() => new XMLHttpRequest(),
97+
() => new window["ActiveXObject"]("Msxml2.XMLHTTP"),
98+
() => new window["ActiveXObject"]("Msxml3.XMLHTTP"),
99+
() => new window["ActiveXObject"]("Microsoft.XMLHTTP")
100+
];
101+
(function (XhrRequest) {
102+
let Method;
103+
(function (Method) {
104+
Method[Method["GET"] = "GET"] = "GET";
105+
Method[Method["POST"] = "POST"] = "POST";
106+
Method[Method["PUT"] = "PUT"] = "PUT";
107+
Method[Method["DELETE"] = "DELETE"] = "DELETE";
108+
})(Method = XhrRequest.Method || (XhrRequest.Method = {}));
109+
})(XhrRequest || (XhrRequest = {}));
110+
export default XhrRequest;

0 commit comments

Comments
 (0)