@m4l/core 0.0.19 → 0.0.22

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,24 @@
1
+ import { createContext, useState } from "react";
2
+ import { jsx } from "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
+ function EnvironmentProvider(props) {
14
+ const {
15
+ children,
16
+ ...other
17
+ } = props;
18
+ const [finalEnvironment] = useState(other);
19
+ return /* @__PURE__ */ jsx(EnvironmentContext.Provider, {
20
+ value: finalEnvironment,
21
+ children
22
+ });
23
+ }
24
+ export { EnvironmentContext as E, EnvironmentProvider as a };
@@ -0,0 +1,24 @@
1
+ import { createContext, useState } from "react";
2
+ import { v as voidFunction } from "./voidFunction.js";
3
+ import { jsx } from "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
+ function HostToolsProvider(props) {
14
+ const {
15
+ children,
16
+ ...hostTools
17
+ } = props;
18
+ const [finalTools] = useState(hostTools);
19
+ return /* @__PURE__ */ jsx(HostToolsContext.Provider, {
20
+ value: finalTools,
21
+ children
22
+ });
23
+ }
24
+ export { HostToolsContext as H, HostToolsProvider as a };
@@ -0,0 +1,26 @@
1
+ import { createContext, useCallback } from "react";
2
+ import { u as useEnvironment } from "./useEnvironment.js";
3
+ import { u as useHostTools } from "./useHostTools.js";
4
+ import { jsx } from "react/jsx-runtime";
5
+ const initialValue = {
6
+ networkOperation: () => Promise.resolve()
7
+ };
8
+ const NetworkContext = createContext(initialValue);
9
+ function NetworkProvider(props) {
10
+ const {
11
+ children,
12
+ axiosOperation
13
+ } = props;
14
+ const environment = useEnvironment();
15
+ const hostTools = useHostTools();
16
+ const networkOperation = useCallback(async (networkProps) => {
17
+ return axiosOperation(networkProps, environment, hostTools);
18
+ }, [axiosOperation]);
19
+ return /* @__PURE__ */ jsx(NetworkContext.Provider, {
20
+ value: {
21
+ networkOperation
22
+ },
23
+ children
24
+ });
25
+ }
26
+ export { NetworkContext as N, NetworkProvider as a };
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 };
@@ -0,0 +1,5 @@
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 };
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,6 @@
1
+ /// <reference types="react" />
2
+ import { HostToolsType } from '../../types';
3
+ import type { HostToolsProviderProps } from './types';
4
+ declare const HostToolsContext: import("react").Context<HostToolsType>;
5
+ declare function HostToolsProvider(props: HostToolsProviderProps): JSX.Element;
6
+ export { HostToolsProvider, HostToolsContext };
@@ -0,0 +1,6 @@
1
+ import { ReactNode } from 'react';
2
+ import { HostToolsType } from '../../types';
3
+ export interface HostToolsProviderProps extends HostToolsType {
4
+ children: ReactNode;
5
+ }
6
+ export declare type HostToolsContextType = HostToolsType;
@@ -0,0 +1,5 @@
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 };
@@ -0,0 +1,15 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ import type { NetworkProviderProps } from './types';
3
+ declare const NetworkContext: import("react").Context<import("./types").NetworkType>;
4
+ declare function NetworkProvider(props: NetworkProviderProps): JSX.Element;
5
+ export { NetworkProvider, NetworkContext };
@@ -0,0 +1,11 @@
1
+ import { ReactNode } from 'react';
2
+ import { AxiosOperation, NetworkProps } from '../../types';
3
+ export declare type EventFunListener = (...args: any[]) => void;
4
+ export interface NetworkType {
5
+ networkOperation: (props: NetworkProps) => Promise<any>;
6
+ }
7
+ export interface NetworkProviderProps {
8
+ axiosOperation: AxiosOperation;
9
+ children: ReactNode;
10
+ }
11
+ export declare type NetworkContextType = NetworkType;
@@ -0,0 +1,5 @@
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';
@@ -0,0 +1,6 @@
1
+ export { useLocalStorage } from './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';
@@ -0,0 +1,3 @@
1
+ import type { Flag, FlagsContextProps } from '../../contexts/FlagsContext/types';
2
+ export declare const useFlags: () => FlagsContextProps;
3
+ export declare const useFlagsPresent: (compareFlags: Array<Flag>) => boolean;
@@ -0,0 +1,2 @@
1
+ export declare const useHostTools: () => import("../..").HostToolsType;
2
+ export default useHostTools;
@@ -0,0 +1 @@
1
+ export declare function useLocalStorage<ValueType>(key: string, initialValue: ValueType): any[];
@@ -0,0 +1 @@
1
+ export declare const useModuleDictionary: () => import("../../contexts/ModuleDictionaryContext/types").ModuleDictionaryContextProps;
@@ -0,0 +1,2 @@
1
+ export declare const useNetwork: () => import("../../contexts/NetworkContext/types").NetworkType;
2
+ export default useNetwork;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,9 @@
1
+ export * from './contexts';
1
2
  export { useEnvironment } from './hooks/useEnvironment';
