@m4l/core 0.0.14 → 0.0.17
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { HostToolsType } from '../../types';
|
|
3
|
-
import { HostToolsProviderProps } from './types';
|
|
3
|
+
import type { HostToolsProviderProps } from './types';
|
|
4
4
|
declare const HostToolsContext: import("react").Context<HostToolsType>;
|
|
5
5
|
declare function HostToolsProvider(props: HostToolsProviderProps): JSX.Element;
|
|
6
6
|
export { HostToolsProvider, HostToolsContext };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { NetworkProviderProps } from './types';
|
|
2
|
+
import type { NetworkProviderProps } from './types';
|
|
3
3
|
declare const NetworkContext: import("react").Context<import("./types").NetworkType>;
|
|
4
4
|
declare function NetworkProvider(props: NetworkProviderProps): JSX.Element;
|
|
5
5
|
export { NetworkProvider, NetworkContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from './contexts';
|
|
2
|
-
export
|
|
2
|
+
export { useEnvironment } from './hooks/useEnvironment';
|
|
3
3
|
export { EmitEvents } from './types';
|
|
4
4
|
export type { Maybe, HostToolsType, NetworkProps, EnvironmentType, AxiosOperation, EventFunListener, } from './types';
|
|
5
5
|
export type { GetLabelType, Dictionary, ModuleDictionary, ComponentDictionary, } from './types/dictionary';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, useState, useContext, useCallback,
|
|
1
|
+
import { createContext, useState, useContext, useCallback, useLayoutEffect } from "react";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
3
|
import { a as axios } from "./axios.js";
|
|
4
4
|
import { s as snakecaseKeys } from "./snakecase-keys.js";
|
|
@@ -25,117 +25,6 @@ function EnvironmentProvider(props) {
|
|
|
25
25
|
}
|
|
26
26
|
function voidFunction() {
|
|
27
27
|
}
|
|
28
|
-
function getPropertyByString(object, propString) {
|
|
29
|
-
let value = object;
|
|
30
|
-
const props = propString.split(".");
|
|
31
|
-
for (let index = 0; index < props.length; index += 1) {
|
|
32
|
-
if (props[index] === void 0)
|
|
33
|
-
break;
|
|
34
|
-
value = value[props[index]];
|
|
35
|
-
}
|
|
36
|
-
return value;
|
|
37
|
-
}
|
|
38
|
-
function getLocalStorage(key, initialValue2) {
|
|
39
|
-
try {
|
|
40
|
-
const item = window.localStorage.getItem(key);
|
|
41
|
-
return item !== null ? JSON.parse(item) : initialValue2;
|
|
42
|
-
} catch (e) {
|
|
43
|
-
return initialValue2;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function setLocalStorage(key, value) {
|
|
47
|
-
try {
|
|
48
|
-
const item = window.localStorage.getItem(key);
|
|
49
|
-
let newValue = item !== null ? JSON.parse(item) : {};
|
|
50
|
-
newValue = { ...newValue, ...value };
|
|
51
|
-
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
52
|
-
} catch (e) {
|
|
53
|
-
console.error(e);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
|
|
57
|
-
EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
|
|
58
|
-
EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
|
|
59
|
-
return EmitEvents2;
|
|
60
|
-
})(EmitEvents || {});
|
|
61
|
-
const axiosInstance = axios.create({});
|
|
62
|
-
function getResponse(endPoint, response) {
|
|
63
|
-
console.log("Axios response", response, typeof response.data);
|
|
64
|
-
if (response && response.data && typeof response.data === "object") {
|
|
65
|
-
if (response.data.error && response.data.error?.code && response.data.error?.msg !== void 0) {
|
|
66
|
-
return Promise.reject({ ...response.data.error, status: response.status });
|
|
67
|
-
}
|
|
68
|
-
return response.data;
|
|
69
|
-
}
|
|
70
|
-
return Promise.reject({
|
|
71
|
-
code: 1,
|
|
72
|
-
msg: `Incorrect endpoint: ${endPoint}`,
|
|
73
|
-
status: response.status
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
function getError(error, hostTools, checkUnAuthorized = true) {
|
|
77
|
-
const { toast } = hostTools;
|
|
78
|
-
let err = {
|
|
79
|
-
message: "",
|
|
80
|
-
status: 1,
|
|
81
|
-
code: 0
|
|
82
|
-
};
|
|
83
|
-
console.log("getError", error);
|
|
84
|
-
if (error?.code !== void 0 && err.status !== void 0 && error.message !== void 0) {
|
|
85
|
-
err = { ...err, ...error };
|
|
86
|
-
} else if (error?.response) {
|
|
87
|
-
if (error.response.data && typeof error.response.data === "object" && error.response.data.error && error.response.data.error?.code && error.response.data.error?.message !== void 0) {
|
|
88
|
-
err = { ...error.response.data.error, status: error.response.status };
|
|
89
|
-
} else {
|
|
90
|
-
err.message = error.message;
|
|
91
|
-
err.status = error.response.status;
|
|
92
|
-
err.code = 0;
|
|
93
|
-
}
|
|
94
|
-
if (checkUnAuthorized && error.response.status === 401) {
|
|
95
|
-
hostTools.events_emit(EmitEvents.EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED, {});
|
|
96
|
-
}
|
|
97
|
-
} else if (error?.request) {
|
|
98
|
-
err.message = `${error?.code} ${error.message}`;
|
|
99
|
-
err.code = -1;
|
|
100
|
-
} else {
|
|
101
|
-
err.message = `${error?.code} ${error.message}`;
|
|
102
|
-
err.status = 0;
|
|
103
|
-
err.code = -2;
|
|
104
|
-
}
|
|
105
|
-
if (checkUnAuthorized) {
|
|
106
|
-
toast(`${err.message} - status: ${err.status} - code: ${err.code}`, { type: "error" });
|
|
107
|
-
}
|
|
108
|
-
return err;
|
|
109
|
-
}
|
|
110
|
-
const axiosOperation = async (props, enviroment, hostTools) => {
|
|
111
|
-
const {
|
|
112
|
-
method,
|
|
113
|
-
endPoint,
|
|
114
|
-
timeout = 5e3,
|
|
115
|
-
parms = {},
|
|
116
|
-
data = {},
|
|
117
|
-
isRemote = true,
|
|
118
|
-
checkUnAuthorized = true
|
|
119
|
-
} = props;
|
|
120
|
-
let baseURL;
|
|
121
|
-
if (isRemote) {
|
|
122
|
-
baseURL = enviroment.host_api_remote;
|
|
123
|
-
} else {
|
|
124
|
-
baseURL = enviroment.host_api_local;
|
|
125
|
-
}
|
|
126
|
-
hostTools.startProgress();
|
|
127
|
-
return axiosInstance({
|
|
128
|
-
baseURL,
|
|
129
|
-
withCredentials: isRemote,
|
|
130
|
-
method,
|
|
131
|
-
url: `/${endPoint}`,
|
|
132
|
-
data: snakecaseKeys(data, { deep: true }),
|
|
133
|
-
params: snakecaseKeys(parms, { deep: true }),
|
|
134
|
-
timeout
|
|
135
|
-
}).then((response) => getResponse(endPoint, response)).catch((error) => Promise.reject(getError(error, hostTools, checkUnAuthorized))).finally(() => {
|
|
136
|
-
hostTools.stopProgress();
|
|
137
|
-
});
|
|
138
|
-
};
|
|
139
28
|
const initialValue$1 = {
|
|
140
29
|
toast: () => 0,
|
|
141
30
|
startProgress: voidFunction,
|
|
@@ -219,25 +108,6 @@ function FlagsProvider({
|
|
|
219
108
|
children
|
|
220
109
|
});
|
|
221
110
|
}
|
|
222
|
-
function useLocalStorage(key, initialValue2) {
|
|
223
|
-
const [value, setValue] = useState(() => {
|
|
224
|
-
try {
|
|
225
|
-
const item = window.localStorage.getItem(key);
|
|
226
|
-
return item !== null ? JSON.parse(item) : initialValue2;
|
|
227
|
-
} catch (e) {
|
|
228
|
-
return initialValue2;
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
const setValueInLocalStorage = (newValue) => {
|
|
232
|
-
try {
|
|
233
|
-
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
234
|
-
setValue(newValue);
|
|
235
|
-
} catch (e) {
|
|
236
|
-
console.error(e);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
return [value, setValueInLocalStorage];
|
|
240
|
-
}
|
|
241
111
|
const useNetwork = () => {
|
|
242
112
|
const context = useContext(NetworkContext);
|
|
243
113
|
if (!context)
|
|
@@ -250,26 +120,6 @@ const useFlags = () => {
|
|
|
250
120
|
throw new Error("useFlags context must be use inside FlagsProvider");
|
|
251
121
|
return context;
|
|
252
122
|
};
|
|
253
|
-
function isFlagsPresent(compareFlags, flags) {
|
|
254
|
-
const filterFlags = compareFlags.filter((findFlag) => flags.findIndex((sFlag) => sFlag === findFlag) !== -1);
|
|
255
|
-
return filterFlags.length === compareFlags.length;
|
|
256
|
-
}
|
|
257
|
-
const useFlagsPresent = (compareFlags) => {
|
|
258
|
-
const context = useFlags();
|
|
259
|
-
const [isPresent, setIsPresent] = useState(isFlagsPresent(compareFlags, context.flags));
|
|
260
|
-
useEffect(() => {
|
|
261
|
-
if (isFlagsPresent(compareFlags, context.flags)) {
|
|
262
|
-
setIsPresent(true);
|
|
263
|
-
}
|
|
264
|
-
}, [context.flags, compareFlags]);
|
|
265
|
-
return isPresent;
|
|
266
|
-
};
|
|
267
|
-
const useModuleDictionary = () => {
|
|
268
|
-
const context = useContext(ModuleDictionaryContext);
|
|
269
|
-
if (!context)
|
|
270
|
-
throw new Error("useModuleDictionary context must be use inside ModuleDictionaryProvider");
|
|
271
|
-
return context;
|
|
272
|
-
};
|
|
273
123
|
const initialState = {
|
|
274
124
|
getLabel: () => "..",
|
|
275
125
|
getModuleLabel: () => "No dictionary context"
|
|
@@ -351,4 +201,114 @@ function ModuleDictionaryProvider(props) {
|
|
|
351
201
|
children
|
|
352
202
|
});
|
|
353
203
|
}
|
|
354
|
-
|
|
204
|
+
var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
|
|
205
|
+
EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
|
|
206
|
+
EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
|
|
207
|
+
return EmitEvents2;
|
|
208
|
+
})(EmitEvents || {});
|
|
209
|
+
function getPropertyByString(object, propString) {
|
|
210
|
+
let value = object;
|
|
211
|
+
const props = propString.split(".");
|
|
212
|
+
for (let index = 0; index < props.length; index += 1) {
|
|
213
|
+
if (props[index] === void 0)
|
|
214
|
+
break;
|
|
215
|
+
value = value[props[index]];
|
|
216
|
+
}
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
function getLocalStorage(key, initialValue2) {
|
|
220
|
+
try {
|
|
221
|
+
const item = window.localStorage.getItem(key);
|
|
222
|
+
return item !== null ? JSON.parse(item) : initialValue2;
|
|
223
|
+
} catch (e) {
|
|
224
|
+
return initialValue2;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function setLocalStorage(key, value) {
|
|
228
|
+
try {
|
|
229
|
+
const item = window.localStorage.getItem(key);
|
|
230
|
+
let newValue = item !== null ? JSON.parse(item) : {};
|
|
231
|
+
newValue = { ...newValue, ...value };
|
|
232
|
+
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
233
|
+
} catch (e) {
|
|
234
|
+
console.error(e);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function getResponse(endPoint, response) {
|
|
238
|
+
console.log("Axios response", response, typeof response.data);
|
|
239
|
+
if (response && response.data && typeof response.data === "object") {
|
|
240
|
+
if (response.data.error && response.data.error?.code && response.data.error?.msg !== void 0) {
|
|
241
|
+
return Promise.reject({ ...response.data.error, status: response.status });
|
|
242
|
+
}
|
|
243
|
+
return response.data;
|
|
244
|
+
}
|
|
245
|
+
return Promise.reject({
|
|
246
|
+
code: 1,
|
|
247
|
+
msg: `Incorrect endpoint: ${endPoint}`,
|
|
248
|
+
status: response.status
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
function getError(error, hostTools, checkUnAuthorized = true) {
|
|
252
|
+
const { toast } = hostTools;
|
|
253
|
+
let err = {
|
|
254
|
+
message: "",
|
|
255
|
+
status: 1,
|
|
256
|
+
code: 0
|
|
257
|
+
};
|
|
258
|
+
console.log("getError", error);
|
|
259
|
+
if (error?.code !== void 0 && err.status !== void 0 && error.message !== void 0) {
|
|
260
|
+
err = { ...err, ...error };
|
|
261
|
+
} else if (error?.response) {
|
|
262
|
+
if (error.response.data && typeof error.response.data === "object" && error.response.data.error && error.response.data.error?.code && error.response.data.error?.message !== void 0) {
|
|
263
|
+
err = { ...error.response.data.error, status: error.response.status };
|
|
264
|
+
} else {
|
|
265
|
+
err.message = error.message;
|
|
266
|
+
err.status = error.response.status;
|
|
267
|
+
err.code = 0;
|
|
268
|
+
}
|
|
269
|
+
if (checkUnAuthorized && error.response.status === 401) {
|
|
270
|
+
hostTools.events_emit(EmitEvents.EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED, {});
|
|
271
|
+
}
|
|
272
|
+
} else if (error?.request) {
|
|
273
|
+
err.message = `${error?.code} ${error.message}`;
|
|
274
|
+
err.code = -1;
|
|
275
|
+
} else {
|
|
276
|
+
err.message = `${error?.code} ${error.message}`;
|
|
277
|
+
err.status = 0;
|
|
278
|
+
err.code = -2;
|
|
279
|
+
}
|
|
280
|
+
if (checkUnAuthorized) {
|
|
281
|
+
toast(`${err.message} - status: ${err.status} - code: ${err.code}`, { type: "error" });
|
|
282
|
+
}
|
|
283
|
+
return err;
|
|
284
|
+
}
|
|
285
|
+
const axiosOperation = async (props, enviroment, hostTools) => {
|
|
286
|
+
const {
|
|
287
|
+
method,
|
|
288
|
+
endPoint,
|
|
289
|
+
timeout = 5e3,
|
|
290
|
+
parms = {},
|
|
291
|
+
data = {},
|
|
292
|
+
isRemote = true,
|
|
293
|
+
checkUnAuthorized = true
|
|
294
|
+
} = props;
|
|
295
|
+
let baseURL;
|
|
296
|
+
if (isRemote) {
|
|
297
|
+
baseURL = enviroment.host_api_remote;
|
|
298
|
+
} else {
|
|
299
|
+
baseURL = enviroment.host_api_local;
|
|
300
|
+
}
|
|
301
|
+
hostTools.startProgress();
|
|
302
|
+
return axios({
|
|
303
|
+
baseURL,
|
|
304
|
+
withCredentials: isRemote,
|
|
305
|
+
method,
|
|
306
|
+
url: `/${endPoint}`,
|
|
307
|
+
data: snakecaseKeys(data, { deep: true }),
|
|
308
|
+
params: snakecaseKeys(parms, { deep: true }),
|
|
309
|
+
timeout
|
|
310
|
+
}).then((response) => getResponse(endPoint, response)).catch((error) => Promise.reject(getError(error, hostTools, checkUnAuthorized))).finally(() => {
|
|
311
|
+
hostTools.stopProgress();
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
export { EmitEvents, EnvironmentContext, EnvironmentProvider, FlagsContext, FlagsProvider, HostToolsContext, HostToolsProvider, ModuleDictionaryContext, ModuleDictionaryProvider, NetworkContext, NetworkProvider, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, voidFunction };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Method } from 'axios';
|
|
2
1
|
import type { Id, ToastContent, ToastOptions } from 'react-toastify';
|
|
3
2
|
import type { BrowserHistory } from 'history';
|
|
4
3
|
export declare type Maybe<T> = T | undefined | null;
|
|
@@ -12,7 +11,7 @@ export declare interface EnvironmentType {
|
|
|
12
11
|
environment: string;
|
|
13
12
|
}
|
|
14
13
|
export declare type NetworkProps = {
|
|
15
|
-
method:
|
|
14
|
+
method: any;
|
|
16
15
|
endPoint: string;
|
|
17
16
|
timeout?: number;
|
|
18
17
|
parms?: object;
|