badmfck-api-server 3.9.6 → 3.9.8

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.
@@ -93,7 +93,7 @@ async function Initializer(services) {
93
93
  }
94
94
  exports.Initializer = Initializer;
95
95
  class APIService extends BaseService_1.BaseService {
96
- version = "3.9.6";
96
+ version = "3.9.8";
97
97
  options;
98
98
  monitor = null;
99
99
  started = new Date();
@@ -1,9 +1,3 @@
1
- type SyncOptions = {
2
- fillOnlyWhenNull?: boolean;
3
- resetOnTypeMismatch?: boolean;
4
- repairArrayItems?: "coerce";
5
- coerceArrayFromObject?: "values" | "numericKeys" | "wrap" | "off";
6
- };
7
1
  export type TValidatorType = "string" | "number" | "boolean" | "date" | "array" | "object" | "email" | "phone";
8
2
  export interface IStructureOptions {
9
3
  optional?: boolean;
@@ -76,7 +70,7 @@ export declare class Validator {
76
70
  static filterStructure(structure: any, object: any): void;
77
71
  static isPlainObject(v: any): boolean;
78
72
  static clone<T>(v: T): T;
79
- static syncStructure(structure: any, object: any, opts?: SyncOptions): any;
73
+ static syncStructure(structure: any, node: any): any;
80
74
  static convertToType<T>(value: any, type: "string" | "number" | "boolean" | "date"): T | null;
81
75
  static validateValue(value: string | number | boolean, opt?: IValidatorOptions): ValidationReport;
82
76
  }
@@ -301,114 +301,84 @@ class Validator {
301
301
  return structuredClone(v);
302
302
  return JSON.parse(JSON.stringify(v));
303
303
  }
304
- static syncStructure(structure, object, opts = {
305
- fillOnlyWhenNull: true,
306
- resetOnTypeMismatch: true,
307
- repairArrayItems: "coerce",
308
- coerceArrayFromObject: "values"
309
- }) {
304
+ static syncStructure(structure, node) {
310
305
  if (structure === null || typeof structure !== "object")
311
- return object;
306
+ return node;
312
307
  if (Array.isArray(structure)) {
313
308
  const tmpl = structure[0];
314
- if (!Array.isArray(object) && this.isPlainObject(object)) {
315
- const mode = opts.coerceArrayFromObject ?? "values";
316
- if (mode === "numericKeys") {
317
- const numeric = Object.keys(object).filter(k => String(+k) === k).map(k => +k).sort((a, b) => a - b);
318
- object = numeric.map(i => object[String(i)]);
309
+ if (!Array.isArray(node) && this.isPlainObject(node)) {
310
+ const numericKeys = Object.keys(node)
311
+ .filter((k) => String(+k) === k)
312
+ .map((k) => +k)
313
+ .sort((a, b) => a - b);
314
+ const items = numericKeys.length > 0
315
+ ? numericKeys.map((i) => node[String(i)])
316
+ : Object.values(node);
317
+ return items.map((it) => this.syncStructure(tmpl, it));
318
+ }
319
+ if (!Array.isArray(node))
320
+ return node;
321
+ if (tmpl === undefined)
322
+ return node;
323
+ const out = new Array(node.length);
324
+ for (let i = 0; i < node.length; i++) {
325
+ const item = node[i];
326
+ if (item === null || item === undefined) {
327
+ out[i] = this.syncStructure(tmpl, this.clone(tmpl));
319
328
  }
320
- else if (mode === "wrap" && this.isPlainObject(tmpl)) {
321
- object = [object];
329
+ else if (this.isPlainObject(tmpl) && this.isPlainObject(item)) {
330
+ out[i] = this.syncStructure(tmpl, item);
322
331
  }
323
- else if (mode === "values") {
324
- object = Object.values(object);
332
+ else if (Array.isArray(tmpl) && Array.isArray(item)) {
333
+ out[i] = this.syncStructure(tmpl, item);
325
334
  }
326
335
  else {
327
- return object;
328
- }
329
- }
330
- if (!Array.isArray(object))
331
- return object;
332
- if (tmpl !== undefined) {
333
- for (let i = 0; i < object.length; i++) {
334
- let item = object[i];
335
- if (item === null || item === undefined) {
336
- object[i] = this.clone(tmpl);
337
- this.syncStructure(tmpl, object[i], opts);
338
- continue;
339
- }
340
- if (this.isPlainObject(tmpl) && this.isPlainObject(item)) {
341
- this.syncStructure(tmpl, item, opts);
342
- continue;
343
- }
344
- if (Array.isArray(tmpl) && Array.isArray(item)) {
345
- this.syncStructure(tmpl, item, opts);
346
- continue;
347
- }
348
- if (opts.repairArrayItems === "coerce") {
349
- object[i] = this.clone(tmpl);
350
- this.syncStructure(tmpl, object[i], opts);
351
- }
336
+ out[i] = this.syncStructure(tmpl, this.clone(tmpl));
352
337
  }
353
338
  }
354
- return object;
355
- }
356
- if (!this.isPlainObject(object))
357
- return object;
358
- for (const key in object) {
359
- if (!(key in structure))
360
- delete object[key];
339
+ return out;
361
340
  }
362
- for (const key in structure) {
363
- const structureValue = structure[key];
364
- const hasKey = Object.prototype.hasOwnProperty.call(object, key);
365
- const { optional = false, skip_validation = false } = (Validator.parseStructureOptions(key, structure) || {});
366
- const objectValue = object[key];
367
- if (!hasKey) {
368
- object[key] = this.clone(structureValue);
369
- continue;
370
- }
371
- if (key.startsWith("$__")) {
372
- object[key] = this.clone(structureValue);
341
+ const out = {};
342
+ const src = this.isPlainObject(node) ? node : {};
343
+ for (const k of Object.keys(structure)) {
344
+ const sv = structure[k];
345
+ if (k.startsWith("$__")) {
346
+ out[k] = this.clone(sv);
373
347
  continue;
374
348
  }
375
- if ((objectValue === null || objectValue === undefined) && structureValue !== null) {
376
- if (!optional || skip_validation) {
377
- object[key] = this.clone(structureValue);
349
+ const has = Object.prototype.hasOwnProperty.call(src, k);
350
+ const ov = has ? src[k] : undefined;
351
+ const { optional, skip_validation, default: def } = this.parseStructureOptions(k, structure);
352
+ if (!has) {
353
+ if (sv && typeof sv === "object") {
354
+ out[k] = this.syncStructure(sv, this.clone(sv));
355
+ }
356
+ else {
357
+ out[k] = this.clone(sv);
378
358
  }
379
359
  continue;
380
360
  }
381
- const svIsObj = this.isPlainObject(structureValue);
382
- const ovIsObj = this.isPlainObject(objectValue);
383
- const svIsArr = Array.isArray(structureValue);
384
- const ovIsArr = Array.isArray(objectValue);
385
- if (svIsObj || svIsArr) {
386
- if ((svIsObj && !ovIsObj) || (svIsArr && !ovIsArr)) {
387
- if (opts.resetOnTypeMismatch && svIsObj) {
388
- const base = this.clone(structureValue);
389
- if (this.isPlainObject(objectValue)) {
390
- for (const k in objectValue)
391
- base[k] = objectValue[k];
392
- this.syncStructure(structureValue, base, opts);
393
- object[key] = base;
394
- }
395
- else {
396
- object[key] = base;
397
- }
398
- }
361
+ if (ov === null || ov === undefined) {
362
+ if (def !== undefined && !optional) {
363
+ out[k] = this.clone(def);
364
+ }
365
+ else if (!optional || skip_validation) {
366
+ out[k] = sv && typeof sv === "object"
367
+ ? this.syncStructure(sv, this.clone(sv))
368
+ : this.clone(sv);
399
369
  }
400
370
  else {
401
- this.syncStructure(structureValue, objectValue, opts);
371
+ out[k] = ov;
402
372
  }
403
373
  continue;
404
374
  }
405
- if (typeof objectValue !== typeof structureValue) {
406
- if (opts.resetOnTypeMismatch) {
407
- object[key] = this.clone(structureValue);
408
- }
375
+ if (sv && typeof sv === "object") {
376
+ out[k] = this.syncStructure(sv, ov);
377
+ continue;
409
378
  }
379
+ out[k] = ov;
410
380
  }
411
- return object;
381
+ return out;
412
382
  }
413
383
  static convertToType(value, type) {
414
384
  if (value === null || value === undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "3.9.6",
3
+ "version": "3.9.8",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",