@smartsoft001/models 2.27.0 → 2.30.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.
package/.eslintrc.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "extends": ["../../../.eslintrc.json"],
3
+ "ignorePatterns": ["!**/*"],
4
+ "overrides": [
5
+ {
6
+ "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7
+ "rules": {}
8
+ },
9
+ {
10
+ "files": ["*.ts", "*.tsx"],
11
+ "rules": {}
12
+ },
13
+ {
14
+ "files": ["*.js", "*.jsx"],
15
+ "rules": {}
16
+ },
17
+ {
18
+ "files": ["*.json"],
19
+ "parser": "jsonc-eslint-parser",
20
+ "rules": {
21
+ "@nx/dependency-checks": [
22
+ "error",
23
+ {
24
+ "ignoredFiles": ["{projectRoot}/esbuild.config.{js,ts,mjs,mts}"]
25
+ }
26
+ ]
27
+ }
28
+ }
29
+ ]
30
+ }
package/jest.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ /* eslint-disable */
2
+ export default {
3
+ displayName: 'models',
4
+ preset: '../../../jest.preset.js',
5
+ testEnvironment: 'node',
6
+ transform: {
7
+ '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
8
+ },
9
+ moduleFileExtensions: ['ts', 'js', 'html'],
10
+ coverageDirectory: '../../../coverage/packages/shared/models',
11
+ };
package/package.json CHANGED
@@ -2,16 +2,11 @@
2
2
  "name": "@smartsoft001/models",
3
3
  "type": "commonjs",
4
4
  "dependencies": {
5
- "@smartsoft001/utils": "^2.27.0",
6
- "flatted": "3.2.9",
7
- "guid-typescript": "^1.0.9",
8
- "lodash": "4.17.21",
9
- "md5": "^2.3.0",
10
5
  "reflect-metadata": "^0.2.1",
11
6
  "rxjs": "^7.8.1",
12
- "tslib": "^2.3.0"
7
+ "@smartsoft001/utils": "^2.30.0"
13
8
  },
14
- "version": "2.27.0",
9
+ "version": "2.30.0",
15
10
  "main": "./src/index.js",
16
11
  "typings": "./src/index.d.ts"
17
- }
12
+ }
package/project.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "models",
3
+ "$schema": "../../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "packages/shared/models/src",
5
+ "projectType": "library",
6
+ "tags": [],
7
+ "targets": {
8
+ "build": {
9
+ "executor": "@nx/esbuild:esbuild",
10
+ "outputs": ["{options.outputPath}"],
11
+ "options": {
12
+ "outputPath": "dist/packages/shared/models",
13
+ "main": "packages/shared/models/src/index.ts",
14
+ "tsConfig": "packages/shared/models/tsconfig.lib.json",
15
+ "assets": ["packages/shared/models/*.md"],
16
+ "generatePackageJson": true,
17
+ "format": ["cjs"]
18
+ }
19
+ },
20
+ "deploy": {
21
+ "executor": "ngx-deploy-npm:deploy",
22
+ "options": {
23
+ "access": "public",
24
+ "distFolderPath": "dist/packages/shared/models"
25
+ },
26
+ "dependsOn": ["build"]
27
+ },
28
+ "lint": {
29
+ "executor": "@nx/eslint:lint"
30
+ },
31
+ "test": {
32
+ "executor": "@nx/jest:jest",
33
+ "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
34
+ "options": {
35
+ "jestConfig": "packages/shared/models/jest.config.ts"
36
+ }
37
+ }
38
+ }
39
+ }
@@ -1,4 +1,5 @@
1
1
  export * from "./lib/symbols";
2
2
  export * from './lib/interfaces';
3
3
  export * from './lib/decorators';
4
+
4
5
  export * from './lib/utils';
