dry-ux 1.38.0 → 1.39.0

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,21 @@
1
+ import { IDajaxiceProxy, MethodArgs } from "./Proxy.interface";
2
+ export declare class DajaxiceCall<TModule, TReturn> {
3
+ readonly module: string;
4
+ readonly method: string;
5
+ readonly config: IDajaxiceProxy<TModule>;
6
+ readonly methodArgs: MethodArgs;
7
+ private readonly loader?;
8
+ private readonly deferred;
9
+ private readonly cacheId;
10
+ private readonly defaultCacheDuration;
11
+ constructor(module: string, method: string, config: IDajaxiceProxy<TModule>, methodArgs: MethodArgs);
12
+ execute(): Promise<TReturn>;
13
+ private hideLoader;
14
+ private showLoader;
15
+ private handleError;
16
+ private handleApiCallSuccess;
17
+ private handleResult;
18
+ private callApi;
19
+ private checkCache;
20
+ private storeCache;
21
+ }
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DajaxiceCall = void 0;
4
+ const utilities_1 = require("../helpers/utilities");
5
+ const Loader_1 = require("../ui-utils/Loader");
6
+ class DajaxiceCall {
7
+ constructor(module, method, config, methodArgs) {
8
+ this.module = module;
9
+ this.method = method;
10
+ this.config = config;
11
+ this.methodArgs = methodArgs;
12
+ this.deferred = new utilities_1.Deferred();
13
+ this.defaultCacheDuration = 15;
14
+ this.hideLoader = () => {
15
+ var _a;
16
+ try {
17
+ (_a = this.loader) === null || _a === void 0 ? void 0 : _a.hide();
18
+ }
19
+ catch (e) { }
20
+ };
21
+ this.showLoader = () => {
22
+ var _a;
23
+ try {
24
+ (_a = this.loader) === null || _a === void 0 ? void 0 : _a.show();
25
+ }
26
+ catch (e) { }
27
+ };
28
+ this.handleError = (e) => {
29
+ var _a, _b, _c;
30
+ try {
31
+ !((_a = this.methodArgs.skip) === null || _a === void 0 ? void 0 : _a.errorHandler) && ((_c = (_b = this.config).onError) === null || _c === void 0 ? void 0 : _c.call(_b, e));
32
+ }
33
+ catch (e) { }
34
+ this.hideLoader();
35
+ this.deferred.reject(e);
36
+ };
37
+ this.handleApiCallSuccess = (result) => {
38
+ this.handleResult(result);
39
+ const { cache = false } = this.methodArgs;
40
+ if (cache) {
41
+ this.storeCache(result);
42
+ }
43
+ };
44
+ this.handleResult = (result) => {
45
+ this.hideLoader();
46
+ this.deferred.resolve(result);
47
+ };
48
+ this.callApi = () => {
49
+ const api = window["Dajaxice"][this.module][this.method];
50
+ const { args, cache = false } = this.methodArgs;
51
+ if (cache) {
52
+ const { exists, data } = this.checkCache();
53
+ if (exists) {
54
+ return this.handleResult(data);
55
+ }
56
+ }
57
+ return api(this.handleApiCallSuccess, args, {
58
+ error_callback: this.handleError,
59
+ });
60
+ };
61
+ this.checkCache = () => {
62
+ let exists = false, data = null;
63
+ try {
64
+ if (utilities_1.StorageUtils.isStorageAvailable() && sessionStorage.getItem(this.cacheId)) {
65
+ const { cacheDuration = this.defaultCacheDuration } = this.methodArgs;
66
+ const cacheData = JSON.parse(sessionStorage.getItem(this.cacheId));
67
+ const timeDiff = Math.floor((new Date().getTime() - new Date(cacheData.storeTime).getTime()) / 1000 / 60);
68
+ if (timeDiff <= cacheDuration) {
69
+ exists = true;
70
+ data = cacheData.data;
71
+ }
72
+ }
73
+ }
74
+ catch (e) { }
75
+ return { exists, data };
76
+ };
77
+ this.storeCache = (data) => {
78
+ try {
79
+ if (utilities_1.StorageUtils.isStorageAvailable()) {
80
+ const cacheData = JSON.stringify({ storeTime: new Date(), data: data });
81
+ sessionStorage.setItem(this.cacheId, cacheData);
82
+ }
83
+ }
84
+ catch (e) { }
85
+ };
86
+ const { skip, loader: showLoader } = this.methodArgs;
87
+ this.loader = (this.config.loader || showLoader) && !(skip === null || skip === void 0 ? void 0 : skip.loader) ? Loader_1.Loader.getInstance() : undefined;
88
+ this.cacheId =
89
+ "dajaxice-cache-" + (0, utilities_1.toHashCode)(JSON.stringify({ api: `${module}.${method}`, args: this.methodArgs.args }));
90
+ }
91
+ execute() {
92
+ const { skip } = this.methodArgs;
93
+ const { authCheck } = this.config;
94
+ const methodName = window["Dajaxice"][this.module][this.method];
95
+ if (methodName) {
96
+ this.showLoader();
97
+ if ((skip === null || skip === void 0 ? void 0 : skip.authCheck) || !authCheck) {
98
+ this.callApi();
99
+ }
100
+ else {
101
+ (0, utilities_1.fnWithAuthCheck)(this.callApi, authCheck.url, authCheck.redirectUrl).catch(this.handleError);
102
+ }
103
+ }
104
+ else {
105
+ this.deferred.reject(new Error(`The method ${this.method} does not exist in the module ${this.module}`));
106
+ }
107
+ return this.deferred.promise;
108
+ }
109
+ }
110
+ exports.DajaxiceCall = DajaxiceCall;
@@ -0,0 +1,7 @@
1
+ import { IDajaxiceProxy } from "./Proxy.interface";
2
+ /**
3
+ * This function is used to create a type safe proxy for the Dajaxice functions.
4
+ * @returns A type safe proxy for calling Dajaxice functions.
5
+ * @param config
6
+ */
7
+ export declare const DajaxiceProxy: <TModule>(config: IDajaxiceProxy<TModule>) => TModule;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DajaxiceProxy = void 0;
4
+ const DajaxiceCall_1 = require("./DajaxiceCall");
5
+ /**
6
+ * This function is used to create a type safe proxy for the Dajaxice functions.
7
+ * @returns A type safe proxy for calling Dajaxice functions.
8
+ * @param config
9
+ */
10
+ const DajaxiceProxy = (config) => new Proxy(config.modules, {
11
+ get(target, module) {
12
+ return new Proxy({}, {
13
+ get(target, method) {
14
+ return (methodArgs = {}) => new DajaxiceCall_1.DajaxiceCall(module, method, config, methodArgs).execute();
15
+ },
16
+ });
17
+ },
18
+ });
19
+ exports.DajaxiceProxy = DajaxiceProxy;
@@ -1,4 +1,4 @@
1
- import { DajaxiceFn } from "./DajaxiceProxy";
1
+ import { DajaxiceFn } from "./Proxy.interface";
2
2
  export declare const modules: DajaxiceModules;
