@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.
Files changed (44) hide show
  1. package/cjs/src/baseGql.js +124 -122
  2. package/cjs/src/classMeta.js +12 -117
  3. package/cjs/src/constantInfo.js +145 -0
  4. package/cjs/src/crystalize.js +89 -0
  5. package/cjs/src/default.js +67 -0
  6. package/cjs/src/fieldInfo.js +98 -0
  7. package/cjs/src/immerify.js +35 -0
  8. package/cjs/src/index.js +6 -11
  9. package/cjs/src/purify.js +110 -0
  10. package/cjs/src/scalar.js +8 -12
  11. package/cjs/src/serialize.js +116 -0
  12. package/esm/src/baseGql.js +129 -132
  13. package/esm/src/classMeta.js +13 -118
  14. package/esm/src/constantInfo.js +126 -0
  15. package/esm/src/crystalize.js +78 -0
  16. package/esm/src/default.js +48 -0
  17. package/esm/src/fieldInfo.js +88 -0
  18. package/esm/src/immerify.js +16 -0
  19. package/esm/src/index.js +6 -7
  20. package/esm/src/purify.js +99 -0
  21. package/esm/src/scalar.js +8 -12
  22. package/esm/src/serialize.js +106 -0
  23. package/package.json +2 -1
  24. package/src/baseGql.d.ts +8 -4
  25. package/src/classMeta.d.ts +5 -21
  26. package/src/constantInfo.d.ts +69 -0
  27. package/src/crystalize.d.ts +6 -0
  28. package/src/default.d.ts +6 -0
  29. package/src/fieldInfo.d.ts +37 -0
  30. package/src/immerify.d.ts +2 -0
  31. package/src/index.d.ts +6 -5
  32. package/src/purify.d.ts +14 -0
  33. package/src/scalar.d.ts +16 -46
  34. package/src/serialize.d.ts +7 -0
  35. package/src/types.d.ts +3 -23
  36. package/cjs/src/constantDecorator.js +0 -67
  37. package/cjs/src/fieldMeta.js +0 -82
  38. package/cjs/src/filterMeta.js +0 -196
  39. package/esm/src/constantDecorator.js +0 -48
  40. package/esm/src/fieldMeta.js +0 -67
  41. package/esm/src/filterMeta.js +0 -181
  42. package/src/constantDecorator.d.ts +0 -30
  43. package/src/fieldMeta.d.ts +0 -7
  44. package/src/filterMeta.d.ts +0 -61
