@m4l/core 0.0.18 → 0.0.21

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.
@@ -0,0 +1,13 @@
1
+ import { createContext } from "react";
2
+ import "react/jsx-runtime";
3
+ const initialValue = {
4
+ isLocalhost: true,
5
+ host: "",
6
+ domain_token: "",
7
+ host_api_local: "",
8
+ host_api_remote: "",
9
+ host_static_assets: "",
10
+ environment: ""
11
+ };
12
+ const EnvironmentContext = createContext(initialValue);
13
+ export { EnvironmentContext as E };
@@ -0,0 +1,13 @@
1
+ import { createContext } from "react";
2
+ import { v as voidFunction } from "./voidFunction.js";
3
+ import "react/jsx-runtime";
4
+ const initialValue = {
5
+ toast: () => 0,
6
+ startProgress: voidFunction,
7
+ stopProgress: voidFunction,
8
+ events_add_listener: voidFunction,
9
+ events_remove_listener: voidFunction,
10
+ events_emit: voidFunction
11
+ };
12
+ const HostToolsContext = createContext(initialValue);
13
+ export { HostToolsContext as H };
@@ -0,0 +1,9 @@
1
+ import { createContext } from "react";
2
+ import "./EnvironmentContext.js";
3
+ import "./HostToolsContext.js";
4
+ import "react/jsx-runtime";
5
+ const initialValue = {
6
+ networkOperation: () => Promise.resolve()
7
+ };
8
+ const NetworkContext = createContext(initialValue);
9
+ export { NetworkContext as N };
package/dist/axios.js CHANGED
@@ -1,18 +1,5 @@
1
- function getAugmentedNamespace(n) {
2
- if (n.__esModule)
3
- return n;
4
- var a = Object.defineProperty({}, "__esModule", { value: true });
5
- Object.keys(n).forEach(function(k) {
6
- var d = Object.getOwnPropertyDescriptor(n, k);
7
- Object.defineProperty(a, k, d.get ? d : {
8
- enumerable: true,
9
- get: function() {
10
- return n[k];
11
- }
12
- });
13
- });
14
- return a;
15
- }
1
+ import { s as snakecaseKeys } from "./snakecase-keys.js";
2
+ import { E as EmitEvents } from "./vendor.js";
16
3
  var axios$2 = { exports: {} };
