@webtypen/webframez-core 0.2.27 → 0.2.29

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.
@@ -37,6 +37,7 @@ export type DataBuilderFieldType = {
37
37
  export declare class DataBuilder {
38
38
  private types;
39
39
  private fieldTypes;
40
+ newDataHandler: null | ((req: Request) => Promise<any>);
40
41
  getType(key: string): DataBuilderType | null;
41
42
  registerType(typeObj: DataBuilderType): this;
42
43
  registerFieldType(key: string, options?: any): this;
@@ -75,6 +76,15 @@ export declare class DataBuilder {
75
76
  status: string;
76
77
  data: any;
77
78
  }>;
79
+ detailsNewData(db: any, req: any): Promise<{
80
+ status: string;
81
+ message: string;
82
+ data?: undefined;
83
+ } | {
84
+ status: string;
85
+ data: any;
86
+ message?: undefined;
87
+ } | undefined>;
78
88
  apiAutoComplete(req: any): Promise<{
79
89
  status: string;
80
90
  data: void;
@@ -19,6 +19,7 @@ class DataBuilder {
19
19
  constructor() {
20
20
  this.types = {};
21
21
  this.fieldTypes = {};
22
+ this.newDataHandler = null;
22
23
  }
23
24
  getType(key) {
24
25
  return this.types[key] && this.types[key].key ? this.types[key] : null;
@@ -177,7 +178,7 @@ class DataBuilder {
177
178
  loadType(req) {
178
179
  return __awaiter(this, void 0, void 0, function* () {
179
180
  const type = this.getTypeFromRequest(req);
180
- const data = Object.assign(Object.assign({}, (yield this.typeForFrontend(type, req))), { fieldtypes: this.getFieldTypesFrontend() });
181
+ const data = Object.assign(Object.assign({}, (yield this.typeForFrontend(type, req))), { fieldtypes: this.getFieldTypesFrontend(), new_data_handler: this.newDataHandler !== null ? true : false });
181
182
  return {
182
183
  status: "success",
183
184
  data: Object.assign(Object.assign({}, data), { schema: Object.assign(Object.assign({}, (data.schema ? data.schema : {})), { fields: yield this.getFieldsFrontend(type.schema
@@ -237,7 +238,6 @@ class DataBuilder {
237
238
  elementVal = value;
238
239
  }
239
240
  }
240
- console.log("applyField ->", fieldPath, elementVal);
241
241
  lodash_1.default.set(element, fieldPath, elementVal);
242
242
  // lodash.set(
243
243
  // element,
@@ -426,6 +426,31 @@ class DataBuilder {
426
426
  };
427
427
  });
428
428
  }
429
+ detailsNewData(db, req) {
430
+ return __awaiter(this, void 0, void 0, function* () {
431
+ if (!this.newDataHandler) {
432
+ return;
433
+ }
434
+ const type = this.getTypeFromRequest(req);
435
+ if (!type || !type.schema || !type.schema.fields) {
436
+ throw new Error("Missing schema fields ...");
437
+ }
438
+ let data = null;
439
+ try {
440
+ data = yield this.newDataHandler(req);
441
+ }
442
+ catch (e) {
443
+ console.error(e);
444
+ }
445
+ if (data === null || data === undefined) {
446
+ return { status: "error", message: "Unexpected error generating the forms 'new-data' ..." };
447
+ }
448
+ return {
449
+ status: "success",
450
+ data: data,
451
+ };
452
+ });
453
+ }
429
454
  apiAutoComplete(req) {
430
455
  return __awaiter(this, void 0, void 0, function* () {
431
456
  const type = this.getTypeFromRequest(req);
@@ -27,6 +27,9 @@ class DataBuilderController extends Controller_1.Controller {
27
27
  else if (req.body.__builder_rest_api === "details") {
28
28
  return res.send(yield this.builder.details(db.client.db(null), req));
29
29
  }
30
+ else if (req.body.__builder_rest_api === "details-newdata") {
31
+ return res.send(yield this.builder.detailsNewData(db.client.db(null), req));
32
+ }
30
33
  else if (req.body.__builder_rest_api === "save") {
31
34
  return res.send(yield this.builder.save(db.client.db(null), req));
32
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.2.27",
3
+ "version": "0.2.29",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -40,6 +40,8 @@ export class DataBuilder {
40
40
  private types: { [key: string]: DataBuilderType } = {};
41
41
  private fieldTypes: { [key: string]: DataBuilderFieldType } = {};
42
42
 
43
+ newDataHandler: null | ((req: Request) => Promise<any>) = null;
44
+
43
45
  getType(key: string) {
44
46
  return this.types[key] && this.types[key].key ? this.types[key] : null;
45
47
  }
@@ -223,6 +225,7 @@ export class DataBuilder {
223
225
  const data = {
224
226
  ...(await this.typeForFrontend(type, req)),
225
227
  fieldtypes: this.getFieldTypesFrontend(),
228
+ new_data_handler: this.newDataHandler !== null ? true : false,
226
229
  };
227
230
  return {
228
231
  status: "success",
@@ -304,7 +307,6 @@ export class DataBuilder {
304
307
  }
305
308
  }
306
309
 
307
- console.log("applyField ->", fieldPath, elementVal);
308
310
  lodash.set(element, fieldPath, elementVal);
309
311
  // lodash.set(
310
312
  // element,
@@ -514,6 +516,33 @@ export class DataBuilder {
514
516
  };
515
517
  }
516
518
 
519
+ async detailsNewData(db: any, req: any) {
520
+ if (!this.newDataHandler) {
521
+ return;
522
+ }
523
+
524
+ const type = this.getTypeFromRequest(req);
525
+ if (!type || !type.schema || !type.schema.fields) {
526
+ throw new Error("Missing schema fields ...");
527
+ }
528
+
529
+ let data: any = null;
530
+ try {
531
+ data = await this.newDataHandler(req);
532
+ } catch (e: any) {
533
+ console.error(e);
534
+ }
535
+
536
+ if (data === null || data === undefined) {
537
+ return { status: "error", message: "Unexpected error generating the forms 'new-data' ..." };
538
+ }
539
+
540
+ return {
541
+ status: "success",
542
+ data: data,
543
+ };
544
+ }
545
+
517
546
  async apiAutoComplete(req: any) {
518
547
  const type = this.getTypeFromRequest(req);
519
548
  const field = req.body.__builder_field ? await this.getField(req, type, req.body.__builder_field) : null;
@@ -18,6 +18,8 @@ export class DataBuilderController extends Controller {
18
18
  return res.send(await this.builder.apiAutoComplete(req));
19
19
  } else if (req.body.__builder_rest_api === "details") {
20
20
  return res.send(await this.builder.details(db.client.db(null), req));
21
+ } else if (req.body.__builder_rest_api === "details-newdata") {
22
+ return res.send(await this.builder.detailsNewData(db.client.db(null), req));
21
23
  } else if (req.body.__builder_rest_api === "save") {
22
24
  return res.send(await this.builder.save(db.client.db(null), req));
23
25
  } else if (req.body.__builder_rest_api === "type") {