@@ -0,0 +1,55 @@
1
+ import 'reflect-metadata';
2
+
3
+ import {ObjectService} from "@smartsoft001/utils";
4
+
5
+ import * as symbols from "../../symbols";
6
+ import {FieldType, IFieldOptions} from "../../interfaces";
7
+
8
+ export const Field = FieldDecorator;
9
+ export function FieldDecorator(options?: IFieldOptions) {
10
+ return <T>(target: T, key: string) => {
11
+
12
+ options = options ? { ...options } : {};
13
+
14
+ if (!(target as any).constructor['__fields']) {
15
+ (target as any).constructor['__fields'] = {};
16
+ }
17
+
18
+ (target as any).constructor['__fields'][key] = true;
19
+
20
+ if (!options.type && key === 'password') {
21
+ options.type = FieldType.password;
22
+ } else if (!options.type) {
23
+ options.type = FieldType.text;
24
+ }
25
+
26
+ if (options.classType) {
27
+ (target as any)['_' + key] = (target as any)[key];
28
+ delete (target as any)[key];
29
+
30
+ Object.defineProperty(target, key, {
31
+ get: function () {
32
+ if (!this['_' + key] && options?.type === FieldType.array) {
33
+ this['_' + key] = [];
34
+ }
35
+ return this['_' + key];
36
+ },
37
+ set: function (v: any) {
38
+ this['_' + key] = options?.type === FieldType.array && v ?
39
+ v.map((i: any) => ObjectService.createByType(i, options?.classType))
40
+ : ObjectService.createByType(v, options?.classType);
41
+ },
42
+ enumerable: true,
43
+ configurable: true
44
+ });
45
+
46
+ if (!(target as any).constructor['__properties']) {
47
+ (target as any).constructor['__properties'] = {};
48
+ }
49
+
50
+ (target as any).constructor['__properties'][key] = true;
51
+ }
52
+
53
+ Reflect.defineMetadata(symbols.SYMBOL_FIELD, options, target as any, key);
54
+ }
55
+ }
@@ -0,0 +1,25 @@
1
+ import 'reflect-metadata';
2
+
3
+ import * as symbols from "../../symbols";
4
+ import {IModelOptions} from "../../interfaces";
5
+
6
+ export const Model = ModelDecorator;
7
+ export function ModelDecorator(options?: IModelOptions) {
8
+ return function<T>(target: any) {
9
+ options = options ? { ...options } : {};
10
+
11
+ Reflect.defineMetadata(symbols.SYMBOL_MODEL, options, target);
12
+
13
+ target.prototype.toJSON = function () {
14
+ const result = { ...this };
15
+
16
+ if (this.constructor['__properties']) {
17
+ Object.keys(this.constructor['__properties']).forEach(protoKey => {
18
+ result[protoKey] = this['_' + protoKey];
19
+ });
20
+ }
21
+
22
+ return result;
23
+ };
24
+ }
25
+ }
@@ -1,5 +1,6 @@
1
- import { Observable } from "rxjs";
2
- export declare enum FieldTypeDef {
1
+ import {Observable} from "rxjs";
2
+
3
+ export enum FieldTypeDef {
3
4
  address = "address",
4
5
  currency = "currency",
5
6
  date = "date",
@@ -32,34 +33,37 @@ export declare enum FieldTypeDef {
32
33
  image = "image",
33
34
  float = "float"
34
35
  }
35
- export declare const FieldType: typeof FieldTypeDef;
36
+ export const FieldType: typeof FieldTypeDef = FieldTypeDef;
37
+
36
38
  export interface ISpecification {
37
39
  readonly criteria: any;
38
40
  }
41
+
39
42
  export interface IModelFilter {
40
43
  label?: string;
41
44
  fieldType?: FieldTypeDef;
42
45
  key: string;
43
46
  type: '=' | '!=' | '>=' | '<=' | '<' | '>' | '~=';
44
- possibilities$?: Observable<{
45
- id: any;
46
- text: string;
47
- }[]>;
47
+ possibilities$?: Observable<{ id: any, text: string }[]>;
48
48
  }
49
+
49
50
  export interface IModelStep {
50
51
  number: number;
51
52
  name: string;
52
53
  }
54
+
53
55
  export interface IModelMetadata {
54
56
  titleKey?: string;
55
57
  permissions?: Array<string>;
56
58
  filters?: Array<IModelFilter>;
57
59
  }
60
+
58
61
  export interface IFieldMetadata extends IFieldModifyMetadata {
59
62
  type?: FieldTypeDef;
60
63
  classType?: any;
61
64
  possibilities?: Array<any> | any;
62
65
  }
66
+
63
67
  export interface IFieldModifyMetadata {
64
68
  required?: boolean;
65
69
  focused?: boolean;
@@ -74,9 +78,11 @@ export interface IFieldModifyMetadata {
74
78
  */
75
79
  step?: IModelStep;
76
80
  }
81
+
77
82
  export interface IFieldEditMetadata extends IFieldModifyMetadata {
78
83
  multi?: boolean;
79
84
  }
85
+
80
86
  export interface IFieldListMetadata {
81
87
  order?: number;
82
88
  filter?: boolean;
@@ -86,16 +92,15 @@ export interface IFieldListMetadata {
86
92
  * @param {string} dynamic.headerKey - column header object key
87
93
  * @param {string} dynamic.rowKey - column row value object key
88
94
  */
89
- dynamic?: {
90
- headerKey: string;
91
- rowKey: string;
92
- };
95
+ dynamic?: { headerKey: string, rowKey: string }
93
96
  }
97
+
94
98
  export interface IFieldDetailsMetadata {
95
99
  order?: number;
96
100
  permissions?: Array<string>;
97
101
  enabled?: ISpecification;
98
102
  }
103
+
99
104
  export interface IModelOptions {
100
105
  titleKey?: string;
101
106
  filters?: Array<IModelFilter>;
@@ -114,13 +119,16 @@ export interface IModelOptions {
114
119
  */
115
120
  import?: boolean;
116
121
  }
122
+
117
123
  export interface IModelModeOptions {
118
124
  permissions?: Array<string>;
119
125
  enabled?: ISpecification;
120
126
  }
127
+
121
128
  export interface IModelModeOptionsCustom extends IModelModeOptions {
122
129
  mode: string;
123
130
  }
131
+
124
132
  export interface IFieldOptions extends IFieldMetadata {
125
133
  create?: IFieldModifyMetadata | boolean;
126
134
  update?: IFieldEditMetadata | boolean;
@@ -130,12 +138,15 @@ export interface IFieldOptions extends IFieldMetadata {
130
138
  search?: boolean;
131
139
  info?: string;
132
140
  }
141
+
133
142
  export interface IModelMetadataCustom extends IModelMetadata {
134
- mode: string;
143
+ mode: string
135
144
  }
145
+
136
146
  export interface IFieldUniqueMetadata {
137
147
  withFields?: Array<string>;
138
148
  }
149
+
139
150
  export interface IFieldCustomMetadata extends IFieldModifyMetadata, IFieldListMetadata {
140
151
  mode: string;
141
152
  }
@@ -0,0 +1,2 @@
1
+ export const SYMBOL_MODEL = Symbol.for('smartsoft:model');
2
+ export const SYMBOL_FIELD = Symbol.for('smartsoft:field');
@@ -0,0 +1,137 @@
1
+ import {Model} from "./decorators/model/model.decorator";
2
+ import {Field} from "./decorators/field/field.decorator";
3
+ import {castModel, getInvalidFields, getModelFieldKeys, getModelFieldOptions, isModel} from "./utils";
4
+ import {FieldType} from "./interfaces";
5
+
6
+ describe('shared-models: utils', () => {
7
+ describe('getModelFieldKeys()', () => {
8
+ it('should return empty array when fields is not defined', () => {
9
+ @Model({})
10
+ class Test {
11
+ }
12
+
13
+ const result = getModelFieldKeys(Test);
14
+
15
+ expect(result).toStrictEqual([ ]);
16
+ });
17
+
18
+ it('should return field keys array', () => {
19
+ @Model({})
20
+ class Test {
21
+ @Field({}) test1!: any;
22
+ @Field({}) test2!: any;
23
+ @Field({}) test3!: any;
24
+ }
25
+
26
+ const result = getModelFieldKeys(Test);
27
+
28
+ expect(result).toStrictEqual([ 'test1', 'test2', 'test3' ]);
29
+ });
30
+ });
31
+
32
+ describe('getModelFieldOptions', () => {
33
+ it('should return options', () => {
34
+ const options = { required: true, type: FieldType.password };
35
+
36
+ @Model({})
37
+ class Test {
38
+ @Field(options) test!: string;
39
+ }
40
+
41
+ expect(getModelFieldOptions(new Test(), 'test')).toStrictEqual(options);
42
+ });
43
+ });
44
+
45
+ describe('isModel', () => {
46
+ it('should return true when model type', () => {
47
+ @Model({})
48
+ class Test {
49
+ @Field({}) test!: string;
50
+ }
51
+
52
+ expect(isModel(new Test())).toBe(true);
53
+ });
54
+
55
+ it('should return false when other type', () => {
56
+ expect(isModel(new Date())).toBe(false);
57
+ });
58
+ });
59
+
60
+ describe('getInvalidFields', () => {
61
+ it('should return empty array when correct', () => {
62
+ @Model({})
63
+ class Test {
64
+ @Field({}) test!: string;
65
+ @Field({
66
+ required: true
67
+ }) test2!: string;
68
+ @Field({
69
+ create: {
70
+ required: true
71
+ }
72
+ }) test3!: string;
73
+ @Field({
74
+ create: {
75
+ required: false
76
+ }
77
+ }) test4!: string;
78
+ }
79
+
80
+ const instance = new Test();
81
+ instance.test2 = "12";
82
+ instance.test3 = "21";
83
+
84
+ expect(getInvalidFields(instance, 'create', null as any).length).toBe(0);
85
+ });
86
+
87
+ it('should return array when incorrect', () => {
88
+ @Model({})
89
+ class Test {
90
+ @Field({}) test!: string;
91
+ @Field({
92
+ required: true
93
+ }) test2!: string;
94
+ @Field({
95
+ create: {
96
+ required: true
97
+ }
98
+ }) test3!: string;
99
+ @Field({
100
+ create: {
101
+ required: false
102
+ }
103
+ }) test4!: string;
104
+ }
105
+
106
+ const instance = new Test();
107
+ instance.test2 = "12";
108
+
109
+ expect(getInvalidFields(instance, 'create', null as any).length).toBe(1);
110
+ });
111
+
112
+ it('should return empty array when not model', () => {
113
+ expect(getInvalidFields(new Date(), 'create', null as any).length).toBe(0);
114
+ });
115
+ });
116
+
117
+ describe('castModel', () => {
118
+ it('should remove unused fields', () => {
119
+ @Model({})
120
+ class Test {
121
+ @Field({}) test!: string;
122
+ @Field({
123
+ create: true
124
+ }) test2!: string;
125
+ }
126
+
127
+ const instance = new Test();
128
+ instance.test = "asd";
129
+ instance.test2 = "aaa";
130
+
131
+ castModel(instance, 'create', null as any);
132
+
133
+ expect(instance.test).not.toBeDefined();
134
+ expect(instance.test2).toBeDefined();
135
+ });
136
+ });
137
+ });
@@ -0,0 +1,118 @@
1
+ import { IFieldOptions, IModelOptions } from "./interfaces";
2
+ import { SYMBOL_FIELD, SYMBOL_MODEL } from "./symbols";
3
+
4
+ export function getModelFieldKeys<T>(type: T): Array<string> {
5
+ if (!(type as any)["__fields"]) return [];
6
+ return Object.keys((type as any)["__fields"]);
7
+ }
8
+
9
+ export function getModelFieldOptions<T>(
10
+ instance: T,
11
+ fieldKey: string
12
+ ): IFieldOptions {
13
+ return Reflect.getMetadata(SYMBOL_FIELD, instance as any, fieldKey);
14
+ }
15
+
16
+ export function getModelFieldsWithOptions<T>(
17
+ instance: T
18
+ ): Array<{ key: string; options: IFieldOptions }> {
19
+ const keys = getModelFieldKeys((instance as any).constructor);
20
+
21
+ return keys.map((item) => {
22
+ return {
23
+ key: item,
24
+ options: getModelFieldOptions(instance, item),
25
+ };
26
+ });
27
+ }
28
+
29
+ export function getModelOptions(type: any): IModelOptions {
30
+ return Reflect.getMetadata(SYMBOL_MODEL, type);
31
+ }
32
+
33
+ export function isModel<T>(instance: T): boolean {
34
+ if (!instance || !instance.constructor) return false;
35
+ return Reflect.hasMetadata(SYMBOL_MODEL, instance.constructor);
36
+ }
37
+
38
+ export function getInvalidFields<T>(
39
+ instance: T,
40
+ mode: "create" | "update" | string,
41
+ permissions: Array<string>
42
+ ): Array<string> {
43
+ const result: any[] = [];
44
+
45
+ getModelFieldsWithOptions(instance).forEach(({ key, options }) => {
46
+ let required = options.required;
47
+
48
+ if (
49
+ (mode === "create" || mode === "update") &&
50
+ options[mode] &&
51
+ options[mode]?.constructor
52
+ ) {
53
+ required = (options[mode] as IFieldOptions).required;
54
+
55
+ if (
56
+ required &&
57
+ permissions &&
58
+ (options[mode] as IFieldOptions).permissions
59
+ ) {
60
+ required = (options[mode] as IFieldOptions)?.permissions?.some((op) =>
61
+ permissions.some((p) => p === op)
62
+ );
63
+ }
64
+ }
65
+
66
+ if (
67
+ required
68
+ && ((instance as any)[key] === null || (instance as any)[key] === undefined || (instance as any)[key] === '')
69
+ ) result.push(key);
70
+ });
71
+
72
+ return result;
73
+ }
74
+
75
+ export function castModel<T>(
76
+ instance: T,
77
+ mode: "create" | "update" | string,
78
+ permissions: Array<string>
79
+ ): void {
80
+ if (!isModel(instance)) return;
81
+
82
+ const fieldsWithOptions = getModelFieldsWithOptions(instance);
83
+
84
+ Object.keys(instance as any)
85
+ .filter((key) => key !== "id")
86
+ .forEach((key) => {
87
+ const fieldWidthOptions =
88
+ fieldsWithOptions.find((f) => f.key === key);
89
+
90
+ if (!fieldWidthOptions) {
91
+ delete (instance as any)[key];
92
+ return;
93
+ }
94
+
95
+ if (
96
+ (mode === "create" || mode === "update") &&
97
+ (!fieldWidthOptions.options[mode] ||
98
+ (permissions &&
99
+ (fieldWidthOptions.options[mode] as IFieldOptions).permissions &&
100
+ !(fieldWidthOptions.options[
101
+ mode
102
+ ] as IFieldOptions)?.permissions?.some((op) =>
103
+ permissions.some((p) => op === p)
104
+ )))
105
+ ) {
106
+ delete (instance as any)[key];
107
+ return;
108
+ } else if (
109
+ mode !== "create" &&
110
+ mode !== "update" &&
111
+ (!fieldWidthOptions.options.customs ||
112
+ !fieldWidthOptions.options.customs.some((c) => c.mode === mode))
113
+ ) {
114
+ delete (instance as any)[key];
115
+ return;
116
+ }
117
+ });
118
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "forceConsistentCasingInFileNames": true,
6
+ "strict": true,
7
+ "noImplicitOverride": true,
8
+ "noPropertyAccessFromIndexSignature": true,
9
+ "noImplicitReturns": true,
10
+ "noFallthroughCasesInSwitch": true
11
+ },
12
+ "files": [],
13
+ "include": [],
14
+ "references": [
15
+ {
16
+ "path": "./tsconfig.lib.json"
17
+ },
18
+ {
19
+ "path": "./tsconfig.spec.json"
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "declaration": true,
6
+ "types": ["node"]
7
+ },
8
+ "include": ["src/**/*.ts"],
9
+ "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
10
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "module": "commonjs",
6
+ "types": ["jest", "node"]
7
+ },
8
+ "include": [
9
+ "jest.config.ts",
10
+ "src/**/*.test.ts",
11
+ "src/**/*.spec.ts",
12
+ "src/**/*.d.ts"
13
+ ]
14
+ }
package/index.cjs DELETED
@@ -1,294 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // packages/shared/models/src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- Field: () => Field,
34
- FieldDecorator: () => FieldDecorator,
35
- FieldType: () => FieldType,
36
- FieldTypeDef: () => FieldTypeDef,
37
- Model: () => Model,
38
- ModelDecorator: () => ModelDecorator,
39
- SYMBOL_FIELD: () => SYMBOL_FIELD,
40
- SYMBOL_MODEL: () => SYMBOL_MODEL,
41
- castModel: () => castModel,
42
- getInvalidFields: () => getInvalidFields,
43
- getModelFieldKeys: () => getModelFieldKeys,
44
- getModelFieldOptions: () => getModelFieldOptions,
45
- getModelFieldsWithOptions: () => getModelFieldsWithOptions,
46
- getModelOptions: () => getModelOptions,
47
- isModel: () => isModel
48
- });
49
- module.exports = __toCommonJS(src_exports);
50
-
51
- // packages/shared/models/src/lib/symbols.ts
52
- var SYMBOL_MODEL = Symbol.for("smartsoft:model");
53
- var SYMBOL_FIELD = Symbol.for("smartsoft:field");
54
-
55
- // packages/shared/models/src/lib/interfaces.ts
56
- var FieldTypeDef = /* @__PURE__ */ ((FieldTypeDef2) => {
57
- FieldTypeDef2["address"] = "address";
58
- FieldTypeDef2["currency"] = "currency";
59
- FieldTypeDef2["date"] = "date";
60
- FieldTypeDef2["dateTime"] = "dateTime";
61
- FieldTypeDef2["dateWithEdit"] = "dateWithEdit";
62
- FieldTypeDef2["email"] = "email";
63
- FieldTypeDef2["enum"] = "enum";
64
- FieldTypeDef2["file"] = "file";
65
- FieldTypeDef2["flag"] = "flag";
66
- FieldTypeDef2["int"] = "int";
67
- FieldTypeDef2["nip"] = "nip";
68
- FieldTypeDef2["object"] = "object";
69
- FieldTypeDef2["array"] = "array";
70
- FieldTypeDef2["password"] = "password";
71
- FieldTypeDef2["radio"] = "radio";
72
- FieldTypeDef2["text"] = "text";
73
- FieldTypeDef2["strings"] = "strings";
74
- FieldTypeDef2["ints"] = "ints";
75
- FieldTypeDef2["longText"] = "longText";
76
- FieldTypeDef2["color"] = "color";
77
- FieldTypeDef2["logo"] = "logo";
78
- FieldTypeDef2["check"] = "check";
79
- FieldTypeDef2["phoneNumber"] = "phoneNumber";
80
- FieldTypeDef2["phoneNumberPl"] = "phoneNumberPl";
81
- FieldTypeDef2["pesel"] = "pesel";
82
- FieldTypeDef2["pdf"] = "pdf";
83
- FieldTypeDef2["video"] = "video";
84
- FieldTypeDef2["attachment"] = "attachment";
85
- FieldTypeDef2["dateRange"] = "dateRange";
86
- FieldTypeDef2["image"] = "image";
87
- FieldTypeDef2["float"] = "float";
88
- return FieldTypeDef2;
89
- })(FieldTypeDef || {});
90
- var FieldType = FieldTypeDef;
91
-
92
- // packages/shared/models/src/lib/decorators/model/model.decorator.ts
93
- var import_reflect_metadata = require("reflect-metadata");
94
- var Model = ModelDecorator;
95
- function ModelDecorator(options) {
96
- return function(target) {
97
- options = options ? { ...options } : {};
98
- Reflect.defineMetadata(SYMBOL_MODEL, options, target);
99
- target.prototype.toJSON = function() {
100
- const result = { ...this };
101
- if (this.constructor["__properties"]) {
102
- Object.keys(this.constructor["__properties"]).forEach((protoKey) => {
103
- result[protoKey] = this["_" + protoKey];
104
- });
105
- }
106
- return result;
107
- };
108
- };
109
- }
110
-
111
- // packages/shared/models/src/lib/decorators/field/field.decorator.ts
112
- var import_reflect_metadata2 = require("reflect-metadata");
113
-
114
- // packages/shared/utils/src/lib/services/password/password.service.ts
115
- var md5_ = __toESM(require("md5"));
116
-
117
- // packages/shared/utils/src/lib/services/object/object.service.ts
118
- var import_flatted = require("flatted");
119
- var ObjectService = class {
120
- /***
121
- * Create object with data
122
- * @param data {object} - data to set
123
- * @param type {type} - new type
124
- * @return - new type object
125
- */
126
- static createByType(data, type) {
127
- if (!data)
128
- return data;
129
- try {
130
- if (data instanceof type)
131
- return data;
132
- } catch (e) {
133
- console.warn(e);
134
- }
135
- const result = new type();
136
- Object.keys(data).forEach((key) => {
137
- result[key] = data[key];
138
- });
139
- return result;
140
- }
141
- /***
142
- * Remove object type from data
143
- * @param obj {object} - object
144
- * @return - object without type
145
- */
146
- static removeTypes(obj) {
147
- if (!obj)
148
- return obj;
149
- const result = {};
150
- Object.keys(obj).forEach((key) => {
151
- if (obj[key] && obj[key].constructor && !(obj[key] instanceof Date)) {
152
- let stringValue = "";
153
- try {
154
- stringValue = JSON.stringify(obj[key]);
155
- } catch (e) {
156
- console.warn("can't stringify without circular package");
157
- stringValue = (0, import_flatted.stringify)(obj[key]);
158
- }
159
- result[key] = JSON.parse(stringValue);
160
- } else {
161
- result[key] = obj[key];
162
- }
163
- });
164
- return result;
165
- }
166
- };
167
-
168
- // packages/shared/utils/src/lib/services/guid/guid.service.ts
169
- var import_guid_typescript = require("guid-typescript");
170
-
171
- // packages/shared/utils/src/lib/services/array/array.service.ts
172
- var _ = __toESM(require("lodash"));
173
-
174
- // packages/shared/models/src/lib/decorators/field/field.decorator.ts
175
- var Field = FieldDecorator;
176
- function FieldDecorator(options) {
177
- return (target, key) => {
178
- options = options ? { ...options } : {};
179
- if (!target.constructor["__fields"]) {
180
- target.constructor["__fields"] = {};
181
- }
182
- target.constructor["__fields"][key] = true;
183
- if (!options.type && key === "password") {
184
- options.type = FieldType.password;
185
- } else if (!options.type) {
186
- options.type = FieldType.text;
187
- }
188
- if (options.classType) {
189
- target["_" + key] = target[key];
190
- delete target[key];
191
- Object.defineProperty(target, key, {
192
- get: function() {
193
- if (!this["_" + key] && options?.type === FieldType.array) {
194
- this["_" + key] = [];
195
- }
196
- return this["_" + key];
197
- },
198
- set: function(v) {
199
- this["_" + key] = options?.type === FieldType.array && v ? v.map((i) => ObjectService.createByType(i, options?.classType)) : ObjectService.createByType(v, options?.classType);
200
- },
201
- enumerable: true,
202
- configurable: true
203
- });
204
- if (!target.constructor["__properties"]) {
205
- target.constructor["__properties"] = {};
206
- }
207
- target.constructor["__properties"][key] = true;
208
- }
209
- Reflect.defineMetadata(SYMBOL_FIELD, options, target, key);
210
- };
211
- }
212
-
213
- // packages/shared/models/src/lib/utils.ts
214
- function getModelFieldKeys(type) {
215
- if (!type["__fields"])
216
- return [];
217
- return Object.keys(type["__fields"]);
218
- }
219
- function getModelFieldOptions(instance, fieldKey) {
220
- return Reflect.getMetadata(SYMBOL_FIELD, instance, fieldKey);
221
- }
222
- function getModelFieldsWithOptions(instance) {
223
- const keys = getModelFieldKeys(instance.constructor);
224
- return keys.map((item) => {
225
- return {
226
- key: item,
227
- options: getModelFieldOptions(instance, item)
228
- };
229
- });
230
- }
231
- function getModelOptions(type) {
232
- return Reflect.getMetadata(SYMBOL_MODEL, type);
233
- }
234
- function isModel(instance) {
235
- if (!instance || !instance.constructor)
236
- return false;
237
- return Reflect.hasMetadata(SYMBOL_MODEL, instance.constructor);
238
- }
239
- function getInvalidFields(instance, mode, permissions) {
240
- const result = [];
241
- getModelFieldsWithOptions(instance).forEach(({ key, options }) => {
242
- let required = options.required;
243
- if ((mode === "create" || mode === "update") && options[mode] && options[mode]?.constructor) {
244
- required = options[mode].required;
245
- if (required && permissions && options[mode].permissions) {
246
- required = options[mode]?.permissions?.some(
247
- (op) => permissions.some((p) => p === op)
248
- );
249
- }
250
- }
251
- if (required && (instance[key] === null || instance[key] === void 0 || instance[key] === ""))
252
- result.push(key);
253
- });
254
- return result;
255
- }
256
- function castModel(instance, mode, permissions) {
257
- if (!isModel(instance))
258
- return;
259
- const fieldsWithOptions = getModelFieldsWithOptions(instance);
260
- Object.keys(instance).filter((key) => key !== "id").forEach((key) => {
261
- const fieldWidthOptions = fieldsWithOptions.find((f) => f.key === key);
262
- if (!fieldWidthOptions) {
263
- delete instance[key];
264
- return;
265
- }
266
- if ((mode === "create" || mode === "update") && (!fieldWidthOptions.options[mode] || permissions && fieldWidthOptions.options[mode].permissions && !fieldWidthOptions.options[mode]?.permissions?.some(
267
- (op) => permissions.some((p) => op === p)
268
- ))) {
269
- delete instance[key];
270
- return;
271
- } else if (mode !== "create" && mode !== "update" && (!fieldWidthOptions.options.customs || !fieldWidthOptions.options.customs.some((c) => c.mode === mode))) {
272
- delete instance[key];
273
- return;
274
- }
275
- });
276
- }
277
- // Annotate the CommonJS export names for ESM import in node:
278
- 0 && (module.exports = {
279
- Field,
280
- FieldDecorator,
281
- FieldType,
282
- FieldTypeDef,
283
- Model,
284
- ModelDecorator,
285
- SYMBOL_FIELD,
286
- SYMBOL_MODEL,
287
- castModel,
288
- getInvalidFields,
289
- getModelFieldKeys,
290
- getModelFieldOptions,
291
- getModelFieldsWithOptions,
292
- getModelOptions,
293
- isModel
294
- });
@@ -1,4 +0,0 @@
1
- import 'reflect-metadata';
2
- import { IFieldOptions } from "../../interfaces";
3
- export declare const Field: typeof FieldDecorator;
4
- export declare function FieldDecorator(options?: IFieldOptions): <T>(target: T, key: string) => void;
@@ -1,4 +0,0 @@
1
- import 'reflect-metadata';
2
- import { IModelOptions } from "../../interfaces";
3
- export declare const Model: typeof ModelDecorator;
4
- export declare function ModelDecorator(options?: IModelOptions): <T>(target: any) => void;
@@ -1,2 +0,0 @@
1
- export declare const SYMBOL_MODEL: unique symbol;
2
- export declare const SYMBOL_FIELD: unique symbol;
@@ -1,11 +0,0 @@
1
- import { IFieldOptions, IModelOptions } from "./interfaces";
2
- export declare function getModelFieldKeys<T>(type: T): Array<string>;
3
- export declare function getModelFieldOptions<T>(instance: T, fieldKey: string): IFieldOptions;
4
- export declare function getModelFieldsWithOptions<T>(instance: T): Array<{
5
- key: string;
6
- options: IFieldOptions;
7
- }>;
8
- export declare function getModelOptions(type: any): IModelOptions;
9
- export declare function isModel<T>(instance: T): boolean;
10
- export declare function getInvalidFields<T>(instance: T, mode: "create" | "update" | string, permissions: Array<string>): Array<string>;
11
- export declare function castModel<T>(instance: T, mode: "create" | "update" | string, permissions: Array<string>): void;
File without changes