@carbonorm/carbonnode 3.0.2 → 3.0.4

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.
@@ -1,109 +1,81 @@
1
- import {C6Constants} from "api/C6Constants";
2
- import {iC6Object, iC6RestfulModel} from "./types/ormInterfaces";
3
-
4
-
5
- export default function <RestTableInterfaces extends { [key:string] : any }>(restfulObject: RestTableInterfaces, tableName: string | string[], C6: iC6Object, regexErrorHandler: (message:string) => void = alert) {
6
-
7
- let payload = {};
8
-
1
+ import { C6Constants } from "api/C6Constants";
2
+ import {iC6Object, iC6RestfulModel, iRestMethods, RequestQueryBody} from "./types/ormInterfaces";
3
+
4
+ export default function <
5
+ RequestMethod extends iRestMethods,
6
+ RestTableInterface extends { [key: string]: any },
7
+ CustomAndRequiredFields extends { [key: string]: any } = {},
8
+ RequestTableOverrides extends { [K in keyof RestTableInterface]?: any } = {}
9
+ >(
10
+ restfulObject: RequestQueryBody<RequestMethod, RestTableInterface, CustomAndRequiredFields, RequestTableOverrides>,
11
+ tableName: string | string[],
12
+ C6: iC6Object,
13
+ regexErrorHandler: (message: string) => void = alert
14
+ ) {
15
+ const payload: Record<string, any> = {};
9
16
  const tableNames = Array.isArray(tableName) ? tableName : [tableName];
10
17
 
11
- let tableDefinitions : (iC6RestfulModel<any, any, any> & any)[] = [];
12
-
13
- tableNames.forEach((tableName) => {
14
-
15
- let tableDefinition = Object.values(C6.TABLES).find((tableDefinition) => tableDefinition.TABLE_NAME === tableName);
16
-
17
- if (undefined === tableDefinition) {
18
-
19
- console.error(`Table name (${tableName}) is not found in the C6.TABLES object.`, C6.TABLES);
20
-
21
- throw new Error(`Table name (${tableName}) is not found in the C6.TABLES object.`);
22
-
18
+ const tableDefinitions: (iC6RestfulModel<any, any, any> & any)[] = tableNames.map((name) => {
19
+ const tableDefinition = Object.values(C6.TABLES).find((t) => t.TABLE_NAME === name);
20
+ if (!tableDefinition) {
21
+ console.error(`Table name (${name}) is not found in the C6.TABLES object.`, C6.TABLES);
22
+ throw new Error(`Table name (${name}) is not found in the C6.TABLES object.`);
23
23
  }
24
+ return tableDefinition;
25
+ });
24
26
 
25
- tableDefinitions.push(tableDefinition);
26
-
27
- })
28
-
29
- tableDefinitions.forEach((tableDefinition) => {
30
-
31
- Object.keys(restfulObject).forEach(value => {
32
-
33
- let shortReference = value.toUpperCase();
34
-
35
- switch (value) {
36
- case C6Constants.GET:
37
- case C6Constants.POST:
38
- case C6Constants.UPDATE:
39
- case C6Constants.REPLACE:
40
- case C6Constants.DELETE:
41
- case C6Constants.WHERE:
42
- case C6Constants.JOIN:
43
- case C6Constants.PAGINATION:
44
- if (Array.isArray(restfulObject[value])) {
45
- payload[value] = restfulObject[value].sort()
46
- } else if (typeof restfulObject[value] === 'object' && restfulObject[value] !== null) {
47
- payload[value] = Object.keys(restfulObject[value])
48
- .sort()
49
- .reduce((acc, key) => ({
50
- ...acc, [key]: restfulObject[value][key]
51
- }), {})
52
- }
53
- return
54
- default:
27
+ for (const tableDefinition of tableDefinitions) {
28
+ for (const value of Object.keys(restfulObject)) {
29
+ const shortReference = value.toUpperCase();
30
+
31
+ if ([
32
+ C6Constants.GET,
33
+ C6Constants.POST,
34
+ C6Constants.UPDATE,
35
+ C6Constants.REPLACE,
36
+ C6Constants.DELETE,
37
+ C6Constants.WHERE,
38
+ C6Constants.JOIN,
39
+ C6Constants.PAGINATION
40
+ ].includes(value)) {
41
+ const val = restfulObject[value];
42
+ if (Array.isArray(val)) {
43
+ payload[value] = val.sort();
44
+ } else if (typeof val === 'object' && val !== null) {
45
+ payload[value] = Object.keys(val)
46
+ .sort()
47
+ .reduce((acc, key) => ({ ...acc, [key]: val[key] }), {});
48
+ }
49
+ continue;
55
50
  }
56
51
 
57
52
  if (shortReference in tableDefinition) {
58
-
59
53
  const longName = tableDefinition[shortReference];
54
+ const columnValue = restfulObject[value];
55
+ payload[longName] = columnValue;
60
56
 
61
- payload[longName] = restfulObject[value]
62
-
63
- const regexValidations = tableDefinition.REGEX_VALIDATION[longName]
57
+ const regexValidations = tableDefinition.REGEX_VALIDATION[longName];
64
58
 
65
59
  if (regexValidations instanceof RegExp) {
66
-
67
- if (false === regexValidations.test(restfulObject[value])) {
68
-
69
- regexErrorHandler('Failed to match regex (' + regexValidations + ') for column (' + longName + ')')
70
-
71
- throw Error('Failed to match regex (' + regexValidations + ') for column (' + longName + ')')
72
-
60
+ if (!regexValidations.test(columnValue)) {
61
+ regexErrorHandler(`Failed to match regex (${regexValidations}) for column (${longName})`);
62
+ throw new Error(`Failed to match regex (${regexValidations}) for column (${longName})`);
73
63
  }
74
-
75
64
  } else if (typeof regexValidations === 'object' && regexValidations !== null) {
76
-
77
- Object.keys(regexValidations)?.forEach((errorMessage) => {
78
-
79
- const regex : RegExp = regexValidations[errorMessage];
80
-
81
- if (false === regex.test(restfulObject[value])) {
82
-
83
- const devErrorMessage = 'Failed to match regex (' + regex + ') for column (' + longName + ')';
84
-
85
- regexErrorHandler(errorMessage ?? devErrorMessage)
86
-
87
- throw Error(devErrorMessage)
88
-
65
+ for (const errorMessage in regexValidations) {
66
+ const regex: RegExp = regexValidations[errorMessage];
67
+ if (!regex.test(columnValue)) {
68
+ const devErrorMessage = `Failed to match regex (${regex}) for column (${longName})`;
69
+ regexErrorHandler(errorMessage || devErrorMessage);
70
+ throw new Error(devErrorMessage);
89
71
  }
90
-
91
- })
92
-
72
+ }
93
73
  }
94
-
95
74
  }
96
-
97
- })
98
-
99
- return true;
100
-
101
- });
75
+ }
76
+ }
102
77
 
103
78
  return Object.keys(payload)
104
79
  .sort()
105
- .reduce((acc, key) => ({
106
- ...acc, [key]: payload[key]
107
- }), {})
108
-
109
- };
80
+ .reduce((acc, key) => ({ ...acc, [key]: payload[key] }), {});
81
+ }
@@ -1,28 +1,82 @@
1
- import {apiReturn, iAPI, iRest} from "@carbonorm/carbonnode";
2
- import {Modify} from "../types/modifyTypes";
1
+ import {
2
+ apiReturn,
3
+ DetermineResponseDataType,
4
+ iRest,
5
+ iRestMethods,
6
+ iRestReactiveLifecycle,
7
+ RequestQueryBody
8
+ } from "@carbonorm/carbonnode";
9
+ import isVerbose from "../../variables/isVerbose";
3
10
 
4
11
  export abstract class Executor<
12
+ RequestMethod extends iRestMethods,
5
13
  RestShortTableName extends string = any,
6
14
  RestTableInterface extends { [key: string]: any } = any,
7
15
  PrimaryKey extends Extract<keyof RestTableInterface, string> = Extract<keyof RestTableInterface, string>,
8
16
  CustomAndRequiredFields extends { [key: string]: any } = any,
9
- RequestTableOverrides extends { [key: string]: any; } = { [key in keyof RestTableInterface]: any },
10
- ResponseDataType = any
17
+ RequestTableOverrides extends { [key in keyof RestTableInterface]: any } = { [key in keyof RestTableInterface]: any }
11
18
  > {
12
-
13
19
  public constructor(
14
20
  protected config: iRest<
21
+ RestShortTableName,
22
+ RestTableInterface,
23
+ PrimaryKey
24
+ >,
25
+ protected request: RequestQueryBody<
26
+ RequestMethod,
27
+ RestTableInterface,
28
+ CustomAndRequiredFields,
29
+ RequestTableOverrides
30
+ >
31
+ ) {}
32
+
33
+ abstract execute(): Promise<apiReturn<DetermineResponseDataType<RequestMethod, RestTableInterface>>>;
34
+
35
+ async runLifecycleHooks<
36
+ Phase extends keyof iRestReactiveLifecycle<
37
+ RequestMethod,
15
38
  RestShortTableName,
16
39
  RestTableInterface,
17
40
  PrimaryKey,
18
41
  CustomAndRequiredFields,
19
- RequestTableOverrides,
20
- ResponseDataType
21
- >,
22
- protected request: iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields = {} as iAPI<Modify<RestTableInterface, RequestTableOverrides>> & CustomAndRequiredFields
23
- ) {
24
- }
42
+ RequestTableOverrides
43
+ >
44
+ >(
45
+ phase: Phase,
46
+ args: Parameters<NonNullable<
47
+ iRestReactiveLifecycle<
48
+ RequestMethod,
49
+ RestShortTableName,
50
+ RestTableInterface,
51
+ PrimaryKey,
52
+ CustomAndRequiredFields,
53
+ RequestTableOverrides
54
+ >[Phase]
55
+ >[string]>[0]
56
+ ): Promise<void> {
57
+ const lifecycleGroup = this.config.restModel.LIFECYCLE_HOOKS[this.config.requestMethod]?.[phase];
58
+
59
+ if (!lifecycleGroup) return;
25
60
 
26
- abstract execute(): Promise<apiReturn<ResponseDataType>>
61
+ for (const [key, fn] of Object.entries(lifecycleGroup)) {
62
+ if (typeof fn === "function") {
63
+ if (isVerbose || (args.request as any).debug) {
64
+ console.groupCollapsed(`[LIFECYCLE] ${this.config.requestMethod}.${String(phase)}:${key}`);
65
+ console.log("config:", args.config);
66
+ console.log("request:", args.request);
67
+ if ("response" in args) {
68
+ console.log("response:", args.response);
69
+ }
70
+ console.groupEnd();
71
+ }
27
72
 
28
- }
73
+ try {
74
+ await fn(args);
75
+ } catch (err) {
76
+ console.error(`[LIFECYCLE ERROR] ${this.config.requestMethod}.${String(phase)}:${key}`, err);
77
+ throw err;
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }