@smartsoft001/models 2.76.0 → 2.80.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/index.cjs +294 -0
- package/package.json +14 -5
- package/src/{index.ts → index.d.ts} +0 -1
- package/src/lib/decorators/field/field.decorator.d.ts +4 -0
- package/src/lib/decorators/model/model.decorator.d.ts +4 -0
- package/src/lib/interfaces.d.ts +141 -0
- package/src/lib/symbols.d.ts +2 -0
- package/src/lib/utils.d.ts +11 -0
- package/.eslintrc.json +0 -24
- package/jest.config.ts +0 -11
- package/project.json +0 -39
- package/src/lib/decorators/field/field.decorator.ts +0 -57
- package/src/lib/decorators/model/model.decorator.ts +0 -25
- package/src/lib/interfaces.ts +0 -154
- package/src/lib/symbols.ts +0 -2
- package/src/lib/utils.spec.ts +0 -151
- package/src/lib/utils.ts +0 -118
- package/tsconfig.json +0 -22
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -14
- /package/src/lib/decorators/{index.ts → index.d.ts} +0 -0
package/index.cjs
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
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(
|
|
200
|
+
(i) => ObjectService.createByType(i, options?.classType)
|
|
201
|
+
) : ObjectService.createByType(v, options?.classType);
|
|
202
|
+
},
|
|
203
|
+
enumerable: true,
|
|
204
|
+
configurable: true
|
|
205
|
+
});
|
|
206
|
+
if (!target.constructor["__properties"]) {
|
|
207
|
+
target.constructor["__properties"] = {};
|
|
208
|
+
}
|
|
209
|
+
target.constructor["__properties"][key] = true;
|
|
210
|
+
}
|
|
211
|
+
Reflect.defineMetadata(SYMBOL_FIELD, options, target, key);
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// packages/shared/models/src/lib/utils.ts
|
|
216
|
+
function getModelFieldKeys(type) {
|
|
217
|
+
if (!type["__fields"])
|
|
218
|
+
return [];
|
|
219
|
+
return Object.keys(type["__fields"]);
|
|
220
|
+
}
|
|
221
|
+
function getModelFieldOptions(instance, fieldKey) {
|
|
222
|
+
return Reflect.getMetadata(SYMBOL_FIELD, instance, fieldKey);
|
|
223
|
+
}
|
|
224
|
+
function getModelFieldsWithOptions(instance) {
|
|
225
|
+
const keys = getModelFieldKeys(instance.constructor);
|
|
226
|
+
return keys.map((item) => {
|
|
227
|
+
return {
|
|
228
|
+
key: item,
|
|
229
|
+
options: getModelFieldOptions(instance, item)
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
function getModelOptions(type) {
|
|
234
|
+
return Reflect.getMetadata(SYMBOL_MODEL, type);
|
|
235
|
+
}
|
|
236
|
+
function isModel(instance) {
|
|
237
|
+
if (!instance || !instance.constructor)
|
|
238
|
+
return false;
|
|
239
|
+
return Reflect.hasMetadata(SYMBOL_MODEL, instance.constructor);
|
|
240
|
+
}
|
|
241
|
+
function getInvalidFields(instance, mode, permissions) {
|
|
242
|
+
const result = [];
|
|
243
|
+
getModelFieldsWithOptions(instance).forEach(({ key, options }) => {
|
|
244
|
+
let required = options.required;
|
|
245
|
+
if ((mode === "create" || mode === "update") && options[mode] && options[mode]?.constructor) {
|
|
246
|
+
required = options[mode].required;
|
|
247
|
+
if (required && permissions && options[mode].permissions) {
|
|
248
|
+
required = options[mode]?.permissions?.some(
|
|
249
|
+
(op) => permissions.some((p) => p === op)
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (required && (instance[key] === null || instance[key] === void 0 || instance[key] === ""))
|
|
254
|
+
result.push(key);
|
|
255
|
+
});
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
function castModel(instance, mode, permissions) {
|
|
259
|
+
if (!isModel(instance))
|
|
260
|
+
return;
|
|
261
|
+
const fieldsWithOptions = getModelFieldsWithOptions(instance);
|
|
262
|
+
Object.keys(instance).filter((key) => key !== "id").forEach((key) => {
|
|
263
|
+
const fieldWidthOptions = fieldsWithOptions.find((f) => f.key === key);
|
|
264
|
+
if (!fieldWidthOptions) {
|
|
265
|
+
delete instance[key];
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if ((mode === "create" || mode === "update") && (!fieldWidthOptions.options[mode] || permissions && fieldWidthOptions.options[mode].permissions && !fieldWidthOptions.options[mode]?.permissions?.some((op) => permissions.some((p) => op === p)))) {
|
|
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
|
+
});
|
package/package.json
CHANGED
|
@@ -2,11 +2,20 @@
|
|
|
2
2
|
"name": "@smartsoft001/models",
|
|
3
3
|
"type": "commonjs",
|
|
4
4
|
"dependencies": {
|
|
5
|
+
"@angular/core": "20.1.0",
|
|
6
|
+
"flatted": "3.3.3",
|
|
7
|
+
"guid-typescript": "^1.0.9",
|
|
8
|
+
"lodash": "4.17.21",
|
|
9
|
+
"md5": "^2.3.0",
|
|
5
10
|
"reflect-metadata": "^0.2.1",
|
|
6
|
-
"
|
|
7
|
-
"@smartsoft001/utils": "^2.76.0"
|
|
11
|
+
"tslib": "^2.3.0"
|
|
8
12
|
},
|
|
9
|
-
"version": "2.
|
|
13
|
+
"version": "2.80.0",
|
|
10
14
|
"main": "./src/index.js",
|
|
11
|
-
"typings": "./src/index.d.ts"
|
|
12
|
-
|
|
15
|
+
"typings": "./src/index.d.ts",
|
|
16
|
+
"resolutions": {
|
|
17
|
+
"paypal-rest-sdk": "1.8.1",
|
|
18
|
+
"tslib": "^2.3.0",
|
|
19
|
+
"zone.js": "~0.15.0"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Signal } from '@angular/core';
|
|
2
|
+
export declare enum FieldTypeDef {
|
|
3
|
+
address = "address",
|
|
4
|
+
currency = "currency",
|
|
5
|
+
date = "date",
|
|
6
|
+
dateTime = "dateTime",
|
|
7
|
+
dateWithEdit = "dateWithEdit",
|
|
8
|
+
email = "email",
|
|
9
|
+
enum = "enum",
|
|
10
|
+
file = "file",
|
|
11
|
+
flag = "flag",
|
|
12
|
+
int = "int",
|
|
13
|
+
nip = "nip",
|
|
14
|
+
object = "object",
|
|
15
|
+
array = "array",
|
|
16
|
+
password = "password",
|
|
17
|
+
radio = "radio",
|
|
18
|
+
text = "text",
|
|
19
|
+
strings = "strings",
|
|
20
|
+
ints = "ints",
|
|
21
|
+
longText = "longText",
|
|
22
|
+
color = "color",
|
|
23
|
+
logo = "logo",
|
|
24
|
+
check = "check",
|
|
25
|
+
phoneNumber = "phoneNumber",
|
|
26
|
+
phoneNumberPl = "phoneNumberPl",
|
|
27
|
+
pesel = "pesel",
|
|
28
|
+
pdf = "pdf",
|
|
29
|
+
video = "video",
|
|
30
|
+
attachment = "attachment",
|
|
31
|
+
dateRange = "dateRange",
|
|
32
|
+
image = "image",
|
|
33
|
+
float = "float"
|
|
34
|
+
}
|
|
35
|
+
export declare const FieldType: typeof FieldTypeDef;
|
|
36
|
+
export interface ISpecification {
|
|
37
|
+
readonly criteria: any;
|
|
38
|
+
}
|
|
39
|
+
export interface IModelFilter {
|
|
40
|
+
label?: string;
|
|
41
|
+
fieldType?: FieldTypeDef;
|
|
42
|
+
key: string;
|
|
43
|
+
type: '=' | '!=' | '>=' | '<=' | '<' | '>' | '~=';
|
|
44
|
+
possibilities?: Signal<{
|
|
45
|
+
id: any;
|
|
46
|
+
text: string;
|
|
47
|
+
}[]>;
|
|
48
|
+
}
|
|
49
|
+
export interface IModelStep {
|
|
50
|
+
number: number;
|
|
51
|
+
name: string;
|
|
52
|
+
}
|
|
53
|
+
export interface IModelMetadata {
|
|
54
|
+
titleKey?: string;
|
|
55
|
+
permissions?: Array<string>;
|
|
56
|
+
filters?: Array<IModelFilter>;
|
|
57
|
+
}
|
|
58
|
+
export interface IFieldMetadata extends IFieldModifyMetadata {
|
|
59
|
+
type?: FieldTypeDef;
|
|
60
|
+
classType?: any;
|
|
61
|
+
possibilities?: Array<any> | any;
|
|
62
|
+
}
|
|
63
|
+
export interface IFieldModifyMetadata {
|
|
64
|
+
required?: boolean;
|
|
65
|
+
focused?: boolean;
|
|
66
|
+
confirm?: boolean;
|
|
67
|
+
permissions?: Array<string>;
|
|
68
|
+
unique?: boolean | IFieldUniqueMetadata;
|
|
69
|
+
defaltValue?: () => any;
|
|
70
|
+
enabled?: ISpecification;
|
|
71
|
+
hide?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* @desc - Model step configuration
|
|
74
|
+
*/
|
|
75
|
+
step?: IModelStep;
|
|
76
|
+
}
|
|
77
|
+
export interface IFieldEditMetadata extends IFieldModifyMetadata {
|
|
78
|
+
multi?: boolean;
|
|
79
|
+
}
|
|
80
|
+
export interface IFieldListMetadata {
|
|
81
|
+
order?: number;
|
|
82
|
+
filter?: boolean;
|
|
83
|
+
permissions?: Array<string>;
|
|
84
|
+
/**
|
|
85
|
+
* Configuration for dynamic list table data
|
|
86
|
+
* @param {string} dynamic.headerKey - column header object key
|
|
87
|
+
* @param {string} dynamic.rowKey - column row value object key
|
|
88
|
+
*/
|
|
89
|
+
dynamic?: {
|
|
90
|
+
headerKey: string;
|
|
91
|
+
rowKey: string;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export interface IFieldDetailsMetadata {
|
|
95
|
+
order?: number;
|
|
96
|
+
permissions?: Array<string>;
|
|
97
|
+
enabled?: ISpecification;
|
|
98
|
+
}
|
|
99
|
+
export interface IModelOptions {
|
|
100
|
+
titleKey?: string;
|
|
101
|
+
filters?: Array<IModelFilter>;
|
|
102
|
+
create?: IModelModeOptions;
|
|
103
|
+
update?: IModelModeOptions;
|
|
104
|
+
list?: IModelModeOptions;
|
|
105
|
+
details?: IModelModeOptions;
|
|
106
|
+
remove?: IModelModeOptions;
|
|
107
|
+
customs?: Array<IModelModeOptionsCustom>;
|
|
108
|
+
/**
|
|
109
|
+
* @desc - Allow export field data
|
|
110
|
+
*/
|
|
111
|
+
export?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* @desc - Allow import field data
|
|
114
|
+
*/
|
|
115
|
+
import?: boolean;
|
|
116
|
+
}
|
|
117
|
+
export interface IModelModeOptions {
|
|
118
|
+
permissions?: Array<string>;
|
|
119
|
+
enabled?: ISpecification;
|
|
120
|
+
}
|
|
121
|
+
export interface IModelModeOptionsCustom extends IModelModeOptions {
|
|
122
|
+
mode: string;
|
|
123
|
+
}
|
|
124
|
+
export interface IFieldOptions extends IFieldMetadata {
|
|
125
|
+
create?: IFieldModifyMetadata | boolean;
|
|
126
|
+
update?: IFieldEditMetadata | boolean;
|
|
127
|
+
list?: IFieldListMetadata | boolean;
|
|
128
|
+
details?: IFieldDetailsMetadata | boolean;
|
|
129
|
+
customs?: Array<IModelMetadataCustom>;
|
|
130
|
+
search?: boolean;
|
|
131
|
+
info?: string;
|
|
132
|
+
}
|
|
133
|
+
export interface IModelMetadataCustom extends IModelMetadata {
|
|
134
|
+
mode: string;
|
|
135
|
+
}
|
|
136
|
+
export interface IFieldUniqueMetadata {
|
|
137
|
+
withFields?: Array<string>;
|
|
138
|
+
}
|
|
139
|
+
export interface IFieldCustomMetadata extends IFieldModifyMetadata, IFieldListMetadata {
|
|
140
|
+
mode: string;
|
|
141
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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;
|
package/.eslintrc.json
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": ["../../../.eslintrc.json"],
|
|
3
|
-
"ignorePatterns": ["!**/*"],
|
|
4
|
-
"rules": {},
|
|
5
|
-
"overrides": [
|
|
6
|
-
{
|
|
7
|
-
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
8
|
-
"rules": {}
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
"files": ["*.ts", "*.tsx"],
|
|
12
|
-
"rules": {}
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
"files": ["*.js", "*.jsx"],
|
|
16
|
-
"rules": {}
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
"files": "package.json",
|
|
20
|
-
"parser": "jsonc-eslint-parser",
|
|
21
|
-
"rules": {}
|
|
22
|
-
}
|
|
23
|
-
]
|
|
24
|
-
}
|
package/jest.config.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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/project.json
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
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,57 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
|
|
3
|
-
import { ObjectService } from '@smartsoft001/utils';
|
|
4
|
-
|
|
5
|
-
import { FieldType, IFieldOptions } from '../../interfaces';
|
|
6
|
-
import * as symbols from '../../symbols';
|
|
7
|
-
|
|
8
|
-
export const Field = FieldDecorator;
|
|
9
|
-
export function FieldDecorator(options?: IFieldOptions) {
|
|
10
|
-
return <T>(target: T, key: string) => {
|
|
11
|
-
options = options ? { ...options } : {};
|
|
12
|
-
|
|
13
|
-
if (!(target as any).constructor['__fields']) {
|
|
14
|
-
(target as any).constructor['__fields'] = {};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
(target as any).constructor['__fields'][key] = true;
|
|
18
|
-
|
|
19
|
-
if (!options.type && key === 'password') {
|
|
20
|
-
options.type = FieldType.password;
|
|
21
|
-
} else if (!options.type) {
|
|
22
|
-
options.type = FieldType.text;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (options.classType) {
|
|
26
|
-
(target as any)['_' + key] = (target as any)[key];
|
|
27
|
-
delete (target as any)[key];
|
|
28
|
-
|
|
29
|
-
Object.defineProperty(target, key, {
|
|
30
|
-
get: function () {
|
|
31
|
-
if (!this['_' + key] && options?.type === FieldType.array) {
|
|
32
|
-
this['_' + key] = [];
|
|
33
|
-
}
|
|
34
|
-
return this['_' + key];
|
|
35
|
-
},
|
|
36
|
-
set: function (v: any) {
|
|
37
|
-
this['_' + key] =
|
|
38
|
-
options?.type === FieldType.array && v
|
|
39
|
-
? v.map((i: any) =>
|
|
40
|
-
ObjectService.createByType(i, options?.classType),
|
|
41
|
-
)
|
|
42
|
-
: ObjectService.createByType(v, options?.classType);
|
|
43
|
-
},
|
|
44
|
-
enumerable: true,
|
|
45
|
-
configurable: true,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
if (!(target as any).constructor['__properties']) {
|
|
49
|
-
(target as any).constructor['__properties'] = {};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
(target as any).constructor['__properties'][key] = true;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
Reflect.defineMetadata(symbols.SYMBOL_FIELD, options, target as any, key);
|
|
56
|
-
};
|
|
57
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
|
|
3
|
-
import { IModelOptions } from '../../interfaces';
|
|
4
|
-
import * as symbols from '../../symbols';
|
|
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
|
-
}
|
package/src/lib/interfaces.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { Signal } from '@angular/core';
|
|
2
|
-
|
|
3
|
-
export enum FieldTypeDef {
|
|
4
|
-
address = 'address',
|
|
5
|
-
currency = 'currency',
|
|
6
|
-
date = 'date',
|
|
7
|
-
dateTime = 'dateTime',
|
|
8
|
-
dateWithEdit = 'dateWithEdit',
|
|
9
|
-
email = 'email',
|
|
10
|
-
enum = 'enum',
|
|
11
|
-
file = 'file',
|
|
12
|
-
flag = 'flag',
|
|
13
|
-
int = 'int',
|
|
14
|
-
nip = 'nip',
|
|
15
|
-
object = 'object',
|
|
16
|
-
array = 'array',
|
|
17
|
-
password = 'password',
|
|
18
|
-
radio = 'radio',
|
|
19
|
-
text = 'text',
|
|
20
|
-
strings = 'strings',
|
|
21
|
-
ints = 'ints',
|
|
22
|
-
longText = 'longText',
|
|
23
|
-
color = 'color',
|
|
24
|
-
logo = 'logo',
|
|
25
|
-
check = 'check',
|
|
26
|
-
phoneNumber = 'phoneNumber',
|
|
27
|
-
phoneNumberPl = 'phoneNumberPl',
|
|
28
|
-
pesel = 'pesel',
|
|
29
|
-
pdf = 'pdf',
|
|
30
|
-
video = 'video',
|
|
31
|
-
attachment = 'attachment',
|
|
32
|
-
dateRange = 'dateRange',
|
|
33
|
-
image = 'image',
|
|
34
|
-
float = 'float',
|
|
35
|
-
}
|
|
36
|
-
export const FieldType: typeof FieldTypeDef = FieldTypeDef;
|
|
37
|
-
|
|
38
|
-
export interface ISpecification {
|
|
39
|
-
readonly criteria: any;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface IModelFilter {
|
|
43
|
-
label?: string;
|
|
44
|
-
fieldType?: FieldTypeDef;
|
|
45
|
-
key: string;
|
|
46
|
-
type: '=' | '!=' | '>=' | '<=' | '<' | '>' | '~=';
|
|
47
|
-
possibilities?: Signal<{ id: any; text: string }[]>;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface IModelStep {
|
|
51
|
-
number: number;
|
|
52
|
-
name: string;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface IModelMetadata {
|
|
56
|
-
titleKey?: string;
|
|
57
|
-
permissions?: Array<string>;
|
|
58
|
-
filters?: Array<IModelFilter>;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export interface IFieldMetadata extends IFieldModifyMetadata {
|
|
62
|
-
type?: FieldTypeDef;
|
|
63
|
-
classType?: any;
|
|
64
|
-
possibilities?: Array<any> | any;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface IFieldModifyMetadata {
|
|
68
|
-
required?: boolean;
|
|
69
|
-
focused?: boolean;
|
|
70
|
-
confirm?: boolean;
|
|
71
|
-
permissions?: Array<string>;
|
|
72
|
-
unique?: boolean | IFieldUniqueMetadata;
|
|
73
|
-
defaltValue?: () => any;
|
|
74
|
-
enabled?: ISpecification;
|
|
75
|
-
hide?: boolean;
|
|
76
|
-
/**
|
|
77
|
-
* @desc - Model step configuration
|
|
78
|
-
*/
|
|
79
|
-
step?: IModelStep;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface IFieldEditMetadata extends IFieldModifyMetadata {
|
|
83
|
-
multi?: boolean;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export interface IFieldListMetadata {
|
|
87
|
-
order?: number;
|
|
88
|
-
filter?: boolean;
|
|
89
|
-
permissions?: Array<string>;
|
|
90
|
-
/**
|
|
91
|
-
* Configuration for dynamic list table data
|
|
92
|
-
* @param {string} dynamic.headerKey - column header object key
|
|
93
|
-
* @param {string} dynamic.rowKey - column row value object key
|
|
94
|
-
*/
|
|
95
|
-
dynamic?: { headerKey: string; rowKey: string };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export interface IFieldDetailsMetadata {
|
|
99
|
-
order?: number;
|
|
100
|
-
permissions?: Array<string>;
|
|
101
|
-
enabled?: ISpecification;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface IModelOptions {
|
|
105
|
-
titleKey?: string;
|
|
106
|
-
filters?: Array<IModelFilter>;
|
|
107
|
-
create?: IModelModeOptions;
|
|
108
|
-
update?: IModelModeOptions;
|
|
109
|
-
list?: IModelModeOptions;
|
|
110
|
-
details?: IModelModeOptions;
|
|
111
|
-
remove?: IModelModeOptions;
|
|
112
|
-
customs?: Array<IModelModeOptionsCustom>;
|
|
113
|
-
/**
|
|
114
|
-
* @desc - Allow export field data
|
|
115
|
-
*/
|
|
116
|
-
export?: boolean;
|
|
117
|
-
/**
|
|
118
|
-
* @desc - Allow import field data
|
|
119
|
-
*/
|
|
120
|
-
import?: boolean;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export interface IModelModeOptions {
|
|
124
|
-
permissions?: Array<string>;
|
|
125
|
-
enabled?: ISpecification;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export interface IModelModeOptionsCustom extends IModelModeOptions {
|
|
129
|
-
mode: string;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export interface IFieldOptions extends IFieldMetadata {
|
|
133
|
-
create?: IFieldModifyMetadata | boolean;
|
|
134
|
-
update?: IFieldEditMetadata | boolean;
|
|
135
|
-
list?: IFieldListMetadata | boolean;
|
|
136
|
-
details?: IFieldDetailsMetadata | boolean;
|
|
137
|
-
customs?: Array<IModelMetadataCustom>;
|
|
138
|
-
search?: boolean;
|
|
139
|
-
info?: string;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export interface IModelMetadataCustom extends IModelMetadata {
|
|
143
|
-
mode: string;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export interface IFieldUniqueMetadata {
|
|
147
|
-
withFields?: Array<string>;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export interface IFieldCustomMetadata
|
|
151
|
-
extends IFieldModifyMetadata,
|
|
152
|
-
IFieldListMetadata {
|
|
153
|
-
mode: string;
|
|
154
|
-
}
|
package/src/lib/symbols.ts
DELETED
package/src/lib/utils.spec.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { Field } from './decorators/field/field.decorator';
|
|
2
|
-
import { Model } from './decorators/model/model.decorator';
|
|
3
|
-
import { FieldType } from './interfaces';
|
|
4
|
-
import {
|
|
5
|
-
castModel,
|
|
6
|
-
getInvalidFields,
|
|
7
|
-
getModelFieldKeys,
|
|
8
|
-
getModelFieldOptions,
|
|
9
|
-
isModel,
|
|
10
|
-
} from './utils';
|
|
11
|
-
|
|
12
|
-
describe('shared-models: utils', () => {
|
|
13
|
-
describe('getModelFieldKeys()', () => {
|
|
14
|
-
it('should return empty array when fields is not defined', () => {
|
|
15
|
-
@Model({})
|
|
16
|
-
class Test {}
|
|
17
|
-
|
|
18
|
-
const result = getModelFieldKeys(Test);
|
|
19
|
-
|
|
20
|
-
expect(result).toStrictEqual([]);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should return field keys array', () => {
|
|
24
|
-
@Model({})
|
|
25
|
-
class Test {
|
|
26
|
-
@Field({}) test1!: any;
|
|
27
|
-
@Field({}) test2!: any;
|
|
28
|
-
@Field({}) test3!: any;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const result = getModelFieldKeys(Test);
|
|
32
|
-
|
|
33
|
-
expect(result).toStrictEqual(['test1', 'test2', 'test3']);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
describe('getModelFieldOptions', () => {
|
|
38
|
-
it('should return options', () => {
|
|
39
|
-
const options = { required: true, type: FieldType.password };
|
|
40
|
-
|
|
41
|
-
@Model({})
|
|
42
|
-
class Test {
|
|
43
|
-
@Field(options) test!: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
expect(getModelFieldOptions(new Test(), 'test')).toStrictEqual(options);
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('isModel', () => {
|
|
51
|
-
it('should return true when model type', () => {
|
|
52
|
-
@Model({})
|
|
53
|
-
class Test {
|
|
54
|
-
@Field({}) test!: string;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
expect(isModel(new Test())).toBe(true);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should return false when other type', () => {
|
|
61
|
-
expect(isModel(new Date())).toBe(false);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
describe('getInvalidFields', () => {
|
|
66
|
-
it('should return empty array when correct', () => {
|
|
67
|
-
@Model({})
|
|
68
|
-
class Test {
|
|
69
|
-
@Field({}) test!: string;
|
|
70
|
-
@Field({
|
|
71
|
-
required: true,
|
|
72
|
-
})
|
|
73
|
-
test2!: string;
|
|
74
|
-
@Field({
|
|
75
|
-
create: {
|
|
76
|
-
required: true,
|
|
77
|
-
},
|
|
78
|
-
})
|
|
79
|
-
test3!: string;
|
|
80
|
-
@Field({
|
|
81
|
-
create: {
|
|
82
|
-
required: false,
|
|
83
|
-
},
|
|
84
|
-
})
|
|
85
|
-
test4!: string;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const instance = new Test();
|
|
89
|
-
instance.test2 = '12';
|
|
90
|
-
instance.test3 = '21';
|
|
91
|
-
|
|
92
|
-
expect(getInvalidFields(instance, 'create', null as any).length).toBe(0);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should return array when incorrect', () => {
|
|
96
|
-
@Model({})
|
|
97
|
-
class Test {
|
|
98
|
-
@Field({}) test!: string;
|
|
99
|
-
@Field({
|
|
100
|
-
required: true,
|
|
101
|
-
})
|
|
102
|
-
test2!: string;
|
|
103
|
-
@Field({
|
|
104
|
-
create: {
|
|
105
|
-
required: true,
|
|
106
|
-
},
|
|
107
|
-
})
|
|
108
|
-
test3!: string;
|
|
109
|
-
@Field({
|
|
110
|
-
create: {
|
|
111
|
-
required: false,
|
|
112
|
-
},
|
|
113
|
-
})
|
|
114
|
-
test4!: string;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const instance = new Test();
|
|
118
|
-
instance.test2 = '12';
|
|
119
|
-
|
|
120
|
-
expect(getInvalidFields(instance, 'create', null as any).length).toBe(1);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('should return empty array when not model', () => {
|
|
124
|
-
expect(getInvalidFields(new Date(), 'create', null as any).length).toBe(
|
|
125
|
-
0,
|
|
126
|
-
);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe('castModel', () => {
|
|
131
|
-
it('should remove unused fields', () => {
|
|
132
|
-
@Model({})
|
|
133
|
-
class Test {
|
|
134
|
-
@Field({}) test!: string;
|
|
135
|
-
@Field({
|
|
136
|
-
create: true,
|
|
137
|
-
})
|
|
138
|
-
test2!: string;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const instance = new Test();
|
|
142
|
-
instance.test = 'asd';
|
|
143
|
-
instance.test2 = 'aaa';
|
|
144
|
-
|
|
145
|
-
castModel(instance, 'create', null as any);
|
|
146
|
-
|
|
147
|
-
expect(instance.test).not.toBeDefined();
|
|
148
|
-
expect(instance.test2).toBeDefined();
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
});
|
package/src/lib/utils.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
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 ||
|
|
69
|
-
(instance as any)[key] === undefined ||
|
|
70
|
-
(instance as any)[key] === '')
|
|
71
|
-
)
|
|
72
|
-
result.push(key);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function castModel<T>(
|
|
79
|
-
instance: T,
|
|
80
|
-
mode: 'create' | 'update' | string,
|
|
81
|
-
permissions: Array<string>,
|
|
82
|
-
): void {
|
|
83
|
-
if (!isModel(instance)) return;
|
|
84
|
-
|
|
85
|
-
const fieldsWithOptions = getModelFieldsWithOptions(instance);
|
|
86
|
-
|
|
87
|
-
Object.keys(instance as any)
|
|
88
|
-
.filter((key) => key !== 'id')
|
|
89
|
-
.forEach((key) => {
|
|
90
|
-
const fieldWidthOptions = fieldsWithOptions.find((f) => f.key === key);
|
|
91
|
-
|
|
92
|
-
if (!fieldWidthOptions) {
|
|
93
|
-
delete (instance as any)[key];
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
(mode === 'create' || mode === 'update') &&
|
|
99
|
-
(!fieldWidthOptions.options[mode] ||
|
|
100
|
-
(permissions &&
|
|
101
|
-
(fieldWidthOptions.options[mode] as IFieldOptions).permissions &&
|
|
102
|
-
!(
|
|
103
|
-
fieldWidthOptions.options[mode] as IFieldOptions
|
|
104
|
-
)?.permissions?.some((op) => permissions.some((p) => op === p))))
|
|
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
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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
|
-
}
|
package/tsconfig.lib.json
DELETED
package/tsconfig.spec.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
}
|
|
File without changes
|