• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16export function mockFetch() {
17  global.sendGroupMessage = global.group.sendGroupMessage;
18  let GroupMessenger = {
19    create: function () {
20      let messenger = {};
21      messenger.send = function (groupName, functionName, ...args) {
22        return new Promise(function (resolve, reject) {
23          let params = messenger.prepareArgs(...args);
24          sendGroupMessage(function (result) {
25            resolve(messenger.parseJsonResult(result));
26          }, function (error) {
27            reject(messenger.parseJsonResult(error));
28          }, groupName, functionName, ...params);
29        })
30      };
31      messenger.parseJsonResult = function (data) {
32        if (data && data.constructor == String) {
33          try {
34            data = JSON.parse(data);
35          } catch (jsonParseErr) {
36            console.warn("parse result exception: " + JSON.stringify(jsonParseErr));
37          }
38        }
39        return data;
40      };
41      messenger.prepareArgs = function (...args) {
42        let result = [...args];
43        for (let i = 0; i < result.length; i++) {
44          if (typeof result[i] === 'function') {
45            result[i] = messenger.packageCallback(result[i]);
46          }
47        }
48        return result;
49      };
50      messenger.packageCallback = function (func) {
51        return function (data) {
52          data = messenger.parseJsonResult(data);
53          if (!Array.isArray(data)) {
54            func(data);
55          } else {
56            func(...data);
57          }
58        };
59      };
60      return messenger;
61    }
62  };
63
64  let CommonCallback = {
65    commonCallback: function commonCallback(callback, flag, data, code) {
66      if (typeof callback === 'function') {
67        switch (flag) {
68          case 'success':
69            callback(data);
70            break;
71          case 'fail':
72            callback(data, code);
73            break;
74          case 'cancel':
75            callback(data);
76            break;
77          case 'complete':
78            callback();
79            break;
80          default:
81            break;
82        }
83      } else {
84        console.warn('callback.' + flag + ' is not function or not present');
85      }
86    }
87  };
88  global.commonCallback = CommonCallback.commonCallback;
89  let CommonCallbackEx = {
90    commonCallbackEx: function commonCallbackEx(callback, result, pluginError) {
91      if ((callback === undefined) || ((callback.success === undefined) && (callback.fail === undefined) && (callback.complete === undefined))) {
92        return CommonCallbackEx.promiseMethod(result, pluginError);
93      } else {
94        return CommonCallbackEx.callbackMethod(callback, result, pluginError);
95      }
96    },
97    promiseMethod: function promiseMethod(result, pluginError) {
98      if (pluginError != undefined) {
99        throw pluginError;
100      }
101      return result;
102    },
103    callbackMethod: function callbackMethod(callback, result, pluginError) {
104      if (pluginError != undefined) {
105        commonCallback(callback.fail, 'fail', pluginError.data, pluginError.code);
106        commonCallback(callback.complete, 'complete');
107        throw pluginError;
108      }
109      commonCallback(callback.success, 'success', result.data);
110      commonCallback(callback.complete, 'complete');
111      return result;
112    },
113    catching: function catching(promise, param) {
114      return promise.then(ret => commonCallbackEx(param, ret))
115        .catch(err => commonCallbackEx(param, null, err));
116    }
117  };
118  global.commonCallbackEx = CommonCallbackEx.commonCallbackEx;
119  global.systemplugin.catching = CommonCallbackEx.catching;
120
121  let FetchObject = {
122    getFetch: function () {
123      let fetch = {}
124      fetch.messenger = GroupMessenger.create();
125      fetch.fetch = async function (param) {
126        return await CommonCallbackEx.catching(this.messenger.send("groupName", "fetch", param), param);
127      }
128      return fetch
129    }
130  }
131
132  global.systemplugin.fetch = FetchObject.getFetch();
133}