@@ -0,0 +1,48 @@
1
+ import { Enum, scalarDefaultMap } from "@akanjs/base";
2
+ import { capitalize } from "@akanjs/common";
3
+ import { constantInfo } from "./constantInfo";
4
+ import { getFieldMetas } from "./scalar";
5
+ class DefaultStorage {
6
+ }
7
+ const getPredefinedDefault = (refName) => {
8
+ const defaultData = Reflect.getMetadata(refName, DefaultStorage.prototype);
9
+ return defaultData;
10
+ };
11
+ const setPredefinedDefault = (refName, defaultData) => {
12
+ Reflect.defineMetadata(refName, defaultData, DefaultStorage.prototype);
13
+ };
14
+ const makeDefault = (modelRef, option = {}) => {
15
+ const refName = constantInfo.getRefName(modelRef);
16
+ const defaultName = `${capitalize(refName)}${constantInfo.isInsight(modelRef) ? "Insight" : ""}`;
17
+ const predefinedDefault = getPredefinedDefault(defaultName);
18
+ if (predefinedDefault && !option.overwrite)
19
+ return predefinedDefault;
20
+ if (option.isChild && constantInfo.isScalar(modelRef))
21
+ return null;
22
+ const metadatas = getFieldMetas(modelRef);
23
+ const result = {};
24
+ for (const metadata of metadatas) {
25
+ if (metadata.fieldType === "hidden")
26
+ result[metadata.key] = null;
27
+ else if (metadata.default) {
28
+ if (typeof metadata.default === "function")
29
+ result[metadata.key] = metadata.default();
30
+ else if (metadata.default instanceof Enum)
31
+ result[metadata.key] = [...metadata.default.values];
32
+ else
33
+ result[metadata.key] = metadata.default;
34
+ } else if (metadata.isArray)
35
+ result[metadata.key] = [];
36
+ else if (metadata.nullable)
37
+ result[metadata.key] = null;
38
+ else if (metadata.isClass)
39
+ result[metadata.key] = metadata.isScalar ? makeDefault(metadata.modelRef) : null;
40
+ else
41
+ result[metadata.key] = scalarDefaultMap.get(metadata.modelRef);
42
+ }
43
+ setPredefinedDefault(defaultName, result);
44
+ return result;
45
+ };
46
+ export {
47
+ makeDefault
48
+ };
@@ -0,0 +1,88 @@
1
+ import {
2
+ arraiedModel,
3
+ Enum,
4
+ getNonArrayModel,
5
+ isGqlMap,
6
+ isGqlScalar
7
+ } from "@akanjs/base";
8
+ import { constantInfo } from "./constantInfo";
9
+ import {
10
+ getFieldMetaMapOnPrototype,
11
+ setFieldMetaMapOnPrototype
12
+ } from "./scalar";
13
+ class FieldInfo {
14
+ value;
15
+ type;
16
+ option;
17
+ explicitType;
18
+ constructor(value, option) {
19
+ this.value = value;
20
+ const [singleValue, arrDepth] = getNonArrayModel(value);
21
+ const isEnum = singleValue instanceof Enum;
22
+ const valueType = isEnum ? arraiedModel(singleValue.type, arrDepth) : value;
23
+ this.type = valueType;
24
+ this.option = { ...option, ...isEnum ? { enum: singleValue } : {} };
25
+ }
26
+ optional() {
27
+ return new FieldInfo(this.value, { ...this.option, nullable: true });
28
+ }
29
+ meta(meta) {
30
+ this.option.meta = meta;
31
+ return this;
32
+ }
33
+ applyFieldMeta(target, key) {
34
+ const [modelRef, arrDepth] = getNonArrayModel(this.type);
35
+ const [option, optArrDepth] = getNonArrayModel(this.option);
36
+ const isArray = arrDepth > 0;
37
+ const isClass = !isGqlScalar(modelRef);
38
+ const isMap = isGqlMap(modelRef);
39
+ if (isMap && !option.of)
40
+ throw new Error("Map type must have 'of' option");
41
+ const metadata = {
42
+ nullable: option.nullable ?? (option.default === "" ? true : false),
43
+ ref: option.ref,
44
+ refPath: option.refPath,
45
+ refType: option.refType,
46
+ default: option.default ?? (isArray ? [] : null),
47
+ type: option.type,
48
+ fieldType: option.fieldType ?? "property",
49
+ immutable: option.immutable ?? false,
50
+ min: option.min,
51
+ max: option.max,
52
+ enum: option.enum,
53
+ select: option.select ?? true,
54
+ minlength: option.minlength,
55
+ maxlength: option.maxlength,
56
+ accumulate: option.accumulate,
57
+ example: option.example,
58
+ validate: option.validate,
59
+ key,
60
+ isClass,
61
+ isScalar: constantInfo.isScalar(modelRef),
62
+ modelRef,
63
+ arrDepth,
64
+ isArray,
65
+ optArrDepth,
66
+ isMap,
67
+ of: option.of,
68
+ text: option.text,
69
+ meta: option.meta ?? {}
70
+ };
71
+ const metadataMap = getFieldMetaMapOnPrototype(target.prototype);
72
+ metadataMap.set(key, metadata);
73
+ setFieldMetaMapOnPrototype(target.prototype, metadataMap);
74
+ }
75
+ }
76
+ const field = (value, option = {}) => new FieldInfo(value, { ...option, fieldType: "property" });
77
+ field.secret = (value, option = {}) => new FieldInfo(value, { ...option, fieldType: "hidden", nullable: true });
78
+ field.hidden = (value, option = {}) => new FieldInfo(value, {
79
+ ...option,
80
+ fieldType: "hidden",
81
+ select: false,
82
+ nullable: true
83
+ });
84
+ const resolve = (value, option = {}) => new FieldInfo(value, { ...option, fieldType: "resolve" });
85
+ export {
86
+ field,
87
+ resolve
88
+ };
@@ -0,0 +1,16 @@
1
+ import { immerable } from "immer";
2
+ import { getFieldMetas } from ".";
3
+ const immerify = (modelRef, objOrArr) => {
4
+ if (Array.isArray(objOrArr))
5
+ return objOrArr.map((val) => immerify(modelRef, val));
6
+ const fieldMetas = getFieldMetas(modelRef);
7
+ const immeredObj = Object.assign({}, objOrArr, { [immerable]: true });
8
+ fieldMetas.forEach((fieldMeta) => {
9
+ if (fieldMeta.isScalar && fieldMeta.isClass && !!objOrArr[fieldMeta.key])
10
+ immeredObj[fieldMeta.key] = immerify(fieldMeta.modelRef, objOrArr[fieldMeta.key]);
11
+ });
12
+ return immeredObj;
13
+ };
14
+ export {
15
+ immerify
16
+ };
package/esm/src/index.js CHANGED
@@ -1,11 +1,10 @@
1
- const Srvs = {};
2
1
  export * from "./types";
3
- export * from "./fieldMeta";
2
+ export * from "./fieldInfo";
4
3
  export * from "./scalar";
5
- export * from "./constantDecorator";
6
- export * from "./filterMeta";
4
+ export * from "./constantInfo";
7
5
  export * from "./baseGql";
8
6
  export * from "./classMeta";