17
4
  var bind$2 = function bind(fn, thisArg) {
18
5
  return function wrap() {
@@ -1232,4 +1219,81 @@ axios$1.isAxiosError = isAxiosError;
1232
1219
  axios$2.exports = axios$1;
1233
1220
  axios$2.exports.default = axios$1;
1234
1221
  var axios = axios$2.exports;
1235
- export { axios as a, getAugmentedNamespace as g };
1222
+ function getResponse(endPoint, response) {
1223
+ console.log("Axios response", response, typeof response.data);
1224
+ if (response && response.data && typeof response.data === "object") {
1225
+ if (response.data.error && response.data.error?.code && response.data.error?.msg !== void 0) {
1226
+ return Promise.reject({ ...response.data.error, status: response.status });
1227
+ }
1228
+ return response.data;
1229
+ }
1230
+ return Promise.reject({
1231
+ code: 1,
1232
+ msg: `Incorrect endpoint: ${endPoint}`,
1233
+ status: response.status
1234
+ });
1235
+ }
1236
+ function getError(error, hostTools, checkUnAuthorized = true) {
1237
+ const { toast } = hostTools;
1238
+ let err = {
1239
+ message: "",
1240
+ status: 1,
1241
+ code: 0
1242
+ };
1243
+ console.log("getError", error);
1244
+ if (error?.code !== void 0 && err.status !== void 0 && error.message !== void 0) {
1245
+ err = { ...err, ...error };
1246
+ } else if (error?.response) {
1247
+ 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) {
1248
+ err = { ...error.response.data.error, status: error.response.status };
1249
+ } else {
1250
+ err.message = error.message;
1251
+ err.status = error.response.status;
1252
+ err.code = 0;
1253
+ }
1254
+ if (checkUnAuthorized && error.response.status === 401) {
1255
+ hostTools.events_emit(EmitEvents.EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED, {});
1256
+ }
1257
+ } else if (error?.request) {
1258
+ err.message = `${error?.code} ${error.message}`;
1259
+ err.code = -1;
1260
+ } else {
1261
+ err.message = `${error?.code} ${error.message}`;
1262
+ err.status = 0;
1263
+ err.code = -2;
1264
+ }
1265
+ if (checkUnAuthorized) {
1266
+ toast(`${err.message} - status: ${err.status} - code: ${err.code}`, { type: "error" });
1267
+ }
1268
+ return err;
1269
+ }
1270
+ const axiosOperation = async (props, enviroment, hostTools) => {
1271
+ const {
1272
+ method,
1273
+ endPoint,
1274
+ timeout = 5e3,
1275
+ parms = {},
1276
+ data: data2 = {},
1277
+ isRemote = true,
1278
+ checkUnAuthorized = true
1279
+ } = props;
1280
+ let baseURL;
1281
+ if (isRemote) {
1282
+ baseURL = enviroment.host_api_remote;
1283
+ } else {
1284
+ baseURL = enviroment.host_api_local;
1285
+ }
1286
+ hostTools.startProgress();
1287
+ return axios({
1288
+ baseURL,
1289
+ withCredentials: isRemote,
1290
+ method,
1291
+ url: `/${endPoint}`,
1292
+ data: snakecaseKeys(data2, { deep: true }),
1293
+ params: snakecaseKeys(parms, { deep: true }),
1294
+ timeout
1295
+ }).then((response) => getResponse(endPoint, response)).catch((error) => Promise.reject(getError(error, hostTools, checkUnAuthorized))).finally(() => {
1296
+ hostTools.stopProgress();
1297
+ });
1298
+ };
1299
+ export { axiosOperation as a };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- export * from './contexts';
2
1
  export { useEnvironment } from './hooks/useEnvironment';
2
+ export { useHostTools } from './hooks/useHostTools';
3
+ export { useLocalStorage } from './hooks/useLocalStorage';
4
+ export { useNetwork } from './hooks/useNetwork';
3
5
  export { EmitEvents } from './types';
4
6
  export type { Maybe, HostToolsType, NetworkProps, EnvironmentType, AxiosOperation, EventFunListener, } from './types';
5
7
  export type { GetLabelType, Dictionary, ModuleDictionary, ComponentDictionary, } from './types/dictionary';
package/dist/index.js CHANGED
@@ -1,314 +1,13 @@
1
- import { createContext, useState, useContext, useCallback, useLayoutEffect } from "react";
2
- import { jsx } from "react/jsx-runtime";
3
- import { a as axios } from "./axios.js";
4
- import { s as snakecaseKeys } from "./snakecase-keys.js";
5
- const initialValue$2 = {
6
- isLocalhost: true,
7
- host: "",
8
- domain_token: "",
9
- host_api_local: "",
10
- host_api_remote: "",
11
- host_static_assets: "",
12
- environment: ""
13
- };
14
- const EnvironmentContext = createContext(initialValue$2);
15
- function EnvironmentProvider(props) {
16
- const {
17
- children,
18
- ...other
19
- } = props;
20
- const [finalEnvironment] = useState(other);
21
- return /* @__PURE__ */ jsx(EnvironmentContext.Provider, {
22
- value: finalEnvironment,
23
- children
24
- });
25
- }
26
- function voidFunction() {
27
- }
28
- const initialValue$1 = {
29
- toast: () => 0,
30
- startProgress: voidFunction,
31
- stopProgress: voidFunction,
32
- events_add_listener: voidFunction,
33
- events_remove_listener: voidFunction,
34
- events_emit: voidFunction
35
- };
36
- const HostToolsContext = createContext(initialValue$1);
37
- function HostToolsProvider(props) {
38
- const {
39
- children,
40
- ...hostTools
41
- } = props;
42
- const [finalTools] = useState(hostTools);
43
- return /* @__PURE__ */ jsx(HostToolsContext.Provider, {
44
- value: finalTools,
45
- children
46
- });
47
- }
48
- const useEnvironment = () => {
49
- const context = useContext(EnvironmentContext);
50
- if (!context)
51
- throw new Error("useEnvironment context must be use inside EnvironmentContext");
52
- return context;
53
- };
54
- const useHostTools = () => {
55
- const context = useContext(HostToolsContext);
56
- if (!context)
57
- throw new Error("useHostTools context must be use inside HostToolsContext");
58
- return context;
59
- };
60
- const initialValue = {
61
- networkOperation: () => Promise.resolve()
62
- };
63
- const NetworkContext = createContext(initialValue);
64
- function NetworkProvider(props) {
65
- const {
66
- children,
67
- axiosOperation: axiosOperation2
68
- } = props;
69
- const environment = useEnvironment();
70
- const hostTools = useHostTools();
71
- const networkOperation = useCallback(async (networkProps) => {
72
- return axiosOperation2(networkProps, environment, hostTools);
73
- }, [axiosOperation2]);
74
- return /* @__PURE__ */ jsx(NetworkContext.Provider, {
75
- value: {
76
- networkOperation
77
- },
78
- children
79
- });
80
- }
81
- const initialState$1 = {
82
- flags: [],
83
- clearFlags: voidFunction,
84
- addFlag: voidFunction
85
- };
86
- const FlagsContext = createContext(initialState$1);
87
- function FlagsProvider({
88
- children
89
- }) {
90
- const [flags, setFlags] = useState([]);
91
- const clearFlags = useCallback(() => {
92
- setFlags([]);
93
- }, []);
94
- const addFlag = useCallback((newFlag) => {
95
- setFlags((oldFlags) => {
96
- if (oldFlags.findIndex((f) => f === newFlag) < 0) {
97
- return [...oldFlags, newFlag];
98
- }
99
- return [...oldFlags];
100
- });
101
- }, []);
102
- return /* @__PURE__ */ jsx(FlagsContext.Provider, {
103
- value: {
104
- flags,
105
- addFlag,
106
- clearFlags
107
- },
108
- children
109
- });
110
- }
111
- const useNetwork = () => {
112
- const context = useContext(NetworkContext);
113
- if (!context)
114
- throw new Error("useNetwork context must be use inside NetworkContext");
115
- return context;
116
- };
117
- const useFlags = () => {
118
- const context = useContext(FlagsContext);
119
- if (!context)
120
- throw new Error("useFlags context must be use inside FlagsProvider");
121
- return context;
122
- };
123
- const initialState = {
124
- getLabel: () => "..",
125
- getModuleLabel: () => "No dictionary context"
126
- };
127
- const ModuleDictionaryContext = createContext(initialState);
128
- function ModuleDictionaryProvider(props) {
129
- const {
130
- children,
131
- componentsDictionary,
132
- moduleId,
133
- moduleName = "module_name",
134
- currentLang = "us",
135
- isAuth = true
136
- } = props;
137
- const {
138
- addFlag
139
- } = useFlags();
140
- const [moduleDictionary, setModuleDictionary] = useState(void 0);
141
- const {
142
- domain_token
143
- } = useEnvironment();
144
- const {
145
- startProgress,
146
- stopProgress
147
- } = useHostTools();
148
- const {
149
- networkOperation
150
- } = useNetwork();
151
- useLayoutEffect(() => {
152
- let mounted = true;
153
- startProgress();
154
- networkOperation({
155
- method: "GET",
156
- endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
157
- parms: {
158
- comps: componentsDictionary,
159
- ...isAuth ? {} : {
160
- domain_token
161
- }
162
- }
163
- }).then((response) => {
164
- if (mounted) {
165
- setModuleDictionary({
166
- ...response
167
- });
168
- addFlag("dictionary_loaded");
169
- }
170
- }).finally(() => {
171
- stopProgress();
172
- });
173
- return function cleanUp() {
174
- mounted = false;
175
- };
176
- }, [currentLang]);
177
- const getLabel = useCallback((key) => {
178
- if (moduleDictionary === void 0)
179
- return "No dictionary";
180
- if (key === void 0)
181
- return "No key";
182
- const parts = key.split(".");
183
- if (parts.length === 2) {
184
- if (moduleDictionary[parts[0]] && moduleDictionary[parts[0]][parts[1]]) {
185
- return moduleDictionary[parts[0]][parts[1]];
186
- }
187
- } else if (parts.length === 1) {
188
- if (moduleDictionary.data && moduleDictionary.data[key]) {
189
- return moduleDictionary.data[key];
190
- }
191
- }
192
- return `No dictionary:${key}`;
193
- }, [moduleDictionary]);
194
- const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
195
- return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
196
- value: {
197
- moduleDictionary,
198
- getLabel,
199
- getModuleLabel
200
- },
201
- children
202
- });
203
- }
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 };
1
+ export { u as useEnvironment } from "./useEnvironment.js";
2
+ export { u as useHostTools } from "./useHostTools.js";
3
+ export { u as useLocalStorage } from "./useLocalStorage.js";
4
+ export { u as useNetwork } from "./useNetwork.js";
5
+ export { E as EmitEvents, a as getLocalStorage, g as getPropertyByString, s as setLocalStorage } from "./vendor.js";
6
+ export { v as voidFunction } from "./voidFunction.js";
7
+ export { a as axiosOperation } from "./axios.js";
8
+ import "react";
9
+ import "./EnvironmentContext.js";
10
+ import "react/jsx-runtime";
11
+ import "./HostToolsContext.js";
12
+ import "./NetworkContext.js";
13
+ import "./snakecase-keys.js";
@@ -1,102 +1,4 @@
1
- import { g as getAugmentedNamespace } from "./axios.js";
2
- var mapObj = { exports: {} };
3
- const isObject = (value) => typeof value === "object" && value !== null;
4
- const mapObjectSkip = Symbol("skip");
5
- const isObjectCustom = (value) => isObject(value) && !(value instanceof RegExp) && !(value instanceof Error) && !(value instanceof Date);
6
- const mapObject = (object, mapper, options, isSeen = /* @__PURE__ */ new WeakMap()) => {
7
- options = {
8
- deep: false,
9
- target: {},
10
- ...options
11
- };
12
- if (isSeen.has(object)) {
13
- return isSeen.get(object);
14
- }
15
- isSeen.set(object, options.target);
16
- const { target } = options;
17
- delete options.target;
18
- const mapArray = (array) => array.map((element) => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element);
19
- if (Array.isArray(object)) {
20
- return mapArray(object);
21
- }
22
- for (const [key, value] of Object.entries(object)) {
23
- const mapResult = mapper(key, value, object);
24
- if (mapResult === mapObjectSkip) {
25
- continue;
26
- }
27
- let [newKey, newValue, { shouldRecurse = true } = {}] = mapResult;
28
- if (newKey === "__proto__") {
29
- continue;
30
- }
31
- if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
32
- newValue = Array.isArray(newValue) ? mapArray(newValue) : mapObject(newValue, mapper, options, isSeen);
33
- }
34
- target[newKey] = newValue;
35
- }
36
- return target;
37
- };
38
- mapObj.exports = (object, mapper, options) => {
39
- if (!isObject(object)) {
40
- throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
41
- }
42
- return mapObject(object, mapper, options);
43
- };
44
- mapObj.exports.mapObjectSkip = mapObjectSkip;
45
- var __assign = function() {
46
- __assign = Object.assign || function __assign2(t) {
47
- for (var s, i = 1, n = arguments.length; i < n; i++) {
48
- s = arguments[i];
49
- for (var p in s)
50
- if (Object.prototype.hasOwnProperty.call(s, p))
51
- t[p] = s[p];
52
- }
53
- return t;
54
- };
55
- return __assign.apply(this, arguments);
56
- };
57
- function lowerCase(str) {
58
- return str.toLowerCase();
59
- }
60
- var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
61
- var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
62
- function noCase(input, options) {
63
- if (options === void 0) {
64
- options = {};
65
- }
66
- var _a = options.splitRegexp, splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a, _b = options.stripRegexp, stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b, _c = options.transform, transform = _c === void 0 ? lowerCase : _c, _d = options.delimiter, delimiter = _d === void 0 ? " " : _d;
67
- var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
68
- var start = 0;
69
- var end = result.length;
70
- while (result.charAt(start) === "\0")
71
- start++;
72
- while (result.charAt(end - 1) === "\0")
73
- end--;
74
- return result.slice(start, end).split("\0").map(transform).join(delimiter);
75
- }
76
- function replace(input, re, value) {
77
- if (re instanceof RegExp)
78
- return input.replace(re, value);
79
- return re.reduce(function(input2, re2) {
80
- return input2.replace(re2, value);
81
- }, input);
82
- }
83
- function dotCase(input, options) {
84
- if (options === void 0) {
85
- options = {};
86
- }
87
- return noCase(input, __assign({ delimiter: "." }, options));
88
- }
89
- function snakeCase$1(input, options) {
90
- if (options === void 0) {
91
- options = {};
92
- }
93
- return dotCase(input, __assign({ delimiter: "_" }, options));
94
- }
95
- const dist_es2015 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
96
- __proto__: null,
97
- snakeCase: snakeCase$1
98
- }, Symbol.toStringTag, { value: "Module" }));
99
- const require$$1 = /* @__PURE__ */ getAugmentedNamespace(dist_es2015);
1
+ import { m as mapObj, r as require$$1 } from "./vendor.js";
100
2
  const map = mapObj.exports;
