@christianriedl/rest 1.0.53 → 1.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/authorize.d.ts +57 -55
- package/dist/authorize.js +137 -131
- package/dist/authorize.js.map +1 -1
- package/dist/dummyOauth.d.ts +10 -10
- package/dist/dummyOauth.js +23 -23
- package/dist/iOauth.d.ts +7 -7
- package/dist/iOauth.js +2 -2
- package/dist/iRest.d.ts +35 -35
- package/dist/iRest.js +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/oauth.d.ts +40 -40
- package/dist/oauth.js +212 -212
- package/dist/oauth.js.map +1 -1
- package/dist/rest.d.ts +32 -32
- package/dist/rest.js +323 -323
- package/dist/rest.js.map +1 -1
- package/package.json +4 -4
package/dist/rest.js
CHANGED
|
@@ -1,324 +1,324 @@
|
|
|
1
|
-
export * from './iRest';
|
|
2
|
-
import { LogLevel } from '@christianriedl/utils';
|
|
3
|
-
export class Rest {
|
|
4
|
-
log;
|
|
5
|
-
logErr;
|
|
6
|
-
auth;
|
|
7
|
-
mode;
|
|
8
|
-
serviceUrl;
|
|
9
|
-
constructor(serviceUrl, mode, oauth, log, logErr) {
|
|
10
|
-
this.serviceUrl = serviceUrl;
|
|
11
|
-
this.log = log;
|
|
12
|
-
if (logErr)
|
|
13
|
-
this.logErr = log;
|
|
14
|
-
this.mode = mode;
|
|
15
|
-
this.auth = oauth;
|
|
16
|
-
}
|
|
17
|
-
;
|
|
18
|
-
static buildUrl(url, parameters) {
|
|
19
|
-
let qs = "";
|
|
20
|
-
if (parameters) {
|
|
21
|
-
for (const key in parameters) {
|
|
22
|
-
if (parameters.hasOwnProperty(key)) {
|
|
23
|
-
const value = parameters[key];
|
|
24
|
-
if (typeof value !== 'undefined' && value !== "") {
|
|
25
|
-
qs += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&";
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (qs.length > 0) {
|
|
31
|
-
qs = qs.substring(0, qs.length - 1); //chop off last "&"
|
|
32
|
-
url = url + "?" + qs;
|
|
33
|
-
}
|
|
34
|
-
return url;
|
|
35
|
-
}
|
|
36
|
-
static fetch(url, method, requestMode, body, authorization, resolve, reject, log, adjustResult, contentType, headers) {
|
|
37
|
-
if (body && !contentType) {
|
|
38
|
-
if (typeof body === 'string') {
|
|
39
|
-
contentType = 'text/plain';
|
|
40
|
-
}
|
|
41
|
-
else if (body instanceof FormData) {
|
|
42
|
-
//contentType = 'multipart/form-data'; no contenttype to get correct boundary
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
contentType = 'application/json';
|
|
46
|
-
body = JSON.stringify(body);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
const init = {
|
|
50
|
-
method: method,
|
|
51
|
-
headers: {
|
|
52
|
-
"Accept": "application/json"
|
|
53
|
-
},
|
|
54
|
-
mode: requestMode
|
|
55
|
-
};
|
|
56
|
-
if (requestMode === 'cors') {
|
|
57
|
-
init.headers["Access-Control-Allow-Origin"] = "*";
|
|
58
|
-
}
|
|
59
|
-
if (authorization) {
|
|
60
|
-
init.headers["Authorization"] = authorization;
|
|
61
|
-
}
|
|
62
|
-
if (headers) {
|
|
63
|
-
for (const key in headers) {
|
|
64
|
-
init.headers[key] = headers[key];
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (body) {
|
|
68
|
-
init.body = body;
|
|
69
|
-
if (contentType)
|
|
70
|
-
init.headers["Content-Type"] = contentType;
|
|
71
|
-
}
|
|
72
|
-
fetch(url, init)
|
|
73
|
-
.then((response) => {
|
|
74
|
-
const combinedResp = Rest.combinedResponse(response, undefined, log);
|
|
75
|
-
return combinedResp;
|
|
76
|
-
})
|
|
77
|
-
.then((res) => {
|
|
78
|
-
if (adjustResult)
|
|
79
|
-
adjustResult(res);
|
|
80
|
-
resolve(res);
|
|
81
|
-
})
|
|
82
|
-
.catch((reason) => reject(reason));
|
|
83
|
-
}
|
|
84
|
-
static fetchBlob(url, requestMode, blob, mediaType, responseType, authorization, resolve, reject, log) {
|
|
85
|
-
const init = {
|
|
86
|
-
method: "POST",
|
|
87
|
-
headers: {
|
|
88
|
-
"Accept": responseType ? responseType : "application/json"
|
|
89
|
-
},
|
|
90
|
-
mode: requestMode
|
|
91
|
-
};
|
|
92
|
-
if (authorization) {
|
|
93
|
-
init.headers["Authorization"] = authorization;
|
|
94
|
-
}
|
|
95
|
-
init.body = blob;
|
|
96
|
-
if (mediaType)
|
|
97
|
-
init.headers["Content-Type"] = mediaType;
|
|
98
|
-
fetch(url, init)
|
|
99
|
-
.then((response) => {
|
|
100
|
-
const combinedResp = Rest.combinedResponse(response, responseType, log);
|
|
101
|
-
return combinedResp;
|
|
102
|
-
})
|
|
103
|
-
.then((res) => {
|
|
104
|
-
resolve(res);
|
|
105
|
-
})
|
|
106
|
-
.catch((reason) => reject(reason));
|
|
107
|
-
}
|
|
108
|
-
static getBlob(url, requestMode, mediaType, authorization, resolve, reject, headers, log) {
|
|
109
|
-
const init = {
|
|
110
|
-
method: "GET",
|
|
111
|
-
headers: {
|
|
112
|
-
"Accept": mediaType
|
|
113
|
-
},
|
|
114
|
-
mode: requestMode
|
|
115
|
-
};
|
|
116
|
-
if (authorization) {
|
|
117
|
-
init.headers["Authorization"] = authorization;
|
|
118
|
-
}
|
|
119
|
-
if (headers) {
|
|
120
|
-
for (const key in headers) {
|
|
121
|
-
init.headers[key] = headers[key];
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
fetch(url, init)
|
|
125
|
-
.then((response) => {
|
|
126
|
-
const combinedResp = Rest.combinedResponse(response, mediaType, log);
|
|
127
|
-
return combinedResp;
|
|
128
|
-
})
|
|
129
|
-
.then((res) => {
|
|
130
|
-
resolve(res);
|
|
131
|
-
})
|
|
132
|
-
.catch((reason) => reject(reason));
|
|
133
|
-
}
|
|
134
|
-
static bearerAuth(token) {
|
|
135
|
-
return token !== '' ? 'Bearer ' + token : undefined;
|
|
136
|
-
}
|
|
137
|
-
toBoolean(result) {
|
|
138
|
-
return new Promise(function (resolve, reject) {
|
|
139
|
-
result
|
|
140
|
-
.then((res) => {
|
|
141
|
-
if (res.ok) {
|
|
142
|
-
if (res.result) {
|
|
143
|
-
if (typeof res.result.result === 'boolean') {
|
|
144
|
-
resolve(res.result.result);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
resolve(true);
|
|
148
|
-
}
|
|
149
|
-
else
|
|
150
|
-
resolve(false);
|
|
151
|
-
})
|
|
152
|
-
.catch((reason) => reject(reason));
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
toData(result) {
|
|
156
|
-
return new Promise(function (resolve, reject) {
|
|
157
|
-
result
|
|
158
|
-
.then((res) => {
|
|
159
|
-
if (res.ok && res.result)
|
|
160
|
-
resolve(res.result);
|
|
161
|
-
else
|
|
162
|
-
reject("no content");
|
|
163
|
-
})
|
|
164
|
-
.catch((reason) => reject(reason));
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
toPromise(data) {
|
|
168
|
-
return new Promise(function (resolve, reject) {
|
|
169
|
-
resolve(data);
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
getData(path, queryParameter, adjustResult) {
|
|
173
|
-
const self = this;
|
|
174
|
-
const url = Rest.buildUrl(this.serviceUrl + path, queryParameter);
|
|
175
|
-
this.trace("getData", url);
|
|
176
|
-
const promise = new Promise(function (resolve, reject) {
|
|
177
|
-
self.auth.getToken()
|
|
178
|
-
.then((token) => {
|
|
179
|
-
Rest.fetch(url, "GET", self.mode, undefined, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
180
|
-
})
|
|
181
|
-
.catch((err) => reject(err));
|
|
182
|
-
});
|
|
183
|
-
return promise;
|
|
184
|
-
}
|
|
185
|
-
postData(path, data) {
|
|
186
|
-
const self = this;
|
|
187
|
-
const url = this.serviceUrl + path;
|
|
188
|
-
this.trace("postData", url, data);
|
|
189
|
-
const promise = new Promise(function (resolve, reject) {
|
|
190
|
-
self.auth.getToken()
|
|
191
|
-
.then((token) => {
|
|
192
|
-
Rest.fetch(url, "POST", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr);
|
|
193
|
-
})
|
|
194
|
-
.catch((err) => reject(err));
|
|
195
|
-
});
|
|
196
|
-
return promise;
|
|
197
|
-
}
|
|
198
|
-
putData(path, data, adjustResult) {
|
|
199
|
-
const self = this;
|
|
200
|
-
const url = this.serviceUrl + path;
|
|
201
|
-
this.trace("putData", url, data);
|
|
202
|
-
const promise = new Promise(function (resolve, reject) {
|
|
203
|
-
self.auth.getToken()
|
|
204
|
-
.then((token) => {
|
|
205
|
-
Rest.fetch(url, "PUT", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
206
|
-
})
|
|
207
|
-
.catch((err) => reject(err));
|
|
208
|
-
});
|
|
209
|
-
return promise;
|
|
210
|
-
}
|
|
211
|
-
postDataEx(path, data, adjustResult) {
|
|
212
|
-
const self = this;
|
|
213
|
-
const url = this.serviceUrl + path;
|
|
214
|
-
this.trace("postDataEx", url, data);
|
|
215
|
-
const promise = new Promise(function (resolve, reject) {
|
|
216
|
-
self.auth.getToken()
|
|
217
|
-
.then((token) => {
|
|
218
|
-
Rest.fetch(url, "POST", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
219
|
-
})
|
|
220
|
-
.catch((err) => reject(err));
|
|
221
|
-
});
|
|
222
|
-
return promise;
|
|
223
|
-
}
|
|
224
|
-
postBlob(path, blob, mediaType, responseType) {
|
|
225
|
-
const self = this;
|
|
226
|
-
const url = this.serviceUrl + path;
|
|
227
|
-
this.trace("postBlob", url);
|
|
228
|
-
const promise = new Promise(function (resolve, reject) {
|
|
229
|
-
self.auth.getToken()
|
|
230
|
-
.then((token) => {
|
|
231
|
-
Rest.fetchBlob(url, self.mode, blob, mediaType, responseType, Rest.bearerAuth(token), resolve, reject, self.logErr);
|
|
232
|
-
})
|
|
233
|
-
.catch((err) => reject(err));
|
|
234
|
-
});
|
|
235
|
-
return promise;
|
|
236
|
-
}
|
|
237
|
-
getBlob(path, mediaType, queryParameter, headers) {
|
|
238
|
-
const self = this;
|
|
239
|
-
const url = Rest.buildUrl(this.serviceUrl + path, queryParameter);
|
|
240
|
-
this.trace("getBlob", url);
|
|
241
|
-
const promise = new Promise(function (resolve, reject) {
|
|
242
|
-
self.auth.getToken()
|
|
243
|
-
.then((token) => {
|
|
244
|
-
Rest.getBlob(url, self.mode, mediaType, Rest.bearerAuth(token), resolve, reject, headers, self.logErr);
|
|
245
|
-
})
|
|
246
|
-
.catch((err) => reject(err));
|
|
247
|
-
});
|
|
248
|
-
return promise;
|
|
249
|
-
}
|
|
250
|
-
deleteData(path, data, adjustResult) {
|
|
251
|
-
const self = this;
|
|
252
|
-
const url = this.serviceUrl + path;
|
|
253
|
-
this.trace("deleteData", url, data);
|
|
254
|
-
const promise = new Promise(function (resolve, reject) {
|
|
255
|
-
self.auth.getToken()
|
|
256
|
-
.then((token) => {
|
|
257
|
-
Rest.fetch(url, "DELETE", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
258
|
-
})
|
|
259
|
-
.catch((err) => reject(err));
|
|
260
|
-
});
|
|
261
|
-
return promise;
|
|
262
|
-
}
|
|
263
|
-
static combinedResponse(response, contentType, log) {
|
|
264
|
-
const promise = new Promise(function (resolve, reject) {
|
|
265
|
-
if (response.status === 204) { // no content
|
|
266
|
-
resolve({
|
|
267
|
-
ok: response.ok, statusCode: response.status, headers: response.headers,
|
|
268
|
-
statusText: response.statusText
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
else if (!response.ok) {
|
|
272
|
-
resolve({
|
|
273
|
-
ok: false, statusCode: response.status, headers: response.headers,
|
|
274
|
-
statusText: response.statusText
|
|
275
|
-
});
|
|
276
|
-
if (log)
|
|
277
|
-
log.warning(`REST error at ${response.url} : ${response.status}`);
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
let promise;
|
|
281
|
-
if (contentType) {
|
|
282
|
-
if (contentType.startsWith("text"))
|
|
283
|
-
promise = response.text();
|
|
284
|
-
else if (contentType.startsWith("image"))
|
|
285
|
-
promise = response.blob();
|
|
286
|
-
else if (contentType.startsWith("audio"))
|
|
287
|
-
promise = response.blob();
|
|
288
|
-
else if (contentType == "application/octet-stream")
|
|
289
|
-
promise = response.blob();
|
|
290
|
-
else
|
|
291
|
-
promise = response.json();
|
|
292
|
-
}
|
|
293
|
-
else
|
|
294
|
-
promise = response.json();
|
|
295
|
-
promise
|
|
296
|
-
.then((res) => {
|
|
297
|
-
resolve({
|
|
298
|
-
ok: response.ok, statusCode: response.status, headers: response.headers,
|
|
299
|
-
statusText: response.statusText, result: res
|
|
300
|
-
});
|
|
301
|
-
})
|
|
302
|
-
.catch((reason) => reject(reason));
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
return promise;
|
|
306
|
-
}
|
|
307
|
-
trace(msg, url, data) {
|
|
308
|
-
if (this.log && this.log.minLogLevel <= LogLevel.Trace) {
|
|
309
|
-
if (data == undefined) {
|
|
310
|
-
this.log.trace(`${msg} to ${url}`);
|
|
311
|
-
}
|
|
312
|
-
else if (typeof data === 'string') {
|
|
313
|
-
this.log.trace(`${msg} to ${url} : ${data}`);
|
|
314
|
-
}
|
|
315
|
-
else if (data instanceof FormData) {
|
|
316
|
-
this.log.trace(`${msg} to ${url} : FormData`);
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
this.log.trace(`${msg} to ${url} : ${JSON.stringify(data)}`);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
1
|
+
export * from './iRest';
|
|
2
|
+
import { LogLevel } from '@christianriedl/utils';
|
|
3
|
+
export class Rest {
|
|
4
|
+
log;
|
|
5
|
+
logErr;
|
|
6
|
+
auth;
|
|
7
|
+
mode;
|
|
8
|
+
serviceUrl;
|
|
9
|
+
constructor(serviceUrl, mode, oauth, log, logErr) {
|
|
10
|
+
this.serviceUrl = serviceUrl;
|
|
11
|
+
this.log = log;
|
|
12
|
+
if (logErr)
|
|
13
|
+
this.logErr = log;
|
|
14
|
+
this.mode = mode;
|
|
15
|
+
this.auth = oauth;
|
|
16
|
+
}
|
|
17
|
+
;
|
|
18
|
+
static buildUrl(url, parameters) {
|
|
19
|
+
let qs = "";
|
|
20
|
+
if (parameters) {
|
|
21
|
+
for (const key in parameters) {
|
|
22
|
+
if (parameters.hasOwnProperty(key)) {
|
|
23
|
+
const value = parameters[key];
|
|
24
|
+
if (typeof value !== 'undefined' && value !== "") {
|
|
25
|
+
qs += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (qs.length > 0) {
|
|
31
|
+
qs = qs.substring(0, qs.length - 1); //chop off last "&"
|
|
32
|
+
url = url + "?" + qs;
|
|
33
|
+
}
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
static fetch(url, method, requestMode, body, authorization, resolve, reject, log, adjustResult, contentType, headers) {
|
|
37
|
+
if (body && !contentType) {
|
|
38
|
+
if (typeof body === 'string') {
|
|
39
|
+
contentType = 'text/plain';
|
|
40
|
+
}
|
|
41
|
+
else if (body instanceof FormData) {
|
|
42
|
+
//contentType = 'multipart/form-data'; no contenttype to get correct boundary
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
contentType = 'application/json';
|
|
46
|
+
body = JSON.stringify(body);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const init = {
|
|
50
|
+
method: method,
|
|
51
|
+
headers: {
|
|
52
|
+
"Accept": "application/json"
|
|
53
|
+
},
|
|
54
|
+
mode: requestMode
|
|
55
|
+
};
|
|
56
|
+
if (requestMode === 'cors') {
|
|
57
|
+
init.headers["Access-Control-Allow-Origin"] = "*";
|
|
58
|
+
}
|
|
59
|
+
if (authorization) {
|
|
60
|
+
init.headers["Authorization"] = authorization;
|
|
61
|
+
}
|
|
62
|
+
if (headers) {
|
|
63
|
+
for (const key in headers) {
|
|
64
|
+
init.headers[key] = headers[key];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (body) {
|
|
68
|
+
init.body = body;
|
|
69
|
+
if (contentType)
|
|
70
|
+
init.headers["Content-Type"] = contentType;
|
|
71
|
+
}
|
|
72
|
+
fetch(url, init)
|
|
73
|
+
.then((response) => {
|
|
74
|
+
const combinedResp = Rest.combinedResponse(response, undefined, log);
|
|
75
|
+
return combinedResp;
|
|
76
|
+
})
|
|
77
|
+
.then((res) => {
|
|
78
|
+
if (adjustResult)
|
|
79
|
+
adjustResult(res);
|
|
80
|
+
resolve(res);
|
|
81
|
+
})
|
|
82
|
+
.catch((reason) => reject(reason));
|
|
83
|
+
}
|
|
84
|
+
static fetchBlob(url, requestMode, blob, mediaType, responseType, authorization, resolve, reject, log) {
|
|
85
|
+
const init = {
|
|
86
|
+
method: "POST",
|
|
87
|
+
headers: {
|
|
88
|
+
"Accept": responseType ? responseType : "application/json"
|
|
89
|
+
},
|
|
90
|
+
mode: requestMode
|
|
91
|
+
};
|
|
92
|
+
if (authorization) {
|
|
93
|
+
init.headers["Authorization"] = authorization;
|
|
94
|
+
}
|
|
95
|
+
init.body = blob;
|
|
96
|
+
if (mediaType)
|
|
97
|
+
init.headers["Content-Type"] = mediaType;
|
|
98
|
+
fetch(url, init)
|
|
99
|
+
.then((response) => {
|
|
100
|
+
const combinedResp = Rest.combinedResponse(response, responseType, log);
|
|
101
|
+
return combinedResp;
|
|
102
|
+
})
|
|
103
|
+
.then((res) => {
|
|
104
|
+
resolve(res);
|
|
105
|
+
})
|
|
106
|
+
.catch((reason) => reject(reason));
|
|
107
|
+
}
|
|
108
|
+
static getBlob(url, requestMode, mediaType, authorization, resolve, reject, headers, log) {
|
|
109
|
+
const init = {
|
|
110
|
+
method: "GET",
|
|
111
|
+
headers: {
|
|
112
|
+
"Accept": mediaType
|
|
113
|
+
},
|
|
114
|
+
mode: requestMode
|
|
115
|
+
};
|
|
116
|
+
if (authorization) {
|
|
117
|
+
init.headers["Authorization"] = authorization;
|
|
118
|
+
}
|
|
119
|
+
if (headers) {
|
|
120
|
+
for (const key in headers) {
|
|
121
|
+
init.headers[key] = headers[key];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
fetch(url, init)
|
|
125
|
+
.then((response) => {
|
|
126
|
+
const combinedResp = Rest.combinedResponse(response, mediaType, log);
|
|
127
|
+
return combinedResp;
|
|
128
|
+
})
|
|
129
|
+
.then((res) => {
|
|
130
|
+
resolve(res);
|
|
131
|
+
})
|
|
132
|
+
.catch((reason) => reject(reason));
|
|
133
|
+
}
|
|
134
|
+
static bearerAuth(token) {
|
|
135
|
+
return token !== '' ? 'Bearer ' + token : undefined;
|
|
136
|
+
}
|
|
137
|
+
toBoolean(result) {
|
|
138
|
+
return new Promise(function (resolve, reject) {
|
|
139
|
+
result
|
|
140
|
+
.then((res) => {
|
|
141
|
+
if (res.ok) {
|
|
142
|
+
if (res.result) {
|
|
143
|
+
if (typeof res.result.result === 'boolean') {
|
|
144
|
+
resolve(res.result.result);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
resolve(true);
|
|
148
|
+
}
|
|
149
|
+
else
|
|
150
|
+
resolve(false);
|
|
151
|
+
})
|
|
152
|
+
.catch((reason) => reject(reason));
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
toData(result) {
|
|
156
|
+
return new Promise(function (resolve, reject) {
|
|
157
|
+
result
|
|
158
|
+
.then((res) => {
|
|
159
|
+
if (res.ok && res.result)
|
|
160
|
+
resolve(res.result);
|
|
161
|
+
else
|
|
162
|
+
reject("no content");
|
|
163
|
+
})
|
|
164
|
+
.catch((reason) => reject(reason));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
toPromise(data) {
|
|
168
|
+
return new Promise(function (resolve, reject) {
|
|
169
|
+
resolve(data);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
getData(path, queryParameter, adjustResult) {
|
|
173
|
+
const self = this;
|
|
174
|
+
const url = Rest.buildUrl(this.serviceUrl + path, queryParameter);
|
|
175
|
+
this.trace("getData", url);
|
|
176
|
+
const promise = new Promise(function (resolve, reject) {
|
|
177
|
+
self.auth.getToken()
|
|
178
|
+
.then((token) => {
|
|
179
|
+
Rest.fetch(url, "GET", self.mode, undefined, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
180
|
+
})
|
|
181
|
+
.catch((err) => reject(err));
|
|
182
|
+
});
|
|
183
|
+
return promise;
|
|
184
|
+
}
|
|
185
|
+
postData(path, data) {
|
|
186
|
+
const self = this;
|
|
187
|
+
const url = this.serviceUrl + path;
|
|
188
|
+
this.trace("postData", url, data);
|
|
189
|
+
const promise = new Promise(function (resolve, reject) {
|
|
190
|
+
self.auth.getToken()
|
|
191
|
+
.then((token) => {
|
|
192
|
+
Rest.fetch(url, "POST", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr);
|
|
193
|
+
})
|
|
194
|
+
.catch((err) => reject(err));
|
|
195
|
+
});
|
|
196
|
+
return promise;
|
|
197
|
+
}
|
|
198
|
+
putData(path, data, adjustResult) {
|
|
199
|
+
const self = this;
|
|
200
|
+
const url = this.serviceUrl + path;
|
|
201
|
+
this.trace("putData", url, data);
|
|
202
|
+
const promise = new Promise(function (resolve, reject) {
|
|
203
|
+
self.auth.getToken()
|
|
204
|
+
.then((token) => {
|
|
205
|
+
Rest.fetch(url, "PUT", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
206
|
+
})
|
|
207
|
+
.catch((err) => reject(err));
|
|
208
|
+
});
|
|
209
|
+
return promise;
|
|
210
|
+
}
|
|
211
|
+
postDataEx(path, data, adjustResult) {
|
|
212
|
+
const self = this;
|
|
213
|
+
const url = this.serviceUrl + path;
|
|
214
|
+
this.trace("postDataEx", url, data);
|
|
215
|
+
const promise = new Promise(function (resolve, reject) {
|
|
216
|
+
self.auth.getToken()
|
|
217
|
+
.then((token) => {
|
|
218
|
+
Rest.fetch(url, "POST", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
219
|
+
})
|
|
220
|
+
.catch((err) => reject(err));
|
|
221
|
+
});
|
|
222
|
+
return promise;
|
|
223
|
+
}
|
|
224
|
+
postBlob(path, blob, mediaType, responseType) {
|
|
225
|
+
const self = this;
|
|
226
|
+
const url = this.serviceUrl + path;
|
|
227
|
+
this.trace("postBlob", url);
|
|
228
|
+
const promise = new Promise(function (resolve, reject) {
|
|
229
|
+
self.auth.getToken()
|
|
230
|
+
.then((token) => {
|
|
231
|
+
Rest.fetchBlob(url, self.mode, blob, mediaType, responseType, Rest.bearerAuth(token), resolve, reject, self.logErr);
|
|
232
|
+
})
|
|
233
|
+
.catch((err) => reject(err));
|
|
234
|
+
});
|
|
235
|
+
return promise;
|
|
236
|
+
}
|
|
237
|
+
getBlob(path, mediaType, queryParameter, headers) {
|
|
238
|
+
const self = this;
|
|
239
|
+
const url = Rest.buildUrl(this.serviceUrl + path, queryParameter);
|
|
240
|
+
this.trace("getBlob", url);
|
|
241
|
+
const promise = new Promise(function (resolve, reject) {
|
|
242
|
+
self.auth.getToken()
|
|
243
|
+
.then((token) => {
|
|
244
|
+
Rest.getBlob(url, self.mode, mediaType, Rest.bearerAuth(token), resolve, reject, headers, self.logErr);
|
|
245
|
+
})
|
|
246
|
+
.catch((err) => reject(err));
|
|
247
|
+
});
|
|
248
|
+
return promise;
|
|
249
|
+
}
|
|
250
|
+
deleteData(path, data, adjustResult) {
|
|
251
|
+
const self = this;
|
|
252
|
+
const url = this.serviceUrl + path;
|
|
253
|
+
this.trace("deleteData", url, data);
|
|
254
|
+
const promise = new Promise(function (resolve, reject) {
|
|
255
|
+
self.auth.getToken()
|
|
256
|
+
.then((token) => {
|
|
257
|
+
Rest.fetch(url, "DELETE", self.mode, data, Rest.bearerAuth(token), resolve, reject, self.logErr, adjustResult);
|
|
258
|
+
})
|
|
259
|
+
.catch((err) => reject(err));
|
|
260
|
+
});
|
|
261
|
+
return promise;
|
|
262
|
+
}
|
|
263
|
+
static combinedResponse(response, contentType, log) {
|
|
264
|
+
const promise = new Promise(function (resolve, reject) {
|
|
265
|
+
if (response.status === 204) { // no content
|
|
266
|
+
resolve({
|
|
267
|
+
ok: response.ok, statusCode: response.status, headers: response.headers,
|
|
268
|
+
statusText: response.statusText
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
else if (!response.ok) {
|
|
272
|
+
resolve({
|
|
273
|
+
ok: false, statusCode: response.status, headers: response.headers,
|
|
274
|
+
statusText: response.statusText
|
|
275
|
+
});
|
|
276
|
+
if (log)
|
|
277
|
+
log.warning(`REST error at ${response.url} : ${response.status}`);
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
let promise;
|
|
281
|
+
if (contentType) {
|
|
282
|
+
if (contentType.startsWith("text"))
|
|
283
|
+
promise = response.text();
|
|
284
|
+
else if (contentType.startsWith("image"))
|
|
285
|
+
promise = response.blob();
|
|
286
|
+
else if (contentType.startsWith("audio"))
|
|
287
|
+
promise = response.blob();
|
|
288
|
+
else if (contentType == "application/octet-stream")
|
|
289
|
+
promise = response.blob();
|
|
290
|
+
else
|
|
291
|
+
promise = response.json();
|
|
292
|
+
}
|
|
293
|
+
else
|
|
294
|
+
promise = response.json();
|
|
295
|
+
promise
|
|
296
|
+
.then((res) => {
|
|
297
|
+
resolve({
|
|
298
|
+
ok: response.ok, statusCode: response.status, headers: response.headers,
|
|
299
|
+
statusText: response.statusText, result: res
|
|
300
|
+
});
|
|
301
|
+
})
|
|
302
|
+
.catch((reason) => reject(reason));
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
return promise;
|
|
306
|
+
}
|
|
307
|
+
trace(msg, url, data) {
|
|
308
|
+
if (this.log && this.log.minLogLevel <= LogLevel.Trace) {
|
|
309
|
+
if (data == undefined) {
|
|
310
|
+
this.log.trace(`${msg} to ${url}`);
|
|
311
|
+
}
|
|
312
|
+
else if (typeof data === 'string') {
|
|
313
|
+
this.log.trace(`${msg} to ${url} : ${data}`);
|
|
314
|
+
}
|
|
315
|
+
else if (data instanceof FormData) {
|
|
316
|
+
this.log.trace(`${msg} to ${url} : FormData`);
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
this.log.trace(`${msg} to ${url} : ${JSON.stringify(data)}`);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
324
|
//# sourceMappingURL=rest.js.map
|