-
-
Notifications
You must be signed in to change notification settings - Fork 505
/
config_ser.js
31 lines (27 loc) · 1.03 KB
/
config_ser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import serializeJs from "serialize-javascript";
import { Utils } from "@react-awesome-query-builder/core";
import mergeWith from "lodash/mergeWith";
import omit from "lodash/omit";
// It's just a test to show ability to serialize an entire config to string and deserialize back
const mergeCustomizerCleanJSX = (_objValue, srcValue, _key, _object, _source, _stack) => {
const { isDirtyJSX, cleanJSX } = Utils.ConfigUtils;
if (isDirtyJSX(srcValue)) {
return cleanJSX(srcValue);
}
};
export const UNSAFE_serializeConfig = (config) => {
const sanitizedConfig = mergeWith({}, omit(config, ["ctx"]), mergeCustomizerCleanJSX);
const strConfig = serializeJs(sanitizedConfig, {
space: 2,
unsafe: true,
});
if (strConfig.includes("__WEBPACK_IMPORTED_MODULE_")) {
throw new Error("Serialized config should not have references to modules imported from webpack.");
}
return strConfig;
};
export const UNSAFE_deserializeConfig = (strConfig, ctx) => {
let config = eval("("+strConfig+")");
config.ctx = ctx;
return config;
};