forked from addvisor-app/add-base-ui5js-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallService.js
144 lines (105 loc) · 4.66 KB
/
callService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
sap.ui.define([], function () {
'use strict';
var host = '/';
return {
getAsync: function (url, data, param, callBackSuccess, callBackError, callBackResponse, ehost) {
return this._asyncCall(url, 'GET', data, param, callBackSuccess, callBackError, callBackResponse, ehost);
},
getSync: async function (url, data, param, callBackSuccess, callBackError, callBackResponse, ehost) {
return await this._syncCall(url, 'GET', data, param, callBackSuccess, callBackError, callBackResponse, ehost);
},
postSync: async function (url, data, param, callBackSuccess, callBackError, callBackResponse, ehost) {
return await this._syncCall(url, 'POST', data, param, callBackSuccess, callBackError, callBackResponse, ehost);
},
postAsync: function (url, data, param, callBackSuccess, callBackError, callBackResponse, ehost) {
return this._asyncCall(url, 'POST', data, param, callBackSuccess, callBackError, callBackResponse, ehost);
},
_syncCall: async function (url, method, data, param, callBackSuccess, callBackError, callBackResponse, ehost) {
let p = this._getParams(data, method, param);
let src = (ehost) ? ehost + host + url : host + url;
if (method == "GET" && data) src = src + data;
return await fetch(src, param || p, 50).then((response) => {
return response.ok ? response.text() : response.text() || response.status;
}).then((data) => {
let resp = {}
try {
resp = JSON.parse(data);
} catch {
}
if (resp && resp.uuid)
throw resp;
if (resp.code && (resp.code === 403)) throw data
if (resp.code && (resp.code < 200 || resp.code > 299)) throw data
return data;
}).catch((err) => {
sap.ui.core.BusyIndicator.hide();
if (err.status)
sap.m.MessageBox.show(err.url,
{
icon: sap.m.MessageBox.Icon.ERROR,
title: err.statusText + " (" + err.status + ")",
actions: [sap.m.MessageBox.Action.OK],
});
if (err.uuid) {
window.open(window.location.origin + "/add/notification/" + err.uuid, '_blank');
}
if (err.toString() === 'TypeError: Failed to fetch') {
window.open(window.location.origin + "/logout", '_blank');
}
});
},
_asyncCall: async function (url, method, data, param, callBackSuccess, callBackError, callBackResponse, ehost) {
let p = this._getParams(data, method, param);
let src = (ehost) ? ehost + host + url : host + url;
if (method == "GET") src = src + data;
return fetch(src, param || p).then((response) => {
if (callBackResponse) {
callBackResponse(response);
} else {
return response.text();
}
}).then((data) => {
if (callBackSuccess) {
callBackSuccess(data);
} else {
return data;
}
}).catch((err) => {
Log.info(err);
callBackError(err);
});
},
setHeaders(headers) {
this._headers = headers;
},
setMethod(method) {
this._method = method;
},
_getParams: function (data, method, param) {
var header = {};
Object.assign(data, {
m: sap.ui.getCore().getModel("userInfo").getData().currentMandt,
a: Object.keys(window["sap-ui-config"]["resourceroots"])[0],
foreground: true
})
header = {
"Content-Type": "application/json",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive"
}
let b = JSON.stringify(data);
this._method = this.__method || "POST";
let defaultParam = {
mode: "cors",
method: this._method,
body: (this._method == "POST") ? b : null,
headers: this._headers || header,
credentials: "include",
timeout: 3000
}
this._headers = null;
return param || defaultParam;
}
}
});