badmfck-api-server 3.4.0 → 3.4.2

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.
@@ -65,7 +65,6 @@ export declare class APIService extends BaseService {
65
65
  private monitor;
66
66
  private started;
67
67
  private requestsCount;
68
- private server;
69
68
  netLog: APIServiceNetworkLogItem[];
70
69
  constructor(options?: APIServiceOptions | null);
71
70
  init(): Promise<void>;
@@ -95,12 +95,11 @@ async function Initializer(services) {
95
95
  exports.Initializer = Initializer;
96
96
  class APIService extends BaseService_1.BaseService {
97
97
  static nextLogID = 0;
98
- version = "3.4.0";
98
+ version = "3.4.1";
99
99
  options;
100
100
  monitor = null;
101
101
  started = new Date();
102
102
  requestsCount = 0;
103
- server = http_1.default;
104
103
  netLog = [];
105
104
  constructor(options) {
106
105
  super('HTTP Service');
@@ -157,7 +156,7 @@ class APIService extends BaseService_1.BaseService {
157
156
  };
158
157
  const app = (0, express_1.default)();
159
158
  const server = http_1.default.createServer(app);
160
- exports.REQ_HTTP_SERVER.listener = async (ignore) => { return { express: app, http: server }; };
159
+ exports.REQ_HTTP_SERVER.listener = async (_) => { return { express: app, http: server }; };
161
160
  if (this.options.isProductionEnvironment)
162
161
  app.set("env", 'production');
163
162
  app.use(express_1.default.json({ limit: '10mb' }));
@@ -386,6 +386,8 @@ class MysqlAdapter {
386
386
  if (fields) {
387
387
  for (let i in fields) {
388
388
  const name = i;
389
+ if (name.startsWith("$__"))
390
+ continue;
389
391
  let queryField = fields[i];
390
392
  if (queryField === undefined)
391
393
  continue;
@@ -409,6 +411,8 @@ class MysqlAdapter {
409
411
  let insertFieldValues = [];
410
412
  for (let i in fields) {
411
413
  let f = fields[i];
414
+ if (f.name?.startsWith("$__"))
415
+ continue;
412
416
  if (f.ignoreInInsert)
413
417
  continue;
414
418
  insertFieldNames.push('`' + f.name + '`');
@@ -423,6 +427,8 @@ class MysqlAdapter {
423
427
  let oninsertValues = [];
424
428
  for (let i in fields) {
425
429
  let f = fields[i];
430
+ if (f.name?.startsWith("$__"))
431
+ continue;
426
432
  if (f.ignoreInInsert)
427
433
  continue;
428
434
  oninsertNames.push(f.name);
@@ -434,6 +440,8 @@ class MysqlAdapter {
434
440
  let onUpdate = [];
435
441
  for (let i in fields) {
436
442
  let f = fields[i];
443
+ if (f.name?.startsWith("$__"))
444
+ continue;
437
445
  if (f.ignoreInUpdate)
438
446
  continue;
439
447
  onUpdate.push('`' + f.name + '` = ' + f.__parsedValue);
@@ -444,6 +452,8 @@ class MysqlAdapter {
444
452
  let onDuplicate = [];
445
453
  for (let i in fields) {
446
454
  let f = fields[i];
455
+ if (f.name?.startsWith("$__"))
456
+ continue;
447
457
  if (!f.useInReplace)
448
458
  continue;
449
459
  onDuplicate.push('`' + f.name + f.__parsedValue);
@@ -19,6 +19,18 @@ export declare enum ValidationReport {
19
19
  VALUE_TOO_SHORT = 5,
20
20
  VALUE_INCORRECT = 6
21
21
  }
22
+ type ExtractOptionalFieldNames<T> = {
23
+ [K in keyof T as K extends `$__${infer Base}_optinal` ? Base extends keyof T ? T[K] extends true ? Base : never : never : never]: true;
24
+ };
25
+ type OptionalFields<T> = keyof ExtractOptionalFieldNames<T> & string;
26
+ type CleanFields<T> = {
27
+ [K in keyof T as K extends `$__${string}` ? never : K]: T[K];
28
+ };
29
+ export type ValidationModel<T> = {
30
+ [K in keyof CleanFields<T> as K extends OptionalFields<T> ? K : never]?: CleanFields<T>[K];
31
+ } & {
32
+ [K in keyof CleanFields<T> as K extends OptionalFields<T> ? never : K]: CleanFields<T>[K];
33
+ };
22
34
  export declare class Validator {
23
35
  static validateObject(fields: string[], object: any): boolean;
24
36
  static documentStructure(structure: any): Record<string, any> | undefined;
@@ -28,3 +40,4 @@ export declare class Validator {
28
40
  static convertToType<T>(value: any, type: "string" | "number" | "boolean" | "date"): T | null;
29
41
  static validateValue(value: string | number | boolean, opt?: IValidatorOptions): ValidationReport;
30
42
  }
43
+ export {};
@@ -91,11 +91,16 @@ class Validator {
91
91
  }
92
92
  let optional = structure['$__' + i + "_optional"] || false;
93
93
  if (!(i in object)) {
94
- if (!optional)
95
- errors.push("no field '" + i + "'");
94
+ if (structure["$__" + i + "_default"]) {
95
+ object[i] = structure["$__" + i + "_default"];
96
+ }
96
97
  else {
97
- foundKeys.push(i);
98
- continue;
98
+ if (!optional)
99
+ errors.push("no field '" + i + "'");
100
+ else {
101
+ foundKeys.push(i);
102
+ continue;
103
+ }
99
104
  }
100
105
  }
101
106
  if (typeof structure[i] === "number" && typeof object[i] === "string") {
@@ -126,6 +131,22 @@ class Validator {
126
131
  errors.push("wrong value for field '" + i + "', expected " + expected.join(", ") + " got: " + value);
127
132
  }
128
133
  }
134
+ if (structure['$__' + i + "_values"]) {
135
+ const values = structure['$__' + i + "_values"];
136
+ if (Array.isArray(values) && values.length) {
137
+ if (!values.includes(object[i])) {
138
+ let caseValue = null;
139
+ for (let i of values) {
140
+ const v = i.toLowerCase();
141
+ if (v === (object[i] + "").toLowerCase()) {
142
+ caseValue = object[i];
143
+ break;
144
+ }
145
+ }
146
+ errors.push("Wrong value for field '" + i + "'" + (caseValue ? ", check case for: " + caseValue : ""));
147
+ }
148
+ }
149
+ }
129
150
  if (typeof structure[i] === "string" && structure["$__" + i + "_regex"]) {
130
151
  const regex = structure["$__" + i + "_regex"];
131
152
  const reg = new RegExp(regex);
@@ -145,6 +166,12 @@ class Validator {
145
166
  }
146
167
  }
147
168
  if (typeof structure[i] === "number") {
169
+ if (typeof object[i] === "string") {
170
+ if (object[i].indexOf("."))
171
+ object[i] = parseFloat(object[i]);
172
+ else
173
+ object[i] = parseInt(object[i]);
174
+ }
148
175
  if (isNaN(object[i]))
149
176
  errors.push("wrong value for field '" + i + "', expected number got " + object[i]);
150
177
  if (structure[i] != 0) {
@@ -170,6 +197,16 @@ class Validator {
170
197
  if (object[i] > max)
171
198
  errors.push("value for field '" + i + "' is too big, expected less than " + max + " got: " + object[i]);
172
199
  }
200
+ if (typeof object[i] === "string" && structure["$__" + i + "_min"]) {
201
+ const min = structure["$__" + i + "_min"];
202
+ if (object[i].length < min)
203
+ errors.push("value length for field '" + i + "' is too small, expected more than " + min + " got: " + object[i].length);
204
+ }
205
+ if (typeof object[i] === "string" && structure["$__" + i + "_max"]) {
206
+ const max = structure["$__" + i + "_max"];
207
+ if (object[i].length > max)
208
+ errors.push("value length for field '" + i + "' is too big, expected less than " + max + " got: " + object[i].length);
209
+ }
173
210
  foundKeys.push(i);
174
211
  }
175
212
  if (errors.length > 0)
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { APIService, Initializer } from "./apiServer/APIService";
2
2
  import { LocalRequest } from "./apiServer/LocalRequest";
3
3
  import { MysqlService } from "./apiServer/MysqlService";
4
- import { Validator } from "./apiServer/helper/Validator";
4
+ import { Validator, ValidationModel } from "./apiServer/helper/Validator";
5
5
  import { LogService } from "./apiServer/LogService";
6
6
  import { ErrorUtils } from "./apiServer/structures/DefaultErrors";
7
7
  import { S_MONITOR_REGISTRATE_ACTION } from "./apiServer/monitor/Monitor";
@@ -9,4 +9,4 @@ import { DataProvider } from "./apiServer/helper/DataProvider";
9
9
  import { UID } from "./apiServer/helper/UID";
10
10
  import { ExternalService } from "./apiServer/external/ExternalService";
11
11
  import { DBService } from "./apiServer/DBService";
12
- export { UID, APIService, Initializer, LocalRequest, MysqlService, Validator, LogService, DataProvider, ErrorUtils, ExternalService, DBService, S_MONITOR_REGISTRATE_ACTION };
12
+ export { UID, APIService, Initializer, LocalRequest, ValidationModel, MysqlService, Validator, LogService, DataProvider, ErrorUtils, ExternalService, DBService, S_MONITOR_REGISTRATE_ACTION };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "3.4.0",
3
+ "version": "3.4.2",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",