3
+ export { useFlags } from './hooks/useFlags';
4
+ export { useHostTools } from './hooks/useHostTools';
5
+ export { useLocalStorage } from './hooks/useLocalStorage';
6
+ export { useNetwork } from './hooks/useNetwork';
2
7
  export { EmitEvents } from './types';
3
8
  export type { Maybe, HostToolsType, NetworkProps, EnvironmentType, AxiosOperation, EventFunListener, } from './types';
4
9
  export type { GetLabelType, Dictionary, ModuleDictionary, ComponentDictionary, } from './types/dictionary';
package/dist/index.js CHANGED
@@ -1,133 +1,13 @@
1
- import { createContext, useContext } from "react";
1
+ export { E as EnvironmentContext, a as EnvironmentProvider } from "./EnvironmentContext.js";
2
+ export { H as HostToolsContext, a as HostToolsProvider } from "./HostToolsContext.js";
3
+ export { N as NetworkContext, a as NetworkProvider } from "./NetworkContext.js";
4
+ export { E as EmitEvents, F as FlagsContext, a as FlagsProvider, M as ModuleDictionaryContext, b as ModuleDictionaryProvider, c as getLocalStorage, g as getPropertyByString, s as setLocalStorage, u as useFlags } from "./vendor.js";
5
+ export { u as useEnvironment } from "./useEnvironment.js";
6
+ export { u as useHostTools } from "./useHostTools.js";
7
+ export { u as useLocalStorage } from "./useLocalStorage.js";
8
+ export { u as useNetwork } from "./useNetwork.js";
9
+ export { v as voidFunction } from "./voidFunction.js";
10
+ export { a as axiosOperation } from "./axios.js";
11
+ import "react";
2
12
  import "react/jsx-runtime";