101
3
  const { snakeCase } = require$$1;
102
4
  var snakecaseKeys = function(obj, options) {
@@ -0,0 +1,9 @@
1
+ import { useContext } from "react";
2
+ import { E as EnvironmentContext } from "./EnvironmentContext.js";
3
+ const useEnvironment = () => {
4
+ const context = useContext(EnvironmentContext);
5
+ if (!context)
6
+ throw new Error("useEnvironment context must be use inside EnvironmentContext");
7
+ return context;
8
+ };
9
+ export { useEnvironment as u };
@@ -0,0 +1,9 @@
1
+ import { useContext } from "react";
2
+ import { H as HostToolsContext } from "./HostToolsContext.js";
3
+ const useHostTools = () => {
4
+ const context = useContext(HostToolsContext);
5
+ if (!context)
6
+ throw new Error("useHostTools context must be use inside HostToolsContext");
7
+ return context;
8
+ };
9
+ export { useHostTools as u };
@@ -0,0 +1,21 @@
1
+ import { useState } from "react";
2
+ function useLocalStorage(key, initialValue) {
3
+ const [value, setValue] = useState(() => {
4
+ try {
5
+ const item = window.localStorage.getItem(key);
6
+ return item !== null ? JSON.parse(item) : initialValue;
7
+ } catch (e) {
8
+ return initialValue;
9
+ }
10
+ });
11
+ const setValueInLocalStorage = (newValue) => {
12
+ try {
13
+ window.localStorage.setItem(key, JSON.stringify(newValue));
14
+ setValue(newValue);
15
+ } catch (e) {
16
+ console.error(e);
17
+ }
18
+ };
19
+ return [value, setValueInLocalStorage];
20
+ }
21
+ export { useLocalStorage as u };
@@ -0,0 +1,9 @@
1
+ import { useContext } from "react";
2
+ import { N as NetworkContext } from "./NetworkContext.js";
3
+ const useNetwork = () => {
4
+ const context = useContext(NetworkContext);
5
+ if (!context)
6
+ throw new Error("useNetwork context must be use inside NetworkContext");
7
+ return context;
8
+ };
9
+ export { useNetwork as u };
package/dist/vendor.js ADDED
@@ -0,0 +1,153 @@
1
+ import "./axios.js";
2
+ import "./useEnvironment.js";
3
+ import "./useHostTools.js";
4
+ import "./useLocalStorage.js";
5
+ import "./useNetwork.js";
6
+ import "./voidFunction.js";
7
+ var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
8
+ EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
9
+ EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
10
+ return EmitEvents2;
11
+ })(EmitEvents || {});
12
+ function getPropertyByString(object, propString) {
13
+ let value = object;
14
+ const props = propString.split(".");
15
+ for (let index = 0; index < props.length; index += 1) {
16
+ if (props[index] === void 0)
17
+ break;
18
+ value = value[props[index]];
19
+ }
20
+ return value;
21
+ }
22
+ function getLocalStorage(key, initialValue) {
23
+ try {
24
+ const item = window.localStorage.getItem(key);
25
+ return item !== null ? JSON.parse(item) : initialValue;
26
+ } catch (e) {
27
+ return initialValue;
28
+ }
29
+ }
30
+ function setLocalStorage(key, value) {
31
+ try {
32
+ const item = window.localStorage.getItem(key);
33
+ let newValue = item !== null ? JSON.parse(item) : {};
34
+ newValue = { ...newValue, ...value };
35
+ window.localStorage.setItem(key, JSON.stringify(newValue));
36
+ } catch (e) {
37
+ console.error(e);
38
+ }
39
+ }
40
+ function getAugmentedNamespace(n) {
41
+ if (n.__esModule)
42
+ return n;
43
+ var a = Object.defineProperty({}, "__esModule", { value: true });
44
+ Object.keys(n).forEach(function(k) {
45
+ var d = Object.getOwnPropertyDescriptor(n, k);
46
+ Object.defineProperty(a, k, d.get ? d : {
47
+ enumerable: true,
48
+ get: function() {
49
+ return n[k];
50
+ }
51
+ });
52
+ });
53
+ return a;
54
+ }
55
+ var mapObj = { exports: {} };
56
+ const isObject = (value) => typeof value === "object" && value !== null;
57
+ const mapObjectSkip = Symbol("skip");
58
+ const isObjectCustom = (value) => isObject(value) && !(value instanceof RegExp) && !(value instanceof Error) && !(value instanceof Date);
59
+ const mapObject = (object, mapper, options, isSeen = /* @__PURE__ */ new WeakMap()) => {
60
+ options = {
61
+ deep: false,
62
+ target: {},
63
+ ...options
64
+ };
65
+ if (isSeen.has(object)) {
66
+ return isSeen.get(object);
67
+ }
68
+ isSeen.set(object, options.target);
69
+ const { target } = options;
70
+ delete options.target;
71
+ const mapArray = (array) => array.map((element) => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element);
72
+ if (Array.isArray(object)) {
73
+ return mapArray(object);
74
+ }
75
+ for (const [key, value] of Object.entries(object)) {
76
+ const mapResult = mapper(key, value, object);
77
+ if (mapResult === mapObjectSkip) {
78
+ continue;
79
+ }
80
+ let [newKey, newValue, { shouldRecurse = true } = {}] = mapResult;
81
+ if (newKey === "__proto__") {
82
+ continue;
83
+ }
84
+ if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
85
+ newValue = Array.isArray(newValue) ? mapArray(newValue) : mapObject(newValue, mapper, options, isSeen);
86
+ }
87
+ target[newKey] = newValue;
88
+ }
89
+ return target;
90
+ };
91
+ mapObj.exports = (object, mapper, options) => {
92
+ if (!isObject(object)) {
93
+ throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
94
+ }
95
+ return mapObject(object, mapper, options);
96
+ };
97
+ mapObj.exports.mapObjectSkip = mapObjectSkip;
98
+ var __assign = function() {
99
+ __assign = Object.assign || function __assign2(t) {
100
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
101
+ s = arguments[i];
102
+ for (var p in s)
103
+ if (Object.prototype.hasOwnProperty.call(s, p))
104
+ t[p] = s[p];
105
+ }
106
+ return t;
107
+ };
108
+ return __assign.apply(this, arguments);
109
+ };
110
+ function lowerCase(str) {
111
+ return str.toLowerCase();
112
+ }
113
+ var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
114
+ var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
115
+ function noCase(input, options) {
116
+ if (options === void 0) {
117
+ options = {};
118
+ }
119
+ var _a = options.splitRegexp, splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a, _b = options.stripRegexp, stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b, _c = options.transform, transform = _c === void 0 ? lowerCase : _c, _d = options.delimiter, delimiter = _d === void 0 ? " " : _d;
120
+ var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
121
+ var start = 0;
122
+ var end = result.length;
123
+ while (result.charAt(start) === "\0")
124
+ start++;
125
+ while (result.charAt(end - 1) === "\0")
126
+ end--;
127
+ return result.slice(start, end).split("\0").map(transform).join(delimiter);
128
+ }
129
+ function replace(input, re, value) {
130
+ if (re instanceof RegExp)
131
+ return input.replace(re, value);
132
+ return re.reduce(function(input2, re2) {
133
+ return input2.replace(re2, value);
134
+ }, input);
135
+ }
136
+ function dotCase(input, options) {
137
+ if (options === void 0) {
138
+ options = {};
139
+ }
140
+ return noCase(input, __assign({ delimiter: "." }, options));
141
+ }
142
+ function snakeCase(input, options) {
143
+ if (options === void 0) {
144
+ options = {};
145
+ }
146
+ return dotCase(input, __assign({ delimiter: "_" }, options));
147
+ }
148
+ const dist_es2015 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
149
+ __proto__: null,
150
+ snakeCase
151
+ }, Symbol.toStringTag, { value: "Module" }));
152
+ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(dist_es2015);
153
+ export { EmitEvents as E, getLocalStorage as a, getPropertyByString as g, mapObj as m, require$$1 as r, setLocalStorage as s };
@@ -0,0 +1,3 @@
1
+ function voidFunction() {
2
+ }
3
+ export { voidFunction as v };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@m4l/core",
3
3
  "private": false,
