mirror of
https://github.com/The-Low-Code-Foundation/OpenNoodl.git
synced 2026-01-12 23:32:55 +01:00
Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com> Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com> Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com> Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com> Co-Authored-By: Johan <4934465+joolsus@users.noreply.github.com> Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com> Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
This commit is contained in:
73
packages/noodl-viewer-react/src/api/cloudfunctions.js
Normal file
73
packages/noodl-viewer-react/src/api/cloudfunctions.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const NoodlRuntime = require('@noodl/runtime');
|
||||
|
||||
function _makeRequest(path, options) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === 4) {
|
||||
var json;
|
||||
try {
|
||||
json = JSON.parse(xhr.response);
|
||||
} catch (e) {}
|
||||
|
||||
if (xhr.status === 200 || xhr.status === 201) {
|
||||
options.success(json);
|
||||
} else options.error(json);
|
||||
}
|
||||
};
|
||||
|
||||
xhr.open(options.method || 'GET', options.endpoint + path, true);
|
||||
|
||||
xhr.setRequestHeader('X-Parse-Application-Id', options.appId);
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
|
||||
const cloudServices = NoodlRuntime.instance.getMetaData('cloudservices');
|
||||
if (cloudServices && cloudServices.deployVersion) {
|
||||
xhr.setRequestHeader('x-noodl-cloud-version', cloudServices.deployVersion);
|
||||
}
|
||||
|
||||
// Check for current users
|
||||
var _cu = localStorage['Parse/' + options.appId + '/currentUser'];
|
||||
if (_cu !== undefined) {
|
||||
try {
|
||||
const currentUser = JSON.parse(_cu);
|
||||
xhr.setRequestHeader('X-Parse-Session-Token', currentUser.sessionToken);
|
||||
} catch (e) {
|
||||
// Failed to extract session token
|
||||
}
|
||||
}
|
||||
|
||||
xhr.send(JSON.stringify(options.content));
|
||||
}
|
||||
|
||||
const cloudfunctions = {
|
||||
async run(functionName, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cloudServices = NoodlRuntime.instance.getMetaData('cloudservices');
|
||||
if (cloudServices === undefined) {
|
||||
reject('No cloud services defined in this project.');
|
||||
return;
|
||||
}
|
||||
|
||||
const appId = cloudServices.appId;
|
||||
const endpoint = NoodlRuntime.instance.editorConnection.isRunningLocally()
|
||||
? `http://${window.location.hostname}:8577`
|
||||
: cloudServices.endpoint;
|
||||
|
||||
_makeRequest('/functions/' + encodeURIComponent(functionName), {
|
||||
appId,
|
||||
endpoint,
|
||||
content: params,
|
||||
method: 'POST',
|
||||
success: (res) => {
|
||||
resolve(res ? res.result : undefined);
|
||||
},
|
||||
error: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = cloudfunctions;
|
||||
Reference in New Issue
Block a user