@akanjs/constant 0.9.47 → 0.9.49
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/cjs/src/baseGql.js +124 -122
- package/cjs/src/classMeta.js +12 -117
- package/cjs/src/constantInfo.js +145 -0
- package/cjs/src/crystalize.js +89 -0
- package/cjs/src/default.js +67 -0
- package/cjs/src/fieldInfo.js +98 -0
- package/cjs/src/immerify.js +35 -0
- package/cjs/src/index.js +6 -11
- package/cjs/src/purify.js +110 -0
- package/cjs/src/scalar.js +8 -12
- package/cjs/src/serialize.js +116 -0
- package/esm/src/baseGql.js +129 -132
- package/esm/src/classMeta.js +13 -118
- package/esm/src/constantInfo.js +126 -0
- package/esm/src/crystalize.js +78 -0
- package/esm/src/default.js +48 -0
- package/esm/src/fieldInfo.js +88 -0
- package/esm/src/immerify.js +16 -0
- package/esm/src/index.js +6 -7
- package/esm/src/purify.js +99 -0
- package/esm/src/scalar.js +8 -12
- package/esm/src/serialize.js +106 -0
- package/package.json +2 -1
- package/src/baseGql.d.ts +8 -4
- package/src/classMeta.d.ts +5 -21
- package/src/constantInfo.d.ts +69 -0
- package/src/crystalize.d.ts +6 -0
- package/src/default.d.ts +6 -0
- package/src/fieldInfo.d.ts +37 -0
- package/src/immerify.d.ts +2 -0
- package/src/index.d.ts +6 -5
- package/src/purify.d.ts +14 -0
- package/src/scalar.d.ts +16 -46
- package/src/serialize.d.ts +7 -0
- package/src/types.d.ts +3 -23
- package/cjs/src/constantDecorator.js +0 -67
- package/cjs/src/fieldMeta.js +0 -82
- package/cjs/src/filterMeta.js +0 -196
- package/esm/src/constantDecorator.js +0 -48
- package/esm/src/fieldMeta.js +0 -67
- package/esm/src/filterMeta.js +0 -181
- package/src/constantDecorator.d.ts +0 -30
- package/src/fieldMeta.d.ts +0 -7
- package/src/filterMeta.d.ts +0 -61
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var crystalize_exports = {};
|
|
19
|
+
__export(crystalize_exports, {
|
|
20
|
+
makeCrystalize: () => makeCrystalize
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(crystalize_exports);
|
|
23
|
+
var import_base = require("@akanjs/base");
|
|
24
|
+
var import_common = require("@akanjs/common");
|
|
25
|
+
var import__ = require(".");
|
|
26
|
+
class CrystalizeStorage {
|
|
27
|
+
}
|
|
28
|
+
const scalarCrystalizeMap = /* @__PURE__ */ new Map([
|
|
29
|
+
[Date, (value) => (0, import_base.dayjs)(value)],
|
|
30
|
+
[String, (value) => value],
|
|
31
|
+
[import_base.ID, (value) => value],
|
|
32
|
+
[Boolean, (value) => value],
|
|
33
|
+
[import_base.Int, (value) => value],
|
|
34
|
+
[import_base.Float, (value) => value],
|
|
35
|
+
[import_base.JSON, (value) => value]
|
|
36
|
+
]);
|
|
37
|
+
const crystalize = (metadata, value) => {
|
|
38
|
+
if (value === void 0 || value === null)
|
|
39
|
+
return value;
|
|
40
|
+
if (metadata.isArray && Array.isArray(value))
|
|
41
|
+
return value.map((v) => crystalize({ ...metadata, isArray: false }, v));
|
|
42
|
+
if (metadata.isMap) {
|
|
43
|
+
const [valueRef] = (0, import_base.getNonArrayModel)(metadata.of);
|
|
44
|
+
const crystalizeValue = scalarCrystalizeMap.get(valueRef) ?? ((value2) => value2);
|
|
45
|
+
return new Map(
|
|
46
|
+
Object.entries(value).map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, crystalizeValue)])
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
if (metadata.isClass)
|
|
50
|
+
return makeCrystalize(metadata.modelRef)(value, true);
|
|
51
|
+
if (metadata.modelRef === Date)
|
|
52
|
+
return (0, import_base.dayjs)(value);
|
|
53
|
+
return (scalarCrystalizeMap.get(metadata.modelRef) ?? ((value2) => value2))(value);
|
|
54
|
+
};
|
|
55
|
+
const getPredefinedCrystalizeFn = (refName) => {
|
|
56
|
+
const crystalize2 = Reflect.getMetadata(refName, CrystalizeStorage.prototype);
|
|
57
|
+
return crystalize2;
|
|
58
|
+
};
|
|
59
|
+
const setPredefinedCrystalizeFn = (refName, crystalize2) => {
|
|
60
|
+
Reflect.defineMetadata(refName, crystalize2, CrystalizeStorage.prototype);
|
|
61
|
+
};
|
|
62
|
+
const makeCrystalize = (modelRef, option = {}) => {
|
|
63
|
+
const refName = import__.constantInfo.getRefName(modelRef);
|
|
64
|
+
const crystalName = `${import__.constantInfo.isLight(modelRef) ? "Light" : ""}${(0, import_common.capitalize)(refName)}${import__.constantInfo.isInsight(modelRef) ? "Insight" : ""}`;
|
|
65
|
+
const crystalizeFn = getPredefinedCrystalizeFn(crystalName);
|
|
66
|
+
if (crystalizeFn && !option.overwrite && !option.partial?.length)
|
|
67
|
+
return crystalizeFn;
|
|
68
|
+
const fieldMetaMap = (0, import__.getFieldMetaMap)(modelRef);
|
|
69
|
+
const fieldKeys = option.partial?.length ? import__.constantInfo.isScalar(modelRef) ? option.partial : ["id", ...option.partial, "updatedAt"] : [...fieldMetaMap.keys()];
|
|
70
|
+
const metadatas = fieldKeys.map((key) => fieldMetaMap.get(key));
|
|
71
|
+
const fn = (self, isChild) => {
|
|
72
|
+
try {
|
|
73
|
+
const result = Object.assign(new modelRef(), self);
|
|
74
|
+
for (const metadata of metadatas.filter((m) => !!self[m.key])) {
|
|
75
|
+
if (metadata.fieldType === "hidden")
|
|
76
|
+
continue;
|
|
77
|
+
result[metadata.key] = crystalize(metadata, self[metadata.key]);
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (isChild)
|
|
82
|
+
throw new Error(err);
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
if (!option.partial?.length)
|
|
87
|
+
setPredefinedCrystalizeFn(crystalName, fn);
|
|
88
|
+
return fn;
|
|
89
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var default_exports = {};
|
|
19
|
+
__export(default_exports, {
|
|
20
|
+
makeDefault: () => makeDefault
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(default_exports);
|
|
23
|
+
var import_base = require("@akanjs/base");
|
|
24
|
+
var import_common = require("@akanjs/common");
|
|
25
|
+
var import_constantInfo = require("./constantInfo");
|
|
26
|
+
var import_scalar = require("./scalar");
|
|
27
|
+
class DefaultStorage {
|
|
28
|
+
}
|
|
29
|
+
const getPredefinedDefault = (refName) => {
|
|
30
|
+
const defaultData = Reflect.getMetadata(refName, DefaultStorage.prototype);
|
|
31
|
+
return defaultData;
|
|
32
|
+
};
|
|
33
|
+
const setPredefinedDefault = (refName, defaultData) => {
|
|
34
|
+
Reflect.defineMetadata(refName, defaultData, DefaultStorage.prototype);
|
|
35
|
+
};
|
|
36
|
+
const makeDefault = (modelRef, option = {}) => {
|
|
37
|
+
const refName = import_constantInfo.constantInfo.getRefName(modelRef);
|
|
38
|
+
const defaultName = `${(0, import_common.capitalize)(refName)}${import_constantInfo.constantInfo.isInsight(modelRef) ? "Insight" : ""}`;
|
|
39
|
+
const predefinedDefault = getPredefinedDefault(defaultName);
|
|
40
|
+
if (predefinedDefault && !option.overwrite)
|
|
41
|
+
return predefinedDefault;
|
|
42
|
+
if (option.isChild && import_constantInfo.constantInfo.isScalar(modelRef))
|
|
43
|
+
return null;
|
|
44
|
+
const metadatas = (0, import_scalar.getFieldMetas)(modelRef);
|
|
45
|
+
const result = {};
|
|
46
|
+
for (const metadata of metadatas) {
|
|
47
|
+
if (metadata.fieldType === "hidden")
|
|
48
|
+
result[metadata.key] = null;
|
|
49
|
+
else if (metadata.default) {
|
|
50
|
+
if (typeof metadata.default === "function")
|
|
51
|
+
result[metadata.key] = metadata.default();
|
|
52
|
+
else if (metadata.default instanceof import_base.Enum)
|
|
53
|
+
result[metadata.key] = [...metadata.default.values];
|
|
54
|
+
else
|
|
55
|
+
result[metadata.key] = metadata.default;
|
|
56
|
+
} else if (metadata.isArray)
|
|
57
|
+
result[metadata.key] = [];
|
|
58
|
+
else if (metadata.nullable)
|
|
59
|
+
result[metadata.key] = null;
|
|
60
|
+
else if (metadata.isClass)
|
|
61
|
+
result[metadata.key] = metadata.isScalar ? makeDefault(metadata.modelRef) : null;
|
|
62
|
+
else
|
|
63
|
+
result[metadata.key] = import_base.scalarDefaultMap.get(metadata.modelRef);
|
|
64
|
+
}
|
|
65
|
+
setPredefinedDefault(defaultName, result);
|
|
66
|
+
return result;
|
|
67
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var fieldInfo_exports = {};
|
|
19
|
+
__export(fieldInfo_exports, {
|
|
20
|
+
field: () => field,
|
|
21
|
+
resolve: () => resolve
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(fieldInfo_exports);
|
|
24
|
+
var import_base = require("@akanjs/base");
|
|
25
|
+
var import_constantInfo = require("./constantInfo");
|
|
26
|
+
var import_scalar = require("./scalar");
|
|
27
|
+
class FieldInfo {
|
|
28
|
+
value;
|
|
29
|
+
type;
|
|
30
|
+
option;
|
|
31
|
+
explicitType;
|
|
32
|
+
constructor(value, option) {
|
|
33
|
+
this.value = value;
|
|
34
|
+
const [singleValue, arrDepth] = (0, import_base.getNonArrayModel)(value);
|
|
35
|
+
const isEnum = singleValue instanceof import_base.Enum;
|
|
36
|
+
const valueType = isEnum ? (0, import_base.arraiedModel)(singleValue.type, arrDepth) : value;
|
|
37
|
+
this.type = valueType;
|
|
38
|
+
this.option = { ...option, ...isEnum ? { enum: singleValue } : {} };
|
|
39
|
+
}
|
|
40
|
+
optional() {
|
|
41
|
+
return new FieldInfo(this.value, { ...this.option, nullable: true });
|
|
42
|
+
}
|
|
43
|
+
meta(meta) {
|
|
44
|
+
this.option.meta = meta;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
applyFieldMeta(target, key) {
|
|
48
|
+
const [modelRef, arrDepth] = (0, import_base.getNonArrayModel)(this.type);
|
|
49
|
+
const [option, optArrDepth] = (0, import_base.getNonArrayModel)(this.option);
|
|
50
|
+
const isArray = arrDepth > 0;
|
|
51
|
+
const isClass = !(0, import_base.isGqlScalar)(modelRef);
|
|
52
|
+
const isMap = (0, import_base.isGqlMap)(modelRef);
|
|
53
|
+
if (isMap && !option.of)
|
|
54
|
+
throw new Error("Map type must have 'of' option");
|
|
55
|
+
const metadata = {
|
|
56
|
+
nullable: option.nullable ?? (option.default === "" ? true : false),
|
|
57
|
+
ref: option.ref,
|
|
58
|
+
refPath: option.refPath,
|
|
59
|
+
refType: option.refType,
|
|
60
|
+
default: option.default ?? (isArray ? [] : null),
|
|
61
|
+
type: option.type,
|
|
62
|
+
fieldType: option.fieldType ?? "property",
|
|
63
|
+
immutable: option.immutable ?? false,
|
|
64
|
+
min: option.min,
|
|
65
|
+
max: option.max,
|
|
66
|
+
enum: option.enum,
|
|
67
|
+
select: option.select ?? true,
|
|
68
|
+
minlength: option.minlength,
|
|
69
|
+
maxlength: option.maxlength,
|
|
70
|
+
accumulate: option.accumulate,
|
|
71
|
+
example: option.example,
|
|
72
|
+
validate: option.validate,
|
|
73
|
+
key,
|
|
74
|
+
isClass,
|
|
75
|
+
isScalar: import_constantInfo.constantInfo.isScalar(modelRef),
|
|
76
|
+
modelRef,
|
|
77
|
+
arrDepth,
|
|
78
|
+
isArray,
|
|
79
|
+
optArrDepth,
|
|
80
|
+
isMap,
|
|
81
|
+
of: option.of,
|
|
82
|
+
text: option.text,
|
|
83
|
+
meta: option.meta ?? {}
|
|
84
|
+
};
|
|
85
|
+
const metadataMap = (0, import_scalar.getFieldMetaMapOnPrototype)(target.prototype);
|
|
86
|
+
metadataMap.set(key, metadata);
|
|
87
|
+
(0, import_scalar.setFieldMetaMapOnPrototype)(target.prototype, metadataMap);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const field = (value, option = {}) => new FieldInfo(value, { ...option, fieldType: "property" });
|
|
91
|
+
field.secret = (value, option = {}) => new FieldInfo(value, { ...option, fieldType: "hidden", nullable: true });
|
|
92
|
+
field.hidden = (value, option = {}) => new FieldInfo(value, {
|
|
93
|
+
...option,
|
|
94
|
+
fieldType: "hidden",
|
|
95
|
+
select: false,
|
|
96
|
+
nullable: true
|
|
97
|
+
});
|
|
98
|
+
const resolve = (value, option = {}) => new FieldInfo(value, { ...option, fieldType: "resolve" });
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var immerify_exports = {};
|
|
19
|
+
__export(immerify_exports, {
|
|
20
|
+
immerify: () => immerify
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(immerify_exports);
|
|
23
|
+
var import_immer = require("immer");
|
|
24
|
+
var import__ = require(".");
|
|
25
|
+
const immerify = (modelRef, objOrArr) => {
|
|
26
|
+
if (Array.isArray(objOrArr))
|
|
27
|
+
return objOrArr.map((val) => immerify(modelRef, val));
|
|
28
|
+
const fieldMetas = (0, import__.getFieldMetas)(modelRef);
|
|
29
|
+
const immeredObj = Object.assign({}, objOrArr, { [import_immer.immerable]: true });
|
|
30
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
31
|
+
if (fieldMeta.isScalar && fieldMeta.isClass && !!objOrArr[fieldMeta.key])
|
|
32
|
+
immeredObj[fieldMeta.key] = immerify(fieldMeta.modelRef, objOrArr[fieldMeta.key]);
|
|
33
|
+
});
|
|
34
|
+
return immeredObj;
|
|
35
|
+
};
|
package/cjs/src/index.js
CHANGED
|
@@ -2,10 +2,6 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
5
|
var __copyProps = (to, from, except, desc) => {
|
|
10
6
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
7
|
for (let key of __getOwnPropNames(from))
|
|
@@ -17,15 +13,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
13
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
14
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
15
|
var src_exports = {};
|
|
20
|
-
__export(src_exports, {
|
|
21
|
-
Srvs: () => Srvs
|
|
22
|
-
});
|
|
23
16
|
module.exports = __toCommonJS(src_exports);
|
|
24
17
|
__reExport(src_exports, require("./types"), module.exports);
|
|
25
|
-
__reExport(src_exports, require("./
|
|
18
|
+
__reExport(src_exports, require("./fieldInfo"), module.exports);
|
|
26
19
|
__reExport(src_exports, require("./scalar"), module.exports);
|
|
27
|
-
__reExport(src_exports, require("./
|
|
28
|
-
__reExport(src_exports, require("./filterMeta"), module.exports);
|
|
20
|
+
__reExport(src_exports, require("./constantInfo"), module.exports);
|
|
29
21
|
__reExport(src_exports, require("./baseGql"), module.exports);
|
|
30
22
|
__reExport(src_exports, require("./classMeta"), module.exports);
|
|
31
|
-
|
|
23
|
+
__reExport(src_exports, require("./default"), module.exports);
|
|
24
|
+
__reExport(src_exports, require("./crystalize"), module.exports);
|
|
25
|
+
__reExport(src_exports, require("./purify"), module.exports);
|
|
26
|
+
__reExport(src_exports, require("./serialize"), module.exports);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var purify_exports = {};
|
|
19
|
+
__export(purify_exports, {
|
|
20
|
+
makePurify: () => makePurify
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(purify_exports);
|
|
23
|
+
var import_base = require("@akanjs/base");
|
|
24
|
+
var import_common = require("@akanjs/common");
|
|
25
|
+
var import__ = require(".");
|
|
26
|
+
class PurifyStorage {
|
|
27
|
+
}
|
|
28
|
+
const scalarPurifyMap = /* @__PURE__ */ new Map([
|
|
29
|
+
[Date, (value) => (0, import_base.dayjs)(value).toDate()],
|
|
30
|
+
[String, (value) => value],
|
|
31
|
+
[import_base.ID, (value) => value],
|
|
32
|
+
[Boolean, (value) => value],
|
|
33
|
+
[import_base.Int, (value) => value],
|
|
34
|
+
[import_base.Float, (value) => value],
|
|
35
|
+
[import_base.JSON, (value) => value]
|
|
36
|
+
]);
|
|
37
|
+
const getPurifyFn = (modelRef) => {
|
|
38
|
+
const [valueRef] = (0, import_base.getNonArrayModel)(modelRef);
|
|
39
|
+
return scalarPurifyMap.get(valueRef) ?? ((value) => value);
|
|
40
|
+
};
|
|
41
|
+
const purify = (metadata, value, self) => {
|
|
42
|
+
if (metadata.nullable && (value === null || value === void 0 || typeof value === "number" && isNaN(value) || typeof value === "string" && !value.length))
|
|
43
|
+
return null;
|
|
44
|
+
if (metadata.isArray) {
|
|
45
|
+
if (!Array.isArray(value))
|
|
46
|
+
throw new Error(`Invalid Array Value in ${metadata.key} for value ${value}`);
|
|
47
|
+
if (metadata.minlength && value.length < metadata.minlength)
|
|
48
|
+
throw new Error(`Invalid Array Length (Min) in ${metadata.key} for value ${value}`);
|
|
49
|
+
else if (metadata.maxlength && value.length > metadata.maxlength)
|
|
50
|
+
throw new Error(`Invalid Array Length (Max) in ${metadata.key} for value ${value}`);
|
|
51
|
+
else if (metadata.optArrDepth === 0 && metadata.validate && !metadata.validate(value, self))
|
|
52
|
+
throw new Error(`Invalid Array Value (Failed to pass validation) in ${metadata.key} for value ${value}`);
|
|
53
|
+
return value.map((v) => purify({ ...metadata, isArray: false }, v, v));
|
|
54
|
+
}
|
|
55
|
+
if (metadata.isMap && metadata.of) {
|
|
56
|
+
const purifyFn2 = getPurifyFn(metadata.of);
|
|
57
|
+
return Object.fromEntries(
|
|
58
|
+
[...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, purifyFn2)])
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
if (metadata.isClass)
|
|
62
|
+
return makePurify(metadata.modelRef)(value, true);
|
|
63
|
+
if (metadata.modelRef === Date && (0, import_base.dayjs)(value).isBefore((0, import_base.dayjs)(/* @__PURE__ */ new Date("0000"))))
|
|
64
|
+
throw new Error(`Invalid Date Value (Default) in ${metadata.key} for value ${value}`);
|
|
65
|
+
if ([String, import_base.ID].includes(metadata.modelRef) && (value === "" || !value))
|
|
66
|
+
throw new Error(`Invalid String Value (Default) in ${metadata.key} for value ${value}`);
|
|
67
|
+
if (metadata.validate && !metadata.validate(value, self))
|
|
68
|
+
throw new Error(`Invalid Value (Failed to pass validation) / ${value} in ${metadata.key}`);
|
|
69
|
+
if (!metadata.nullable && !value && value !== 0 && value !== false)
|
|
70
|
+
throw new Error(`Invalid Value (Nullable) in ${metadata.key} for value ${value}`);
|
|
71
|
+
const purifyFn = getPurifyFn(metadata.modelRef);
|
|
72
|
+
return purifyFn(value);
|
|
73
|
+
};
|
|
74
|
+
const getPredefinedPurifyFn = (refName) => {
|
|
75
|
+
const purify2 = Reflect.getMetadata(refName, PurifyStorage.prototype);
|
|
76
|
+
return purify2;
|
|
77
|
+
};
|
|
78
|
+
const setPredefinedPurifyFn = (refName, purify2) => {
|
|
79
|
+
Reflect.defineMetadata(refName, purify2, PurifyStorage.prototype);
|
|
80
|
+
};
|
|
81
|
+
const makePurify = (modelRef, option = {}) => {
|
|
82
|
+
const refName = import__.constantInfo.getRefName(modelRef);
|
|
83
|
+
const purifyFn = getPredefinedPurifyFn(refName);
|
|
84
|
+
if (purifyFn && !option.overwrite)
|
|
85
|
+
return purifyFn;
|
|
86
|
+
const metadatas = (0, import__.getFieldMetas)(modelRef);
|
|
87
|
+
const fn = (self, isChild) => {
|
|
88
|
+
try {
|
|
89
|
+
if (isChild && !import__.constantInfo.isScalar(modelRef)) {
|
|
90
|
+
const id = self.id;
|
|
91
|
+
if (!id)
|
|
92
|
+
throw new Error(`Invalid Value (No ID) for id ${refName}`);
|
|
93
|
+
return id;
|
|
94
|
+
}
|
|
95
|
+
const result = {};
|
|
96
|
+
for (const metadata of metadatas) {
|
|
97
|
+
const value = self[metadata.key];
|
|
98
|
+
result[metadata.key] = purify(metadata, value, self);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
if (isChild)
|
|
103
|
+
throw new Error(err);
|
|
104
|
+
import_common.Logger.debug(err);
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
setPredefinedPurifyFn(refName, fn);
|
|
109
|
+
return fn;
|
|
110
|
+
};
|
package/cjs/src/scalar.js
CHANGED
|
@@ -17,8 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
var scalar_exports = {};
|
|
19
19
|
__export(scalar_exports, {
|
|
20
|
-
|
|
21
|
-
getClassMeta: () => getClassMeta,
|
|
20
|
+
fieldPresets: () => fieldPresets,
|
|
22
21
|
getFieldMetaMap: () => getFieldMetaMap,
|
|
23
22
|
getFieldMetaMapOnPrototype: () => getFieldMetaMapOnPrototype,
|
|
24
23
|
getFieldMetas: () => getFieldMetas,
|
|
@@ -32,6 +31,8 @@ __export(scalar_exports, {
|
|
|
32
31
|
module.exports = __toCommonJS(scalar_exports);
|
|
33
32
|
var import_reflect_metadata = require("reflect-metadata");
|
|
34
33
|
var import_base = require("@akanjs/base");
|
|
34
|
+
var import_common = require("@akanjs/common");
|
|
35
|
+
var import_constantInfo = require("./constantInfo");
|
|
35
36
|
const scalarExampleMap = /* @__PURE__ */ new Map([
|
|
36
37
|
[import_base.ID, "1234567890abcdef12345678"],
|
|
37
38
|
[import_base.Int, 0],
|
|
@@ -44,20 +45,15 @@ const scalarExampleMap = /* @__PURE__ */ new Map([
|
|
|
44
45
|
[Map, {}]
|
|
45
46
|
]);
|
|
46
47
|
const getScalarExample = (ref) => scalarExampleMap.get(ref) ?? null;
|
|
47
|
-
const getGqlTypeStr = (ref) => import_base.scalarNameMap.get(ref) ??
|
|
48
|
-
const
|
|
49
|
-
const getClassMeta = (modelRef) => {
|
|
50
|
-
const [target] = (0, import_base.getNonArrayModel)(modelRef);
|
|
51
|
-
const classMeta = Reflect.getMetadata("class", target.prototype);
|
|
52
|
-
if (!classMeta)
|
|
53
|
-
throw new Error(`No ClassMeta for this target ${target.name}`);
|
|
54
|
-
return classMeta;
|
|
55
|
-
};
|
|
48
|
+
const getGqlTypeStr = (ref) => import_base.scalarNameMap.get(ref) ?? `${import_constantInfo.constantInfo.isLight(ref) ? "Light" : ""}${(0, import_common.capitalize)(import_constantInfo.constantInfo.getRefName(ref))}${import_constantInfo.constantInfo.isInsight(ref) ? "Insight" : ""}`;
|
|
49
|
+
const fieldPresets = ["email", "password", "url"];
|
|
56
50
|
const getFieldMetas = (modelRef) => {
|
|
57
51
|
const [target] = (0, import_base.getNonArrayModel)(modelRef);
|
|
58
52
|
const metadataMap = Reflect.getMetadata("fields", target.prototype) ?? /* @__PURE__ */ new Map();
|
|
59
53
|
const keySortMap = { id: -1, createdAt: 1, updatedAt: 2, removedAt: 3 };
|
|
60
|
-
return [...metadataMap.values()].sort(
|
|
54
|
+
return [...metadataMap.values()].sort(
|
|
55
|
+
(a, b) => (keySortMap[a.key] ?? 0) - (keySortMap[b.key] ?? 0)
|
|
56
|
+
);
|
|
61
57
|
};
|
|
62
58
|
const isConstantModel = (modelRef) => {
|
|
63
59
|
return Reflect.getMetadata("class", modelRef.prototype) !== void 0;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var serialize_exports = {};
|
|
19
|
+
__export(serialize_exports, {
|
|
20
|
+
deserializeArg: () => deserializeArg,
|
|
21
|
+
serializeArg: () => serializeArg
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(serialize_exports);
|
|
24
|
+
var import_base = require("@akanjs/base");
|
|
25
|
+
var import__ = require(".");
|
|
26
|
+
const scalarSerializeMap = /* @__PURE__ */ new Map([
|
|
27
|
+
[Date, (value) => (0, import_base.dayjs)(value).toDate()],
|
|
28
|
+
[String, (value) => value],
|
|
29
|
+
[import_base.ID, (value) => value],
|
|
30
|
+
[Boolean, (value) => value],
|
|
31
|
+
[import_base.Int, (value) => value],
|
|
32
|
+
[import_base.Float, (value) => value],
|
|
33
|
+
[import_base.JSON, (value) => value]
|
|
34
|
+
]);
|
|
35
|
+
const getSerializeFn = (inputRef) => {
|
|
36
|
+
const serializeFn = scalarSerializeMap.get(inputRef);
|
|
37
|
+
if (!serializeFn)
|
|
38
|
+
return (value) => value;
|
|
39
|
+
else
|
|
40
|
+
return serializeFn;
|
|
41
|
+
};
|
|
42
|
+
const serializeInput = (value, inputRef, arrDepth) => {
|
|
43
|
+
if (arrDepth && Array.isArray(value))
|
|
44
|
+
return value.map((v) => serializeInput(v, inputRef, arrDepth - 1));
|
|
45
|
+
else if (inputRef.prototype === Map.prototype) {
|
|
46
|
+
const [valueRef] = (0, import_base.getNonArrayModel)(inputRef);
|
|
47
|
+
const serializeFn = getSerializeFn(valueRef);
|
|
48
|
+
return Object.fromEntries(
|
|
49
|
+
[...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, serializeFn)])
|
|
50
|
+
);
|
|
51
|
+
} else if ((0, import_base.isGqlScalar)(inputRef)) {
|
|
52
|
+
const serializeFn = getSerializeFn(inputRef);
|
|
53
|
+
return serializeFn(value);
|
|
54
|
+
}
|
|
55
|
+
if (!import__.constantInfo.isScalar(inputRef))
|
|
56
|
+
return value;
|
|
57
|
+
else
|
|
58
|
+
return Object.fromEntries(
|
|
59
|
+
(0, import__.getFieldMetas)(inputRef).map((fieldMeta) => [
|
|
60
|
+
fieldMeta.key,
|
|
61
|
+
serializeInput(value[fieldMeta.key], fieldMeta.modelRef, fieldMeta.arrDepth)
|
|
62
|
+
])
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
const serializeArg = (argRef, arrDepth, value, { nullable = false }) => {
|
|
66
|
+
if (nullable && (value === null || value === void 0))
|
|
67
|
+
return null;
|
|
68
|
+
else if (!nullable && (value === null || value === void 0))
|
|
69
|
+
throw new Error(`Invalid Value (Nullable) in ${argRef} for value ${value}`);
|
|
70
|
+
return serializeInput(value, argRef, arrDepth);
|
|
71
|
+
};
|
|
72
|
+
const scalarDeserializeMap = /* @__PURE__ */ new Map([
|
|
73
|
+
[Date, (value) => (0, import_base.dayjs)(value)],
|
|
74
|
+
[String, (value) => value],
|
|
75
|
+
[import_base.ID, (value) => value],
|
|
76
|
+
[Boolean, (value) => value],
|
|
77
|
+
[import_base.Int, (value) => value],
|
|
78
|
+
[import_base.Float, (value) => value],
|
|
79
|
+
[import_base.JSON, (value) => value]
|
|
80
|
+
]);
|
|
81
|
+
const getDeserializeFn = (inputRef) => {
|
|
82
|
+
const deserializeFn = scalarDeserializeMap.get(inputRef);
|
|
83
|
+
if (!deserializeFn)
|
|
84
|
+
return (value) => value;
|
|
85
|
+
return deserializeFn;
|
|
86
|
+
};
|
|
87
|
+
const deserializeInput = (value, inputRef, arrDepth) => {
|
|
88
|
+
if (arrDepth && Array.isArray(value))
|
|
89
|
+
return value.map((v) => deserializeInput(v, inputRef, arrDepth - 1));
|
|
90
|
+
else if (inputRef.prototype === Map.prototype) {
|
|
91
|
+
const [valueRef] = (0, import_base.getNonArrayModel)(inputRef);
|
|
92
|
+
const deserializeFn = getDeserializeFn(valueRef);
|
|
93
|
+
return Object.fromEntries(
|
|
94
|
+
[...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, deserializeFn)])
|
|
95
|
+
);
|
|
96
|
+
} else if ((0, import_base.isGqlScalar)(inputRef)) {
|
|
97
|
+
const deserializeFn = getDeserializeFn(inputRef);
|
|
98
|
+
return deserializeFn(value);
|
|
99
|
+
}
|
|
100
|
+
if (!import__.constantInfo.isScalar(inputRef))
|
|
101
|
+
return value;
|
|
102
|
+
else
|
|
103
|
+
return Object.fromEntries(
|
|
104
|
+
(0, import__.getFieldMetas)(inputRef).map((fieldMeta) => [
|
|
105
|
+
fieldMeta.key,
|
|
106
|
+
deserializeInput(value[fieldMeta.key], fieldMeta.modelRef, fieldMeta.arrDepth)
|
|
107
|
+
])
|
|
108
|
+
);
|
|
109
|
+
};
|
|
110
|
+
const deserializeArg = (argRef, arrDepth, value, { nullable = false }) => {
|
|
111
|
+
if (nullable && (value === null || value === void 0))
|
|
112
|
+
return null;
|
|
113
|
+
else if (!nullable && (value === null || value === void 0))
|
|
114
|
+
throw new Error(`Invalid Value (Nullable) in ${argRef} for value ${value}`);
|
|
115
|
+
return deserializeInput(value, argRef, arrDepth);
|
|
116
|
+
};
|