9
- export {
10
- Srvs
11
- };
7
+ export * from "./default";
8
+ export * from "./crystalize";
9
+ export * from "./purify";
10
+ export * from "./serialize";
@@ -0,0 +1,99 @@
1
+ import {
2
+ applyFnToArrayObjects,
3
+ dayjs,
4
+ Float,
5
+ getNonArrayModel,
6
+ ID,
7
+ Int,
8
+ JSON as GqlJSON
9
+ } from "@akanjs/base";
10
+ import { Logger } from "@akanjs/common";
11
+ import { constantInfo, getFieldMetas } from ".";
12
+ class PurifyStorage {
13
+ }
14
+ const scalarPurifyMap = /* @__PURE__ */ new Map([
15
+ [Date, (value) => dayjs(value).toDate()],
16
+ [String, (value) => value],
17
+ [ID, (value) => value],
18
+ [Boolean, (value) => value],
19
+ [Int, (value) => value],
20
+ [Float, (value) => value],
21
+ [GqlJSON, (value) => value]
22
+ ]);
23
+ const getPurifyFn = (modelRef) => {
24
+ const [valueRef] = getNonArrayModel(modelRef);
25
+ return scalarPurifyMap.get(valueRef) ?? ((value) => value);
26
+ };
27
+ const purify = (metadata, value, self) => {
28
+ if (metadata.nullable && (value === null || value === void 0 || typeof value === "number" && isNaN(value) || typeof value === "string" && !value.length))
29
+ return null;
30
+ if (metadata.isArray) {
31
+ if (!Array.isArray(value))
32
+ throw new Error(`Invalid Array Value in ${metadata.key} for value ${value}`);
33
+ if (metadata.minlength && value.length < metadata.minlength)
34
+ throw new Error(`Invalid Array Length (Min) in ${metadata.key} for value ${value}`);
35
+ else if (metadata.maxlength && value.length > metadata.maxlength)
36
+ throw new Error(`Invalid Array Length (Max) in ${metadata.key} for value ${value}`);
37
+ else if (metadata.optArrDepth === 0 && metadata.validate && !metadata.validate(value, self))
38
+ throw new Error(`Invalid Array Value (Failed to pass validation) in ${metadata.key} for value ${value}`);
39
+ return value.map((v) => purify({ ...metadata, isArray: false }, v, v));
40
+ }
41
+ if (metadata.isMap && metadata.of) {
42
+ const purifyFn2 = getPurifyFn(metadata.of);
43
+ return Object.fromEntries(
44
+ [...value.entries()].map(([key, val]) => [key, applyFnToArrayObjects(val, purifyFn2)])
45
+ );
46
+ }
47
+ if (metadata.isClass)
48
+ return makePurify(metadata.modelRef)(value, true);
49
+ if (metadata.modelRef === Date && dayjs(value).isBefore(dayjs(/* @__PURE__ */ new Date("0000"))))
50
+ throw new Error(`Invalid Date Value (Default) in ${metadata.key} for value ${value}`);
51
+ if ([String, ID].includes(metadata.modelRef) && (value === "" || !value))
52
+ throw new Error(`Invalid String Value (Default) in ${metadata.key} for value ${value}`);
53
+ if (metadata.validate && !metadata.validate(value, self))
54
+ throw new Error(`Invalid Value (Failed to pass validation) / ${value} in ${metadata.key}`);
55
+ if (!metadata.nullable && !value && value !== 0 && value !== false)
56
+ throw new Error(`Invalid Value (Nullable) in ${metadata.key} for value ${value}`);
57
+ const purifyFn = getPurifyFn(metadata.modelRef);
58
+ return purifyFn(value);
59
+ };
60
+ const getPredefinedPurifyFn = (refName) => {
61
+ const purify2 = Reflect.getMetadata(refName, PurifyStorage.prototype);
62
+ return purify2;
63
+ };
64
+ const setPredefinedPurifyFn = (refName, purify2) => {
65
+ Reflect.defineMetadata(refName, purify2, PurifyStorage.prototype);
66
+ };
67
+ const makePurify = (modelRef, option = {}) => {
68
+ const refName = constantInfo.getRefName(modelRef);
69
+ const purifyFn = getPredefinedPurifyFn(refName);
70
+ if (purifyFn && !option.overwrite)
71
+ return purifyFn;
72
+ const metadatas = getFieldMetas(modelRef);
73
+ const fn = (self, isChild) => {
74
+ try {
75
+ if (isChild && !constantInfo.isScalar(modelRef)) {
76
+ const id = self.id;
77
+ if (!id)
78
+ throw new Error(`Invalid Value (No ID) for id ${refName}`);
79
+ return id;
80
+ }
81
+ const result = {};
82
+ for (const metadata of metadatas) {
83
+ const value = self[metadata.key];
84
+ result[metadata.key] = purify(metadata, value, self);
85
+ }
86
+ return result;
87
+ } catch (err) {
88
+ if (isChild)
89
+ throw new Error(err);
90
+ Logger.debug(err);
91
+ return null;
92
+ }
93
+ };
94
+ setPredefinedPurifyFn(refName, fn);
95
+ return fn;
96
+ };
97
+ export {
98
+ makePurify
99
+ };
package/esm/src/scalar.js CHANGED
@@ -8,6 +8,8 @@ import {
8
8
  scalarNameMap,
9
9
  Upload
10
10
  } from "@akanjs/base";
