@akanjs/constant 1.0.5 → 1.0.6
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 +89 -98
- package/cjs/src/classMeta.js +6 -21
- package/cjs/src/constantInfo.js +1 -12
- package/cjs/src/crystalize.js +18 -35
- package/cjs/src/default.js +15 -36
- package/cjs/src/fieldInfo.js +117 -16
- package/cjs/src/immerify.js +3 -5
- package/cjs/src/purify.js +31 -44
- package/cjs/src/scalar.js +1 -30
- package/cjs/src/serialize.js +6 -6
- package/esm/src/baseGql.js +90 -98
- package/esm/src/classMeta.js +6 -21
- package/esm/src/constantInfo.js +1 -12
- package/esm/src/crystalize.js +19 -36
- package/esm/src/default.js +15 -36
- package/esm/src/fieldInfo.js +119 -19
- package/esm/src/immerify.js +3 -5
- package/esm/src/purify.js +32 -45
- package/esm/src/scalar.js +1 -31
- package/esm/src/serialize.js +7 -7
- package/package.json +1 -1
- package/src/baseGql.d.ts +29 -8
- package/src/classMeta.d.ts +5 -8
- package/src/constantInfo.d.ts +18 -23
- package/src/crystalize.d.ts +5 -2
- package/src/default.d.ts +2 -5
- package/src/fieldInfo.d.ts +93 -13
- package/src/immerify.d.ts +4 -1
- package/src/purify.d.ts +4 -4
- package/src/scalar.d.ts +0 -5
- package/src/serialize.d.ts +3 -3
- package/src/types.d.ts +4 -4
package/src/fieldInfo.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { type Dayjs, EnumInstance, Float, type GqlScalar, ID, Int, JSON, type Type, type UnType } from "@akanjs/base";
|
|
2
|
-
import {
|
|
1
|
+
import { type Dayjs, EnumInstance, Float, type GqlScalar, ID, Int, JSON as GqlJSON, SingleValue, type Type, type UnType } from "@akanjs/base";
|
|
2
|
+
import type { AccumulatorOperator } from "mongoose";
|
|
3
|
+
import { Cnst } from "./baseGql";
|
|
4
|
+
import { ConstantFieldProps, FieldPreset } from "./scalar";
|
|
3
5
|
export type ParamFieldType = typeof ID | typeof Int | StringConstructor | typeof Date | BooleanConstructor | EnumInstance<string, string>;
|
|
4
|
-
export type ConstantFieldType = ParamFieldType | DateConstructor | typeof Float | typeof
|
|
6
|
+
export type ConstantFieldType = ParamFieldType | DateConstructor | typeof Float | typeof GqlJSON | Type | MapConstructor | EnumInstance<string, number>;
|
|
5
7
|
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
|
|
8
|
+
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 GqlJSON ? any : Field extends EnumInstance<string, infer V> ? V : Field extends Type ? UnType<Field> : never;
|
|
7
9
|
export interface FieldInfoObject {
|
|
8
10
|
[key: string]: FieldInfo<ConstantFieldTypeInput | null, any>;
|
|
9
11
|
}
|
|
@@ -11,25 +13,103 @@ export type ExtractFieldInfoObject<Obj extends FieldInfoObject> = {
|
|
|
11
13
|
[K in keyof Obj]: Obj[K] extends FieldInfo<infer F, infer E, infer M> ? unknown extends E ? FieldToValue<F, M> : E : never;
|
|
12
14
|
};
|
|
13
15
|
declare class FieldInfo<Value extends ConstantFieldTypeInput | null = null, ExplicitType = unknown, MapValue = Value extends MapConstructor ? GqlScalar : never> {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
readonly value: Value;
|
|
17
|
+
readonly type: ConstantFieldTypeInput;
|
|
18
|
+
readonly option: ConstantFieldProps;
|
|
17
19
|
explicitType: ExplicitType;
|
|
18
20
|
constructor(value: Value, option: ConstantFieldProps<any, MapValue>);
|
|
19
21
|
optional(): FieldInfo<Value | null, unknown, any>;
|
|
20
22
|
meta(meta: ConstantFieldProps["meta"]): this;
|
|
21
|
-
|
|
23
|
+
toField(): ConstantField<any, ExplicitType, MapValue>;
|
|
24
|
+
}
|
|
25
|
+
interface ConstantFieldBuildProps<FieldValue = any, MapValue = any, Metadata = {
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}> {
|
|
28
|
+
nullable: boolean;
|
|
29
|
+
ref?: string;
|
|
30
|
+
refPath?: string;
|
|
31
|
+
refType?: "child" | "parent" | "relation";
|
|
32
|
+
default: FieldValue | ((doc: {
|
|
33
|
+
id: string;
|
|
34
|
+
}) => FieldValue);
|
|
35
|
+
type?: FieldPreset;
|
|
36
|
+
fieldType: "property" | "hidden" | "resolve";
|
|
37
|
+
immutable: boolean;
|
|
38
|
+
min?: number;
|
|
39
|
+
max?: number;
|
|
40
|
+
enum?: EnumInstance;
|
|
41
|
+
select: boolean;
|
|
42
|
+
minlength?: number;
|
|
43
|
+
maxlength?: number;
|
|
44
|
+
accumulate?: AccumulatorOperator;
|
|
45
|
+
example?: FieldValue;
|
|
46
|
+
of?: MapValue;
|
|
47
|
+
validate?: (value: FieldValue, model: any) => boolean;
|
|
48
|
+
text?: "search" | "filter";
|
|
49
|
+
modelRef: Cnst;
|
|
50
|
+
arrDepth: number;
|
|
51
|
+
optArrDepth: number;
|
|
52
|
+
meta: Metadata;
|
|
53
|
+
}
|
|
54
|
+
export interface FieldProps extends ConstantFieldBuildProps<any, any, any> {
|
|
55
|
+
isClass: boolean;
|
|
56
|
+
isScalar: boolean;
|
|
57
|
+
isArray: boolean;
|
|
58
|
+
isMap: boolean;
|
|
59
|
+
}
|
|
60
|
+
export declare class ConstantField<FieldValue = any, MapValue = any, Metadata = {
|
|
61
|
+
[key: string]: any;
|
|
62
|
+
}> implements ConstantFieldBuildProps<FieldValue, MapValue, Metadata> {
|
|
63
|
+
static getBaseModelField(): FieldObject;
|
|
64
|
+
static getBaseInsightField(): FieldObject;
|
|
65
|
+
readonly nullable: boolean;
|
|
66
|
+
readonly ref?: string;
|
|
67
|
+
readonly refPath?: string;
|
|
68
|
+
readonly refType?: "child" | "parent" | "relation";
|
|
69
|
+
readonly default: FieldValue | ((doc: {
|
|
70
|
+
id: string;
|
|
71
|
+
}) => FieldValue);
|
|
72
|
+
readonly type?: FieldPreset;
|
|
73
|
+
readonly fieldType: "property" | "hidden" | "resolve";
|
|
74
|
+
readonly immutable: boolean;
|
|
75
|
+
readonly min?: number;
|
|
76
|
+
readonly max?: number;
|
|
77
|
+
readonly enum?: EnumInstance;
|
|
78
|
+
readonly select: boolean;
|
|
79
|
+
readonly minlength?: number;
|
|
80
|
+
readonly maxlength?: number;
|
|
81
|
+
readonly accumulate?: AccumulatorOperator;
|
|
82
|
+
readonly example?: FieldValue;
|
|
83
|
+
readonly of?: MapValue;
|
|
84
|
+
readonly validate?: (value: FieldValue, model: any) => boolean;
|
|
85
|
+
readonly text?: "search" | "filter";
|
|
86
|
+
readonly modelRef: Cnst;
|
|
87
|
+
readonly arrDepth: number;
|
|
88
|
+
readonly optArrDepth: number;
|
|
89
|
+
readonly meta: Metadata;
|
|
90
|
+
constructor(props: ConstantFieldBuildProps<FieldValue, MapValue, Metadata>);
|
|
91
|
+
static fromFieldInfo<FieldValue = any, MapValue = any, Metadata = {
|
|
92
|
+
[key: string]: any;
|
|
93
|
+
}>(fieldInfo: FieldInfo<any, MapValue, Metadata>): ConstantField<FieldValue, MapValue, Metadata>;
|
|
94
|
+
get isClass(): boolean;
|
|
95
|
+
get isScalar(): boolean;
|
|
96
|
+
get isArray(): boolean;
|
|
97
|
+
get isMap(): boolean;
|
|
98
|
+
getProps(): FieldProps;
|
|
99
|
+
}
|
|
100
|
+
export interface FieldObject {
|
|
101
|
+
[key: string]: ConstantField;
|
|
22
102
|
}
|
|
23
103
|
type FieldOption<Value extends ConstantFieldTypeInput, MapValue = Value extends MapConstructor ? GqlScalar : never, Metadata extends {
|
|
24
104
|
[key: string]: any;
|
|
25
105
|
} = {
|
|
26
106
|
[key: string]: any;
|
|
27
|
-
}, _FieldToValue = FieldToValue<Value> | null | undefined> = Omit<ConstantFieldProps<_FieldToValue, MapValue, Metadata>, "enum" | "meta" | "nullable" | "fieldType" | "select"> | Omit<ConstantFieldProps<_FieldToValue
|
|
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
|
|
107
|
+
}, _FieldToValue = FieldToValue<Value> | null | undefined> = Omit<ConstantFieldProps<_FieldToValue, MapValue, Metadata>, "enum" | "meta" | "nullable" | "fieldType" | "select"> | Omit<ConstantFieldProps<SingleValue<_FieldToValue>, MapValue, Metadata>, "enum" | "meta" | "nullable" | "fieldType" | "select">[];
|
|
108
|
+
export type PlainTypeToFieldType<PlainType> = PlainType extends [infer First, ...infer Rest] ? PlainTypeToFieldType<First>[] : PlainType extends number ? typeof Int | typeof Float : PlainType extends string ? StringConstructor : typeof GqlJSON;
|
|
29
109
|
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
|
|
31
|
-
secret<ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof
|
|
32
|
-
hidden<ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof
|
|
110
|
+
<ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof GqlJSON | StringConstructor | BooleanConstructor | DateConstructor | MapConstructor : never>(value: Value, option?: FieldOption<Value, MapValue>): FieldInfo<Value, ExplicitType, MapValue>;
|
|
111
|
+
secret<ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof GqlJSON | StringConstructor | BooleanConstructor | DateConstructor | MapConstructor : never>(value: Value, option?: FieldOption<Value, MapValue>): FieldInfo<Value | null, ExplicitType | null, MapValue>;
|
|
112
|
+
hidden<ExplicitType, Value extends ConstantFieldTypeInput = PlainTypeToFieldType<ExplicitType>, MapValue = Value extends MapConstructor ? typeof Int | typeof import("@akanjs/base").Upload | typeof Float | typeof ID | typeof GqlJSON | StringConstructor | BooleanConstructor | DateConstructor | MapConstructor : never>(value: Value, option?: FieldOption<Value, MapValue>): FieldInfo<Value | null, ExplicitType | null, MapValue>;
|
|
33
113
|
};
|
|
34
114
|
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
115
|
export type FieldBuilder = typeof field;
|
package/src/immerify.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import { type Type } from "@akanjs/base";
|
|
2
|
-
|
|
2
|
+
import { FieldObject } from "./fieldInfo";
|
|
3
|
+
export declare const immerify: <T extends object>(modelRef: Type<any, {
|
|
4
|
+
field: FieldObject;
|
|
5
|
+
}>, objOrArr: T) => T;
|
package/src/purify.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseObject, Dayjs, GetStateObject, Type, Upload } from "@akanjs/base";
|
|
2
|
-
import { DefaultOf } from ".";
|
|
2
|
+
import { DefaultOf, FieldObject } from ".";
|
|
3
3
|
type Purified<O> = O extends BaseObject ? string : O extends BaseObject[] ? string[] : O extends Dayjs ? Dayjs : O extends {
|
|
4
4
|
[key: string]: any;
|
|
5
5
|
} ? PurifiedModel<O> : O;
|
|
@@ -8,7 +8,7 @@ type PurifiedWithObjectToId<T, StateKeys extends keyof GetStateObject<T> = keyof
|
|
|
8
8
|
};
|
|
9
9
|
export type PurifiedModel<T> = T extends Upload[] ? FileList : T extends (infer S)[] ? PurifiedModel<S>[] : T extends string | number | boolean | Dayjs ? T : T extends Map<infer K, infer V> ? Map<K, PurifiedModel<V>> : PurifiedWithObjectToId<T>;
|
|
10
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
|
|
12
|
-
|
|
13
|
-
}) => PurifyFunc<I>;
|
|
11
|
+
export declare const makePurify: <I>(modelRef: Type<I, {
|
|
12
|
+
field: FieldObject;
|
|
13
|
+
}>) => PurifyFunc<I>;
|
|
14
14
|
export {};
|
package/src/scalar.d.ts
CHANGED
|
@@ -59,9 +59,4 @@ export type ConstantFieldMeta<FieldValue = any, MapValue = any, Metadata = {
|
|
|
59
59
|
isMap: boolean;
|
|
60
60
|
meta: Metadata;
|
|
61
61
|
};
|
|
62
|
-
export declare const getFieldMetas: (modelRef: Type) => ConstantFieldMeta[];
|
|
63
62
|
export declare const isConstantModel: (modelRef: Type) => boolean;
|
|
64
|
-
export declare const getFieldMetaMap: (modelRef: Type) => Map<string, ConstantFieldMeta>;
|
|
65
|
-
export declare const setFieldMetaMap: (modelRef: Type, metadataMap: Map<string, ConstantFieldMeta>) => void;
|
|
66
|
-
export declare const getFieldMetaMapOnPrototype: (prototype: object) => Map<string, ConstantFieldMeta>;
|
|
67
|
-
export declare const setFieldMetaMapOnPrototype: (prototype: object, metadataMap: Map<string, ConstantFieldMeta>) => void;
|
package/src/serialize.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const serializeArg: (argRef:
|
|
1
|
+
import { Cnst } from ".";
|
|
2
|
+
export declare const serializeArg: (argRef: Cnst, arrDepth: number, value: any, { nullable }: {
|
|
3
3
|
nullable?: boolean;
|
|
4
4
|
}) => object[] | null;
|
|
5
|
-
export declare const deserializeArg: (argRef:
|
|
5
|
+
export declare const deserializeArg: (argRef: Cnst, arrDepth: number, value: any, { nullable }: {
|
|
6
6
|
nullable?: boolean;
|
|
7
7
|
}) => object[] | null;
|
package/src/types.d.ts
CHANGED
|
@@ -4,18 +4,18 @@ export type { FilterQuery as QueryOf };
|
|
|
4
4
|
type ObjectToId<O> = O extends BaseObject ? string : O extends BaseObject[] ? string[] : O extends Dayjs ? Dayjs : O extends {
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
} ? DocumentModel<O> : O;
|
|
7
|
-
type
|
|
7
|
+
type Docify<T, StateKeys extends keyof GetStateObject<T> = keyof GetStateObject<T>> = {
|
|
8
8
|
[K in StateKeys as null extends T[K] ? never : K]: ObjectToId<T[K]>;
|
|
9
9
|
} & {
|
|
10
10
|
[K in StateKeys as null extends T[K] ? K : never]?: ObjectToId<Exclude<T[K], null>> | undefined;
|
|
11
11
|
};
|
|
12
|
-
export type DocumentModel<T> = T extends (infer S)[] ? DocumentModel<S>[] : T extends string | number | boolean | Dayjs | Upload ? T : T extends Map<infer K, infer V> ? Map<K, DocumentModel<V>> :
|
|
12
|
+
export type DocumentModel<T> = T extends (infer S)[] ? DocumentModel<S>[] : T extends string | number | boolean | Dayjs | Upload ? T : T extends Map<infer K, infer V> ? Map<K, DocumentModel<V>> : Docify<T>;
|
|
13
13
|
export type FieldState<T> = T extends {
|
|
14
14
|
id: string;
|
|
15
15
|
} ? T | null : T;
|
|
16
|
-
export type DefaultOf<S> = {
|
|
16
|
+
export type DefaultOf<S> = GetStateObject<{
|
|
17
17
|
[K in keyof S]: FieldState<S[K]>;
|
|
18
|
-
}
|
|
18
|
+
}>;
|
|
19
19
|
export type GetPlainObject<T, O extends string> = Omit<{
|
|
20
20
|
[K in keyof T as T[K] extends (...args: any) => any ? never : K extends keyof HydratedDocument<any> ? never : K]: T[K];
|
|
21
21
|
}, O>;
|