@arc-js/cp-request 0.0.1
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/README.md +700 -0
- package/cp-request.all.js +6594 -0
- package/cp-request.all.min.js +2 -0
- package/cp-request.all.min.js.map +1 -0
- package/index.d.ts +116 -0
- package/index.js +642 -0
- package/index.min.d.ts +116 -0
- package/index.min.js +2 -0
- package/index.min.js.map +1 -0
- package/package.json +22 -0
- package/tsconfig.json +19 -0
package/index.js
ADDED
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var qust = require('@arc-js/qust');
|
|
6
|
+
var Pajo = require('@arc-js/pajo');
|
|
7
|
+
var JON = require('@arc-js/jon');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
24
|
+
var e = new Error(message);
|
|
25
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const NODEENV = 'development';
|
|
29
|
+
const tabAlphabetique = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
|
|
30
|
+
[...tabAlphabetique, ...tabAlphabetique.map(x => x.toUpperCase())];
|
|
31
|
+
|
|
32
|
+
function defaultMapAction$1(code, type, message, data, cleanedData, error, errors) {
|
|
33
|
+
return ({
|
|
34
|
+
response: {
|
|
35
|
+
code,
|
|
36
|
+
type,
|
|
37
|
+
message,
|
|
38
|
+
},
|
|
39
|
+
data: cleanedData,
|
|
40
|
+
error,
|
|
41
|
+
errors,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function defaultErrMapAction$1(code, type, message, data, cleanedData, error, errors) {
|
|
45
|
+
return ({
|
|
46
|
+
response: {
|
|
47
|
+
code,
|
|
48
|
+
type,
|
|
49
|
+
message,
|
|
50
|
+
},
|
|
51
|
+
data: cleanedData,
|
|
52
|
+
error,
|
|
53
|
+
errors,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function defaultSuccessAction$1(response) {
|
|
57
|
+
{
|
|
58
|
+
console.log('HttpRequest - actionRequest | successActionF - response:: ', response);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function defaultErrorAction$1(response) {
|
|
62
|
+
{
|
|
63
|
+
console.log('HttpRequest - actionRequest | errorActionF - response:: ', response, ' && (typeof response):: ', (typeof response));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function getNotifRequestType(status = undefined) {
|
|
67
|
+
let type = "success";
|
|
68
|
+
if ([200].includes(status) || !status) {
|
|
69
|
+
status = 200;
|
|
70
|
+
type = "success";
|
|
71
|
+
}
|
|
72
|
+
else if ([422].includes(status)) {
|
|
73
|
+
type = "warning";
|
|
74
|
+
}
|
|
75
|
+
else if ([403, 409].includes(status)) {
|
|
76
|
+
type = "error_auth";
|
|
77
|
+
}
|
|
78
|
+
else if ([419, 401].includes(status)) {
|
|
79
|
+
type = "error_auth_required";
|
|
80
|
+
}
|
|
81
|
+
else if ([404, 405, 500, 400].includes(status)) {
|
|
82
|
+
type = "error";
|
|
83
|
+
}
|
|
84
|
+
else if ([501].includes(status)) {
|
|
85
|
+
type = "info";
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
type = "error";
|
|
89
|
+
}
|
|
90
|
+
return type;
|
|
91
|
+
}
|
|
92
|
+
class HttpRequest {
|
|
93
|
+
constructor() {
|
|
94
|
+
this.lang = 'fr';
|
|
95
|
+
}
|
|
96
|
+
initConfig(host, lang) {
|
|
97
|
+
if (typeof host === 'string' && host.length > 0) {
|
|
98
|
+
this.host = host;
|
|
99
|
+
}
|
|
100
|
+
if (['fr', 'en'].includes(lang)) {
|
|
101
|
+
this.lang = lang;
|
|
102
|
+
}
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
getDefaultError() {
|
|
106
|
+
const message = {
|
|
107
|
+
fr: 'Error lors de la recuperation de la reponse',
|
|
108
|
+
en: 'Error lors de la recuperation de la reponse',
|
|
109
|
+
}[this.lang];
|
|
110
|
+
return {
|
|
111
|
+
response: {
|
|
112
|
+
code: 404,
|
|
113
|
+
type: 'error',
|
|
114
|
+
message: message
|
|
115
|
+
},
|
|
116
|
+
data: undefined,
|
|
117
|
+
error: new Error(message),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
actionRequest() {
|
|
121
|
+
return __awaiter(this, arguments, void 0, function* (urlParams = {
|
|
122
|
+
path: "",
|
|
123
|
+
method: "GET",
|
|
124
|
+
}, otherParams = {}, schema = undefined, mapAction, errMapAction, successAction, errorAction) {
|
|
125
|
+
var _a, _b, _c;
|
|
126
|
+
{
|
|
127
|
+
console.log('HttpRequest - actionRequest | urlParams:: ', urlParams);
|
|
128
|
+
console.log('HttpRequest - actionRequest | otherParams:: ', otherParams);
|
|
129
|
+
console.log('HttpRequest - actionRequest | schema:: ', schema);
|
|
130
|
+
}
|
|
131
|
+
const FetchClass = this.getFetchClass();
|
|
132
|
+
urlParams = (typeof urlParams === "object" &&
|
|
133
|
+
Array.isArray(urlParams) === false) ? urlParams : {
|
|
134
|
+
path: "",
|
|
135
|
+
method: "GET",
|
|
136
|
+
};
|
|
137
|
+
const responseType = (!!(urlParams === null || urlParams === void 0 ? void 0 : urlParams.responseType) &&
|
|
138
|
+
["json", "arraybuffer", "blob", "text"].includes(urlParams === null || urlParams === void 0 ? void 0 : urlParams.responseType)) ? urlParams === null || urlParams === void 0 ? void 0 : urlParams.responseType : 'json';
|
|
139
|
+
const method = [
|
|
140
|
+
"GET",
|
|
141
|
+
"POST",
|
|
142
|
+
"PUT",
|
|
143
|
+
"DELETE",
|
|
144
|
+
].includes(urlParams === null || urlParams === void 0 ? void 0 : urlParams.method) ? urlParams === null || urlParams === void 0 ? void 0 : urlParams.method : "GET";
|
|
145
|
+
let path = (typeof (urlParams === null || urlParams === void 0 ? void 0 : urlParams.path) === "string") ? urlParams === null || urlParams === void 0 ? void 0 : urlParams.path : "";
|
|
146
|
+
let headers = (typeof (urlParams === null || urlParams === void 0 ? void 0 : urlParams.headers) === "object" &&
|
|
147
|
+
Array.isArray(urlParams === null || urlParams === void 0 ? void 0 : urlParams.headers) === false &&
|
|
148
|
+
Object.keys(urlParams === null || urlParams === void 0 ? void 0 : urlParams.headers).length > 0) ? urlParams === null || urlParams === void 0 ? void 0 : urlParams.headers : undefined;
|
|
149
|
+
let params = (typeof (urlParams === null || urlParams === void 0 ? void 0 : urlParams.params) === "object" &&
|
|
150
|
+
Array.isArray(urlParams === null || urlParams === void 0 ? void 0 : urlParams.params) === false &&
|
|
151
|
+
Object.keys(urlParams === null || urlParams === void 0 ? void 0 : urlParams.params).length > 0) ? urlParams === null || urlParams === void 0 ? void 0 : urlParams.params : undefined;
|
|
152
|
+
let body = ((typeof (urlParams === null || urlParams === void 0 ? void 0 : urlParams.body) === "object" &&
|
|
153
|
+
Array.isArray(urlParams === null || urlParams === void 0 ? void 0 : urlParams.body) === false) || (urlParams === null || urlParams === void 0 ? void 0 : urlParams.body) instanceof FormData) ? urlParams === null || urlParams === void 0 ? void 0 : urlParams.body : undefined;
|
|
154
|
+
if (typeof body === "object" &&
|
|
155
|
+
Array.isArray(body) === false) {
|
|
156
|
+
body = JSON.stringify(urlParams === null || urlParams === void 0 ? void 0 : urlParams.body);
|
|
157
|
+
}
|
|
158
|
+
otherParams = (typeof otherParams === "object" &&
|
|
159
|
+
Array.isArray(otherParams) == false) ? otherParams : {};
|
|
160
|
+
if (!!(urlParams === null || urlParams === void 0 ? void 0 : urlParams.auth)) {
|
|
161
|
+
headers = Object.assign(Object.assign({}, headers), { Authorization: `Basic ${btoa(`${(_a = urlParams === null || urlParams === void 0 ? void 0 : urlParams.auth) === null || _a === void 0 ? void 0 : _a.username}:${(_b = urlParams === null || urlParams === void 0 ? void 0 : urlParams.auth) === null || _b === void 0 ? void 0 : _b.password}`)}` });
|
|
162
|
+
}
|
|
163
|
+
const mapActionF = (!!mapAction) ? mapAction : defaultMapAction$1;
|
|
164
|
+
const errMapActionF = (!!errMapAction) ? errMapAction : defaultErrMapAction$1;
|
|
165
|
+
const successActionF = !!successAction ? successAction : defaultSuccessAction$1;
|
|
166
|
+
const errorActionF = (!!errorAction) ? errorAction : defaultErrorAction$1;
|
|
167
|
+
if (!(schema instanceof JON.Object)) {
|
|
168
|
+
schema = new JON.Object(this.lang);
|
|
169
|
+
}
|
|
170
|
+
schema = schema.required();
|
|
171
|
+
try {
|
|
172
|
+
const newPath = (typeof this.host === 'string' &&
|
|
173
|
+
this.host.length > 0) ? Pajo.joinWithHost(this.host, path) : Pajo.join(path);
|
|
174
|
+
if (!!newPath) {
|
|
175
|
+
path = newPath;
|
|
176
|
+
}
|
|
177
|
+
if (!!params) {
|
|
178
|
+
path = `${path}${qust.qust.stringify(params)}`;
|
|
179
|
+
}
|
|
180
|
+
let resConfig = Object.assign({ method,
|
|
181
|
+
headers }, otherParams);
|
|
182
|
+
if (!!body) {
|
|
183
|
+
resConfig.body = body;
|
|
184
|
+
}
|
|
185
|
+
if (NODEENV === 'development') {
|
|
186
|
+
console.log('HttpRequest - actionRequest | path (final):: ', path);
|
|
187
|
+
console.log('HttpRequest - actionRequest | resConfig:: ', resConfig);
|
|
188
|
+
}
|
|
189
|
+
const res = yield FetchClass(path, Object.assign({}, resConfig));
|
|
190
|
+
const hasContentType = !!res.headers.get("content-type");
|
|
191
|
+
const isJson = (_c = res.headers.get("content-type")) === null || _c === void 0 ? void 0 : _c.includes("application/json");
|
|
192
|
+
if (NODEENV === 'development') {
|
|
193
|
+
console.log('HttpRequest - actionRequest | res:: ', res);
|
|
194
|
+
}
|
|
195
|
+
const statusResponse = res.status;
|
|
196
|
+
const typeResponse = getNotifRequestType(statusResponse);
|
|
197
|
+
let httpRes = undefined;
|
|
198
|
+
if (responseType === 'text' && hasContentType) {
|
|
199
|
+
httpRes = yield res.text();
|
|
200
|
+
}
|
|
201
|
+
else if (responseType === 'json' && !!isJson) {
|
|
202
|
+
httpRes = yield res.json();
|
|
203
|
+
}
|
|
204
|
+
else if (responseType === 'arraybuffer' && hasContentType) {
|
|
205
|
+
httpRes = yield res.arrayBuffer();
|
|
206
|
+
}
|
|
207
|
+
let errors = undefined;
|
|
208
|
+
if (typeof httpRes === 'object' &&
|
|
209
|
+
!Array.isArray(httpRes)) {
|
|
210
|
+
errors = httpRes === null || httpRes === void 0 ? void 0 : httpRes.errors;
|
|
211
|
+
}
|
|
212
|
+
else if (typeof httpRes === 'string' &&
|
|
213
|
+
!(['info', 'success'].includes(typeResponse))) {
|
|
214
|
+
errors = httpRes;
|
|
215
|
+
}
|
|
216
|
+
if (NODEENV === 'development') {
|
|
217
|
+
console.log('HttpRequest - actionRequest | httpRes:: ', httpRes);
|
|
218
|
+
}
|
|
219
|
+
let messageNotif = undefined;
|
|
220
|
+
if (!res.ok) {
|
|
221
|
+
if (typeof errors === 'object' &&
|
|
222
|
+
!Array.isArray(errors) &&
|
|
223
|
+
Object.keys(errors).length > 0) {
|
|
224
|
+
messageNotif = errors[Object.keys(errors)[Object.keys(errors).length - 1]];
|
|
225
|
+
}
|
|
226
|
+
else if (!!(httpRes === null || httpRes === void 0 ? void 0 : httpRes.message)) {
|
|
227
|
+
messageNotif = httpRes === null || httpRes === void 0 ? void 0 : httpRes.message;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
messageNotif = {
|
|
231
|
+
en: "unknown error",
|
|
232
|
+
fr: "erreur inconnue",
|
|
233
|
+
}[this.lang];
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (NODEENV === 'development') {
|
|
237
|
+
console.log("HttpRequest - actionRequest | messageNotif:: ", messageNotif);
|
|
238
|
+
}
|
|
239
|
+
let codeF = statusResponse || (!!res.ok ? 200 : 404);
|
|
240
|
+
let typeF = (!!statusResponse ? typeResponse : (!!res.ok ? "success" : "error"));
|
|
241
|
+
let messageF = messageNotif;
|
|
242
|
+
let correctDataF = schema.isValid(httpRes);
|
|
243
|
+
let dataF = ((!!res.ok) ? httpRes : undefined);
|
|
244
|
+
let cleanedDataF = !!correctDataF ? dataF : this.getDefaultError();
|
|
245
|
+
let errorsF = undefined;
|
|
246
|
+
if (typeof errors === 'object' && !Array.isArray(errors)) {
|
|
247
|
+
errorsF = Object.values(errors);
|
|
248
|
+
}
|
|
249
|
+
else if (typeof errors === 'object' && !!Array.isArray(errors)) {
|
|
250
|
+
errorsF = errorsF;
|
|
251
|
+
}
|
|
252
|
+
else if (typeof errors === 'string' && errors.length > 0) {
|
|
253
|
+
errorsF = [errorsF];
|
|
254
|
+
}
|
|
255
|
+
let errorF = errorsF === null || errorsF === void 0 ? void 0 : errorsF[0];
|
|
256
|
+
errorF = (!(errorF instanceof Error || typeof errorF === 'undefined' || errorF === null)) ? new Error(`${errorF}`) : errorF;
|
|
257
|
+
if (!errorF && !!messageNotif) {
|
|
258
|
+
errorF = new Error(`${messageNotif}`);
|
|
259
|
+
errorsF = [messageNotif];
|
|
260
|
+
}
|
|
261
|
+
const cleanedData = (['success', 'info'].includes(typeF)) ? mapActionF(codeF, typeF, messageF, dataF, cleanedDataF, errorF, errorsF) : errMapActionF(codeF, typeF, messageF, dataF, cleanedDataF, errorF, errorsF);
|
|
262
|
+
if (NODEENV === 'development') {
|
|
263
|
+
console.log("HttpRequest - actionRequest | cleanedData (OLD):: ", cleanedData);
|
|
264
|
+
}
|
|
265
|
+
if (cleanedData.response.type === "success") {
|
|
266
|
+
yield successActionF(cleanedData);
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
yield errorActionF(cleanedData);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
let message = 'erreur inconnue';
|
|
274
|
+
if (typeof (error === null || error === void 0 ? void 0 : error.message) === 'string') {
|
|
275
|
+
message = error === null || error === void 0 ? void 0 : error.message;
|
|
276
|
+
}
|
|
277
|
+
else if (typeof error === 'string') {
|
|
278
|
+
message = error;
|
|
279
|
+
}
|
|
280
|
+
if (message.toLowerCase() === 'failed to fetch') {
|
|
281
|
+
message = {
|
|
282
|
+
en: "Service unavailable. Please try again later.",
|
|
283
|
+
fr: "Service indisponible. Veuillez réessayer plus tard.",
|
|
284
|
+
}[this.lang];
|
|
285
|
+
}
|
|
286
|
+
{
|
|
287
|
+
console.log("[ERROR] HttpRequest - actionRequest | error:: ", error);
|
|
288
|
+
console.log("[ERROR] HttpRequest - actionRequest | message:: ", message);
|
|
289
|
+
}
|
|
290
|
+
let codeF = (error === null || error === void 0 ? void 0 : error.status) || 404;
|
|
291
|
+
let typeF = !!(error === null || error === void 0 ? void 0 : error.status) ? getNotifRequestType(error === null || error === void 0 ? void 0 : error.status) : "error";
|
|
292
|
+
let messageF = message;
|
|
293
|
+
let dataF = error;
|
|
294
|
+
let correctDataF = schema.isValid(dataF);
|
|
295
|
+
let cleanedDataF = !!correctDataF ? dataF : this.getDefaultError();
|
|
296
|
+
let errorsF = [message];
|
|
297
|
+
if (typeof errorsF === 'object' && !Array.isArray(errorsF)) {
|
|
298
|
+
errorsF = Object.values(errorsF);
|
|
299
|
+
}
|
|
300
|
+
else if (typeof errorsF === 'object' && !!Array.isArray(errorsF)) {
|
|
301
|
+
errorsF = errorsF;
|
|
302
|
+
}
|
|
303
|
+
else if (typeof errorsF === 'string' && errorsF.length > 0) {
|
|
304
|
+
errorsF = [errorsF];
|
|
305
|
+
}
|
|
306
|
+
let errorF = errorsF === null || errorsF === void 0 ? void 0 : errorsF[0];
|
|
307
|
+
errorF = (!(errorF instanceof Error || typeof errorF === 'undefined' || errorF === null)) ? new Error(`${errorF}`) : errorF;
|
|
308
|
+
if (!errorF && !!message) {
|
|
309
|
+
errorF = new Error(`${message}`);
|
|
310
|
+
errorsF = [message];
|
|
311
|
+
}
|
|
312
|
+
{
|
|
313
|
+
console.log("[ERROR] HttpRequest - actionRequest | codeF:: ", codeF);
|
|
314
|
+
console.log("[ERROR] HttpRequest - actionRequest | typeF:: ", typeF);
|
|
315
|
+
console.log("[ERROR] HttpRequest - actionRequest | dataF:: ", dataF);
|
|
316
|
+
console.log("[ERROR] HttpRequest - actionRequest | correctDataF:: ", correctDataF);
|
|
317
|
+
console.log("[ERROR] HttpRequest - actionRequest | cleanedDataF:: ", cleanedDataF);
|
|
318
|
+
console.log("[ERROR] HttpRequest - actionRequest | errorsF:: ", errorsF);
|
|
319
|
+
console.log("[ERROR] HttpRequest - actionRequest | errorsF:: ", errorsF);
|
|
320
|
+
}
|
|
321
|
+
const errorDatas = errMapActionF(codeF, typeF, messageF, dataF, cleanedDataF, errorF, errorsF);
|
|
322
|
+
{
|
|
323
|
+
console.log("[ERROR] HttpRequest - actionRequest | errorDatas:: ", errorDatas);
|
|
324
|
+
}
|
|
325
|
+
yield errorActionF(errorDatas);
|
|
326
|
+
return errorDatas;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
findAllRequests() {
|
|
331
|
+
return __awaiter(this, arguments, void 0, function* (urlParams = {
|
|
332
|
+
path: "",
|
|
333
|
+
method: "GET",
|
|
334
|
+
}, otherParams = {}, successAction = defaultSuccessAction$1, errorAction = defaultErrorAction$1) {
|
|
335
|
+
const schema = new JON.Object(this.lang).struct({
|
|
336
|
+
response: new JON.Object(this.lang).struct({
|
|
337
|
+
type: new JON.Enum(this.lang).choices('warning', 'success', 'danger', 'info').required(),
|
|
338
|
+
code: new JON.Number(this.lang).required(),
|
|
339
|
+
message: new JON.String(this.lang),
|
|
340
|
+
}),
|
|
341
|
+
datas: new JON.Array(this.lang).types(new JON.Object(this.lang).min(1).required()).required().default([]),
|
|
342
|
+
});
|
|
343
|
+
const mapAction = (code, type, message, data, cleanedData, error, errors) => {
|
|
344
|
+
return ({
|
|
345
|
+
response: {
|
|
346
|
+
code,
|
|
347
|
+
type,
|
|
348
|
+
message,
|
|
349
|
+
},
|
|
350
|
+
data: cleanedData === null || cleanedData === void 0 ? void 0 : cleanedData.datas,
|
|
351
|
+
error,
|
|
352
|
+
errors,
|
|
353
|
+
});
|
|
354
|
+
};
|
|
355
|
+
const errMapAction = (code, type, message, data, cleanedData, error, errors) => {
|
|
356
|
+
return ({
|
|
357
|
+
response: {
|
|
358
|
+
code,
|
|
359
|
+
type,
|
|
360
|
+
message,
|
|
361
|
+
},
|
|
362
|
+
data: cleanedData === null || cleanedData === void 0 ? void 0 : cleanedData.datas,
|
|
363
|
+
error,
|
|
364
|
+
errors,
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
yield this.actionRequest(urlParams, otherParams, schema, mapAction, errMapAction, successAction, errorAction);
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
findOneRequest() {
|
|
371
|
+
return __awaiter(this, arguments, void 0, function* (urlParams = {
|
|
372
|
+
path: "",
|
|
373
|
+
method: "GET",
|
|
374
|
+
}, otherParams = {}, successAction = defaultSuccessAction$1, errorAction = defaultErrorAction$1) {
|
|
375
|
+
const schema = new JON.Object(this.lang).struct({
|
|
376
|
+
response: new JON.Object(this.lang).struct({
|
|
377
|
+
type: new JON.Enum(this.lang).choices('warning', 'success', 'danger', 'info').required(),
|
|
378
|
+
code: new JON.Number(this.lang).required(),
|
|
379
|
+
message: new JON.String(this.lang),
|
|
380
|
+
}),
|
|
381
|
+
data: new JON.Object(this.lang).min(1),
|
|
382
|
+
});
|
|
383
|
+
const mapAction = (code, type, message, data, cleanedData, error, errors) => {
|
|
384
|
+
return ({
|
|
385
|
+
response: {
|
|
386
|
+
code,
|
|
387
|
+
type,
|
|
388
|
+
message,
|
|
389
|
+
},
|
|
390
|
+
data: cleanedData === null || cleanedData === void 0 ? void 0 : cleanedData.data,
|
|
391
|
+
error,
|
|
392
|
+
errors,
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
const errMapAction = (code, type, message, data, cleanedData, error, errors) => {
|
|
396
|
+
return ({
|
|
397
|
+
response: {
|
|
398
|
+
code,
|
|
399
|
+
type,
|
|
400
|
+
message,
|
|
401
|
+
},
|
|
402
|
+
data: cleanedData === null || cleanedData === void 0 ? void 0 : cleanedData.data,
|
|
403
|
+
error,
|
|
404
|
+
errors,
|
|
405
|
+
});
|
|
406
|
+
};
|
|
407
|
+
yield this.actionRequest(urlParams, otherParams, schema, mapAction, errMapAction, successAction, errorAction);
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
getFetchClass() {
|
|
411
|
+
return (typeof window !== "undefined") ? window.fetch : fetch;
|
|
412
|
+
}
|
|
413
|
+
static exposeToGlobal() {
|
|
414
|
+
if (typeof window !== "undefined") {
|
|
415
|
+
window.HttpRequest = HttpRequest;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (typeof window !== "undefined") {
|
|
420
|
+
window.HttpRequest = HttpRequest;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function defaultMapAction(code, type, message, data, cleanedData, error, errors) {
|
|
424
|
+
return {
|
|
425
|
+
response: { code, type, message },
|
|
426
|
+
data: cleanedData,
|
|
427
|
+
error,
|
|
428
|
+
errors,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function defaultErrMapAction(code, type, message, data, cleanedData, error, errors) {
|
|
432
|
+
return {
|
|
433
|
+
response: { code, type, message },
|
|
434
|
+
data: cleanedData,
|
|
435
|
+
error,
|
|
436
|
+
errors,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
function defaultSuccessAction(res) {
|
|
440
|
+
{
|
|
441
|
+
console.log("WsRequest | success :: ", res);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
function defaultErrorAction(res) {
|
|
445
|
+
{
|
|
446
|
+
console.log("WsRequest | error :: ", res);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
class WsRequest {
|
|
450
|
+
constructor() {
|
|
451
|
+
this.lang = "fr";
|
|
452
|
+
this.socket = null;
|
|
453
|
+
this.connected = false;
|
|
454
|
+
this.autoReconnect = true;
|
|
455
|
+
this.reconnectDelay = 2000;
|
|
456
|
+
this.maxReconnectAttempts = 10;
|
|
457
|
+
this.reconnectAttempts = 0;
|
|
458
|
+
this.messageQueue = [];
|
|
459
|
+
}
|
|
460
|
+
initConfig(host, lang) {
|
|
461
|
+
if (host && typeof host === "string") {
|
|
462
|
+
this.host = host;
|
|
463
|
+
}
|
|
464
|
+
if (["fr", "en"].includes(lang)) {
|
|
465
|
+
this.lang = lang;
|
|
466
|
+
}
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
connect(params) {
|
|
470
|
+
const { path, auth, headers = {}, autoReconnect = true, reconnectDelay = 2000, maxReconnectAttempts = 10, } = params;
|
|
471
|
+
let urlParams = (typeof (params === null || params === void 0 ? void 0 : params.params) === "object" &&
|
|
472
|
+
!Array.isArray(params === null || params === void 0 ? void 0 : params.params) &&
|
|
473
|
+
Object.keys(params === null || params === void 0 ? void 0 : params.params).length > 0) ? params === null || params === void 0 ? void 0 : params.params : undefined;
|
|
474
|
+
this.autoReconnect = autoReconnect;
|
|
475
|
+
this.reconnectDelay = reconnectDelay;
|
|
476
|
+
this.maxReconnectAttempts = maxReconnectAttempts;
|
|
477
|
+
let url = (typeof this.host === 'string' &&
|
|
478
|
+
this.host.length > 0) ? Pajo.joinWithHost(this.host, path) : Pajo.join(path);
|
|
479
|
+
if (!!urlParams) {
|
|
480
|
+
url = `${url}${qust.qust.stringify(urlParams)}`;
|
|
481
|
+
}
|
|
482
|
+
if (auth) {
|
|
483
|
+
headers["Authorization"] =
|
|
484
|
+
"Basic " + btoa(`${auth.username}:${auth.password}`);
|
|
485
|
+
}
|
|
486
|
+
{
|
|
487
|
+
console.log("WsRequest | connecting to:", url);
|
|
488
|
+
}
|
|
489
|
+
this.socket = new (this.getWsClass())(url || '');
|
|
490
|
+
this.socket.onopen = () => {
|
|
491
|
+
var _a;
|
|
492
|
+
this.connected = true;
|
|
493
|
+
this.reconnectAttempts = 0;
|
|
494
|
+
{
|
|
495
|
+
console.log("WsRequest | WebSocket connected");
|
|
496
|
+
}
|
|
497
|
+
while (this.messageQueue.length > 0) {
|
|
498
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(this.messageQueue.shift());
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
this.socket.onerror = (err) => {
|
|
502
|
+
{
|
|
503
|
+
console.log("WsRequest | error:", err);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
this.socket.onclose = () => {
|
|
507
|
+
this.connected = false;
|
|
508
|
+
{
|
|
509
|
+
console.log("WsRequest | closed");
|
|
510
|
+
}
|
|
511
|
+
if (this.autoReconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
512
|
+
setTimeout(() => {
|
|
513
|
+
this.reconnectAttempts++;
|
|
514
|
+
{
|
|
515
|
+
console.log("WsRequest | reconnect attempt:", this.reconnectAttempts);
|
|
516
|
+
}
|
|
517
|
+
this.connect(params);
|
|
518
|
+
}, this.reconnectDelay);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
actionMessage(message_1) {
|
|
523
|
+
return __awaiter(this, arguments, void 0, function* (message, schema = undefined, mapAction = defaultMapAction, errMapAction = defaultErrMapAction, successAction = defaultSuccessAction, errorAction = defaultErrorAction) {
|
|
524
|
+
if (!this.connected || !this.socket) {
|
|
525
|
+
this.messageQueue.push(JSON.stringify(message));
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (!(schema instanceof JON.Object)) {
|
|
529
|
+
schema = new JON.Object(this.lang);
|
|
530
|
+
}
|
|
531
|
+
schema = schema.required();
|
|
532
|
+
const payload = JSON.stringify(message);
|
|
533
|
+
return new Promise((resolve) => {
|
|
534
|
+
this.socket.onmessage = (e) => __awaiter(this, void 0, void 0, function* () {
|
|
535
|
+
var _a, _b, _c, _d, _e, _f;
|
|
536
|
+
let resJson = undefined;
|
|
537
|
+
try {
|
|
538
|
+
resJson = JSON.parse(e.data);
|
|
539
|
+
}
|
|
540
|
+
catch (err) {
|
|
541
|
+
resJson = e.data;
|
|
542
|
+
}
|
|
543
|
+
let isValid = schema.isValid(resJson);
|
|
544
|
+
let cleaned = isValid ? resJson : undefined;
|
|
545
|
+
let codeF = (_b = (_a = resJson === null || resJson === void 0 ? void 0 : resJson.response) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : 200;
|
|
546
|
+
let typeF = (_d = (_c = resJson === null || resJson === void 0 ? void 0 : resJson.response) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : "success";
|
|
547
|
+
let messageF = (_f = (_e = resJson === null || resJson === void 0 ? void 0 : resJson.response) === null || _e === void 0 ? void 0 : _e.message) !== null && _f !== void 0 ? _f : undefined;
|
|
548
|
+
let errorsF = [];
|
|
549
|
+
if (resJson === null || resJson === void 0 ? void 0 : resJson.errors) {
|
|
550
|
+
errorsF = Array.isArray(resJson.errors)
|
|
551
|
+
? resJson.errors
|
|
552
|
+
: Object.values(resJson.errors);
|
|
553
|
+
}
|
|
554
|
+
let errorF = errorsF === null || errorsF === void 0 ? void 0 : errorsF[0];
|
|
555
|
+
if (!(errorF instanceof Error)) {
|
|
556
|
+
if (typeof errorF !== "undefined")
|
|
557
|
+
errorF = new Error(`${errorF}`);
|
|
558
|
+
}
|
|
559
|
+
const responseCleaned = typeF === "success" || typeF === "info"
|
|
560
|
+
? mapAction(codeF, typeF, messageF, resJson, cleaned, errorF, errorsF)
|
|
561
|
+
: errMapAction(codeF, typeF, messageF, resJson, cleaned, errorF, errorsF);
|
|
562
|
+
if (responseCleaned.response.type === "success") {
|
|
563
|
+
yield successAction(responseCleaned);
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
yield errorAction(responseCleaned);
|
|
567
|
+
}
|
|
568
|
+
resolve(responseCleaned);
|
|
569
|
+
});
|
|
570
|
+
this.socket.send(payload);
|
|
571
|
+
});
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
findAllMessages(message_1) {
|
|
575
|
+
return __awaiter(this, arguments, void 0, function* (message, successAction = defaultSuccessAction, errorAction = defaultErrorAction) {
|
|
576
|
+
const schema = new JON.Object(this.lang).struct({
|
|
577
|
+
response: new JON.Object(this.lang).struct({
|
|
578
|
+
type: new JON.Enum(this.lang)
|
|
579
|
+
.choices("warning", "success", "danger", "info")
|
|
580
|
+
.required(),
|
|
581
|
+
code: new JON.Number(this.lang).required(),
|
|
582
|
+
message: new JON.String(this.lang),
|
|
583
|
+
}),
|
|
584
|
+
datas: new JON.Array(this.lang)
|
|
585
|
+
.types(new JON.Object(this.lang).min(1).required())
|
|
586
|
+
.default([]),
|
|
587
|
+
});
|
|
588
|
+
const mapAction = (code, type, message, data, cleaned, error, errors) => {
|
|
589
|
+
return {
|
|
590
|
+
response: { code, type, message },
|
|
591
|
+
data: (cleaned === null || cleaned === void 0 ? void 0 : cleaned.datas) || [],
|
|
592
|
+
error,
|
|
593
|
+
errors,
|
|
594
|
+
};
|
|
595
|
+
};
|
|
596
|
+
return yield this.actionMessage(message, schema, mapAction, mapAction, successAction, errorAction);
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
findOneMessage(message_1) {
|
|
600
|
+
return __awaiter(this, arguments, void 0, function* (message, successAction = defaultSuccessAction, errorAction = defaultErrorAction) {
|
|
601
|
+
const schema = new JON.Object(this.lang).struct({
|
|
602
|
+
response: new JON.Object(this.lang).struct({
|
|
603
|
+
type: new JON.Enum(this.lang)
|
|
604
|
+
.choices("warning", "success", "danger", "info")
|
|
605
|
+
.required(),
|
|
606
|
+
code: new JON.Number(this.lang).required(),
|
|
607
|
+
message: new JON.String(this.lang),
|
|
608
|
+
}),
|
|
609
|
+
data: new JON.Object(this.lang).min(1),
|
|
610
|
+
});
|
|
611
|
+
const mapAction = (code, type, message, data, cleaned, error, errors) => {
|
|
612
|
+
return {
|
|
613
|
+
response: { code, type, message },
|
|
614
|
+
data: (cleaned === null || cleaned === void 0 ? void 0 : cleaned.data) || null,
|
|
615
|
+
error,
|
|
616
|
+
errors,
|
|
617
|
+
};
|
|
618
|
+
};
|
|
619
|
+
return yield this.actionMessage(message, schema, mapAction, mapAction, successAction, errorAction);
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
getWsClass() {
|
|
623
|
+
return (typeof window !== "undefined") ? window.WebSocket : WebSocket;
|
|
624
|
+
}
|
|
625
|
+
static exposeToGlobal() {
|
|
626
|
+
if (typeof window !== "undefined") {
|
|
627
|
+
window.WsRequest = WsRequest;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
if (typeof window !== "undefined") {
|
|
632
|
+
window.WsRequest = WsRequest;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
var index = {
|
|
636
|
+
HttpRequest,
|
|
637
|
+
WsRequest,
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
exports.HttpRequest = HttpRequest;
|
|
641
|
+
exports.WsRequest = WsRequest;
|
|
642
|
+
exports.default = index;
|