3
3
  export declare type DajaxiceModules = {
4
4
  employee?: {
@@ -6,7 +6,7 @@ exports.modules = {};
6
6
  const Api = (0, DajaxiceProxy_1.DajaxiceProxy)({
7
7
  modules: exports.modules,
8
8
  });
9
- Api.employee.getDetails({ args: { id: 1 } }).then(res => {
9
+ Api.employee.getDetails({ args: { id: 1 }, loader: true }).then(res => {
10
10
  console.log(res.name, res.id);
11
11
  });
12
12
  Api.organization.setName({ args: { name: "test" } }).then(res => {
@@ -1,4 +1,4 @@
1
- declare type Args<T = void> = {
1
+ export declare type Args<T = void> = {
2
2
  args: T;
3
3
  /**
4
4
  * If true, it will show a loader when a Dajaxice function is called. Defaults to false.
@@ -29,15 +29,8 @@ declare type Args<T = void> = {
29
29
  };
30
30
  declare type ArgType<T> = T extends void ? Omit<Args, "args"> | void : Args<T>;
31
31
  export declare type DajaxiceFn<TArgs = void> = <TResult = any>(args: ArgType<TArgs>) => Promise<TResult>;
32
- /**
33
- * This function is used to create a type safe proxy for the Dajaxice functions.
34
- * @param modules: The modules object from the Dajaxice generated file.
35
- * @param authCheck The auth check object. If undefined, it will not check for authentication.
36
- * @param onError: Default handler for error
37
- * @param loader: If true, it will show a loader when a Dajaxice function is called.
38
- * @returns A type safe proxy for calling Dajaxice functions.
39
- */
40
- export declare const DajaxiceProxy: <TModule>({ modules, authCheck, onError, loader, }: {
32
+ export declare type MethodArgs = Partial<Args<any>>;
33
+ export interface IDajaxiceProxy<TModule> {
41
34
  modules: TModule;
42
35
  authCheck?: {
43
36
  url: string;
@@ -45,5 +38,5 @@ export declare const DajaxiceProxy: <TModule>({ modules, authCheck, onError, loa
45
38
  };
46
39
  onError?: (error: any) => void;
47
40
  loader?: boolean;
48
- }) => TModule;
41
+ }
49
42
  export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -41,4 +41,15 @@ export declare const StorageUtils: {
41
41
  };
42
42
  export declare const toHashCode: (input: string) => number;
43
43
  export declare const insertUrlParam: (key: string, value: any) => void;
44
+ export declare const insertUrlParams: (params: {
45
+ [key: string]: any;
46
+ }) => void;
44
47
  export declare const getUrlParams: <T>() => T;
48
+ export declare class Deferred<T> {
49
+ _resolve: (result: T) => void;
50
+ _reject: (error: any) => void;
51
+ readonly promise: Promise<T>;
52
+ constructor();
53
+ get resolve(): (result: T) => void;
54
+ get reject(): (error: any) => void;
55
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getUrlParams = exports.insertUrlParam = exports.toHashCode = exports.StorageUtils = exports.fnWithAuthCheck = exports.formatDollar = exports.useCountdown = exports.importStyleSheet = exports.importScript = exports.preventDefault = void 0;
3
+ exports.Deferred = exports.getUrlParams = exports.insertUrlParams = exports.insertUrlParam = exports.toHashCode = exports.StorageUtils = exports.fnWithAuthCheck = exports.formatDollar = exports.useCountdown = exports.importStyleSheet = exports.importScript = exports.preventDefault = void 0;
4
4
  const React = require("react");
5
5
  /**
6
6
  * Returns a function that will call the given handler and prevent the default event behavior.
@@ -143,6 +143,8 @@ const insertUrlParam = (key, value) => {
143
143
  }
144
144
  };
145
145
  exports.insertUrlParam = insertUrlParam;
146
+ const insertUrlParams = (params) => Object.keys(params).forEach(key => (0, exports.insertUrlParam)(key, params[key]));
147
+ exports.insertUrlParams = insertUrlParams;
146
148
  const getUrlParams = () => {
147
149
  const searchParams = new URLSearchParams(window.location.search);
148
150
  const params = {};
@@ -152,3 +154,18 @@ const getUrlParams = () => {
152
154
  return params;
153
155
  };
154
156
  exports.getUrlParams = getUrlParams;
157
+ class Deferred {
158
+ constructor() {
159
+ this.promise = new Promise((resolve, reject) => {
160
+ this._resolve = resolve;
161
+ this._reject = reject;
162
+ });
163
+ }
164
+ get resolve() {
165
+ return this._resolve;
166
+ }
167
+ get reject() {
168
+ return this._reject;
169
+ }
170
+ }
171
+ exports.Deferred = Deferred;
package/dist/index.d.ts CHANGED
@@ -6,7 +6,8 @@ export { DryUXProvider } from "./provider";
6
6
  export * from "./helpers/utilities";
7
7
  export { classSet } from "./helpers/classSet";
8
8
  export { flatten, unflatten } from "./helpers/flat";
9
- export { DajaxiceFn, DajaxiceProxy } from "./helpers/DajaxiceProxy";
9
+ export { DajaxiceProxy } from "./dajaxice/DajaxiceProxy";
10
+ export { DajaxiceFn } from "./dajaxice/Proxy.interface";
10
11
  export { ErrorBoundary } from "./error/ErrorBoundary";
11
12
  export { ErrorScreen } from "./error/ErrorScreen";
12
13
  export { Loader } from "./ui-utils/Loader";
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ Object.defineProperty(exports, "classSet", { enumerable: true, get: function ()
31
31
  var flat_1 = require("./helpers/flat");
32
32
  Object.defineProperty(exports, "flatten", { enumerable: true, get: function () { return flat_1.flatten; } });
33
33
  Object.defineProperty(exports, "unflatten", { enumerable: true, get: function () { return flat_1.unflatten; } });
34
- var DajaxiceProxy_1 = require("./helpers/DajaxiceProxy");
34
+ var DajaxiceProxy_1 = require("./dajaxice/DajaxiceProxy");
35
35
  Object.defineProperty(exports, "DajaxiceProxy", { enumerable: true, get: function () { return DajaxiceProxy_1.DajaxiceProxy; } });
36
36
  var ErrorBoundary_1 = require("./error/ErrorBoundary");
37
37
  Object.defineProperty(exports, "ErrorBoundary", { enumerable: true, get: function () { return ErrorBoundary_1.ErrorBoundary; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dry-ux",
3
- "version": "1.38.0",
3
+ "version": "1.39.0",
4
4
  "description": "",
5
5
  "main": "dist/index",
6
6
  "scripts": {
@@ -1,108 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DajaxiceProxy = void 0;
4
- const utilities_1 = require("./utilities");
5
- const Loader_1 = require("../ui-utils/Loader");
6
- /**
7
- * This function is used to create a type safe proxy for the Dajaxice functions.
8
- * @param modules: The modules object from the Dajaxice generated file.
9
- * @param authCheck The auth check object. If undefined, it will not check for authentication.
10
- * @param onError: Default handler for error
11
- * @param loader: If true, it will show a loader when a Dajaxice function is called.
12
- * @returns A type safe proxy for calling Dajaxice functions.
13
- */
14
- const DajaxiceProxy = ({ modules, authCheck, onError, loader: showLoaderGlobal, }) => new Proxy(modules, {
15
- get(target, module) {
16
- return new Proxy({}, {
17
- get(target, method) {
18
- return function ({ args, skip, loader: showLoader, cache = false, cacheDuration = 15, } = {}) {
19
- return new Promise((resolve, reject) => {
20
- const methodName = window["Dajaxice"][module][method];
21
- const api = `${module}.${method}`;
22
- const loader = (showLoaderGlobal || showLoader) && !(skip === null || skip === void 0 ? void 0 : skip.loader)
23
- ? Loader_1.Loader.getInstance()
24
- : undefined;
25
- const hideLoader = () => {
26
- try {
27
- loader === null || loader === void 0 ? void 0 : loader.hide();
28
- }
29
- catch (e) { }
30
- };
31
- if (methodName) {
32
- const handleError = (e) => {
33
- try {
34
- !(skip === null || skip === void 0 ? void 0 : skip.errorHandler) && (onError === null || onError === void 0 ? void 0 : onError(e));
35
- }
36
- catch (e) { }
37
- hideLoader();
38
- reject(e);
39
- };
40
- const handleSuccess = (result) => {
41
- handleResult(result);
42
- if (cache) {
43
- storeCache(api, args, result);
44
- }
45
- };
46
- const handleResult = (result) => {
47
- hideLoader();
48
- resolve(result);
49
- };
50
- const fn = () => {
51
- if (cache) {
52
- const cached = checkCache(api, args, cacheDuration);
53
- if (cached.exists) {
54
- handleResult(cached.data);
55
- return;
56
- }
57
- }
58
- return methodName(handleSuccess, args, {
59
- error_callback: handleError,
60
- });
61
- };
62
- try {
63
- loader === null || loader === void 0 ? void 0 : loader.show();
64
- }
65
- catch (e) { }
66
- if ((skip === null || skip === void 0 ? void 0 : skip.authCheck) || !authCheck) {
67
- fn();
68
- }
69
- else {
70
- (0, utilities_1.fnWithAuthCheck)(fn, authCheck.url, authCheck.redirectUrl).catch(handleError);
71
- }
72
- }
73
- else {
74
- reject(new Error(`The method ${method} does not exist in the module ${module}`));
75
- }
76
- });
77
- };
78
- },
79
- });
80
- },
81
- });
82
- exports.DajaxiceProxy = DajaxiceProxy;
83
- const getCacheId = (api, args) => {
84
- return "dajaxice-cache-" + (0, utilities_1.toHashCode)(JSON.stringify({ api, args }));
85
- };
86
- const checkCache = (api, args, cacheDuration) => {
87
- const cacheId = getCacheId(api, args);
88
- let exists = false, data = null;
89
- if (utilities_1.StorageUtils.isStorageAvailable() && sessionStorage.getItem(cacheId)) {
90
- const cacheData = JSON.parse(sessionStorage.getItem(cacheId));
91
- const timeDiff = Math.floor((new Date().getTime() - new Date(cacheData.storeTime).getTime()) / 1000 / 60);
92
- if (timeDiff <= cacheDuration) {
93
- exists = true;
94
- data = cacheData.data;
95
- }
96
- }
97
- return { exists, data };
98
- };
99
- const storeCache = (api, args, data) => {
100
- const cacheId = getCacheId(api, args);
101
- if (utilities_1.StorageUtils.isStorageAvailable()) {
102
- try {
103
- const cacheData = JSON.stringify({ storeTime: new Date(), data: data });
104
- sessionStorage.setItem(cacheId, cacheData);
105
- }
106
- catch (e) { }
107
- }
108
- };