@jd-data-limited/easy-fm 4.0.14 → 4.1.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.
Files changed (65) hide show
  1. package/package.json +12 -6
  2. package/src/FMError.d.ts +2 -2
  3. package/src/FMError.js +507 -508
  4. package/src/FMError.ts +510 -511
  5. package/src/bin/cli.js +7 -5
  6. package/src/bin/generateTypes.js +215 -121
  7. package/src/connection/FMHost.d.ts +28 -6
  8. package/src/connection/FMHost.js +52 -25
  9. package/src/connection/FMHost.ts +75 -43
  10. package/src/connection/HostBase.d.ts +7 -3
  11. package/src/connection/HostBase.ts +8 -4
  12. package/src/connection/database.d.ts +50 -17
  13. package/src/connection/database.js +94 -66
  14. package/src/connection/database.ts +125 -105
  15. package/src/connection/databaseBase.d.ts +11 -6
  16. package/src/connection/databaseBase.ts +9 -8
  17. package/src/connection/generateAuthorizationHeaders.d.ts +1 -1
  18. package/src/connection/generateAuthorizationHeaders.js +10 -10
  19. package/src/connection/generateAuthorizationHeaders.ts +21 -21
  20. package/src/databaseStructure.d.ts +2 -4
  21. package/src/databaseStructure.ts +3 -5
  22. package/src/index.d.ts +17 -17
  23. package/src/index.js +14 -14
  24. package/src/index.ts +28 -25
  25. package/src/layouts/layout.d.ts +17 -6
  26. package/src/layouts/layout.js +19 -8
  27. package/src/layouts/layout.ts +33 -23
  28. package/src/layouts/layoutBase.d.ts +5 -5
  29. package/src/layouts/layoutBase.ts +6 -6
  30. package/src/layouts/layoutInterface.d.ts +3 -5
  31. package/src/layouts/layoutInterface.ts +4 -6
  32. package/src/layouts/layoutRecordManager.d.ts +29 -6
  33. package/src/layouts/layoutRecordManager.js +32 -9
  34. package/src/layouts/layoutRecordManager.ts +42 -19
  35. package/src/layouts/recordFieldsMap.d.ts +2 -4
  36. package/src/layouts/recordFieldsMap.ts +2 -4
  37. package/src/models/apiResults.d.ts +7 -13
  38. package/src/models/apiResults.ts +58 -58
  39. package/src/records/field.d.ts +42 -12
  40. package/src/records/field.js +69 -59
  41. package/src/records/field.ts +102 -81
  42. package/src/records/getOperations/recordGetOperation.d.ts +44 -24
  43. package/src/records/getOperations/recordGetOperation.js +89 -58
  44. package/src/records/getOperations/recordGetOperation.ts +137 -108
  45. package/src/records/layoutRecord.d.ts +28 -14
  46. package/src/records/layoutRecord.js +75 -65
  47. package/src/records/layoutRecord.ts +126 -123
  48. package/src/records/layoutRecordBase.d.ts +2 -2
  49. package/src/records/layoutRecordBase.ts +3 -3
  50. package/src/records/portal.d.ts +19 -5
  51. package/src/records/portal.js +19 -5
  52. package/src/records/portal.ts +28 -16
  53. package/src/records/portalBase.d.ts +5 -5
  54. package/src/records/portalBase.ts +6 -6
  55. package/src/records/portalRecord.d.ts +16 -8
  56. package/src/records/portalRecord.js +16 -6
  57. package/src/records/portalRecord.ts +29 -19
  58. package/src/records/recordBase.d.ts +17 -7
  59. package/src/records/recordBase.js +32 -22
  60. package/src/records/recordBase.ts +50 -43
  61. package/src/types.d.ts +21 -24
  62. package/src/types.ts +55 -59
  63. package/src/utils/query.d.ts +12 -5
  64. package/src/utils/query.js +28 -9
  65. package/src/utils/query.ts +40 -15
