From dcbeb5721d64603f8d7283fd93db8446bfc75ef6 Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Sun, 12 Feb 2023 15:33:48 +0100
Subject: [PATCH] feat: add callback configuration to restapi

---
 .../source/data/datasource/server/restapi.mjs       | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/application/source/data/datasource/server/restapi.mjs b/application/source/data/datasource/server/restapi.mjs
index d4eaa2d9d..174a0b2ec 100644
--- a/application/source/data/datasource/server/restapi.mjs
+++ b/application/source/data/datasource/server/restapi.mjs
@@ -49,6 +49,7 @@ class RestAPI extends Server {
      * @property {Object} write.init={} An options object containing any custom settings that you want to apply to the request. The parameters are identical to those of the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request|Request constructor}
      * @property {string} write.init.method=POST
      * @property {Object} write.init.headers Object containing any custom headers that you want to apply to the request.
+     * @property {string} write.responseCallback Callback function to be executed after the request has been completed.
      * @property {string} write.acceptedStatus=[200,201]
      * @property {string} write.url URL
      * @property {Object} write.mapping the mapping is applied before writing.
@@ -74,6 +75,7 @@ class RestAPI extends Server {
                 init: {
                     method: "POST",
                 },
+                responseCallback: undefined,
                 acceptedStatus: [200, 201],
                 url: undefined,
                 mapping: {
@@ -92,6 +94,7 @@ class RestAPI extends Server {
                 init: {
                     method: "GET",
                 },
+                responseCallback: undefined,
                 acceptedStatus: [200],
                 url: undefined,
                 mapping: {
@@ -115,9 +118,12 @@ class RestAPI extends Server {
         if (!isObject(init)) init = {};
         if (!init["method"]) init["method"] = "GET";
 
-        return fetchData.call(this, "read", (obj) => {
+        let callback = self.getOption("read.responseCallback");
+        if(!callback) callback = (obj) => {
             self.set(self.transformServerPayload.call(self, obj));
-        });
+        };
+        
+        return fetchData.call(this, "read", callback);
     }
 
     /**
@@ -139,7 +145,8 @@ class RestAPI extends Server {
         let obj = self.prepareServerPayload(self.get());
         init["body"] = JSON.stringify(obj);
 
-        return fetchData.call(this, init, "write");
+        let callback = self.getOption("write.responseCallback");
+        return fetchData.call(this, init, "write", callback);
     }
 
     /**
-- 
GitLab