grocy/js/lib/api.js
Katharina Bogad 7f598058bf i18n: static pot to json compilation
Translations aren't embedded on every page load anymore;
this saves roughly 55kB on every single request, because
these can now be cached.
2021-06-20 23:12:17 +02:00

257 lines
4.7 KiB
JavaScript

class GrocyApi
{
constructor(Grocy)
{
this.Grocy = Grocy;
}
// This throws a deprecation warning in the console.
// The "clean" solution would be to move all translations
// To be promise-based async stuff, but Grocy not in a shape
// to make this easily possible right now.
// The introduction of a frontend framework like react or vue
// will make this obsolete as well.
LoadLanguageSync(locale)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", this.Grocy.FormatUrl('/js/locales/grocy/' + locale + '.json'), false);
xmlhttp.send();
// if Status OK or NOT MODIFIED
if ((xmlhttp.status == 200 || xmlhttp.status == 304) && xmlhttp.readyState == 4)
{
return JSON.parse(xmlhttp.responseText);
}
else
{
return null;
}
}
Get(apiFunction, success, error)
{
var xhr = new XMLHttpRequest();
var url = this.Grocy.FormatUrl('/api/' + apiFunction);
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200 || xhr.status === 204)
{
if (success)
{
if (xhr.status === 200)
{
success(JSON.parse(xhr.responseText));
}
else
{
success({});
}
}
}
else
{
if (error)
{
error(xhr);
}
}
}
}
xhr.open('GET', url, true);
xhr.send();
}
Post(apiFunction, jsonData, success, error)
{
var xhr = new XMLHttpRequest();
var url = this.Grocy.FormatUrl('/api/' + apiFunction);
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200 || xhr.status === 204)
{
if (success)
{
if (xhr.status === 200)
{
success(JSON.parse(xhr.responseText));
}
else
{
success({});
}
}
}
else
{
if (error)
{
error(xhr);
}
}
}
};
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(jsonData));
}
Put(apiFunction, jsonData, success, error)
{
var xhr = new XMLHttpRequest();
var url = this.Grocy.FormatUrl('/api/' + apiFunction);
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200 || xhr.status === 204)
{
if (success)
{
if (xhr.status === 200)
{
success(JSON.parse(xhr.responseText));
}
else
{
success({});
}
}
}
else
{
if (error)
{
error(xhr);
}
}
}
}
xhr.open('PUT', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(jsonData));
}
Delete(apiFunction, jsonData, success, error)
{
var xhr = new XMLHttpRequest();
var url = this.Grocy.FormatUrl('/api/' + apiFunction);
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200 || xhr.status === 204)
{
if (success)
{
if (xhr.status === 200)
{
success(JSON.parse(xhr.responseText));
}
else
{
success({});
}
}
}
else
{
if (error)
{
error(xhr);
}
}
}
};
xhr.open('DELETE', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(jsonData));
}
UploadFile(file, group, fileName, success, error)
{
var xhr = new XMLHttpRequest();
var url = this.Grocy.FormatUrl('/api/files/' + group + '/' + btoa(fileName));
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200 || xhr.status === 204)
{
if (success)
{
if (xhr.status === 200)
{
success(JSON.parse(xhr.responseText));
}
else
{
success({});
}
}
}
else
{
if (error)
{
error(xhr);
}
}
}
}
xhr.open('PUT', url, true);
xhr.setRequestHeader('Content-type', 'application/octet-stream');
xhr.send(file);
}
DeleteFile(fileName, group, success, error)
{
var xhr = new XMLHttpRequest();
var url = this.Grocy.FormatUrl('/api/files/' + group + '/' + btoa(fileName));
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE)
{
if (xhr.status === 200 || xhr.status === 204)
{
if (success)
{
if (xhr.status === 200)
{
success(JSON.parse(xhr.responseText));
}
else
{
success({});
}
}
}
else
{
if (error)
{
error(xhr);
}
}
}
};
xhr.open('DELETE', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send();
}
}
export { GrocyApi };