From 239bfe896e87cd87f67a1f28cdc604bca8c2af58 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 19 May 2019 15:56:44 +0200 Subject: [PATCH] Can make API requests with promises --- assets/js/common/api.js | 25 +++++++++++++++++++++++++ assets/js/common/functionsSchema.js | 6 ++++++ assets/js/common/shorcuts.js | 13 +++++++++++++ 3 files changed, 44 insertions(+) diff --git a/assets/js/common/api.js b/assets/js/common/api.js index da18fc91..2489bcfc 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -4,6 +4,31 @@ * @author Pierre HUBERT */ ComunicWeb.common.api = { + + /** + * Make an asynchronous request over the API + * + * @returns {Promise} + */ + exec: function(apiURI, args, withLogin){ + + if(!args) + args = {}; + + return new Promise((resolve, reject) => { + + this.makeAPIrequest(apiURI, args, withLogin, result => { + + if(result.error) + reject(result.error); + + else + resolve(result); + + }); + }); + }, + /** * Make an API request * diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index c39a89e6..dadbd682 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -47,6 +47,12 @@ var ComunicWeb = { * API functions */ api: { + + /** + * Make an asynchronous request + */ + exec: function(apiURI, args, withLogin){}, + /** * Make an API request */ diff --git a/assets/js/common/shorcuts.js b/assets/js/common/shorcuts.js index b5df072d..07ce3177 100644 --- a/assets/js/common/shorcuts.js +++ b/assets/js/common/shorcuts.js @@ -4,6 +4,19 @@ * @author Pierre HUBERT */ +/** + * Perform an API request + * + * @param {String} uri The URI of the request on the API + * @param {Object} args The list of arguments to pass with the request + * @param {Bool} withLogin Specify whether login is required or not to + * achieve the request + * @return {Promise} + */ +function api(uri, args, withLogin){ + return ComunicWeb.common.api.exec(uri, args, withLogin); +} + /** * Create a quick language access function shorcut *