@@ -1,13 +1,12 @@
1
1
  /*
2
2
  * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
- import { RecordBase } from "./recordBase.js";
5
- import { PortalRecord } from "./portalRecord.js";
6
- import { Portal } from "./portal.js";
7
- import { FMError } from "../FMError.js";
8
- import * as moment from "moment/moment.js";
4
+ import { RecordBase } from './recordBase.js';
5
+ import { PortalRecord } from './portalRecord.js';
6
+ import { Portal } from './portal.js';
7
+ import { FMError } from '../FMError.js';
8
+ import * as moment from 'moment/moment.js';
9
9
  export class LayoutRecord extends RecordBase {
10
- // @ts-ignore
11
10
  portals = {};
12
11
  portalsToInclude;
13
12
  constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null, portalsToInclude = []) {
@@ -20,38 +19,44 @@ export class LayoutRecord extends RecordBase {
20
19
  get portalsArray() {
21
20
  return Object.values(this.portals);
22
21
  }
22
+ /**
23
+ * Asynchronously commits the changes made to the current record.
24
+ * @param {extraBodyOptions} extraBody - The options for the extra body elements.
25
+ * @returns {Promise<this>} - A promise that resolves with the modified object.
26
+ * @throws {FMError} - If an error occurs during the commit process.
27
+ */
23
28
  async commit(extraBody = {}) {
24
- let data = this.toObject();
29
+ const data = this.toObject();
25
30
  delete data.recordId;
26
31
  delete data.modId;
27
32
  if (extraBody.scripts?.after) {
28
- data["script"] = extraBody.scripts.after.name;
33
+ data.script = extraBody.scripts.after.name;
29
34
  if (extraBody.scripts.after.parameter)
30
- data["script.param"] = extraBody.scripts.after.parameter;
35
+ data['script.param'] = extraBody.scripts.after.parameter;
31
36
  }
32
37
  if (extraBody.scripts?.prerequest) {
33
- data["script.prerequest"] = extraBody.scripts.prerequest.name;
38
+ data['script.prerequest'] = extraBody.scripts.prerequest.name;
34
39
  if (extraBody.scripts.prerequest.parameter)
35
- data["script.prerequest.param"] = extraBody.scripts.prerequest.parameter;
40
+ data['script.prerequest.param'] = extraBody.scripts.prerequest.parameter;
36
41
  }
37
42
  if (extraBody.scripts?.presort) {
38
- data["script.presort"] = extraBody.scripts.presort.name;
43
+ data['script.presort'] = extraBody.scripts.presort.name;
39
44
  if (extraBody.scripts.presort.parameter)
40
- data["script.presort.param"] = extraBody.scripts.presort.parameter;
45
+ data['script.presort.param'] = extraBody.scripts.presort.parameter;
41
46
  }
42
47
  if (this.recordId === -1) {
43
48
  // This is a new LayoutRecord
44
- let res = await this.layout.database.apiRequestJSON(`${this.layout.endpoint}/records`, {
45
- method: "POST",
49
+ const res = await this.layout.database._apiRequestJSON(`${this.layout.endpoint}/records`, {
50
+ method: 'POST',
46
51
  body: JSON.stringify(data)
47
52
  });
48
53
  if (!res.response) {
49
54
  throw new FMError(res.messages[0].code, res.httpStatus, res);
50
55
  }
51
- else if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
56
+ else if (typeof res.response.scriptError !== 'undefined' && res.response.scriptError !== '0') {
52
57
  throw new FMError(res.response.scriptError, res.httpStatus, res);
53
58
  }
54
- else if (res.messages[0].code === "0") {
59
+ else if (res.messages[0].code === '0') {
55
60
  this.recordId = parseInt(res.response.recordId);
56
61
  this.modId = parseInt(res.response.modId);
57
62
  return this;
@@ -61,17 +66,17 @@ export class LayoutRecord extends RecordBase {
61
66
  }
62
67
  }
63
68
  // for (let item of Object.keys(data)) extraBody[item] = data[item]
64
- let res = await this.layout.database.apiRequestJSON(this.endpoint, {
65
- method: "PATCH",
69
+ const res = await this.layout.database._apiRequestJSON(this.endpoint, {
70
+ method: 'PATCH',
66
71
  body: JSON.stringify(data)
67
72
  });
68
73
  if (!res.response) {
69
74
  throw new FMError(res.messages[0].code, res.httpStatus, res);
70
75
  }
71
- else if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
76
+ else if (typeof res.response.scriptError !== 'undefined' && res.response.scriptError !== '0') {
72
77
  throw new FMError(res.response.scriptError, res.httpStatus, res);
73
78
  }
74
- else if (res.messages[0].code === "0") {
79
+ else if (res.messages[0].code === '0') {
75
80
  this.modId = +res.response.modId;
76
81
  this._onSave();
77
82
  return this;
@@ -81,29 +86,36 @@ export class LayoutRecord extends RecordBase {
81
86
  }
82
87
  }
83
88
  processPortalData(portalData) {
84
- for (let portalName of Object.keys(portalData)) {
85
- let _portal = new Portal(this, portalName);
89
+ for (const portalName of Object.keys(portalData)) {
90
+ const _portal = new Portal(this, portalName);
86
91
  _portal.records = portalData[portalName].map(item => {
87
- let fieldData = item;
92
+ const fieldData = item;
88
93
  delete fieldData.recordId;
89
94
  delete fieldData.modId;
90
95
  return new PortalRecord(this, _portal, parseInt(item.recordId), parseInt(item.modId), fieldData);
91
96
  });
92
- // @ts-ignore
97
+ // @ts-expect-error - This code is actually correct, but throws a typescript error
93
98
  this.portals[portalName] = _portal;
94
99
  }
95
100
  }
101
+ /**
102
+ * Re-fetches the current record from the database server.
103
+ * Throws an error if commit() has not been called.
104
+ *
105
+ * @return {Promise<this>} A Promise that resolves to this RecordBase instance if the record is successfully retrieved.
106
+ * @throws {Error} If commit() has not been called.
107
+ * @throws {FMError} If the retrieval fails.
108
+ */
96
109
  async get() {
97
- let trace = new Error();
98
110
  if (this.recordId === -1) {
99
- throw "Cannot get this RecordBase until a commit() is done.";
111
+ throw new Error('Cannot get this RecordBase until a commit() is done.');
100
112
  }
101
113
  if (!this.layout.metadata)
102
114
  await this.layout.getLayoutMeta();
103
- let res = await this.layout.database.apiRequestJSON(this.endpoint, {
104
- method: "GET"
115
+ const res = await this.layout.database._apiRequestJSON(this.endpoint, {
116
+ method: 'GET'
105
117
  });
106
- if (res.response && res.messages[0].code === "0") {
118
+ if (res.response && res.messages[0].code === '0') {
107
119
  // console.log(res, res.response.data)
108
120
  this.modId = +res.response.data[0].modId;
109
121
  this.processFieldData(res.response.data[0].fieldData);
@@ -117,20 +129,20 @@ export class LayoutRecord extends RecordBase {
117
129
  }
118
130
  }
119
131
  async duplicate() {
120
- let trace = new Error();
121
- let res = await this.layout.database.apiRequestJSON(this.endpoint, {
122
- method: "POST"
132
+ const trace = new Error();
133
+ const res = await this.layout.database._apiRequestJSON(this.endpoint, {
134
+ method: 'POST'
123
135
  });
124
136
  if (!res.response) {
125
137
  throw new FMError(res.messages[0].code, res.httpStatus, res, trace);
126
138
  }
127
- else if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
139
+ else if (typeof res.response.scriptError !== 'undefined' && res.response.scriptError !== '0') {
128
140
  throw new FMError(res.response.scriptError, res.httpStatus, res, trace);
129
141
  }
130
- else if (res.messages[0].code === "0") {
131
- let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false);
132
- let _res = new LayoutRecord(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData);
133
- this.emit("duplicated");
142
+ else if (res.messages[0].code === '0') {
143
+ const data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false);
144
+ const _res = new LayoutRecord(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData);
145
+ this.emit('duplicated');
134
146
  return _res;
135
147
  }
136
148
  else {
@@ -138,63 +150,61 @@ export class LayoutRecord extends RecordBase {
138
150
  }
139
151
  }
140
152
  async delete() {
141
- let res = await this.layout.database.apiRequestJSON(this.endpoint, {
142
- method: "DELETE"
153
+ const res = await this.layout.database._apiRequestJSON(this.endpoint, {
154
+ method: 'DELETE'
143
155
  });
144
156
  console.log(res);
145
- if (typeof res.response?.scriptError !== "undefined" && res.response?.scriptError !== '0') {
157
+ if (typeof res.response?.scriptError !== 'undefined' && res.response?.scriptError !== '0') {
146
158
  throw new FMError(res.response.scriptError, res.httpStatus, res);
147
159
  }
148
- else if (res.messages[0].code === "0") {
149
- this.emit("deleted");
150
- return;
160
+ else if (res.messages[0].code === '0') {
161
+ this.emit('deleted');
151
162
  }
152
163
  else {
153
164
  throw new FMError(res.messages[0].code, res.httpStatus, res);
154
165
  }
155
166
  }
156
167
  fieldsToObject(filter = (a) => a.edited) {
157
- let fields_processed = {};
168
+ const fields_processed = {};
158
169
  let field;
159
170
  for (field of this.fieldsArray.filter(field => filter(field))) {
160
171
  let value = field.value;
161
172
  if (value instanceof Date) {
162
- // @ts-ignore
163
- let _value = moment.default(value)
164
- .utcOffset(this.layout.database.host.timezoneOffset);
165
- // @ts-ignore
173
+ let _value = moment.default(value);
174
+ _value = _value
175
+ .utcOffset(this.layout.database.host.timezoneOffsetFunc(_value));
166
176
  switch (field.metadata.result) {
167
- case "time":
168
- value = _value.format(this.layout.database.host.metadata.productInfo.timeFormat.replace("dd", "DD"));
177
+ case 'time':
178
+ value = _value.format(this.layout.database.host.timeFormat);
169
179
  break;
170
- case "date":
171
- value = _value.format(this.layout.database.host.metadata.productInfo.dateFormat.replace("dd", "DD"));
180
+ case 'date':
181
+ value = _value.format(this.layout.database.host.dateFormat);
172
182
  break;
173
183
  default:
174
- value = _value.format(this.layout.database.host.metadata.productInfo.timeStampFormat.replace("dd", "DD"));
184
+ value = _value.format(this.layout.database.host.timeStampFormat);
175
185
  }
176
186
  }
177
187
  fields_processed[field.id] = value;
178
188
  }
179
- let obj = {
180
- "recordId": this.recordId.toString(),
181
- "modId": this.modId.toString(),
182
- "fieldData": fields_processed
189
+ const obj = {
190
+ recordId: this.recordId.toString(),
191
+ modId: this.modId.toString(),
192
+ fieldData: fields_processed
183
193
  };
184
194
  return obj;
185
195
  }
186
196
  toObject(filter = (a) => a.edited, portalFilter = (a) => a.records.find(record => record.edited), portalRowFilter = (a) => a.edited, portalFieldFilter = (a) => a.edited) {
187
- let obj = {
197
+ const obj = {
188
198
  ...this.fieldsToObject(filter),
189
- portalData: {},
199
+ portalData: {}
190
200
  };
191
201
  // Check if there's been any edited portal information
192
- let portals = this.portalsArray.filter(a => portalFilter(a));
202
+ const portals = this.portalsArray.filter(a => portalFilter(a));
193
203
  if (portals) {
194
- obj["portalData"] = {};
195
- for (let portal of portals) {
196
- // @ts-ignore
197
- obj["portalData"][portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
204
+ obj.portalData = {};
205
+ for (const portal of portals) {
206
+ // @ts-expect-error
207
+ obj.portalData[portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
198
208
  return record.toObject(portalFieldFilter);
199
209
  });
200
210
  }
@@ -2,135 +2,146 @@
2
2
  * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
- import {extraBodyOptions} from "../types.js";
6
- import {RecordBase} from "./recordBase.js";
7
- import {PortalRecord} from "./portalRecord.js";
8
- import {Portal} from "./portal.js";
9
- import {LayoutInterface} from "../layouts/layoutInterface.js";
10
- import {FMError} from "../FMError.js";
11
- import {LayoutRecordBase} from "./layoutRecordBase.js";
12
- import {ApiFieldData, ApiPortalData, ApiRecordResponseObj, ApiRowDataDef} from "../models/apiResults.js";
13
- import {LayoutBase} from "../layouts/layoutBase.js"
14
- import * as moment from "moment/moment.js";
15
- import {Field, FieldValue} from "./field.js";
16
-
17
- export class LayoutRecord<LAYOUT extends LayoutInterface> extends RecordBase<LAYOUT["fields"]> implements LayoutRecordBase {
18
- // @ts-ignore
19
- portals: LAYOUT["portals"] = {}
20
- private readonly portalsToInclude: (string | number | symbol)[]
21
-
22
- constructor(
5
+ import {type extraBodyOptions} from '../types.js'
6
+ import {RecordBase} from './recordBase.js'
7
+ import {PortalRecord} from './portalRecord.js'
8
+ import {Portal} from './portal.js'
9
+ import {type LayoutInterface} from '../layouts/layoutInterface.js'
10
+ import {FMError} from '../FMError.js'
11
+ import {type LayoutRecordBase} from './layoutRecordBase.js'
12
+ import {
13
+ type ApiFieldData,
14
+ type ApiPortalData,
15
+ type ApiRecordResponseObj,
16
+ type ApiRowDataDef
17
+ } from '../models/apiResults.js'
18
+ import {type LayoutBase} from '../layouts/layoutBase.js'
19
+ import * as moment from 'moment/moment.js'
20
+ import {type Field, type FieldValue} from './field.js'
21
+
22
+ export class LayoutRecord<LAYOUT extends LayoutInterface> extends RecordBase<LAYOUT['fields']> implements LayoutRecordBase {
23
+ portals: LAYOUT['portals'] = {}
24
+ private readonly portalsToInclude: Array<string | number | symbol>
25
+
26
+ constructor (
23
27
  layout: LayoutBase,
24
28
  recordId: number | string,
25
29
  modId = recordId,
26
30
  fieldData: Record<string, string | number> = {},
27
- portalData: ApiPortalData | null = null, portalsToInclude: (keyof LAYOUT["portals"])[] = [])
28
- {
29
- super(layout, parseInt(recordId as string), parseInt(modId as string), fieldData);
31
+ portalData: ApiPortalData | null = null, portalsToInclude: Array<keyof LAYOUT['portals']> = []) {
32
+ super(layout, parseInt(recordId as string), parseInt(modId as string), fieldData)
30
33
  this.portalsToInclude = portalsToInclude
31
34
  if (portalData) {
32
35
  this.processPortalData(portalData)
33
36
  }
34
37
  }
35
38
 
36
- get portalsArray(): Portal<any>[] {
39
+ get portalsArray (): Array<Portal<any>> {
37
40
  return Object.values(this.portals)
38
41
  }
39
42
 
40
- async commit(extraBody: extraBodyOptions = {}): Promise<this> {
41
- let data: any = this.toObject()
43
+ /**
44
+ * Asynchronously commits the changes made to the current record.
45
+ * @param {extraBodyOptions} extraBody - The options for the extra body elements.
46
+ * @returns {Promise<this>} - A promise that resolves with the modified object.
47
+ * @throws {FMError} - If an error occurs during the commit process.
48
+ */
49
+ async commit (extraBody: extraBodyOptions = {}): Promise<this> {
50
+ const data: any = this.toObject()
42
51
  delete data.recordId
43
52
  delete data.modId
44
53
 
45
54
  if (extraBody.scripts?.after) {
46
- data["script"] = extraBody.scripts.after.name
47
- if (extraBody.scripts.after.parameter) data["script.param"] = extraBody.scripts.after.parameter
55
+ data.script = extraBody.scripts.after.name
56
+ if (extraBody.scripts.after.parameter) data['script.param'] = extraBody.scripts.after.parameter
48
57
  }
49
58
  if (extraBody.scripts?.prerequest) {
50
- data["script.prerequest"] = extraBody.scripts.prerequest.name
51
- if (extraBody.scripts.prerequest.parameter) data["script.prerequest.param"] = extraBody.scripts.prerequest.parameter
59
+ data['script.prerequest'] = extraBody.scripts.prerequest.name
60
+ if (extraBody.scripts.prerequest.parameter) data['script.prerequest.param'] = extraBody.scripts.prerequest.parameter
52
61
  }
53
62
  if (extraBody.scripts?.presort) {
54
- data["script.presort"] = extraBody.scripts.presort.name
55
- if (extraBody.scripts.presort.parameter) data["script.presort.param"] = extraBody.scripts.presort.parameter
63
+ data['script.presort'] = extraBody.scripts.presort.name
64
+ if (extraBody.scripts.presort.parameter) data['script.presort.param'] = extraBody.scripts.presort.parameter
56
65
  }
57
66
 
58
67
  if (this.recordId === -1) {
59
68
  // This is a new LayoutRecord
60
- let res = await this.layout.database.apiRequestJSON<{ recordId: string, modId: string }>(`${this.layout.endpoint}/records`, {
61
- method: "POST",
62
- body: JSON.stringify(data)
63
- })
69
+ const res = await this.layout.database._apiRequestJSON<{ recordId: string, modId: string }>(`${this.layout.endpoint}/records`, {
70
+ method: 'POST',
71
+ body: JSON.stringify(data)
72
+ })
64
73
 
65
- if (!res.response) {
66
- throw new FMError(res.messages[0].code, res.httpStatus, res)
67
- }
68
- else if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
69
- throw new FMError(res.response.scriptError, res.httpStatus, res)
70
- }
71
- else if (res.messages[0].code === "0") {
72
- this.recordId = parseInt(res.response.recordId)
73
- this.modId = parseInt(res.response.modId)
74
- return this
75
- }
76
- else {
77
- throw new FMError(res.messages[0].code, res.httpStatus, res)
78
- }
74
+ if (!res.response) {
75
+ throw new FMError(res.messages[0].code, res.httpStatus, res)
76
+ } else if (typeof res.response.scriptError !== 'undefined' && res.response.scriptError !== '0') {
77
+ throw new FMError(res.response.scriptError, res.httpStatus, res)
78
+ } else if (res.messages[0].code === '0') {
79
+ this.recordId = parseInt(res.response.recordId)
80
+ this.modId = parseInt(res.response.modId)
81
+ return this
82
+ } else {
83
+ throw new FMError(res.messages[0].code, res.httpStatus, res)
84
+ }
79
85
  }
80
86
 
81
87
  // for (let item of Object.keys(data)) extraBody[item] = data[item]
82
- let res = await this.layout.database.apiRequestJSON<{
83
- modId: string, newPortalRecordInfo: {
84
- tableName: string,
85
- recordId: string,
88
+ const res = await this.layout.database._apiRequestJSON<{
89
+ modId: string,
90
+ newPortalRecordInfo: Array<{
91
+ tableName: string
92
+ recordId: string
86
93
  modId: string
87
- }[]
94
+ }>
88
95
  }>(this.endpoint, {
89
- method: "PATCH",
96
+ method: 'PATCH',
90
97
  body: JSON.stringify(data)
91
98
  })
92
99
  if (!res.response) {
93
100
  throw new FMError(res.messages[0].code, res.httpStatus, res)
94
- }
95
- else if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
101
+ } else if (typeof res.response.scriptError !== 'undefined' && res.response.scriptError !== '0') {
96
102
  throw new FMError(res.response.scriptError, res.httpStatus, res)
97
- }
98
- else if (res.messages[0].code === "0") {
103
+ } else if (res.messages[0].code === '0') {
99
104
  this.modId = +res.response.modId
100
105
  this._onSave()
101
106
  return this
102
- }
103
- else {
107
+ } else {
104
108
  throw new FMError(res.messages[0].code, res.httpStatus, res)
105
109
  }
106
110
  }
107
111
 
108
- protected processPortalData(portalData: ApiPortalData): void {
109
- for (let portalName of Object.keys(portalData)) {
110
- let _portal = new Portal(this, portalName)
112
+ protected processPortalData (portalData: ApiPortalData): void {
113
+ for (const portalName of Object.keys(portalData)) {
114
+ const _portal = new Portal(this, portalName)
111
115
  _portal.records = portalData[portalName].map(item => {
112
- let fieldData = item;
113
- delete fieldData.recordId;
114
- delete fieldData.modId;
115
- return new PortalRecord(this, _portal, parseInt(item.recordId as string), parseInt(item.modId as string), fieldData);
116
+ const fieldData = item
117
+ delete fieldData.recordId
118
+ delete fieldData.modId
119
+ return new PortalRecord(this, _portal, parseInt(item.recordId as string), parseInt(item.modId as string), fieldData)
116
120
  })
117
121
 
118
- // @ts-ignore
119
- this.portals[portalName] = _portal;
122
+ // @ts-expect-error - This code is actually correct, but throws a typescript error
123
+ this.portals[portalName] = _portal
120
124
  }
121
125
  }
122
126
 
123
- async get(): Promise<this> {
124
- let trace = new Error()
127
+ /**
128
+ * Re-fetches the current record from the database server.
129
+ * Throws an error if commit() has not been called.
130
+ *
131
+ * @return {Promise<this>} A Promise that resolves to this RecordBase instance if the record is successfully retrieved.
132
+ * @throws {Error} If commit() has not been called.
133
+ * @throws {FMError} If the retrieval fails.
134
+ */
135
+ async get (): Promise<this> {
125
136
  if (this.recordId === -1) {
126
- throw "Cannot get this RecordBase until a commit() is done."
137
+ throw new Error('Cannot get this RecordBase until a commit() is done.')
127
138
  }
128
139
  if (!this.layout.metadata) await this.layout.getLayoutMeta()
129
- let res = await this.layout.database.apiRequestJSON<ApiRecordResponseObj>(this.endpoint, {
130
- method: "GET"
140
+ const res = await this.layout.database._apiRequestJSON<ApiRecordResponseObj>(this.endpoint, {
141
+ method: 'GET'
131
142
  })
132
143
 
133
- if (res.response && res.messages[0].code === "0") {
144
+ if (res.response && res.messages[0].code === '0') {
134
145
  // console.log(res, res.response.data)
135
146
  this.modId = +res.response.data[0].modId
136
147
  this.processFieldData(res.response.data[0].fieldData)
@@ -142,97 +153,89 @@ export class LayoutRecord<LAYOUT extends LayoutInterface> extends RecordBase<LAY
142
153
  }
143
154
  }
144
155
 
145
- async duplicate(): Promise<LayoutRecord<LAYOUT>> {
146
- let trace = new Error()
147
- let res = await this.layout.database.apiRequestJSON<{recordId: string, modId: string}>(this.endpoint, {
148
- method: "POST"
156
+ async duplicate (): Promise<LayoutRecord<LAYOUT>> {
157
+ const trace = new Error()
158
+ const res = await this.layout.database._apiRequestJSON<{ recordId: string, modId: string }>(this.endpoint, {
159
+ method: 'POST'
149
160
  })
150
161
  if (!res.response) {
151
162
  throw new FMError(res.messages[0].code, res.httpStatus, res, trace)
152
- }
153
- else if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
163
+ } else if (typeof res.response.scriptError !== 'undefined' && res.response.scriptError !== '0') {
154
164
  throw new FMError(res.response.scriptError, res.httpStatus, res, trace)
155
- }
156
- else if (res.messages[0].code === "0") {
157
- let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false)
158
- let _res = new LayoutRecord<LAYOUT>(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData)
165
+ } else if (res.messages[0].code === '0') {
166
+ const data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false)
167
+ const _res = new LayoutRecord<LAYOUT>(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData)
159
168
 
160
- this.emit("duplicated")
169
+ this.emit('duplicated')
161
170
  return _res
162
- }
163
- else {
171
+ } else {
164
172
  throw new FMError(res.messages[0].code, res.httpStatus, res, trace)
165
173
  }
166
174
  }
167
175
 
168
- async delete(): Promise<void> {
169
- let res = await this.layout.database.apiRequestJSON(this.endpoint, {
170
- method: "DELETE"
176
+ async delete (): Promise<void> {
177
+ const res = await this.layout.database._apiRequestJSON(this.endpoint, {
178
+ method: 'DELETE'
171
179
  })
172
180
  console.log(res)
173
- if (typeof res.response?.scriptError !== "undefined" && res.response?.scriptError !== '0') {
181
+ if (typeof res.response?.scriptError !== 'undefined' && res.response?.scriptError !== '0') {
174
182
  throw new FMError(res.response.scriptError, res.httpStatus, res)
175
- }
176
- else if (res.messages[0].code === "0") {
177
- this.emit("deleted")
178
- return
179
- }
180
- else {
183
+ } else if (res.messages[0].code === '0') {
184
+ this.emit('deleted')
185
+ } else {
181
186
  throw new FMError(res.messages[0].code, res.httpStatus, res)
182
187
  }
183
188
  }
184
189
 
185
- fieldsToObject(filter = (a: Field<FieldValue>) => a.edited): Omit<ApiRowDataDef, "portalData"> {
186
- let fields_processed: ApiFieldData = {}
190
+ fieldsToObject (filter = (a: Field<FieldValue>) => a.edited): Omit<ApiRowDataDef, 'portalData'> {
191
+ const fields_processed: ApiFieldData = {}
187
192
  let field: Field<FieldValue>
188
193
  for (field of this.fieldsArray.filter(field => filter(field))) {
189
194
  let value = field.value as string | number | Date
190
195
  if (value instanceof Date) {
191
- // @ts-ignore
192
196
  let _value = moment.default(value)
193
- .utcOffset(this.layout.database.host.timezoneOffset)
194
-
195
- // @ts-ignore
197
+ _value = _value
198
+ .utcOffset(this.layout.database.host.timezoneOffsetFunc(_value))
196
199
 
197
200
  switch (field.metadata.result) {
198
- case "time":
199
- value = _value.format(this.layout.database.host.metadata.productInfo.timeFormat.replace("dd", "DD"))
201
+ case 'time':
202
+ value = _value.format(this.layout.database.host.timeFormat)
200
203
  break
201
- case "date":
202
- value = _value.format(this.layout.database.host.metadata.productInfo.dateFormat.replace("dd", "DD"))
204
+ case 'date':
205
+ value = _value.format(this.layout.database.host.dateFormat)
203
206
  break
204
207
  default:
205
- value = _value.format(this.layout.database.host.metadata.productInfo.timeStampFormat.replace("dd", "DD"))
208
+ value = _value.format(this.layout.database.host.timeStampFormat)
206
209
  }
207
210
  }
208
211
  fields_processed[field.id] = value
209
212
  }
210
- let obj = {
211
- "recordId": this.recordId.toString(),
212
- "modId": this.modId.toString(),
213
- "fieldData": fields_processed
213
+ const obj = {
214
+ recordId: this.recordId.toString(),
215
+ modId: this.modId.toString(),
216
+ fieldData: fields_processed
214
217
  }
215
218
  return obj
216
219
  }
217
220
 
218
- toObject(
221
+ toObject (
219
222
  filter: (a: Field<FieldValue>) => any = (a) => a.edited,
220
223
  portalFilter: (a: Portal<any>) => any = (a) => a.records.find(record => record.edited),
221
224
  portalRowFilter: (a: PortalRecord<never>) => any = (a) => a.edited,
222
225
  portalFieldFilter: (a: Field<FieldValue>) => any = (a) => a.edited
223
226
  ) {
224
- let obj: ApiRowDataDef = {
227
+ const obj: ApiRowDataDef = {
225
228
  ...this.fieldsToObject(filter),
226
- portalData: {},
227
- };
229
+ portalData: {}
230
+ }
228
231
 
229
232
  // Check if there's been any edited portal information
230
- let portals = this.portalsArray.filter(a => portalFilter(a))
233
+ const portals = this.portalsArray.filter(a => portalFilter(a))
231
234
  if (portals) {
232
- obj["portalData"] = {}
233
- for (let portal of portals) {
234
- // @ts-ignore
235
- obj["portalData"][portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
235
+ obj.portalData = {}
236
+ for (const portal of portals) {
237
+ // @ts-expect-error
238
+ obj.portalData[portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
236
239
  return record.toObject(portalFieldFilter)
237
240
  })
238
241
  }
@@ -240,4 +243,4 @@ export class LayoutRecord<LAYOUT extends LayoutInterface> extends RecordBase<LAY
240
243
 
241
244
  return obj
242
245
  }
243
- }
246
+ }
@@ -1,5 +1,5 @@
1
- import { LayoutBase } from "../layouts/layoutBase.js";
1
+ import { type LayoutBase } from '../layouts/layoutBase.js';
2
2
  export interface LayoutRecordBase {
3
3
  layout: LayoutBase;
4
- commit(extraBody: object): Promise<this>;
4
+ commit: (extraBody: object) => Promise<this>;
5
5
  }