11
+ import { capitalize } from "@akanjs/common";
12
+ import { constantInfo } from "./constantInfo";
11
13
  const scalarExampleMap = /* @__PURE__ */ new Map([
12
14
  [ID, "1234567890abcdef12345678"],
13
15
  [Int, 0],
@@ -20,20 +22,15 @@ const scalarExampleMap = /* @__PURE__ */ new Map([
20
22
  [Map, {}]
21
23
  ]);
22
24
  const getScalarExample = (ref) => scalarExampleMap.get(ref) ?? null;
23
- const getGqlTypeStr = (ref) => scalarNameMap.get(ref) ?? getClassMeta(ref).refName;
24
- const fieldTypes = ["email", "password", "url"];
25
- const getClassMeta = (modelRef) => {
26
- const [target] = getNonArrayModel(modelRef);
27
- const classMeta = Reflect.getMetadata("class", target.prototype);
28
- if (!classMeta)
29
- throw new Error(`No ClassMeta for this target ${target.name}`);
30
- return classMeta;
31
- };
25
+ const getGqlTypeStr = (ref) => scalarNameMap.get(ref) ?? `${constantInfo.isLight(ref) ? "Light" : ""}${capitalize(constantInfo.getRefName(ref))}${constantInfo.isInsight(ref) ? "Insight" : ""}`;
26
+ const fieldPresets = ["email", "password", "url"];
32
27
  const getFieldMetas = (modelRef) => {
33
28
  const [target] = getNonArrayModel(modelRef);
34
29
  const metadataMap = Reflect.getMetadata("fields", target.prototype) ?? /* @__PURE__ */ new Map();
35
30
  const keySortMap = { id: -1, createdAt: 1, updatedAt: 2, removedAt: 3 };
36
- return [...metadataMap.values()].sort((a, b) => (keySortMap[a.key] ?? 0) - (keySortMap[b.key] ?? 0));
31
+ return [...metadataMap.values()].sort(
32
+ (a, b) => (keySortMap[a.key] ?? 0) - (keySortMap[b.key] ?? 0)
33
+ );
37
34
  };
38
35
  const isConstantModel = (modelRef) => {
39
36
  return Reflect.getMetadata("class", modelRef.prototype) !== void 0;
@@ -55,8 +52,7 @@ const setFieldMetaMapOnPrototype = (prototype, metadataMap) => {
55
52
  Reflect.defineMetadata("fields", metadataMap, prototype);
56
53
  };
57
54
  export {
58
- fieldTypes,
59
- getClassMeta,
55
+ fieldPresets,
60
56
  getFieldMetaMap,
61
57
  getFieldMetaMapOnPrototype,
62
58
  getFieldMetas,
@@ -0,0 +1,106 @@
1
+ import {
2
+ applyFnToArrayObjects,
3
+ dayjs,
4
+ Float,
5
+ getNonArrayModel,
6
+ ID,
7
+ Int,
8
+ isGqlScalar,
9
+ JSON as GqlJSON
10
+ } from "@akanjs/base";
11
+ import { constantInfo, getFieldMetas } from ".";
12
+ const scalarSerializeMap = /* @__PURE__ */ new Map([
13
+ [Date, (value) => dayjs(value).toDate()],
14
+ [String, (value) => value],
15
+ [ID, (value) => value],
16
+ [Boolean, (value) => value],
17
+ [Int, (value) => value],
18
+ [Float, (value) => value],
19
+ [GqlJSON, (value) => value]
20
+ ]);
21
+ const getSerializeFn = (inputRef) => {
22
+ const serializeFn = scalarSerializeMap.get(inputRef);
23
+ if (!serializeFn)
24
+ return (value) => value;
25
+ else
26
+ return serializeFn;
27
+ };
28
+ const serializeInput = (value, inputRef, arrDepth) => {
29
+ if (arrDepth && Array.isArray(value))
30
+ return value.map((v) => serializeInput(v, inputRef, arrDepth - 1));
31
+ else if (inputRef.prototype === Map.prototype) {
32
+ const [valueRef] = getNonArrayModel(inputRef);
33
+ const serializeFn = getSerializeFn(valueRef);
34
+ return Object.fromEntries(
35
+ [...value.entries()].map(([key, val]) => [key, applyFnToArrayObjects(val, serializeFn)])
36
+ );
37
+ } else if (isGqlScalar(inputRef)) {
38
+ const serializeFn = getSerializeFn(inputRef);
39
+ return serializeFn(value);
40
+ }
41
+ if (!constantInfo.isScalar(inputRef))
42
+ return value;
43
+ else
44
+ return Object.fromEntries(
45
+ getFieldMetas(inputRef).map((fieldMeta) => [
46
+ fieldMeta.key,
47
+ serializeInput(value[fieldMeta.key], fieldMeta.modelRef, fieldMeta.arrDepth)
48
+ ])
49
+ );
50
+ };
51
+ const serializeArg = (argRef, arrDepth, value, { nullable = false }) => {
52
+ if (nullable && (value === null || value === void 0))
53
+ return null;
54
+ else if (!nullable && (value === null || value === void 0))
55
+ throw new Error(`Invalid Value (Nullable) in ${argRef} for value ${value}`);
56
+ return serializeInput(value, argRef, arrDepth);
57
+ };
58
+ const scalarDeserializeMap = /* @__PURE__ */ new Map([
59
+ [Date, (value) => dayjs(value)],
60
+ [String, (value) => value],
61
+ [ID, (value) => value],
62
+ [Boolean, (value) => value],
63
+ [Int, (value) => value],
64
+ [Float, (value) => value],
65
+ [GqlJSON, (value) => value]
66
+ ]);
67
+ const getDeserializeFn = (inputRef) => {
68
+ const deserializeFn = scalarDeserializeMap.get(inputRef);
69
+ if (!deserializeFn)
70
+ return (value) => value;
71
+ return deserializeFn;
72
+ };
73
+ const deserializeInput = (value, inputRef, arrDepth) => {
74
+ if (arrDepth && Array.isArray(value))
75
+ return value.map((v) => deserializeInput(v, inputRef, arrDepth - 1));
76
+ else if (inputRef.prototype === Map.prototype) {
77
+ const [valueRef] = getNonArrayModel(inputRef);
78
+ const deserializeFn = getDeserializeFn(valueRef);
79
+ return Object.fromEntries(
80
+ [...value.entries()].map(([key, val]) => [key, applyFnToArrayObjects(val, deserializeFn)])
81
+ );
82
+ } else if (isGqlScalar(inputRef)) {
83
+ const deserializeFn = getDeserializeFn(inputRef);
84
+ return deserializeFn(value);
85
+ }
86
+ if (!constantInfo.isScalar(inputRef))
87
+ return value;
88
+ else
89
+ return Object.fromEntries(
90
+ getFieldMetas(inputRef).map((fieldMeta) => [
91
+ fieldMeta.key,
92
+ deserializeInput(value[fieldMeta.key], fieldMeta.modelRef, fieldMeta.arrDepth)
93
+ ])
94
+ );
95
+ };
96
+ const deserializeArg = (argRef, arrDepth, value, { nullable = false }) => {
97
+ if (nullable && (value === null || value === void 0))
98
+ return null;
99
+ else if (!nullable && (value === null || value === void 0))
100
+ throw new Error(`Invalid Value (Nullable) in ${argRef} for value ${value}`);
101
+ return deserializeInput(value, argRef, arrDepth);
102
+ };
103
+ export {
104
+ deserializeArg,
105
+ serializeArg
106
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/constant",
3
- "version": "0.9.47",
3
+ "version": "0.9.49",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -15,6 +15,7 @@
15
15
  "node": ">=20"
16
16
  },
17
17
  "dependencies": {
18
+ "immer": "^10.1.1",
18
19
  "reflect-metadata": "^0.2.2"
19
20
  },
20
21
  "exports": {
package/src/baseGql.d.ts CHANGED
@@ -1,10 +1,14 @@
1
1
  import "reflect-metadata";
2
2
  import { BaseObject, type MergeAllTypes, type Prettify, Type } from "@akanjs/base";
3
+ import { type ExtractFieldInfoObject, type FieldBuilder, type FieldInfoObject, FieldResolver } from "./fieldInfo";
3
4
  import type { NonFunctionalKeys } from "./types";
4
- export declare const as: <T>(modelRef: Type<T>) => Type<T>;
5
5
  type BaseFields = "id" | "createdAt" | "updatedAt" | "removedAt";
6
6
  type OmitBase<T> = Omit<T, BaseFields>;
7
- export declare function via<T>(modelRef: Type<T>): Type<Prettify<T & BaseObject>>;
8
- export declare function via<T, Light, FullModels extends Type[]>(modelRef: Type<T>, lightModelRef: Type<Light>, ...fullModelRefs: FullModels): Type<Prettify<MergeAllTypes<FullModels> & T & Light & BaseObject>>;
9
- export declare function via<T, K extends NonFunctionalKeys<OmitBase<T>>, LightModels extends Type[]>(modelRef: Type<T>, fields: readonly K[], ...lightModelRefs: LightModels): Type<Prettify<MergeAllTypes<LightModels> & Pick<T, K> & BaseObject>>;
7
+ export declare function via<T extends BaseObject, K extends NonFunctionalKeys<OmitBase<T>>, ResolveField extends (resolve: FieldResolver) => FieldInfoObject, LightModels extends Type[]>(modelRef: Type<T>, fields: readonly K[], resolveField: ResolveField, ...lightModelRefs: LightModels): Type<Prettify<MergeAllTypes<LightModels> & Pick<T, K> & BaseObject & ExtractFieldInfoObject<ReturnType<ResolveField>>>>;
8
+ export declare function via<BuildField extends (builder: FieldBuilder) => FieldInfoObject, Inputs extends Type[]>(buildField: BuildField, ...extendInputRefs: Inputs): Type<MergeAllTypes<Inputs> & ExtractFieldInfoObject<ReturnType<BuildField>>>;
9
+ export declare function via<T extends BaseObject, BuildField extends (builder: FieldBuilder) => FieldInfoObject, Insights extends Type[]>(modelRef: Type<T>, buildField: BuildField, ...extendInsightRefs: Insights): Type<MergeAllTypes<Insights> & T & {
10
+ count: number;
11
+ } & ExtractFieldInfoObject<ReturnType<BuildField>>>;
12
+ export declare function via<T, BuildField extends (builder: FieldBuilder) => FieldInfoObject, ObjectModels extends Type[]>(inputRef: Type<T>, buildField: BuildField, ...extendObjectRefs: ObjectModels): Type<Prettify<MergeAllTypes<ObjectModels> & T & BaseObject & ExtractFieldInfoObject<ReturnType<BuildField>>>>;
13
+ export declare function via<T, Light, ResolveField extends (resolve: FieldResolver) => FieldInfoObject, FullModels extends Type[]>(objectRef: Type<T>, lightModelRef: Type<Light>, resolveField: ResolveField, ...fullModelRefs: FullModels): Type<Prettify<MergeAllTypes<FullModels> & T & Light & ExtractFieldInfoObject<ReturnType<ResolveField>>>>;
10
14
  export {};
@@ -1,28 +1,12 @@
1
1
  import "reflect-metadata";
2
2
  import { type Enum, type Type } from "@akanjs/base";
3
- export declare const getFullModelRef: (refName: string) => Type;
4
- export declare const getFullModelRefs: (refName: string) => Type[];
5
- export declare const getInputModelRef: (refName: string) => Type;
6
- export declare const getInputModelRefs: (refName: string) => Type[];
7
- export declare const getScalarModelRef: (refName: string) => Type;
8
- export declare const getScalarModelRefs: (refName: string) => Type[];
3
+ export declare const setExtendRef: (modelRef: Type, extendRef: Type) => void;
4
+ export declare const getExtendRef: <AllowEmpty extends boolean = false>(modelRef: Type, { allowEmpty }?: {
5
+ allowEmpty?: AllowEmpty;
6
+ }) => AllowEmpty extends true ? Type | undefined : Type;
9
7
  export declare const getChildClassRefs: (target: Type) => Type[];
10
8
  export declare const getFieldEnumMetas: (modelRef: Type) => {
11
9
  key: string;
12
- enum: Enum<string | number>;
10
+ enum: Enum;
13
11
  }[];
14
12
  export declare const hasTextField: (modelRef: Type) => boolean;
15
- export type ClassType = "light" | "full" | "input" | "scalar";
16
- export declare const Model: {
17
- Light: (refName: string) => (target: Type) => void;
18
- Object: (refName: string) => (target: Type) => void;
19
- Full: (refName: string) => (target: Type) => void;
20
- Input: (refName: string) => (target: Type) => void;
21
- Scalar: (refName: string) => (target: Type) => void;
22
- Insight: (refName: string) => (target: Type) => void;
23
- Filter: (refName: string) => (target: Type) => void;
24
- };
25
- export declare const getLightModelRef: (modelRef: Type) => Type;
26
- export declare const getAllFullModelRefs: () => Type[];
27
- export declare const getAllScalarModelRefs: () => Type[];
28
- export declare const getAllFilterModelRefs: () => Type[];
@@ -0,0 +1,69 @@
1
+ import "reflect-metadata";
2
+ import { type GetStateObject, GqlScalar, type Type } from "@akanjs/base";
3
+ import { CrystalizeFunc, PurifiedModel, PurifyFunc } from ".";
4
+ import type { DefaultOf, DocumentModel, QueryOf } from "./types";
5
+ export type ModelType = "input" | "object" | "full" | "light" | "insight" | "filter" | "scalar";
6
+ export declare const constantInfo: {
7
+ database: Map<string, ConstantModel<any, any, any, any, any, any, DefaultOf<any>, DefaultOf<any>, GetStateObject<any>, GetStateObject<any>, DefaultOf<any>, any, any, any, QueryOf<any>>>;
8
+ scalar: Map<string, ScalarConstantModel<any, any, any, any, any>>;
9
+ modelRefNameMap: Map<Type, string>;
10
+ getRefName<AllowEmpty extends boolean = false>(modelRef: Type, { allowEmpty }?: {
11
+ allowEmpty?: AllowEmpty;
12
+ }): AllowEmpty extends true ? string | undefined : string;
13
+ getModelType<AllowEmpty extends boolean = false>(modelRef: Type, { allowEmpty }?: {
14
+ allowEmpty?: AllowEmpty;
15
+ }): AllowEmpty extends true ? ModelType | undefined : ModelType;
16
+ setModelType(modelRef: Type, modelType: ModelType): void;
17
+ isObject(modelRef: Type): boolean;
18
+ isFull(modelRef: Type): boolean;
19
+ isLight(modelRef: Type): boolean;
20
+ isInsight(modelRef: Type): boolean;
21
+ isFilter(modelRef: Type): boolean;
22
+ isScalar(modelRef: Type): boolean;
23
+ setDatabase(refName: string, cnst: ConstantModel<any, any, any, any, any, any>): void;
24
+ getDatabase<AllowEmpty extends boolean = false>(refName: string, { allowEmpty }?: {
25
+ allowEmpty?: AllowEmpty;
26
+ }): AllowEmpty extends true ? ConstantModel<any, any, any, any, any, any> | undefined : ConstantModel<any, any, any, any, any, any>;
27
+ setScalar(refName: string, cnst: ScalarConstantModel<any, any, any, any, any>): void;
28
+ getScalar<AllowEmpty extends boolean = false>(refName: string, { allowEmpty }?: {
29
+ allowEmpty?: AllowEmpty;
30
+ }): AllowEmpty extends true ? ScalarConstantModel<any, any, any, any, any> | undefined : ScalarConstantModel<any, any, any, any, any>;
31
+ getModelRef(refName: string, modelType: "input" | "full" | "light" | "insight" | "scalar"): Type | GqlScalar;
32
+ };
33
+ export interface ConstantModel<T extends string, Input, Full, Light, Insight, _CapitalizedT extends string = Capitalize<T>, _Default = DefaultOf<Full>, _DefaultInput = DefaultOf<Input>, _DefaultState = GetStateObject<Full>, _DefaultStateInput = GetStateObject<Input>, _DefaultInsight = DefaultOf<Insight>, _PurifiedInput = PurifiedModel<Input>, _Doc = DocumentModel<Full>, _DocInput = DocumentModel<Input>, _QueryOfDoc = QueryOf<_Doc>> {
34
+ refName: T;
35
+ input: Type<Input>;
36
+ full: Type<Full>;
37
+ light: Type<Light>;
38
+ insight: Type<Insight>;
39
+ crystalize: CrystalizeFunc<Full>;
40
+ lightCrystalize: CrystalizeFunc<Light>;
41
+ crystalizeInsight: CrystalizeFunc<Insight>;
42
+ purify: PurifyFunc<Input, _DefaultInput, _PurifiedInput>;
43
+ getDefault: () => _Default;
44
+ getDefaultInsight: () => _DefaultInsight;
45
+ _CapitalizedT: _CapitalizedT;
46
+ _Default: _Default;
47
+ _DefaultInput: _DefaultInput;
48
+ _DefaultState: _DefaultState;
49
+ _DefaultStateInput: _DefaultStateInput;
50
+ _DefaultInsight: _DefaultInsight;
51
+ _PurifiedInput: _PurifiedInput;
52
+ _Doc: _Doc;
53
+ _DocInput: _DocInput;
54
+ _QueryOfDoc: _QueryOfDoc;
55
+ }
56
+ export declare const cnstOf: <T extends string, Input, Full, Light, Insight>(refName: T, Input: Type<Input>, Full: Type<Full>, Light: Type<Light>, Insight: Type<Insight>, option?: {
57
+ overwrite?: any;
58
+ }) => ConstantModel<T, Input, Full, Light, Insight, Capitalize<T>, DefaultOf<Full>, DefaultOf<Input>, GetStateObject<Full>, GetStateObject<Input>, DefaultOf<Insight>, PurifiedModel<Input>, DocumentModel<Full>, DocumentModel<Input>, QueryOf<DocumentModel<Full>>>;
59
+ export interface ScalarConstantModel<T extends string, Model, _Default = DefaultOf<Model>, _Doc = DocumentModel<Model>, _PurifiedInput = PurifiedModel<Model>> {
60
+ refName: T;
61
+ model: Type<Model>;
62
+ crystalize: CrystalizeFunc<Model>;
63
+ purify: PurifyFunc<Model, _Default, _PurifiedInput>;
64
+ getDefault: () => _Default;
65
+ _Default: _Default;
66
+ _Doc: _Doc;
67
+ _PurifiedInput: _PurifiedInput;
68
+ }
69
+ export declare const scalarCnstOf: <T extends string, Model>(refName: T, Model: Type<Model>) => ScalarConstantModel<T, Model>;
@@ -0,0 +1,6 @@
1
+ import { GetStateObject, Type } from "@akanjs/base";
2
+ export type CrystalizeFunc<Model> = (self: GetStateObject<Model>, isChild?: boolean) => Model;
3
+ export declare const makeCrystalize: <M>(modelRef: Type<M>, option?: {
4
+ overwrite?: any;
5
+ partial?: string[];
6
+ }) => CrystalizeFunc<M>;
@@ -0,0 +1,6 @@
1
+ import { Type } from "@akanjs/base";
2
+ import { DefaultOf } from "./types";
3
+ export declare const makeDefault: <T>(modelRef: Type<T>, option?: {
4
+ isChild?: boolean;
5
+ overwrite?: any;
6
+ }) => DefaultOf<T>;
@@ -0,0 +1,37 @@
1
+ import { type Dayjs, Enum, Float, type GqlScalar, ID, Int, JSON, type Type, type UnType } from "@akanjs/base";
2
+ import { ConstantFieldProps } from "./scalar";
3
+ export type ParamFieldType = typeof ID | typeof Int | StringConstructor | typeof Date | BooleanConstructor | Enum<string>;
4
+ export type ConstantFieldType = ParamFieldType | DateConstructor | typeof Float | typeof JSON | Type | MapConstructor | Enum<number>;
5
+ export type ConstantFieldTypeInput = ConstantFieldType | ConstantFieldType[] | ConstantFieldType[][] | ConstantFieldType[][][];
6
+ export type FieldToValue<Field, MapValue = any> = Field extends null ? FieldToValue<Exclude<Field, null>> | null : Field extends MapConstructor ? Map<string, FieldToValue<MapValue>> : Field extends (infer F)[] ? FieldToValue<F>[] : Field extends typeof ID ? string : Field extends DateConstructor ? Dayjs : Field extends StringConstructor ? string : Field extends BooleanConstructor ? boolean : Field extends typeof Int ? number : Field extends typeof Float ? number : Field extends typeof JSON ? any : Field extends Type ? UnType<Field> : Field extends Enum<infer V> ? V : never;
7
+ export interface FieldInfoObject {
8
+ [key: string]: FieldInfo<ConstantFieldTypeInput | null, any>;
9
+ }
10
+ export type ExtractFieldInfoObject<Obj extends FieldInfoObject> = {
11
+ [K in keyof Obj]: Obj[K] extends FieldInfo<infer F, infer E, infer M> ? unknown extends E ? FieldToValue<F, M> : E : never;
12
+ };
13
+ declare class FieldInfo<Value extends ConstantFieldTypeInput | null = null, ExplicitType = unknown, MapValue = Value extends MapConstructor ? GqlScalar : never> {
14
+ private readonly value;
15
+ private readonly type;
16
+ private readonly option;
17
+ explicitType: ExplicitType;
18
+ constructor(value: Value, option: ConstantFieldProps<any, MapValue>);
19
+ optional(): FieldInfo<Value | null, unknown, any>;
20
+ meta(meta: ConstantFieldProps["meta"]): this;
21
+ applyFieldMeta(target: Type, key: string): void;
22
+ }
23
+ type FieldOption<Value extends ConstantFieldTypeInput, MapValue = Value extends MapConstructor ? GqlScalar : never, Metadata extends {
24
+ [key: string]: any;
25
+ } = {
26
+ [key: string]: any;
27
+ }, _FieldToValue = FieldToValue<Value> | null | undefined> = Omit<ConstantFieldProps<_FieldToValue, MapValue, Metadata>, "enum" | "meta" | "nullable" | "fieldType" | "select"> | Omit<ConstantFieldProps<_FieldToValue, MapValue, Metadata>, "enum" | "meta" | "nullable" | "fieldType" | "select">[];
28
+ export type PlainTypeToFieldType<PlainType> = PlainType extends [infer First, ...infer Rest] ? PlainTypeToFieldType<First>[] : PlainType extends number ? typeof Int | typeof Float : PlainType extends string ? StringConstructor : typeof JSON;
29
+ export declare const field: {
30
+ <ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof JSON | StringConstructor | BooleanConstructor | DateConstructor | MapConstructor : never>(value: Value, option?: FieldOption<Value, MapValue>): FieldInfo<Value, ExplicitType, MapValue>;
31
+ secret<ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof JSON | StringConstructor | BooleanConstructor | DateConstructor | MapConstructor : never>(value: Value, option?: FieldOption<Value, MapValue>): FieldInfo<Value | null, ExplicitType, MapValue>;
32
+ hidden<ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof JSON | StringConstructor | BooleanConstructor | DateConstructor | MapConstructor : never>(value: Value, option?: FieldOption<Value, MapValue>): FieldInfo<Value | null, ExplicitType, MapValue>;
33
+ };
34
+ export declare const resolve: <ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? GqlScalar : never>(value: Value, option?: FieldOption<Value, MapValue>) => FieldInfo<Value, ExplicitType, MapValue>;
35
+ export type FieldBuilder = typeof field;
36
+ export type FieldResolver = typeof resolve;
37
+ export {};
@@ -0,0 +1,2 @@
1
+ import { type Type } from "@akanjs/base";
2
+ export declare const immerify: <T extends object | object[]>(modelRef: Type, objOrArr: T) => T;
package/src/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import type { AllSrvs } from "@akanjs/service";
2
- export declare const Srvs: AllSrvs;
3
1
  export * from "./types";
4
- export * from "./fieldMeta";
2
+ export * from "./fieldInfo";
5
3
  export * from "./scalar";
6
- export * from "./constantDecorator";
7
- export * from "./filterMeta";
4
+ export * from "./constantInfo";
8
5
  export * from "./baseGql";
9
6
  export * from "./classMeta";
7
+ export * from "./default";
8
+ export * from "./crystalize";
9
+ export * from "./purify";
10
+ export * from "./serialize";
@@ -0,0 +1,14 @@
1
+ import { BaseObject, Dayjs, GetStateObject, Type, Upload } from "@akanjs/base";
2
+ import { DefaultOf } from ".";
3
+ type Purified<O> = O extends BaseObject ? string : O extends BaseObject[] ? string[] : O extends Dayjs ? Dayjs : O extends {
4
+ [key: string]: any;
5
+ } ? PurifiedModel<O> : O;
6
+ type PurifiedWithObjectToId<T, StateKeys extends keyof GetStateObject<T> = keyof GetStateObject<T>> = {
7
+ [K in StateKeys]: Purified<T[K]>;
8
+ };
9
+ export type PurifiedModel<T> = T extends (infer S)[] ? PurifiedModel<S>[] : T extends string | number | boolean | Dayjs | Upload ? T : T extends Map<infer K, infer V> ? Map<K, PurifiedModel<V>> : PurifiedWithObjectToId<T>;
10
+ export type PurifyFunc<Input, _DefaultInput = DefaultOf<Input>, _PurifiedInput = PurifiedModel<Input>> = (self: _DefaultInput, isChild?: boolean) => _PurifiedInput | null;
11
+ export declare const makePurify: <I>(modelRef: Type<I>, option?: {
12
+ overwrite?: any;
13
+ }) => PurifyFunc<I>;
14
+ export {};