@jd-data-limited/easy-fm 3.0.3 → 3.0.5
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.
- package/package.json +1 -1
- package/src/index.js +33 -20
- package/src/index.ts +179 -147
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1289,6 +1289,7 @@ export class Layout {
|
|
|
1289
1289
|
return this.records.find();
|
|
1290
1290
|
}
|
|
1291
1291
|
runScript(script) {
|
|
1292
|
+
let trace = new Error();
|
|
1292
1293
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
1293
1294
|
let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`;
|
|
1294
1295
|
if (script.parameter)
|
|
@@ -1303,7 +1304,7 @@ export class Layout {
|
|
|
1303
1304
|
}
|
|
1304
1305
|
else {
|
|
1305
1306
|
// console.log(res)
|
|
1306
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
1307
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1307
1308
|
}
|
|
1308
1309
|
})
|
|
1309
1310
|
.catch(e => {
|
|
@@ -1365,8 +1366,8 @@ class LayoutRecordManager {
|
|
|
1365
1366
|
range(start = 0, limit = 100) {
|
|
1366
1367
|
return new RecordGetRange(this.layout, start, limit);
|
|
1367
1368
|
}
|
|
1368
|
-
find() {
|
|
1369
|
-
return new Find(this.layout);
|
|
1369
|
+
find(start = 0, limit = 100) {
|
|
1370
|
+
return new Find(this.layout, start, limit);
|
|
1370
1371
|
}
|
|
1371
1372
|
}
|
|
1372
1373
|
export class RecordBase extends EventEmitter {
|
|
@@ -1429,6 +1430,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1429
1430
|
});
|
|
1430
1431
|
}
|
|
1431
1432
|
get() {
|
|
1433
|
+
let trace = new Error();
|
|
1432
1434
|
if (this.recordId === -1) {
|
|
1433
1435
|
throw "Cannot get this RecordBase until a commit() is done.";
|
|
1434
1436
|
}
|
|
@@ -1450,7 +1452,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1450
1452
|
resolve(this);
|
|
1451
1453
|
}
|
|
1452
1454
|
else {
|
|
1453
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
1455
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1454
1456
|
}
|
|
1455
1457
|
})
|
|
1456
1458
|
.catch(e => {
|
|
@@ -1459,21 +1461,23 @@ export class RecordBase extends EventEmitter {
|
|
|
1459
1461
|
}));
|
|
1460
1462
|
}
|
|
1461
1463
|
commit(extraBody = {}) {
|
|
1464
|
+
let trace = new Error();
|
|
1462
1465
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
1466
|
+
var _a, _b, _c;
|
|
1463
1467
|
let data = this.toObject();
|
|
1464
1468
|
delete data.recordId;
|
|
1465
1469
|
delete data.modId;
|
|
1466
|
-
if (extraBody.scripts.after) {
|
|
1470
|
+
if ((_a = extraBody.scripts) === null || _a === void 0 ? void 0 : _a.after) {
|
|
1467
1471
|
data["script"] = extraBody.scripts.after.name;
|
|
1468
1472
|
if (extraBody.scripts.after.parameter)
|
|
1469
1473
|
data["param"] = extraBody.scripts.after.parameter;
|
|
1470
1474
|
}
|
|
1471
|
-
if (extraBody.scripts.prerequest) {
|
|
1475
|
+
if ((_b = extraBody.scripts) === null || _b === void 0 ? void 0 : _b.prerequest) {
|
|
1472
1476
|
data["script"] = extraBody.scripts.after.name;
|
|
1473
1477
|
if (extraBody.scripts.prerequest.parameter)
|
|
1474
1478
|
data["param"] = extraBody.scripts.prerequest.parameter;
|
|
1475
1479
|
}
|
|
1476
|
-
if (extraBody.scripts.presort) {
|
|
1480
|
+
if ((_c = extraBody.scripts) === null || _c === void 0 ? void 0 : _c.presort) {
|
|
1477
1481
|
data["script"] = extraBody.scripts.presort.name;
|
|
1478
1482
|
if (extraBody.scripts.presort.parameter)
|
|
1479
1483
|
data["param"] = extraBody.scripts.presort.parameter;
|
|
@@ -1487,7 +1491,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1487
1491
|
})
|
|
1488
1492
|
.then(res => {
|
|
1489
1493
|
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1490
|
-
reject(new FMError(res.response.scriptError, res.status, res));
|
|
1494
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace));
|
|
1491
1495
|
}
|
|
1492
1496
|
else if (res.messages[0].code === "0") {
|
|
1493
1497
|
this.recordId = parseInt(res.response.recordId);
|
|
@@ -1495,7 +1499,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1495
1499
|
resolve(this);
|
|
1496
1500
|
}
|
|
1497
1501
|
else {
|
|
1498
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
1502
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1499
1503
|
}
|
|
1500
1504
|
})
|
|
1501
1505
|
.catch(e => {
|
|
@@ -1511,7 +1515,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1511
1515
|
})
|
|
1512
1516
|
.then(res => {
|
|
1513
1517
|
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1514
|
-
reject(new FMError(res.response.scriptError, res.status, res));
|
|
1518
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace));
|
|
1515
1519
|
}
|
|
1516
1520
|
else if (res.messages[0].code === "0") {
|
|
1517
1521
|
this.modId = res.response.modId;
|
|
@@ -1519,7 +1523,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1519
1523
|
resolve(this);
|
|
1520
1524
|
}
|
|
1521
1525
|
else {
|
|
1522
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
1526
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1523
1527
|
}
|
|
1524
1528
|
})
|
|
1525
1529
|
.catch(e => {
|
|
@@ -1539,6 +1543,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1539
1543
|
return this.portals.find(p => p.name === portal);
|
|
1540
1544
|
}
|
|
1541
1545
|
delete() {
|
|
1546
|
+
let trace = new Error();
|
|
1542
1547
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
1543
1548
|
this.layout.database.apiRequest(this.endpoint, {
|
|
1544
1549
|
port: 443,
|
|
@@ -1546,14 +1551,14 @@ export class RecordBase extends EventEmitter {
|
|
|
1546
1551
|
})
|
|
1547
1552
|
.then(res => {
|
|
1548
1553
|
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1549
|
-
reject(new FMError(res.response.scriptError, res.status, res));
|
|
1554
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace));
|
|
1550
1555
|
}
|
|
1551
1556
|
else if (res.messages[0].code === "0") {
|
|
1552
1557
|
this.emit("deleted");
|
|
1553
1558
|
resolve();
|
|
1554
1559
|
}
|
|
1555
1560
|
else {
|
|
1556
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
1561
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1557
1562
|
}
|
|
1558
1563
|
})
|
|
1559
1564
|
.catch(e => {
|
|
@@ -1562,6 +1567,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1562
1567
|
}));
|
|
1563
1568
|
}
|
|
1564
1569
|
duplicate() {
|
|
1570
|
+
let trace = new Error();
|
|
1565
1571
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
1566
1572
|
this.layout.database.apiRequest(this.endpoint, {
|
|
1567
1573
|
port: 443,
|
|
@@ -1569,7 +1575,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1569
1575
|
})
|
|
1570
1576
|
.then(res => {
|
|
1571
1577
|
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1572
|
-
reject(new FMError(res.response.scriptError, res.status, res));
|
|
1578
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace));
|
|
1573
1579
|
}
|
|
1574
1580
|
else if (res.messages[0].code === "0") {
|
|
1575
1581
|
let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false);
|
|
@@ -1578,7 +1584,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1578
1584
|
resolve(_res);
|
|
1579
1585
|
}
|
|
1580
1586
|
else {
|
|
1581
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
1587
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1582
1588
|
}
|
|
1583
1589
|
})
|
|
1584
1590
|
.catch(e => {
|
|
@@ -1717,6 +1723,7 @@ export class Field {
|
|
|
1717
1723
|
return this._value;
|
|
1718
1724
|
}
|
|
1719
1725
|
upload(buffer, filename, mime) {
|
|
1726
|
+
let trace = new Error();
|
|
1720
1727
|
if (this.metadata.result !== "container")
|
|
1721
1728
|
throw "Cannot upload a file to the field; " + this.id + " (not a container field)";
|
|
1722
1729
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1733,7 +1740,7 @@ export class Field {
|
|
|
1733
1740
|
resolve();
|
|
1734
1741
|
}
|
|
1735
1742
|
else {
|
|
1736
|
-
reject(new FMError(_res.messages[0].code, _res.status, data));
|
|
1743
|
+
reject(new FMError(_res.messages[0].code, _res.status, data, trace));
|
|
1737
1744
|
}
|
|
1738
1745
|
})
|
|
1739
1746
|
.catch(e => {
|
|
@@ -1903,6 +1910,7 @@ export class RecordGetRange extends RecordGetOperation {
|
|
|
1903
1910
|
return this.fetch(extraBody);
|
|
1904
1911
|
}
|
|
1905
1912
|
fetch(extraBody = {}) {
|
|
1913
|
+
let trace = new Error();
|
|
1906
1914
|
return new Promise((resolve, reject) => {
|
|
1907
1915
|
// console.log(this.#toObject())
|
|
1908
1916
|
this.layout.getLayoutMeta().then(() => {
|
|
@@ -1922,7 +1930,7 @@ export class RecordGetRange extends RecordGetOperation {
|
|
|
1922
1930
|
resolve(data);
|
|
1923
1931
|
}
|
|
1924
1932
|
else {
|
|
1925
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
1933
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1926
1934
|
}
|
|
1927
1935
|
}))
|
|
1928
1936
|
.catch(e => {
|
|
@@ -1932,8 +1940,10 @@ export class RecordGetRange extends RecordGetOperation {
|
|
|
1932
1940
|
}
|
|
1933
1941
|
}
|
|
1934
1942
|
export class Find extends RecordGetOperation {
|
|
1935
|
-
constructor(layout) {
|
|
1943
|
+
constructor(layout, start = 0, limit = 100) {
|
|
1936
1944
|
super(layout);
|
|
1945
|
+
this.setOffset(start);
|
|
1946
|
+
this.setLimit(limit);
|
|
1937
1947
|
this.queries = [];
|
|
1938
1948
|
}
|
|
1939
1949
|
toObject() {
|
|
@@ -1972,6 +1982,7 @@ export class Find extends RecordGetOperation {
|
|
|
1972
1982
|
return this.fetch();
|
|
1973
1983
|
}
|
|
1974
1984
|
fetch() {
|
|
1985
|
+
let trace = new Error();
|
|
1975
1986
|
return new Promise((resolve, reject) => {
|
|
1976
1987
|
// console.log(this.#toObject())
|
|
1977
1988
|
this.layout.getLayoutMeta()
|
|
@@ -1994,7 +2005,7 @@ export class Find extends RecordGetOperation {
|
|
|
1994
2005
|
resolve(data);
|
|
1995
2006
|
}
|
|
1996
2007
|
else {
|
|
1997
|
-
reject(new FMError(res.messages[0].code, res.status, res));
|
|
2008
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1998
2009
|
}
|
|
1999
2010
|
}))
|
|
2000
2011
|
.catch(e => {
|
|
@@ -2004,7 +2015,7 @@ export class Find extends RecordGetOperation {
|
|
|
2004
2015
|
}
|
|
2005
2016
|
}
|
|
2006
2017
|
export class FMError extends Error {
|
|
2007
|
-
constructor(code, httpStatus, res) {
|
|
2018
|
+
constructor(code, httpStatus, res, trace) {
|
|
2008
2019
|
if (typeof code === "string")
|
|
2009
2020
|
code = parseInt(code);
|
|
2010
2021
|
super(errs.find(err => err.e === code).d || "Unknown error");
|
|
@@ -2013,6 +2024,8 @@ export class FMError extends Error {
|
|
|
2013
2024
|
this.messages = res.messages;
|
|
2014
2025
|
this.code = typeof code === "string" ? parseInt(code) : code;
|
|
2015
2026
|
Error.captureStackTrace(this, FMError);
|
|
2027
|
+
if (trace)
|
|
2028
|
+
this.stack = trace.stack;
|
|
2016
2029
|
}
|
|
2017
2030
|
}
|
|
2018
2031
|
function generateAuthorizationHeaders(credentials) {
|
package/src/index.ts
CHANGED
|
@@ -1423,14 +1423,14 @@ export class Layout {
|
|
|
1423
1423
|
/**
|
|
1424
1424
|
* @deprecated use layout.records.create() instead
|
|
1425
1425
|
*/
|
|
1426
|
-
createRecord(): Promise<
|
|
1426
|
+
createRecord(): Promise<LayoutRecord> {
|
|
1427
1427
|
return this.records.create()
|
|
1428
1428
|
}
|
|
1429
1429
|
|
|
1430
1430
|
/**
|
|
1431
1431
|
* @deprecated use layout.records.get() instead
|
|
1432
1432
|
*/
|
|
1433
|
-
getRecord(recordId): Promise<
|
|
1433
|
+
getRecord(recordId): Promise<LayoutRecord> {
|
|
1434
1434
|
return this.records.get(recordId)
|
|
1435
1435
|
}
|
|
1436
1436
|
|
|
@@ -1441,7 +1441,8 @@ export class Layout {
|
|
|
1441
1441
|
return this.records.find()
|
|
1442
1442
|
}
|
|
1443
1443
|
|
|
1444
|
-
runScript(script): Promise<string
|
|
1444
|
+
runScript(script): Promise<string> {
|
|
1445
|
+
let trace = new Error()
|
|
1445
1446
|
return new Promise(async (resolve, reject) => {
|
|
1446
1447
|
let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`
|
|
1447
1448
|
if (script.parameter) url += "?script.param=" + encodeURIComponent(script.parameter)
|
|
@@ -1455,7 +1456,7 @@ export class Layout {
|
|
|
1455
1456
|
}
|
|
1456
1457
|
else {
|
|
1457
1458
|
// console.log(res)
|
|
1458
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
1459
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
1459
1460
|
}
|
|
1460
1461
|
})
|
|
1461
1462
|
.catch(e => {
|
|
@@ -1487,7 +1488,7 @@ class LayoutRecordManager {
|
|
|
1487
1488
|
this.layout = layout
|
|
1488
1489
|
}
|
|
1489
1490
|
|
|
1490
|
-
create(): Promise<
|
|
1491
|
+
create(): Promise<LayoutRecord> {
|
|
1491
1492
|
return new Promise((resolve, reject) => {
|
|
1492
1493
|
// Get the layout's metadata
|
|
1493
1494
|
this.layout.getLayoutMeta().then(layout => {
|
|
@@ -1504,7 +1505,7 @@ class LayoutRecordManager {
|
|
|
1504
1505
|
})
|
|
1505
1506
|
}
|
|
1506
1507
|
|
|
1507
|
-
get(recordId): Promise<
|
|
1508
|
+
get(recordId): Promise<LayoutRecord> {
|
|
1508
1509
|
return new Promise((resolve, reject) => {
|
|
1509
1510
|
let record
|
|
1510
1511
|
this.layout.getLayoutMeta()
|
|
@@ -1525,8 +1526,8 @@ class LayoutRecordManager {
|
|
|
1525
1526
|
return new RecordGetRange(this.layout, start, limit)
|
|
1526
1527
|
}
|
|
1527
1528
|
|
|
1528
|
-
find(): Find {
|
|
1529
|
-
return new Find(this.layout)
|
|
1529
|
+
find(start = 0, limit = 100): Find {
|
|
1530
|
+
return new Find(this.layout, start, limit)
|
|
1530
1531
|
}
|
|
1531
1532
|
}
|
|
1532
1533
|
|
|
@@ -1553,20 +1554,6 @@ export class RecordBase extends EventEmitter {
|
|
|
1553
1554
|
return !!this.fields.find(i => i.edited)
|
|
1554
1555
|
}
|
|
1555
1556
|
|
|
1556
|
-
protected processPortalData(portalData): void {
|
|
1557
|
-
this.portals = []
|
|
1558
|
-
for (let item of Object.keys(portalData)) {
|
|
1559
|
-
let _portal = new Portal(this, item)
|
|
1560
|
-
_portal.records = portalData[item].map(item => {
|
|
1561
|
-
let fieldData = item;
|
|
1562
|
-
delete fieldData.recordId;
|
|
1563
|
-
delete fieldData.modId;
|
|
1564
|
-
return new PortalRecord(this, _portal, item.recordId, item.modId, fieldData);
|
|
1565
|
-
})
|
|
1566
|
-
this.portals.push(_portal);
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
1557
|
processFieldData(fieldData): Field[] {
|
|
1571
1558
|
return Object.keys(fieldData).map(item => {
|
|
1572
1559
|
let _field = new Field(this, item, fieldData[item])
|
|
@@ -1601,51 +1588,91 @@ export class RecordBase extends EventEmitter {
|
|
|
1601
1588
|
})
|
|
1602
1589
|
}
|
|
1603
1590
|
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1591
|
+
_onSave() {
|
|
1592
|
+
this.emit("saved")
|
|
1593
|
+
for (let field of this.fields) field.edited = false
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
getField(field) {
|
|
1597
|
+
return this.fields.find(_field => _field.id === field)
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
toObject(filter = (a) => a.edited,
|
|
1601
|
+
portalFilter = (a) => a.records.find(record => record.edited),
|
|
1602
|
+
portalRowFilter = (a) => a.edited,
|
|
1603
|
+
portalFieldFilter = (a) => a.edited
|
|
1604
|
+
): recordObject {
|
|
1605
|
+
let fields_processed = {}
|
|
1606
|
+
for (let field of this.fields.filter(field => filter(field))) {
|
|
1607
|
+
let value = field.value
|
|
1608
|
+
if (value instanceof Date) {
|
|
1609
|
+
// @ts-ignore
|
|
1610
|
+
let _value = moment.default(value)
|
|
1611
|
+
.utcOffset(this.layout.database.host.timezoneOffset)
|
|
1612
|
+
|
|
1613
|
+
// @ts-ignore
|
|
1614
|
+
|
|
1615
|
+
switch (field.metadata.result) {
|
|
1616
|
+
case "time":
|
|
1617
|
+
value = _value.format(this.layout.database.host.metadata.productInfo.timeFormat.replace("dd", "DD"))
|
|
1618
|
+
break
|
|
1619
|
+
case "date":
|
|
1620
|
+
value = _value.format(this.layout.database.host.metadata.productInfo.dateFormat.replace("dd", "DD"))
|
|
1621
|
+
break
|
|
1622
|
+
default:
|
|
1623
|
+
value = _value.format(this.layout.database.host.metadata.productInfo.timeStampFormat.replace("dd", "DD"))
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
fields_processed[field.id] = value
|
|
1627
|
+
}
|
|
1628
|
+
let obj = {
|
|
1629
|
+
"recordId": this.recordId,
|
|
1630
|
+
"modId": this.modId,
|
|
1631
|
+
"fieldData": fields_processed
|
|
1607
1632
|
}
|
|
1608
|
-
return new Promise(async (resolve, reject) => {
|
|
1609
|
-
if (!this.layout.metadata) await this.layout.getLayoutMeta()
|
|
1610
1633
|
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
}
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
this.modId = res.response.data[0].modId
|
|
1619
|
-
this.fields = this.processFieldData(res.response.data[0].fieldData)
|
|
1620
|
-
this.portalData = []
|
|
1621
|
-
if (res.response.data[0].portalData) this.processPortalData(res.response.data[0].portalData)
|
|
1622
|
-
resolve(this)
|
|
1623
|
-
}
|
|
1624
|
-
else {
|
|
1625
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
1626
|
-
}
|
|
1627
|
-
})
|
|
1628
|
-
.catch(e => {
|
|
1629
|
-
reject(e)
|
|
1634
|
+
// Check if there's been any edited portal information
|
|
1635
|
+
let portals = this.portals.filter(a => portalFilter(a))
|
|
1636
|
+
if (portals) {
|
|
1637
|
+
obj["portalData"] = {}
|
|
1638
|
+
for (let portal of portals) {
|
|
1639
|
+
obj["portalData"][portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
|
|
1640
|
+
return record.toObject(portalFieldFilter)
|
|
1630
1641
|
})
|
|
1631
|
-
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
return obj
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
export class LayoutRecord extends RecordBase {
|
|
1649
|
+
portals: Portal[];
|
|
1650
|
+
|
|
1651
|
+
constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
|
|
1652
|
+
super(layout, recordId, modId);
|
|
1653
|
+
this.fields = this.processFieldData(fieldData)
|
|
1654
|
+
this.portals = []
|
|
1655
|
+
if (portalData) {
|
|
1656
|
+
this.processPortalData(portalData)
|
|
1657
|
+
}
|
|
1632
1658
|
}
|
|
1633
1659
|
|
|
1634
|
-
commit(extraBody: extraBodyOptions = {}): Promise<
|
|
1660
|
+
commit(extraBody: extraBodyOptions = {}): Promise<this> {
|
|
1661
|
+
let trace = new Error()
|
|
1635
1662
|
return new Promise(async (resolve, reject) => {
|
|
1636
1663
|
let data = this.toObject()
|
|
1637
1664
|
delete data.recordId
|
|
1638
1665
|
delete data.modId
|
|
1639
1666
|
|
|
1640
|
-
if (extraBody.scripts
|
|
1667
|
+
if (extraBody.scripts?.after) {
|
|
1641
1668
|
data["script"] = extraBody.scripts.after.name
|
|
1642
1669
|
if (extraBody.scripts.after.parameter) data["param"] = extraBody.scripts.after.parameter
|
|
1643
1670
|
}
|
|
1644
|
-
if (extraBody.scripts
|
|
1671
|
+
if (extraBody.scripts?.prerequest) {
|
|
1645
1672
|
data["script"] = extraBody.scripts.after.name
|
|
1646
1673
|
if (extraBody.scripts.prerequest.parameter) data["param"] = extraBody.scripts.prerequest.parameter
|
|
1647
1674
|
}
|
|
1648
|
-
if (extraBody.scripts
|
|
1675
|
+
if (extraBody.scripts?.presort) {
|
|
1649
1676
|
data["script"] = extraBody.scripts.presort.name
|
|
1650
1677
|
if (extraBody.scripts.presort.parameter) data["param"] = extraBody.scripts.presort.parameter
|
|
1651
1678
|
}
|
|
@@ -1660,7 +1687,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1660
1687
|
})
|
|
1661
1688
|
.then(res => {
|
|
1662
1689
|
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1663
|
-
reject(new FMError(res.response.scriptError, res.status, res))
|
|
1690
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace))
|
|
1664
1691
|
}
|
|
1665
1692
|
else if (res.messages[0].code === "0") {
|
|
1666
1693
|
this.recordId = parseInt(res.response.recordId)
|
|
@@ -1668,7 +1695,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1668
1695
|
resolve(this)
|
|
1669
1696
|
}
|
|
1670
1697
|
else {
|
|
1671
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
1698
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
1672
1699
|
}
|
|
1673
1700
|
})
|
|
1674
1701
|
.catch(e => {
|
|
@@ -1686,7 +1713,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1686
1713
|
})
|
|
1687
1714
|
.then(res => {
|
|
1688
1715
|
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1689
|
-
reject(new FMError(res.response.scriptError, res.status, res))
|
|
1716
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace))
|
|
1690
1717
|
}
|
|
1691
1718
|
else if (res.messages[0].code === "0") {
|
|
1692
1719
|
this.modId = res.response.modId
|
|
@@ -1694,7 +1721,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1694
1721
|
resolve(this)
|
|
1695
1722
|
}
|
|
1696
1723
|
else {
|
|
1697
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
1724
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
1698
1725
|
}
|
|
1699
1726
|
})
|
|
1700
1727
|
.catch(e => {
|
|
@@ -1703,35 +1730,43 @@ export class RecordBase extends EventEmitter {
|
|
|
1703
1730
|
})
|
|
1704
1731
|
}
|
|
1705
1732
|
|
|
1706
|
-
|
|
1707
|
-
this.
|
|
1708
|
-
for (let
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1733
|
+
protected processPortalData(portalData): void {
|
|
1734
|
+
this.portals = []
|
|
1735
|
+
for (let item of Object.keys(portalData)) {
|
|
1736
|
+
let _portal = new Portal(this, item)
|
|
1737
|
+
_portal.records = portalData[item].map(item => {
|
|
1738
|
+
let fieldData = item;
|
|
1739
|
+
delete fieldData.recordId;
|
|
1740
|
+
delete fieldData.modId;
|
|
1741
|
+
return new PortalRecord(this, _portal, item.recordId, item.modId, fieldData);
|
|
1742
|
+
})
|
|
1743
|
+
this.portals.push(_portal);
|
|
1744
|
+
}
|
|
1717
1745
|
}
|
|
1718
1746
|
|
|
1719
|
-
|
|
1747
|
+
get(): Promise<this> {
|
|
1748
|
+
let trace = new Error()
|
|
1749
|
+
if (this.recordId === -1) {
|
|
1750
|
+
throw "Cannot get this RecordBase until a commit() is done."
|
|
1751
|
+
}
|
|
1720
1752
|
return new Promise(async (resolve, reject) => {
|
|
1753
|
+
if (!this.layout.metadata) await this.layout.getLayoutMeta()
|
|
1754
|
+
|
|
1721
1755
|
this.layout.database.apiRequest(this.endpoint, {
|
|
1722
1756
|
port: 443,
|
|
1723
|
-
method: "
|
|
1757
|
+
method: "GET"
|
|
1724
1758
|
})
|
|
1725
1759
|
.then(res => {
|
|
1726
|
-
if (
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
this.
|
|
1731
|
-
|
|
1760
|
+
if (res.messages[0].code === "0") {
|
|
1761
|
+
// console.log(res, res.response.data)
|
|
1762
|
+
this.modId = res.response.data[0].modId
|
|
1763
|
+
this.fields = this.processFieldData(res.response.data[0].fieldData)
|
|
1764
|
+
this.portalData = []
|
|
1765
|
+
if (res.response.data[0].portalData) this.processPortalData(res.response.data[0].portalData)
|
|
1766
|
+
resolve(this)
|
|
1732
1767
|
}
|
|
1733
1768
|
else {
|
|
1734
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
1769
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
1735
1770
|
}
|
|
1736
1771
|
})
|
|
1737
1772
|
.catch(e => {
|
|
@@ -1740,7 +1775,12 @@ export class RecordBase extends EventEmitter {
|
|
|
1740
1775
|
})
|
|
1741
1776
|
}
|
|
1742
1777
|
|
|
1743
|
-
|
|
1778
|
+
getPortal(portal) {
|
|
1779
|
+
return this.portals.find(p => p.name === portal)
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
duplicate(): Promise<LayoutRecord> {
|
|
1783
|
+
let trace = new Error()
|
|
1744
1784
|
return new Promise(async (resolve, reject) => {
|
|
1745
1785
|
this.layout.database.apiRequest(this.endpoint, {
|
|
1746
1786
|
port: 443,
|
|
@@ -1748,7 +1788,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1748
1788
|
})
|
|
1749
1789
|
.then(res => {
|
|
1750
1790
|
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1751
|
-
reject(new FMError(res.response.scriptError, res.status, res))
|
|
1791
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace))
|
|
1752
1792
|
}
|
|
1753
1793
|
else if (res.messages[0].code === "0") {
|
|
1754
1794
|
let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false)
|
|
@@ -1758,7 +1798,7 @@ export class RecordBase extends EventEmitter {
|
|
|
1758
1798
|
resolve(_res)
|
|
1759
1799
|
}
|
|
1760
1800
|
else {
|
|
1761
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
1801
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
1762
1802
|
}
|
|
1763
1803
|
})
|
|
1764
1804
|
.catch(e => {
|
|
@@ -1767,65 +1807,29 @@ export class RecordBase extends EventEmitter {
|
|
|
1767
1807
|
})
|
|
1768
1808
|
}
|
|
1769
1809
|
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
value = _value.format(this.layout.database.host.metadata.productInfo.timeFormat.replace("dd", "DD"))
|
|
1789
|
-
break
|
|
1790
|
-
case "date":
|
|
1791
|
-
value = _value.format(this.layout.database.host.metadata.productInfo.dateFormat.replace("dd", "DD"))
|
|
1792
|
-
break
|
|
1793
|
-
default:
|
|
1794
|
-
value = _value.format(this.layout.database.host.metadata.productInfo.timeStampFormat.replace("dd", "DD"))
|
|
1795
|
-
}
|
|
1796
|
-
}
|
|
1797
|
-
fields_processed[field.id] = value
|
|
1798
|
-
}
|
|
1799
|
-
let obj = {
|
|
1800
|
-
"recordId": this.recordId,
|
|
1801
|
-
"modId": this.modId,
|
|
1802
|
-
"fieldData": fields_processed
|
|
1803
|
-
}
|
|
1804
|
-
|
|
1805
|
-
// Check if there's been any edited portal information
|
|
1806
|
-
let portals = this.portals.filter(a => portalFilter(a))
|
|
1807
|
-
if (portals) {
|
|
1808
|
-
obj["portalData"] = {}
|
|
1809
|
-
for (let portal of portals) {
|
|
1810
|
-
obj["portalData"][portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
|
|
1811
|
-
return record.toObject(portalFieldFilter)
|
|
1810
|
+
delete(): Promise<void> {
|
|
1811
|
+
let trace = new Error()
|
|
1812
|
+
return new Promise(async (resolve, reject) => {
|
|
1813
|
+
this.layout.database.apiRequest(this.endpoint, {
|
|
1814
|
+
port: 443,
|
|
1815
|
+
method: "DELETE"
|
|
1816
|
+
})
|
|
1817
|
+
.then(res => {
|
|
1818
|
+
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1819
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace))
|
|
1820
|
+
}
|
|
1821
|
+
else if (res.messages[0].code === "0") {
|
|
1822
|
+
this.emit("deleted")
|
|
1823
|
+
resolve()
|
|
1824
|
+
}
|
|
1825
|
+
else {
|
|
1826
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
1827
|
+
}
|
|
1812
1828
|
})
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
}
|
|
1818
|
-
|
|
1819
|
-
export class LayoutRecord extends RecordBase {
|
|
1820
|
-
portals: Portal[];
|
|
1821
|
-
|
|
1822
|
-
constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
|
|
1823
|
-
super(layout, recordId, modId);
|
|
1824
|
-
this.fields = this.processFieldData(fieldData)
|
|
1825
|
-
this.portals = []
|
|
1826
|
-
if (portalData) {
|
|
1827
|
-
this.processPortalData(portalData)
|
|
1828
|
-
}
|
|
1829
|
+
.catch(e => {
|
|
1830
|
+
reject(e)
|
|
1831
|
+
})
|
|
1832
|
+
})
|
|
1829
1833
|
}
|
|
1830
1834
|
}
|
|
1831
1835
|
|
|
@@ -1919,7 +1923,29 @@ export class Field {
|
|
|
1919
1923
|
return this._value
|
|
1920
1924
|
}
|
|
1921
1925
|
|
|
1922
|
-
|
|
1926
|
+
get string(): string {
|
|
1927
|
+
if (typeof this._value === "string") {
|
|
1928
|
+
return this._value
|
|
1929
|
+
}
|
|
1930
|
+
throw "Field value is not a string"
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
get date(): Date {
|
|
1934
|
+
if (this._value instanceof Date) {
|
|
1935
|
+
return this._value
|
|
1936
|
+
}
|
|
1937
|
+
throw "Field value is not a date"
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
get number(): number {
|
|
1941
|
+
if (typeof this._value === "number") {
|
|
1942
|
+
return this._value
|
|
1943
|
+
}
|
|
1944
|
+
throw "Field value is not a number"
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
upload(buffer: Buffer, filename: string, mime: string): Promise<void> {
|
|
1948
|
+
let trace = new Error()
|
|
1923
1949
|
if (this.metadata.result !== "container") throw "Cannot upload a file to the field; " + this.id + " (not a container field)"
|
|
1924
1950
|
return new Promise(async (resolve, reject) => {
|
|
1925
1951
|
let form = new FormData()
|
|
@@ -1936,7 +1962,7 @@ export class Field {
|
|
|
1936
1962
|
resolve()
|
|
1937
1963
|
}
|
|
1938
1964
|
else {
|
|
1939
|
-
reject(new FMError(_res.messages[0].code, _res.status, data))
|
|
1965
|
+
reject(new FMError(_res.messages[0].code, _res.status, data, trace))
|
|
1940
1966
|
}
|
|
1941
1967
|
})
|
|
1942
1968
|
.catch(e => {
|
|
@@ -1979,11 +2005,11 @@ export class Field {
|
|
|
1979
2005
|
}
|
|
1980
2006
|
|
|
1981
2007
|
export class Portal {
|
|
1982
|
-
readonly record:
|
|
2008
|
+
readonly record: LayoutRecord;
|
|
1983
2009
|
readonly name: string;
|
|
1984
2010
|
public records: PortalRecord[];
|
|
1985
2011
|
|
|
1986
|
-
constructor(record:
|
|
2012
|
+
constructor(record: LayoutRecord, name: string) {
|
|
1987
2013
|
this.record = record
|
|
1988
2014
|
this.name = name
|
|
1989
2015
|
}
|
|
@@ -2120,11 +2146,12 @@ export class RecordGetRange extends RecordGetOperation {
|
|
|
2120
2146
|
/**
|
|
2121
2147
|
* @deprecated in favour of .fetch()
|
|
2122
2148
|
*/
|
|
2123
|
-
run(extraBody: extraBodyOptions = {}): Promise<
|
|
2149
|
+
run(extraBody: extraBodyOptions = {}): Promise<LayoutRecord[]> {
|
|
2124
2150
|
return this.fetch(extraBody)
|
|
2125
2151
|
}
|
|
2126
2152
|
|
|
2127
|
-
fetch(extraBody: extraBodyOptions = {}): Promise<
|
|
2153
|
+
fetch(extraBody: extraBodyOptions = {}): Promise<LayoutRecord[]> {
|
|
2154
|
+
let trace = new Error()
|
|
2128
2155
|
return new Promise((resolve, reject) => {
|
|
2129
2156
|
// console.log(this.#toObject())
|
|
2130
2157
|
this.layout.getLayoutMeta().then(() => {
|
|
@@ -2143,7 +2170,7 @@ export class RecordGetRange extends RecordGetOperation {
|
|
|
2143
2170
|
resolve(data)
|
|
2144
2171
|
}
|
|
2145
2172
|
else {
|
|
2146
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
2173
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
2147
2174
|
}
|
|
2148
2175
|
})
|
|
2149
2176
|
.catch(e => {
|
|
@@ -2156,8 +2183,10 @@ export class RecordGetRange extends RecordGetOperation {
|
|
|
2156
2183
|
export class Find extends RecordGetOperation {
|
|
2157
2184
|
protected queries: object[]
|
|
2158
2185
|
|
|
2159
|
-
constructor(layout) {
|
|
2186
|
+
constructor(layout, start = 0, limit = 100) {
|
|
2160
2187
|
super(layout)
|
|
2188
|
+
this.setOffset(start)
|
|
2189
|
+
this.setLimit(limit)
|
|
2161
2190
|
this.queries = []
|
|
2162
2191
|
}
|
|
2163
2192
|
|
|
@@ -2197,7 +2226,8 @@ export class Find extends RecordGetOperation {
|
|
|
2197
2226
|
return this.fetch()
|
|
2198
2227
|
}
|
|
2199
2228
|
|
|
2200
|
-
fetch(): Promise<
|
|
2229
|
+
fetch(): Promise<LayoutRecord[]> {
|
|
2230
|
+
let trace = new Error()
|
|
2201
2231
|
return new Promise((resolve, reject) => {
|
|
2202
2232
|
// console.log(this.#toObject())
|
|
2203
2233
|
this.layout.getLayoutMeta()
|
|
@@ -2219,7 +2249,7 @@ export class Find extends RecordGetOperation {
|
|
|
2219
2249
|
resolve(data)
|
|
2220
2250
|
}
|
|
2221
2251
|
else {
|
|
2222
|
-
reject(new FMError(res.messages[0].code, res.status, res))
|
|
2252
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
2223
2253
|
}
|
|
2224
2254
|
})
|
|
2225
2255
|
.catch(e => {
|
|
@@ -2236,7 +2266,7 @@ export class FMError extends Error {
|
|
|
2236
2266
|
readonly message: string
|
|
2237
2267
|
readonly messages: string
|
|
2238
2268
|
|
|
2239
|
-
constructor(code, httpStatus, res) {
|
|
2269
|
+
constructor(code, httpStatus, res, trace?: Error) {
|
|
2240
2270
|
if (typeof code === "string") code = parseInt(code)
|
|
2241
2271
|
super(errs.find(err => err.e === code).d || "Unknown error");
|
|
2242
2272
|
this.httpStatus = httpStatus
|
|
@@ -2244,6 +2274,8 @@ export class FMError extends Error {
|
|
|
2244
2274
|
this.messages = res.messages
|
|
2245
2275
|
this.code = typeof code === "string" ? parseInt(code) : code
|
|
2246
2276
|
Error.captureStackTrace(this, FMError)
|
|
2277
|
+
|
|
2278
|
+
if (trace) this.stack = trace.stack
|
|
2247
2279
|
}
|
|
2248
2280
|
}
|
|
2249
2281
|
|