4
- "version": "0.0.18",
4
+ "version": "0.0.21",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {
@@ -1,5 +0,0 @@
1
- /// <reference types="react" />
2
- import { FlagsContextProps, FlagsProviderProps } from './types';
3
- declare const FlagsContext: import("react").Context<FlagsContextProps>;
4
- declare function FlagsProvider({ children }: FlagsProviderProps): JSX.Element;
5
- export { FlagsProvider, FlagsContext };
@@ -1,10 +0,0 @@
1
- import { ReactNode } from 'react';
2
- export interface FlagsProviderProps {
3
- children: ReactNode;
4
- }
5
- export declare type Flag = string;
6
- export interface FlagsContextProps {
7
- flags: Array<Flag>;
8
- clearFlags: () => void;
9
- addFlag: (flag: Flag) => void;
10
- }
@@ -1,5 +0,0 @@
1
- /// <reference types="react" />
2
- import { ModuleDictionaryContextProps, ModuleDictionaryProviderProps } from './types';
3
- declare const ModuleDictionaryContext: import("react").Context<ModuleDictionaryContextProps>;
4
- declare function ModuleDictionaryProvider(props: ModuleDictionaryProviderProps): JSX.Element;
5
- export { ModuleDictionaryProvider, ModuleDictionaryContext };
@@ -1,15 +0,0 @@
1
- import { ReactNode } from 'react';
2
- import type { GetLabelType, ModuleDictionary } from 'src/types/dictionary';
3
- export interface ModuleDictionaryProviderProps {
4
- currentLang?: string;
5
- isAuth?: boolean;
6
- moduleId: number;
7
- moduleName?: string;
8
- componentsDictionary: string[];
9
- children: ReactNode;
10
- }
11
- export interface ModuleDictionaryContextProps {
12
- moduleDictionary?: ModuleDictionary;
13
- getLabel: GetLabelType;
14
- getModuleLabel: () => string;
15
- }
@@ -1,5 +0,0 @@
1
- export { EnvironmentContext, EnvironmentProvider } from './EnvironmentContext';
2
- export { HostToolsContext, HostToolsProvider } from './HostToolsContext';
3
- export { NetworkContext, NetworkProvider } from './NetworkContext';
4
- export { FlagsContext, FlagsProvider } from './FlagsContext';
5
- export { ModuleDictionaryContext, ModuleDictionaryProvider } from './ModuleDictionaryContext';
@@ -1,6 +0,0 @@
1
- export { useLocalStorage } from './useLocalStorage/useLocalStorage';
2
- export { useHostTools } from './useHostTools';
3
- export { useNetwork } from './useNetwork';
4
- export { useEnvironment } from './useEnvironment';
5
- export { useFlags, useFlagsPresent } from './useFlags';
6
- export { useModuleDictionary } from './useModuleDictionary';
@@ -1,3 +0,0 @@
1
- import type { Flag, FlagsContextProps } from '../../contexts/FlagsContext/types';
2
- export declare const useFlags: () => FlagsContextProps;
3
- export declare const useFlagsPresent: (compareFlags: Array<Flag>) => boolean;
@@ -1 +0,0 @@
1
- export declare const useModuleDictionary: () => import("../../contexts/ModuleDictionaryContext/types").ModuleDictionaryContextProps;