@m4l/core 0.0.21 → 0.0.24
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/.eslintignore +2 -0
- package/.eslintrc.cjs +109 -0
- package/.prettierignore +2 -0
- package/.prettierrc.json +26 -0
- package/.vscode/settings.json +50 -0
- package/README copy.md +0 -0
- package/index.html +13 -0
- package/package.json +2 -4
- package/package_with_dependicies.json +75 -0
- package/src/contexts/EnvironmentContext/index.tsx +37 -0
- package/{dist/contexts/EnvironmentContext/types.d.ts → src/contexts/EnvironmentContext/types.ts} +3 -1
- package/src/contexts/FlagsContext/index.tsx +50 -0
- package/src/contexts/FlagsContext/types.ts +16 -0
- package/src/contexts/HostToolsContext/index.tsx +34 -0
- package/{dist/contexts/HostToolsContext/types.d.ts → src/contexts/HostToolsContext/types.ts} +3 -1
- package/src/contexts/ModuleDictionaryContext/index.tsx +109 -0
- package/src/contexts/ModuleDictionaryContext/types.ts +18 -0
- package/src/contexts/NetworkContext/index.tsx +44 -0
- package/{dist/contexts/NetworkContext/types.d.ts → src/contexts/NetworkContext/types.ts} +6 -2
- package/src/contexts/index.ts +5 -0
- package/src/hooks/index.ts +7 -0
- package/src/hooks/useEnvironment/index.test.tsx +36 -0
- package/src/hooks/useEnvironment/index.ts +13 -0
- package/src/hooks/useFlags/index.ts +33 -0
- package/src/hooks/useHostTools/index.ts +13 -0
- package/src/hooks/useLocalStorage/index.ts +23 -0
- package/src/hooks/useModuleDictionary/index.ts +11 -0
- package/src/hooks/useNetwork/index.ts +13 -0
- package/src/index.ts +43 -0
- package/src/indexFlags.ts +1 -0
- package/src/isolation/App.tsx +24 -0
- package/src/main.tsx +10 -0
- package/src/prueba.d1.t1 +10 -0
- package/src/test/setup.ts +1 -0
- package/src/test/utils.tsx +20 -0
- package/{dist/types/dictionary.d.ts → src/types/dictionary.ts} +5 -1
- package/{dist/types/index.d.ts → src/types/index.ts} +20 -5
- package/src/utils/axiosOperation/index.ts +140 -0
- package/{dist/utils/axiosOperation/types.d.ts → src/utils/axiosOperation/types.ts} +1 -1
- package/src/utils/getLocalStorage.ts +8 -0
- package/src/utils/getPropertyByString.ts +10 -0
- package/{dist/utils/index.d.ts → src/utils/index.ts} +0 -0
- package/src/utils/setLocalStorage.ts +12 -0
- package/src/utils/voidFunction.ts +2 -0
- package/{dist → src}/vite-env.d.ts +0 -0
- package/tsconfig.json +37 -0
- package/tsconfig.node.json +10 -0
- package/vite.config.ts +178 -0
- package/dist/EnvironmentContext.js +0 -13
- package/dist/HostToolsContext.js +0 -13
- package/dist/NetworkContext.js +0 -9
- package/dist/axios.js +0 -1299
- package/dist/contexts/EnvironmentContext/index.d.ts +0 -5
- package/dist/contexts/HostToolsContext/index.d.ts +0 -6
- package/dist/contexts/NetworkContext/index.d.ts +0 -5
- package/dist/hooks/useEnvironment/index.d.ts +0 -2
- package/dist/hooks/useHostTools/index.d.ts +0 -2
- package/dist/hooks/useLocalStorage/index.d.ts +0 -1
- package/dist/hooks/useNetwork/index.d.ts +0 -2
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -13
- package/dist/snakecase-keys.js +0 -18
- package/dist/useEnvironment.js +0 -9
- package/dist/useHostTools.js +0 -9
- package/dist/useLocalStorage.js +0 -21
- package/dist/useNetwork.js +0 -9
- package/dist/utils/axiosOperation/index.d.ts +0 -2
- package/dist/utils/getLocalStorage.d.ts +0 -1
- package/dist/utils/getPropertyByString.d.ts +0 -1
- package/dist/utils/setLocalStorage.d.ts +0 -1
- package/dist/utils/voidFunction.d.ts +0 -1
- package/dist/vendor.js +0 -153
- package/dist/voidFunction.js +0 -3
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
|
|
7
|
+
export { useModuleDictionary } from './useModuleDictionary';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//import { render, screen } from '../../../test/utils';
|
|
2
|
+
import { render, screen } from '../../test/utils';
|
|
3
|
+
import { it } from 'vitest';
|
|
4
|
+
import { EnvironmentProvider } from '../../contexts/EnvironmentContext';
|
|
5
|
+
import useEnvironment from '.';
|
|
6
|
+
|
|
7
|
+
it('useEnvironment tenga acceso a las variables del contexto', () => {
|
|
8
|
+
const obValue = {
|
|
9
|
+
isLocalhost: true,
|
|
10
|
+
host: 'h1',
|
|
11
|
+
domain_token: '1',
|
|
12
|
+
host_api_local: '2',
|
|
13
|
+
host_api_remote: '3',
|
|
14
|
+
host_static_assets: '4',
|
|
15
|
+
environment: '5',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const stringValue = JSON.stringify(obValue);
|
|
19
|
+
|
|
20
|
+
const Component = () => {
|
|
21
|
+
const value = useEnvironment();
|
|
22
|
+
return <div data-testid="useDivHostId">{JSON.stringify(value)}</div>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// const { container } =
|
|
26
|
+
render(
|
|
27
|
+
<EnvironmentProvider {...obValue}>
|
|
28
|
+
<Component />
|
|
29
|
+
</EnvironmentProvider>,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
// container.querySelector('#useDivHostId');
|
|
33
|
+
//console.log('Q trajo', window);
|
|
34
|
+
|
|
35
|
+
expect(screen.getByTestId('useDivHostId').textContent).toBe(stringValue);
|
|
36
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { EnvironmentContext } from '../../contexts/EnvironmentContext';
|
|
3
|
+
|
|
4
|
+
// ----------------------------------------------------------------------
|
|
5
|
+
export const useEnvironment = () => {
|
|
6
|
+
const context = useContext(EnvironmentContext);
|
|
7
|
+
|
|
8
|
+
if (!context) throw new Error('useEnvironment context must be use inside EnvironmentContext');
|
|
9
|
+
|
|
10
|
+
return context;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default useEnvironment;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { FlagsContext } from '../../contexts/FlagsContext';
|
|
3
|
+
import type { Flag, FlagsContextProps } from '../../contexts/FlagsContext/types';
|
|
4
|
+
|
|
5
|
+
// ----------------------------------------------------------------------
|
|
6
|
+
export const useFlags = (): FlagsContextProps => {
|
|
7
|
+
const context = useContext(FlagsContext);
|
|
8
|
+
|
|
9
|
+
if (!context) throw new Error('useFlags context must be use inside FlagsProvider');
|
|
10
|
+
|
|
11
|
+
return context;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function isFlagsPresent(compareFlags: Array<Flag>, flags: Array<Flag>) {
|
|
15
|
+
const filterFlags = compareFlags.filter(
|
|
16
|
+
findFlag => flags.findIndex(sFlag => sFlag === findFlag) !== -1,
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
return filterFlags.length === compareFlags.length;
|
|
20
|
+
}
|
|
21
|
+
export const useFlagsPresent = (compareFlags: Array<Flag>): boolean => {
|
|
22
|
+
const context = useFlags();
|
|
23
|
+
|
|
24
|
+
const [isPresent, setIsPresent] = useState(isFlagsPresent(compareFlags, context.flags));
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (isFlagsPresent(compareFlags, context.flags)) {
|
|
28
|
+
setIsPresent(true);
|
|
29
|
+
}
|
|
30
|
+
}, [context.flags, compareFlags]);
|
|
31
|
+
|
|
32
|
+
return isPresent;
|
|
33
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { HostToolsContext } from '../../contexts/HostToolsContext';
|
|
3
|
+
|
|
4
|
+
// ----------------------------------------------------------------------
|
|
5
|
+
export const useHostTools = () => {
|
|
6
|
+
const context = useContext(HostToolsContext);
|
|
7
|
+
|
|
8
|
+
if (!context) throw new Error('useHostTools context must be use inside HostToolsContext');
|
|
9
|
+
|
|
10
|
+
return context;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default useHostTools;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useLocalStorage<ValueType>(key: string, initialValue: ValueType) {
|
|
4
|
+
const [value, setValue] = useState(() => {
|
|
5
|
+
try {
|
|
6
|
+
const item = window.localStorage.getItem(key);
|
|
7
|
+
return item !== null ? JSON.parse(item) : initialValue;
|
|
8
|
+
} catch (e) {
|
|
9
|
+
return initialValue;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const setValueInLocalStorage = (newValue: ValueType) => {
|
|
14
|
+
try {
|
|
15
|
+
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
16
|
+
setValue(newValue);
|
|
17
|
+
} catch (e) {
|
|
18
|
+
console.error(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
return [value, setValueInLocalStorage];
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { ModuleDictionaryContext } from '../../contexts/ModuleDictionaryContext';
|
|
3
|
+
|
|
4
|
+
// ----------------------------------------------------------------------
|
|
5
|
+
export const useModuleDictionary = () => {
|
|
6
|
+
const context = useContext(ModuleDictionaryContext);
|
|
7
|
+
if (!context)
|
|
8
|
+
throw new Error('useModuleDictionary context must be use inside ModuleDictionaryProvider');
|
|
9
|
+
|
|
10
|
+
return context;
|
|
11
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { NetworkContext } from '../../contexts/NetworkContext';
|
|
3
|
+
|
|
4
|
+
// ----------------------------------------------------------------------
|
|
5
|
+
export const useNetwork = () => {
|
|
6
|
+
const context = useContext(NetworkContext);
|
|
7
|
+
|
|
8
|
+
if (!context) throw new Error('useNetwork context must be use inside NetworkContext');
|
|
9
|
+
|
|
10
|
+
return context;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default useNetwork;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// //context
|
|
2
|
+
export * from './contexts';
|
|
3
|
+
|
|
4
|
+
//hooks
|
|
5
|
+
//export * from './hooks';
|
|
6
|
+
|
|
7
|
+
export { useEnvironment } from './hooks/useEnvironment';
|
|
8
|
+
export { useFlags } from './hooks/useFlags';
|
|
9
|
+
export { useHostTools } from './hooks/useHostTools';
|
|
10
|
+
export { useLocalStorage } from './hooks/useLocalStorage';
|
|
11
|
+
export { useNetwork } from './hooks/useNetwork';
|
|
12
|
+
|
|
13
|
+
//enums
|
|
14
|
+
export { EmitEvents } from './types';
|
|
15
|
+
|
|
16
|
+
//types
|
|
17
|
+
|
|
18
|
+
export type {
|
|
19
|
+
Maybe,
|
|
20
|
+
HostToolsType,
|
|
21
|
+
NetworkProps,
|
|
22
|
+
EnvironmentType,
|
|
23
|
+
AxiosOperation,
|
|
24
|
+
EventFunListener,
|
|
25
|
+
} from './types';
|
|
26
|
+
|
|
27
|
+
export type {
|
|
28
|
+
GetLabelType,
|
|
29
|
+
Dictionary,
|
|
30
|
+
ModuleDictionary,
|
|
31
|
+
ComponentDictionary,
|
|
32
|
+
} from './types/dictionary';
|
|
33
|
+
|
|
34
|
+
// //utils
|
|
35
|
+
export * from './utils';
|
|
36
|
+
|
|
37
|
+
// export { axiosOperation } from './utils/axiosOperation/index';
|
|
38
|
+
// export function fn(arr: number[]) {
|
|
39
|
+
// const arr2 = [1, ...arr];
|
|
40
|
+
|
|
41
|
+
// const a = { ...{ A: 1 } };
|
|
42
|
+
// console.log(a, arr2);
|
|
43
|
+
// }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useFlags } from './hooks/useFlags';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//react
|
|
2
|
+
|
|
3
|
+
// mui
|
|
4
|
+
import { EnvironmentProvider } from '../contexts/EnvironmentContext';
|
|
5
|
+
|
|
6
|
+
function App() {
|
|
7
|
+
return (
|
|
8
|
+
<EnvironmentProvider
|
|
9
|
+
{...{
|
|
10
|
+
isLocalhost: true,
|
|
11
|
+
host: 'h1',
|
|
12
|
+
domain_token: '1',
|
|
13
|
+
host_api_local: '2',
|
|
14
|
+
host_api_remote: '3',
|
|
15
|
+
host_static_assets: '4',
|
|
16
|
+
environment: '5',
|
|
17
|
+
}}
|
|
18
|
+
>
|
|
19
|
+
<div>Host tools</div>
|
|
20
|
+
</EnvironmentProvider>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default App;
|
package/src/main.tsx
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ReactDOM from 'react-dom/client';
|
|
3
|
+
import App from './isolation/App';
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
6
|
+
ReactDOM.createRoot(document.getElementById('root')!).render(
|
|
7
|
+
<React.StrictMode>
|
|
8
|
+
<App />
|
|
9
|
+
</React.StrictMode>,
|
|
10
|
+
);
|
package/src/prueba.d1.t1
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { voidFunction } from './utils/voidFunction';
|
|
2
|
+
|
|
3
|
+
export { EnvironmentProvider } from './contexts/EnvironmentContext';
|
|
4
|
+
|
|
5
|
+
// export function fn(arr: number[]) {
|
|
6
|
+
// const arr2 = [1, ...arr];
|
|
7
|
+
|
|
8
|
+
// const a = { ...{ A: 1 } };
|
|
9
|
+
// console.log(a, arr2);
|
|
10
|
+
// }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { cleanup, render } from '@testing-library/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { afterEach } from 'vitest';
|
|
4
|
+
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
cleanup();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const customRender = (ui: React.ReactElement, options = {}) => {
|
|
10
|
+
return render(ui, {
|
|
11
|
+
// wrap provider(s) here if needed
|
|
12
|
+
wrapper: ({ children }) => children,
|
|
13
|
+
...options,
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export * from '@testing-library/react';
|
|
18
|
+
export { default as userEvent } from '@testing-library/user-event';
|
|
19
|
+
|
|
20
|
+
export { customRender as render };
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type GetLabelType = (key: string) => string;
|
|
2
|
+
|
|
2
3
|
export interface ComponentDictionary {
|
|
3
4
|
[key: string]: string;
|
|
4
5
|
}
|
|
6
|
+
|
|
5
7
|
export interface Dictionary {
|
|
6
8
|
[key: string]: ComponentDictionary | boolean | undefined | string;
|
|
7
9
|
}
|
|
10
|
+
|
|
8
11
|
export interface DataDictionary extends ComponentDictionary {
|
|
9
12
|
module_name: string;
|
|
10
13
|
}
|
|
14
|
+
|
|
11
15
|
export interface ModuleDictionary extends Dictionary {
|
|
12
16
|
data: DataDictionary;
|
|
13
17
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
// import type { Method } from 'axios';
|
|
1
2
|
import type { Id, ToastContent, ToastOptions } from 'react-toastify';
|
|
2
3
|
import type { BrowserHistory } from 'history';
|
|
4
|
+
|
|
3
5
|
export declare type Maybe<T> = T | undefined | null;
|
|
4
6
|
export declare interface EnvironmentType {
|
|
5
7
|
isLocalhost: boolean;
|
|
@@ -10,6 +12,7 @@ export declare interface EnvironmentType {
|
|
|
10
12
|
host_static_assets: string;
|
|
11
13
|
environment: string;
|
|
12
14
|
}
|
|
15
|
+
|
|
13
16
|
export declare type NetworkProps = {
|
|
14
17
|
method: any;
|
|
15
18
|
endPoint: string;
|
|
@@ -20,19 +23,31 @@ export declare type NetworkProps = {
|
|
|
20
23
|
isRemote?: boolean;
|
|
21
24
|
showSuccesInfo?: boolean;
|
|
22
25
|
};
|
|
26
|
+
|
|
23
27
|
export declare type ToastFunction = (content: ToastContent, options?: ToastOptions) => Id;
|
|
24
28
|
export declare type EventFunListener = (...args: any[]) => void;
|
|
29
|
+
|
|
25
30
|
export declare interface HostToolsType {
|
|
26
31
|
history?: BrowserHistory;
|
|
27
|
-
toast: ToastFunction;
|
|
32
|
+
toast: ToastFunction; // Funcion for toast
|
|
28
33
|
startProgress: VoidFunction;
|
|
29
34
|
stopProgress: VoidFunction;
|
|
35
|
+
|
|
30
36
|
events_add_listener: (eventName: string, handler: EventFunListener) => void;
|
|
31
37
|
events_remove_listener: (eventName: string, handler: Maybe<EventFunListener>) => void;
|
|
32
38
|
events_emit: (eventName: string, arg: any) => void;
|
|
33
39
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
|
|
41
|
+
// eslint-disable-next-line no-shadow
|
|
42
|
+
export enum EmitEvents {
|
|
43
|
+
EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED = `netsevice_unauthorized`,
|
|
44
|
+
EMMIT_EVENT_HOST_THEME_CHANGE = 'host_theme_change',
|
|
37
45
|
}
|
|
38
|
-
|
|
46
|
+
|
|
47
|
+
export declare type AxiosOperation = (
|
|
48
|
+
props: NetworkProps,
|
|
49
|
+
enviroment: EnvironmentType,
|
|
50
|
+
hostTools: HostToolsType,
|
|
51
|
+
) => Promise<any>;
|
|
52
|
+
|
|
53
|
+
// export type { Method } from 'axios';
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
import axios from 'axios';
|
|
4
|
+
import type { AxiosError } from 'axios';
|
|
5
|
+
|
|
6
|
+
import snakecaseKeys from 'snakecase-keys';
|
|
7
|
+
import { EmitEvents } from '../../types';
|
|
8
|
+
import type { EnvironmentType, HostToolsType, NetworkProps } from '../../types';
|
|
9
|
+
|
|
10
|
+
import { AxiosOperationError } from './types';
|
|
11
|
+
|
|
12
|
+
// config
|
|
13
|
+
|
|
14
|
+
// const axiosInstance = axios.create({});
|
|
15
|
+
|
|
16
|
+
// ----------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
function getResponse(endPoint: string, response: any) {
|
|
19
|
+
console.log('Axios response', response, typeof response.data);
|
|
20
|
+
|
|
21
|
+
if (response && response.data && typeof response.data === 'object') {
|
|
22
|
+
if (
|
|
23
|
+
response.data.error &&
|
|
24
|
+
response.data.error?.code &&
|
|
25
|
+
response.data.error?.msg !== undefined
|
|
26
|
+
) {
|
|
27
|
+
// Si respondió con codigo de error interno
|
|
28
|
+
return Promise.reject({ ...response.data.error, status: response.status });
|
|
29
|
+
}
|
|
30
|
+
return response.data;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return Promise.reject({
|
|
34
|
+
code: 1,
|
|
35
|
+
msg: `Incorrect endpoint: ${endPoint}`,
|
|
36
|
+
status: response.status,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getError(error: any, hostTools: HostToolsType, checkUnAuthorized = true) {
|
|
41
|
+
const { toast } = hostTools;
|
|
42
|
+
|
|
43
|
+
let err: AxiosOperationError = {
|
|
44
|
+
message: '',
|
|
45
|
+
status: 1,
|
|
46
|
+
code: 0,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
console.log('getError', error);
|
|
50
|
+
if (
|
|
51
|
+
error?.code !== undefined &&
|
|
52
|
+
err.status !== undefined &&
|
|
53
|
+
(error as any).message !== undefined
|
|
54
|
+
) {
|
|
55
|
+
err = { ...err, ...error };
|
|
56
|
+
} else if (error?.response) {
|
|
57
|
+
// The request was made and the server responded with a status code
|
|
58
|
+
// that falls out of the range of 2xx
|
|
59
|
+
if (
|
|
60
|
+
error.response.data &&
|
|
61
|
+
typeof error.response.data === 'object' &&
|
|
62
|
+
error.response.data.error &&
|
|
63
|
+
error.response.data.error?.code &&
|
|
64
|
+
error.response.data.error?.message !== undefined
|
|
65
|
+
) {
|
|
66
|
+
// Si respondió con codigo de error interno
|
|
67
|
+
|
|
68
|
+
err = { ...error.response.data.error, status: error.response.status };
|
|
69
|
+
} else {
|
|
70
|
+
err.message = error.message;
|
|
71
|
+
err.status = error.response.status;
|
|
72
|
+
err.code = 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (checkUnAuthorized && error.response.status === 401) {
|
|
76
|
+
// unauthorized
|
|
77
|
+
hostTools.events_emit(EmitEvents.EMMIT_EVENT_NET_SERVICE_UNAUTHORIZED, {});
|
|
78
|
+
}
|
|
79
|
+
} else if (error?.request) {
|
|
80
|
+
// The request was made but no response was received
|
|
81
|
+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
82
|
+
// http.ClientRequest in node.js
|
|
83
|
+
err.message = `${error?.code} ${error.message}`;
|
|
84
|
+
err.code = -1;
|
|
85
|
+
} else {
|
|
86
|
+
// Something happened in setting up the request that triggered an Error
|
|
87
|
+
err.message = `${error?.code} ${error.message}`;
|
|
88
|
+
err.status = 0;
|
|
89
|
+
err.code = -2;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (checkUnAuthorized) {
|
|
93
|
+
toast(`${err.message} - status: ${err.status} - code: ${err.code}`, { type: 'error' });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return err;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const axiosOperation = async (
|
|
100
|
+
props: NetworkProps,
|
|
101
|
+
enviroment: EnvironmentType,
|
|
102
|
+
hostTools: HostToolsType,
|
|
103
|
+
): Promise<any> => {
|
|
104
|
+
/// /console.log("Axios getWithParms request", endPoint);
|
|
105
|
+
|
|
106
|
+
const {
|
|
107
|
+
method,
|
|
108
|
+
endPoint,
|
|
109
|
+
timeout = 5000,
|
|
110
|
+
parms = {},
|
|
111
|
+
data = {},
|
|
112
|
+
isRemote = true,
|
|
113
|
+
checkUnAuthorized = true,
|
|
114
|
+
} = props;
|
|
115
|
+
|
|
116
|
+
let baseURL: string;
|
|
117
|
+
|
|
118
|
+
if (isRemote) {
|
|
119
|
+
baseURL = enviroment.host_api_remote;
|
|
120
|
+
} else {
|
|
121
|
+
baseURL = enviroment.host_api_local;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
hostTools.startProgress();
|
|
125
|
+
|
|
126
|
+
return axios({
|
|
127
|
+
baseURL,
|
|
128
|
+
withCredentials: isRemote,
|
|
129
|
+
method,
|
|
130
|
+
url: `/${endPoint}`,
|
|
131
|
+
data: snakecaseKeys(data, { deep: true }),
|
|
132
|
+
params: snakecaseKeys(parms, { deep: true }),
|
|
133
|
+
timeout,
|
|
134
|
+
})
|
|
135
|
+
.then((response: any) => getResponse(endPoint, response))
|
|
136
|
+
.catch((error: AxiosError) => Promise.reject(getError(error, hostTools, checkUnAuthorized)))
|
|
137
|
+
.finally(() => {
|
|
138
|
+
hostTools.stopProgress();
|
|
139
|
+
});
|
|
140
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function getPropertyByString(object: Record<string, unknown>, propString: string) {
|
|
2
|
+
let value: any = object;
|
|
3
|
+
|
|
4
|
+
const props = propString.split('.');
|
|
5
|
+
for (let index = 0; index < props.length; index += 1) {
|
|
6
|
+
if (props[index] === undefined) break;
|
|
7
|
+
value = value[props[index]];
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function setLocalStorage<ValueType>(key: string, value: ValueType) {
|
|
2
|
+
try {
|
|
3
|
+
const item: string | null = window.localStorage.getItem(key);
|
|
4
|
+
|
|
5
|
+
let newValue = item !== null ? JSON.parse(item) : {};
|
|
6
|
+
newValue = { ...newValue, ...value };
|
|
7
|
+
|
|
8
|
+
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
9
|
+
} catch (e) {
|
|
10
|
+
console.error(e);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
File without changes
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
|
|
4
|
+
"target": "esNext",
|
|
5
|
+
"useDefineForClassFields": true,
|
|
6
|
+
"lib": ["ESNext"],
|
|
7
|
+
"allowJs": false,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"esModuleInterop": false,
|
|
10
|
+
"downlevelIteration":true,
|
|
11
|
+
"importHelpers": true,
|
|
12
|
+
"experimentalDecorators": true,
|
|
13
|
+
"allowSyntheticDefaultImports": true,
|
|
14
|
+
"strict": true,
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
"module": "ES2020",
|
|
17
|
+
"moduleResolution": "Node",
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"isolatedModules": true,
|
|
20
|
+
"noEmit": true,
|
|
21
|
+
"jsx": "preserve",
|
|
22
|
+
"baseUrl": ".",
|
|
23
|
+
"importsNotUsedAsValues":"remove",
|
|
24
|
+
|
|
25
|
+
"types":["jest"],
|
|
26
|
+
"paths": {
|
|
27
|
+
"@utils": [
|
|
28
|
+
"src/index.ts"
|
|
29
|
+
],
|
|
30
|
+
"@testutils": [
|
|
31
|
+
"src/test/utils"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"include": ["src"],
|
|
36
|
+
"references": [{ "path": "./tsconfig.node.json" }]
|
|
37
|
+
}
|