3
- import { a as axios } from "./axios.js";
4
- import { s as snakecaseKeys } from "./snakecase-keys.js";
5
- const initialValue = {
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);
15
- const useEnvironment = () => {
16
- const context = useContext(EnvironmentContext);
17
- if (!context)
18
- throw new Error("useEnvironment context must be use inside EnvironmentContext");
19
- return context;
20
- };
21
- var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
22
- EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
23
- EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
24
- return EmitEvents2;
25
- })(EmitEvents || {});
26
- function voidFunction() {
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
- function getResponse(endPoint, response) {
57
- console.log("Axios response", response, typeof response.data);
58
- if (response && response.data && typeof response.data === "object") {
59
- if (response.data.error && response.data.error?.code && response.data.error?.msg !== void 0) {
60
- return Promise.reject({ ...response.data.error, status: response.status });
61
- }
62
- return response.data;
63
- }
64
- return Promise.reject({
65
- code: 1,
66
- msg: `Incorrect endpoint: ${endPoint}`,
67
- status: response.status
68
- });
69
- }
70
- function getError(error, hostTools, checkUnAuthorized = true) {
71
- const { toast } = hostTools;
72
- let err = {
73
- message: "",
74
- status: 1,
75
- code: 0
76
- };
77
- console.log("getError", error);
78
- if (error?.code !== void 0 && err.status !== void 0 && error.message !== void 0) {
79
- err = { ...err, ...error };
80
- } else if (error?.response) {
81
- 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) {
82
- err = { ...error.response.data.error, status: error.response.status };
83
- } else {
84
- err.message = error.message;
85
- err.status = error.response.status;
86
- err.code = 0;
87
- }
88
- if (checkUnAuthorized && error.response.status === 401) {
89
- hostTools.events_emit(EmitEvents.EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED, {});
90
- }
91
- } else if (error?.request) {
92
- err.message = `${error?.code} ${error.message}`;
93
- err.code = -1;
94
- } else {
95
- err.message = `${error?.code} ${error.message}`;
96
- err.status = 0;
97
- err.code = -2;
98
- }
99
- if (checkUnAuthorized) {
100
- toast(`${err.message} - status: ${err.status} - code: ${err.code}`, { type: "error" });
101
- }
102
- return err;
103
- }
104
- const axiosOperation = async (props, enviroment, hostTools) => {
105
- const {
106
- method,
107
- endPoint,
108
- timeout = 5e3,
109
- parms = {},
110
- data = {},
111
- isRemote = true,
112
- checkUnAuthorized = true
113
- } = props;
114
- let baseURL;
115
- if (isRemote) {
116
- baseURL = enviroment.host_api_remote;
117
- } else {
118
- baseURL = enviroment.host_api_local;
119
- }
120
- hostTools.startProgress();
121
- return axios({
122
- baseURL,
123
- withCredentials: isRemote,
124
- method,
125
- url: `/${endPoint}`,
126
- data: snakecaseKeys(data, { deep: true }),
127
- params: snakecaseKeys(parms, { deep: true }),
128
- timeout
129
- }).then((response) => getResponse(endPoint, response)).catch((error) => Promise.reject(getError(error, hostTools, checkUnAuthorized))).finally(() => {
130
- hostTools.stopProgress();
131
- });
132
- };
133
- export { EmitEvents, axiosOperation, getLocalStorage, getPropertyByString, setLocalStorage, useEnvironment, voidFunction };
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,275 @@
1
+ import { createContext, useState, useCallback, useContext, useLayoutEffect } from "react";
2
+ import "./HostToolsContext.js";
3
+ import "./NetworkContext.js";
4
+ import "./EnvironmentContext.js";
5
+ import "./axios.js";
6
+ import { v as voidFunction } from "./voidFunction.js";
7
+ import { jsx } from "react/jsx-runtime";
8
+ import { u as useHostTools } from "./useHostTools.js";
9
+ import { u as useNetwork } from "./useNetwork.js";
10
+ import { u as useEnvironment } from "./useEnvironment.js";
11
+ import "./useLocalStorage.js";
12
+ const initialState$1 = {
13
+ flags: [],
14
+ clearFlags: voidFunction,
15
+ addFlag: voidFunction
16
+ };
17
+ const FlagsContext = createContext(initialState$1);
18
+ function FlagsProvider({
19
+ children
20
+ }) {
21
+ const [flags, setFlags] = useState([]);
22
+ const clearFlags = useCallback(() => {
23
+ setFlags([]);
24
+ }, []);
25
+ const addFlag = useCallback((newFlag) => {
26
+ setFlags((oldFlags) => {
27
+ if (oldFlags.findIndex((f) => f === newFlag) < 0) {
28
+ return [...oldFlags, newFlag];
29
+ }
30
+ return [...oldFlags];
31
+ });
32
+ }, []);
33
+ return /* @__PURE__ */ jsx(FlagsContext.Provider, {
34
+ value: {
35
+ flags,
36
+ addFlag,
37
+ clearFlags
38
+ },
39
+ children
40
+ });
41
+ }
42
+ const useFlags = () => {
43
+ const context = useContext(FlagsContext);
44
+ if (!context)
45
+ throw new Error("useFlags context must be use inside FlagsProvider");
46
+ return context;
47
+ };
48
+ const initialState = {
49
+ getLabel: () => "..",
50
+ getModuleLabel: () => "No dictionary context"
51
+ };
52
+ const ModuleDictionaryContext = createContext(initialState);
53
+ function ModuleDictionaryProvider(props) {
54
+ const {
55
+ children,
56
+ componentsDictionary,
57
+ moduleId,
58
+ moduleName = "module_name",
59
+ currentLang = "us",
60
+ isAuth = true
61
+ } = props;
62
+ const {
63
+ addFlag
64
+ } = useFlags();
65
+ const [moduleDictionary, setModuleDictionary] = useState(void 0);
66
+ const {
67
+ domain_token
68
+ } = useEnvironment();
69
+ const {
70
+ startProgress,
71
+ stopProgress
72
+ } = useHostTools();
73
+ const {
74
+ networkOperation
75
+ } = useNetwork();
76
+ useLayoutEffect(() => {
77
+ let mounted = true;
78
+ startProgress();
79
+ networkOperation({
80
+ method: "GET",
81
+ endPoint: isAuth ? `dictionaries/${moduleId}` : `na/dictionaries/${moduleId}`,
82
+ parms: {
83
+ comps: componentsDictionary,
84
+ ...isAuth ? {} : {
85
+ domain_token
86
+ }
87
+ }
88
+ }).then((response) => {
89
+ if (mounted) {
90
+ setModuleDictionary({
91
+ ...response
92
+ });
93
+ addFlag("dictionary_loaded");
94
+ }
95
+ }).finally(() => {
96
+ stopProgress();
97
+ });
98
+ return function cleanUp() {
99
+ mounted = false;
100
+ };
101
+ }, [currentLang]);
102
+ const getLabel = useCallback((key) => {
103
+ if (moduleDictionary === void 0)
104
+ return "No dictionary";
105
+ if (key === void 0)
106
+ return "No key";
107
+ const parts = key.split(".");
108
+ if (parts.length === 2) {
109
+ if (moduleDictionary[parts[0]] && moduleDictionary[parts[0]][parts[1]]) {
110
+ return moduleDictionary[parts[0]][parts[1]];
111
+ }
112
+ } else if (parts.length === 1) {
113
+ if (moduleDictionary.data && moduleDictionary.data[key]) {
114
+ return moduleDictionary.data[key];
115
+ }
116
+ }
117
+ return `No dictionary:${key}`;
118
+ }, [moduleDictionary]);
119
+ const getModuleLabel = useCallback(() => getLabel(moduleName), [moduleName, getLabel]);
120
+ return /* @__PURE__ */ jsx(ModuleDictionaryContext.Provider, {
121
+ value: {
122
+ moduleDictionary,
123
+ getLabel,
124
+ getModuleLabel
125
+ },
126
+ children
127
+ });
128
+ }
129
+ var EmitEvents = /* @__PURE__ */ ((EmitEvents2) => {
130
+ EmitEvents2["EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED"] = `netsevice_unauthorized`;
131
+ EmitEvents2["EMMIT_EVENT_HOST_THEME_CHANGE"] = "host_theme_change";
132
+ return EmitEvents2;
133
+ })(EmitEvents || {});
134
+ function getPropertyByString(object, propString) {
135
+ let value = object;
136
+ const props = propString.split(".");
137
+ for (let index = 0; index < props.length; index += 1) {
138
+ if (props[index] === void 0)
139
+ break;
140
+ value = value[props[index]];
141
+ }
142
+ return value;
143
+ }
144
+ function getLocalStorage(key, initialValue) {
145
+ try {
146
+ const item = window.localStorage.getItem(key);
147
+ return item !== null ? JSON.parse(item) : initialValue;
148
+ } catch (e) {
149
+ return initialValue;
150
+ }
151
+ }
152
+ function setLocalStorage(key, value) {
153
+ try {
154
+ const item = window.localStorage.getItem(key);
155
+ let newValue = item !== null ? JSON.parse(item) : {};
156
+ newValue = { ...newValue, ...value };
157
+ window.localStorage.setItem(key, JSON.stringify(newValue));
158
+ } catch (e) {
159
+ console.error(e);
160
+ }
161
+ }
162
+ function getAugmentedNamespace(n) {
163
+ if (n.__esModule)
164
+ return n;
165
+ var a = Object.defineProperty({}, "__esModule", { value: true });
166
+ Object.keys(n).forEach(function(k) {
167
+ var d = Object.getOwnPropertyDescriptor(n, k);
168
+ Object.defineProperty(a, k, d.get ? d : {
169
+ enumerable: true,
170
+ get: function() {
171
+ return n[k];
172
+ }
173
+ });
174
+ });
175
+ return a;
176
+ }
177
+ var mapObj = { exports: {} };
178
+ const isObject = (value) => typeof value === "object" && value !== null;
179
+ const mapObjectSkip = Symbol("skip");
180
+ const isObjectCustom = (value) => isObject(value) && !(value instanceof RegExp) && !(value instanceof Error) && !(value instanceof Date);
181
+ const mapObject = (object, mapper, options, isSeen = /* @__PURE__ */ new WeakMap()) => {
182
+ options = {
183
+ deep: false,
184
+ target: {},
185
+ ...options
186
+ };
187
+ if (isSeen.has(object)) {
188
+ return isSeen.get(object);
189
+ }
190
+ isSeen.set(object, options.target);
191
+ const { target } = options;
192
+ delete options.target;
193
+ const mapArray = (array) => array.map((element) => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element);
194
+ if (Array.isArray(object)) {
195
+ return mapArray(object);
196
+ }
197
+ for (const [key, value] of Object.entries(object)) {
198
+ const mapResult = mapper(key, value, object);
199
+ if (mapResult === mapObjectSkip) {
200
+ continue;
201
+ }
202
+ let [newKey, newValue, { shouldRecurse = true } = {}] = mapResult;
203
+ if (newKey === "__proto__") {
204
+ continue;
205
+ }
206
+ if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
207
+ newValue = Array.isArray(newValue) ? mapArray(newValue) : mapObject(newValue, mapper, options, isSeen);
208
+ }
209
+ target[newKey] = newValue;
210
+ }
211
+ return target;
212
+ };
213
+ mapObj.exports = (object, mapper, options) => {
214
+ if (!isObject(object)) {
215
+ throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
216
+ }
217
+ return mapObject(object, mapper, options);
218
+ };
219
+ mapObj.exports.mapObjectSkip = mapObjectSkip;
220
+ var __assign = function() {
221
+ __assign = Object.assign || function __assign2(t) {
222
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
223
+ s = arguments[i];
224
+ for (var p in s)
225
+ if (Object.prototype.hasOwnProperty.call(s, p))
226
+ t[p] = s[p];
227
+ }
228
+ return t;
229
+ };
230
+ return __assign.apply(this, arguments);
231
+ };
232
+ function lowerCase(str) {
233
+ return str.toLowerCase();
234
+ }
235
+ var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
236
+ var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
237
+ function noCase(input, options) {
238
+ if (options === void 0) {
239
+ options = {};
240
+ }
241
+ 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;
242
+ var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
243
+ var start = 0;
244
+ var end = result.length;
245
+ while (result.charAt(start) === "\0")
246
+ start++;
247
+ while (result.charAt(end - 1) === "\0")
248
+ end--;
249
+ return result.slice(start, end).split("\0").map(transform).join(delimiter);
250
+ }
251
+ function replace(input, re, value) {
252
+ if (re instanceof RegExp)
253
+ return input.replace(re, value);
254
+ return re.reduce(function(input2, re2) {
255
+ return input2.replace(re2, value);
256
+ }, input);
257
+ }
258
+ function dotCase(input, options) {
259
+ if (options === void 0) {
260
+ options = {};
261
+ }
262
+ return noCase(input, __assign({ delimiter: "." }, options));
263
+ }
264
+ function snakeCase(input, options) {
265
+ if (options === void 0) {
266
+ options = {};
267
+ }
268
+ return dotCase(input, __assign({ delimiter: "_" }, options));
269
+ }
270
+ const dist_es2015 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
271
+ __proto__: null,
272
+ snakeCase
273
+ }, Symbol.toStringTag, { value: "Module" }));
274
+ const require$$1 = /* @__PURE__ */ getAugmentedNamespace(dist_es2015);
275
+ export { EmitEvents as E, FlagsContext as F, ModuleDictionaryContext as M, FlagsProvider as a, ModuleDictionaryProvider as b, getLocalStorage as c, getPropertyByString as g, mapObj as m, require$$1 as r, setLocalStorage as s, useFlags as u };
@@ -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.19",
4
+ "version": "0.0.22",
5
5
  "license": "UNLICENSED",
6
6
  "author": "M4L Team",
7
7
  "scripts": {