@jd-data-limited/easy-fm 3.2.8 → 3.2.10

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 CHANGED
@@ -1,10 +1,8 @@
1
1
  {
2
2
  "name": "@jd-data-limited/easy-fm",
3
- "version": "3.2.8",
3
+ "version": "3.2.10",
4
4
  "dependencies": {
5
- "@types/btoa": "^1.2.3",
6
- "@types/events": "^3.0.0",
7
- "follow-redirects": "^1.15.3",
5
+ "follow-redirects": "^1.15.5",
8
6
  "moment": "^2.29.4",
9
7
  "node-fetch": "3.3.2"
10
8
  },
@@ -36,9 +34,11 @@
36
34
  "url": "https://github.com/JD-Data-Limited/easy-fm/issues"
37
35
  },
38
36
  "devDependencies": {
37
+ "@types/btoa": "^1.2.3",
38
+ "@types/events": "^3.0.0",
39
+ "dotenv": "^16.3.1",
39
40
  "typescript": "^5.2.2",
40
41
  "webpack": "^5.81.0",
41
- "webpack-cli": "^5.0.2",
42
- "dotenv": "^16.3.1"
42
+ "webpack-cli": "^5.0.2"
43
43
  }
44
44
  }
package/src/FMError.js CHANGED
@@ -1013,11 +1013,6 @@ export const errs = [
1013
1013
  }
1014
1014
  ];
1015
1015
  export class FMError extends Error {
1016
- httpStatus;
1017
- res;
1018
- code;
1019
- message;
1020
- messages;
1021
1016
  constructor(code, httpStatus, res, trace) {
1022
1017
  if (typeof code === "string")
1023
1018
  code = parseInt(code);
@@ -1,15 +1,20 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { generateAuthorizationHeaders } from "./generateAuthorizationHeaders.js";
5
14
  import fetch from "node-fetch";
6
15
  import { FMError } from "../FMError.js";
7
16
  import { Database } from "./database.js";
8
17
  export default class FMHost {
9
- hostname;
10
- timezoneOffset;
11
- verify;
12
- metadata;
13
18
  constructor(_hostname, timezoneOffset = 0 - (new Date()).getTimezoneOffset(), verify = true) {
14
19
  if (!(/^https?:\/\//).test(_hostname))
15
20
  throw "hostname MUST begin with either http:// or https://";
@@ -17,43 +22,47 @@ export default class FMHost {
17
22
  this.timezoneOffset = timezoneOffset;
18
23
  this.verify = verify;
19
24
  }
20
- async listDatabases(credentials) {
21
- let headers = {};
22
- if (credentials) {
23
- headers = generateAuthorizationHeaders(credentials);
24
- }
25
- let _fetch = await fetch(`${this.hostname}/fmi/data/v2/databases`, {
26
- method: "GET",
27
- headers
25
+ listDatabases(credentials) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ let headers = {};
28
+ if (credentials) {
29
+ headers = generateAuthorizationHeaders(credentials);
30
+ }
31
+ let _fetch = yield fetch(`${this.hostname}/fmi/data/v2/databases`, {
32
+ method: "GET",
33
+ headers
34
+ });
35
+ let data = yield _fetch.json();
36
+ // console.log(data.messages[0])
37
+ if (data.messages[0].code === "0") {
38
+ return data.response.databases;
39
+ }
40
+ else {
41
+ // @ts-ignore
42
+ throw new FMError(data.messages[0].code, data.status, data);
43
+ }
28
44
  });
29
- let data = await _fetch.json();
30
- // console.log(data.messages[0])
31
- if (data.messages[0].code === "0") {
32
- return data.response.databases;
33
- }
34
- else {
35
- // @ts-ignore
36
- throw new FMError(data.messages[0].code, data.status, data);
37
- }
38
45
  }
39
46
  database(data) {
40
47
  return new Database(this, data);
41
48
  }
42
- async getMetadata() {
43
- if (this.metadata)
44
- return this.metadata;
45
- let _fetch = await fetch(`${this.hostname}/fmi/data/v2/productInfo`, {
46
- method: "GET",
49
+ getMetadata() {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ if (this.metadata)
52
+ return this.metadata;
53
+ let _fetch = yield fetch(`${this.hostname}/fmi/data/v2/productInfo`, {
54
+ method: "GET",
55
+ });
56
+ let data = yield _fetch.json();
57
+ // console.log(data.messages[0])
58
+ if (data.messages[0].code === "0") {
59
+ this.metadata = data.response;
60
+ return data.response;
61
+ }
62
+ else {
63
+ // @ts-ignore
64
+ throw new FMError(data.messages[0].code, data.status, data);
65
+ }
47
66
  });
48
- let data = await _fetch.json();
49
- // console.log(data.messages[0])
50
- if (data.messages[0].code === "0") {
51
- this.metadata = data.response;
52
- return data.response;
53
- }
54
- else {
55
- // @ts-ignore
56
- throw new FMError(data.messages[0].code, data.status, data);
57
- }
58
67
  }
59
68
  }
@@ -1,6 +1,15 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { EventEmitter } from "events";
5
14
  import fetch from "node-fetch";
6
15
  import { generateAuthorizationHeaders } from "./generateAuthorizationHeaders.js";
@@ -8,13 +17,9 @@ import { FMError } from "../FMError.js";
8
17
  import { Layout } from "../layouts/layout.js";
9
18
  import HTTP_REDIRECT from "follow-redirects";
10
19
  export class Database extends EventEmitter {
11
- _token;
12
- host;
13
- connection_details;
14
- cookies = {};
15
- name;
16
20
  constructor(host, conn) {
17
21
  super();
22
+ this.cookies = {};
18
23
  this.host = host;
19
24
  this.name = conn.database;
20
25
  this.connection_details = conn;
@@ -32,53 +37,57 @@ export class Database extends EventEmitter {
32
37
  throw "Not yet supported login method";
33
38
  }
34
39
  }
35
- async logout() {
36
- if (!this.token)
37
- throw new Error("Not logged in");
38
- let _fetch = await fetch(`${this.endpoint}/sessions/${this.token}`, {
39
- method: "DELETE",
40
- headers: {
41
- "content-type": "application/json"
42
- }
40
+ logout() {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ if (!this.token)
43
+ throw new Error("Not logged in");
44
+ let _fetch = yield fetch(`${this.endpoint}/sessions/${this.token}`, {
45
+ method: "DELETE",
46
+ headers: {
47
+ "content-type": "application/json"
48
+ }
49
+ });
50
+ let data = yield _fetch.json();
51
+ // console.log(data)
52
+ this._token = null;
43
53
  });
44
- let data = await _fetch.json();
45
- // console.log(data)
46
- this._token = null;
47
54
  }
48
- async login() {
49
- // Reset cookies
50
- this.cookies = {};
51
- await this.host.getMetadata();
52
- if (this.token)
53
- throw new Error("Already logged in. Run logout() first");
54
- if (this.connection_details.credentials.method === "token") {
55
- this._token = this.connection_details.credentials.token;
56
- return this.token;
57
- }
58
- else {
59
- console.time();
60
- let res = await fetch(`${this.endpoint}/sessions`, {
61
- hostname: this.host.hostname,
62
- port: 443,
63
- method: "POST",
64
- headers: generateAuthorizationHeaders(this.connection_details.credentials),
65
- body: JSON.stringify({
66
- fmDataSource: this.connection_details.externalSources.map(i => {
67
- let _i = i;
68
- return this.generateExternalSourceLogin(_i);
69
- })
70
- })
71
- });
72
- console.time();
73
- let _res = (await res.json());
74
- if (res.status === 200) {
75
- this._token = res.headers.get('x-fm-data-access-token');
76
- return this._token;
55
+ login() {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ // Reset cookies
58
+ this.cookies = {};
59
+ yield this.host.getMetadata();
60
+ if (this.token)
61
+ throw new Error("Already logged in. Run logout() first");
62
+ if (this.connection_details.credentials.method === "token") {
63
+ this._token = this.connection_details.credentials.token;
64
+ return this.token;
77
65
  }
78
66
  else {
79
- throw new FMError(_res.messages[0].code, _res.status, res);
67
+ console.time();
68
+ let res = yield fetch(`${this.endpoint}/sessions`, {
69
+ hostname: this.host.hostname,
70
+ port: 443,
71
+ method: "POST",
72
+ headers: generateAuthorizationHeaders(this.connection_details.credentials),
73
+ body: JSON.stringify({
74
+ fmDataSource: this.connection_details.externalSources.map(i => {
75
+ let _i = i;
76
+ return this.generateExternalSourceLogin(_i);
77
+ })
78
+ })
79
+ });
80
+ console.time();
81
+ let _res = (yield res.json());
82
+ if (res.status === 200) {
83
+ this._token = res.headers.get('x-fm-data-access-token');
84
+ return this._token;
85
+ }
86
+ else {
87
+ throw new FMError(_res.messages[0].code, _res.status, res);
88
+ }
80
89
  }
81
- }
90
+ });
82
91
  }
83
92
  get token() {
84
93
  return this._token;
@@ -86,40 +95,44 @@ export class Database extends EventEmitter {
86
95
  get endpoint() {
87
96
  return `${this.host.hostname}/fmi/data/v2/databases/${this.name}`;
88
97
  }
89
- async apiRequest(url, options = {}, autoRelogin = true) {
90
- if (!options.headers)
91
- options.headers = {};
92
- options.headers["content-type"] = options.headers["content-type"] ? options.headers["content-type"] : "application/json";
93
- options.headers["authorization"] = "Bearer " + this._token;
94
- options.rejectUnauthorized = this.host.verify;
95
- let _fetch = await fetch(url, options);
96
- if (_fetch.headers.get('set-cookie')) {
97
- for (let cookie of _fetch.headers.get('set-cookie')) {
98
- let cookie_split = cookie.split("=");
99
- this.cookies[cookie_split[0]] = cookie_split[1];
98
+ apiRequest(url, options = {}, autoRelogin = true) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ if (!options.headers)
101
+ options.headers = {};
102
+ options.headers["content-type"] = options.headers["content-type"] ? options.headers["content-type"] : "application/json";
103
+ options.headers["authorization"] = "Bearer " + this._token;
104
+ options.rejectUnauthorized = this.host.verify;
105
+ let _fetch = yield fetch(url, options);
106
+ if (_fetch.headers.get('set-cookie')) {
107
+ for (let cookie of _fetch.headers.get('set-cookie')) {
108
+ let cookie_split = cookie.split("=");
109
+ this.cookies[cookie_split[0]] = cookie_split[1];
110
+ }
100
111
  }
101
- }
102
- let data = await _fetch.json();
103
- // Remove response if it is empty. This makes checking for an empty response easier
104
- if (Object.keys(data.response).length === 0)
105
- delete data.response;
106
- // console.log(data.messages[0])
107
- if (data.messages[0].code === "952" && autoRelogin) {
108
- this._token = null;
109
- await this.login();
110
- return await this.apiRequest(url, options, false);
111
- }
112
- else if (data.messages[0].code !== "0") {
113
- throw new FMError(data.messages[0].code, _fetch.status, data);
114
- }
115
- data.httpStatus = _fetch.status;
116
- return data;
112
+ let data = yield _fetch.json();
113
+ // Remove response if it is empty. This makes checking for an empty response easier
114
+ if (Object.keys(data.response).length === 0)
115
+ delete data.response;
116
+ // console.log(data.messages[0])
117
+ if (data.messages[0].code === "952" && autoRelogin) {
118
+ this._token = null;
119
+ yield this.login();
120
+ return yield this.apiRequest(url, options, false);
121
+ }
122
+ else if (data.messages[0].code !== "0") {
123
+ throw new FMError(data.messages[0].code, _fetch.status, data);
124
+ }
125
+ data.httpStatus = _fetch.status;
126
+ return data;
127
+ });
117
128
  }
118
- async listLayouts() {
119
- let req = await this.apiRequest(`${this.endpoint}/layouts`);
120
- if (!req.response)
121
- throw new FMError(req.messages[0].code, req.httpStatus, req.messages[0].message);
122
- return req.response.layouts.filter(i => !i.isFolder).map(layout => new Layout(this, layout.name));
129
+ listLayouts() {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ let req = yield this.apiRequest(`${this.endpoint}/layouts`);
132
+ if (!req.response)
133
+ throw new FMError(req.messages[0].code, req.httpStatus, req.messages[0].message);
134
+ return req.response.layouts.filter(i => !i.isFolder).map(layout => new Layout(this, layout.name));
135
+ });
123
136
  }
124
137
  getLayout(name) {
125
138
  return new Layout(this, name);
@@ -127,11 +140,13 @@ export class Database extends EventEmitter {
127
140
  /*
128
141
  @deprecated
129
142
  */
130
- async setGlobals(globalFields) {
131
- // console.log({globalFields})
132
- await this.apiRequest(`${this.endpoint}/globals`, {
133
- method: "PATCH",
134
- body: JSON.stringify({ globalFields })
143
+ setGlobals(globalFields) {
144
+ return __awaiter(this, void 0, void 0, function* () {
145
+ // console.log({globalFields})
146
+ yield this.apiRequest(`${this.endpoint}/globals`, {
147
+ method: "PATCH",
148
+ body: JSON.stringify({ globalFields })
149
+ });
135
150
  });
136
151
  }
137
152
  script(name, parameter = "") {
@@ -1,14 +1,20 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { LayoutRecordManager } from "./layoutRecordManager.js";
5
14
  import { FMError } from "../FMError.js";
6
15
  export class Layout {
7
- database;
8
- records = new LayoutRecordManager(this);
9
- name;
10
- metadata;
11
16
  constructor(database, name) {
17
+ this.records = new LayoutRecordManager(this);
12
18
  this.database = database;
13
19
  this.name = name;
14
20
  }
@@ -33,28 +39,32 @@ export class Layout {
33
39
  newFind() {
34
40
  return this.records.find();
35
41
  }
36
- async runScript(script) {
37
- let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`;
38
- if (script.parameter)
39
- url += "?script.param=" + encodeURIComponent(script.parameter);
40
- let res = await this.database.apiRequest(url, {
41
- port: 443,
42
- method: "GET"
42
+ runScript(script) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`;
45
+ if (script.parameter)
46
+ url += "?script.param=" + encodeURIComponent(script.parameter);
47
+ let res = yield this.database.apiRequest(url, {
48
+ port: 443,
49
+ method: "GET"
50
+ });
51
+ if (res.messages[0].code === "0") {
52
+ let error = parseInt(res.response.scriptError);
53
+ return {
54
+ scriptError: error ? new FMError(error, 200, res) : undefined,
55
+ scriptResult: res.response.scriptResult
56
+ };
57
+ }
43
58
  });
44
- if (res.messages[0].code === "0") {
45
- let error = parseInt(res.response.scriptError);
46
- return {
47
- scriptError: error ? new FMError(error, 200, res) : undefined,
48
- scriptResult: res.response.scriptResult
49
- };
50
- }
51
59
  }
52
- async getLayoutMeta() {
53
- if (this.metadata) {
60
+ getLayoutMeta() {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ if (this.metadata) {
63
+ return this.metadata;
64
+ }
65
+ let res = yield this.database.apiRequest(this.endpoint);
66
+ this.metadata = res.response;
54
67
  return this.metadata;
55
- }
56
- let res = await this.database.apiRequest(this.endpoint);
57
- this.metadata = res.response;
58
- return this.metadata;
68
+ });
59
69
  }
60
70
  }
@@ -1,30 +1,42 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { LayoutRecord } from "../records/layoutRecord.js";
5
14
  import { RecordGetRange } from "../records/getOperations/recordGetRange.js";
6
15
  import { Find } from "../records/getOperations/find.js";
7
16
  export class LayoutRecordManager {
8
- layout;
9
17
  constructor(layout) {
10
18
  this.layout = layout;
11
19
  }
12
- async create() {
13
- await this.layout.getLayoutMeta();
14
- let fields = {};
15
- for (let _field of this.layout.metadata.fieldMetaData) {
16
- fields[_field.name] = "";
17
- }
18
- let portals = {};
19
- for (let _portal of Object.keys(this.layout.metadata.portalMetaData))
20
- portals[_portal] = [];
21
- return new LayoutRecord(this.layout, -1, 0, fields, portals);
20
+ create() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ yield this.layout.getLayoutMeta();
23
+ let fields = {};
24
+ for (let _field of this.layout.metadata.fieldMetaData) {
25
+ fields[_field.name] = "";
26
+ }
27
+ let portals = {};
28
+ for (let _portal of Object.keys(this.layout.metadata.portalMetaData))
29
+ portals[_portal] = [];
30
+ return new LayoutRecord(this.layout, -1, 0, fields, portals);
31
+ });
22
32
  }
23
- async get(recordId) {
24
- await this.layout.getLayoutMeta();
25
- let record = new LayoutRecord(this.layout, recordId);
26
- await record.get();
27
- return record;
33
+ get(recordId) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ yield this.layout.getLayoutMeta();
36
+ let record = new LayoutRecord(this.layout, recordId);
37
+ yield record.get();
38
+ return record;
39
+ });
28
40
  }
29
41
  range(start = 0, limit = 100) {
30
42
  return new RecordGetRange(this.layout, start, limit);
@@ -1,15 +1,20 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { RecordTypes } from "./recordBase.js";
5
14
  import { File, FormData } from "node-fetch";
6
15
  import { DOWNLOAD_MODES } from "../types.js";
7
16
  import { ApiFieldDisplayTypes, ApiFieldResultTypes, ApiFieldTypes } from "../models/apiResults.js";
8
17
  export class Field {
9
- record;
10
- id;
11
- _value;
12
- edited;
13
18
  constructor(record, id, contents) {
14
19
  this.record = record;
15
20
  this.id = id;
@@ -116,39 +121,43 @@ export class Field {
116
121
  }
117
122
  throw "Field value is not a number";
118
123
  }
119
- async upload(buffer, filename, mime) {
120
- let trace = new Error();
121
- if (this.metadata.result !== "container")
122
- throw "Cannot upload a file to the field; " + this.id + " (not a container field)";
123
- let form = new FormData();
124
- form.append("upload", new File([buffer], filename, { type: mime }));
125
- await this.record.layout.database.apiRequest(`${this.record.endpoint}/containers/${this.id}/1`, {
126
- method: "POST",
127
- // @ts-ignore
128
- headers: { "Authorization": "Bearer " + this.record.layout.database.token },
129
- body: form
124
+ upload(buffer, filename, mime) {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ let trace = new Error();
127
+ if (this.metadata.result !== "container")
128
+ throw "Cannot upload a file to the field; " + this.id + " (not a container field)";
129
+ let form = new FormData();
130
+ form.append("upload", new File([buffer], filename, { type: mime }));
131
+ yield this.record.layout.database.apiRequest(`${this.record.endpoint}/containers/${this.id}/1`, {
132
+ method: "POST",
133
+ // @ts-ignore
134
+ headers: { "Authorization": "Bearer " + this.record.layout.database.token, "content-type": "multipart/form-data" },
135
+ body: form
136
+ });
130
137
  });
131
138
  }
132
- async download(mode = DOWNLOAD_MODES.Stream) {
133
- if (this.metadata.result !== ApiFieldResultTypes.CONTAINER)
134
- throw new Error("Cannot perform download() on a non-container field");
135
- let stream = await this.record.layout.database.streamURL(this.string);
136
- if (mode === DOWNLOAD_MODES.Stream) {
137
- return stream;
138
- }
139
- let body = [];
140
- return new Promise((resolve, reject) => {
141
- stream.on("data", chunk => {
142
- body.push(chunk);
143
- });
144
- stream.on("error", (e) => {
145
- reject(e);
146
- });
147
- stream.on("end", () => {
148
- resolve({
149
- buffer: Buffer.concat(body),
150
- mime: stream.headers["content-type"],
151
- request: stream
139
+ download(mode = DOWNLOAD_MODES.Stream) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ if (this.metadata.result !== ApiFieldResultTypes.CONTAINER)
142
+ throw new Error("Cannot perform download() on a non-container field");
143
+ let stream = yield this.record.layout.database.streamURL(this.string);
144
+ if (mode === DOWNLOAD_MODES.Stream) {
145
+ return stream;
146
+ }
147
+ let body = [];
148
+ return new Promise((resolve, reject) => {
149
+ stream.on("data", chunk => {
150
+ body.push(chunk);
151
+ });
152
+ stream.on("error", (e) => {
153
+ reject(e);
154
+ });
155
+ stream.on("end", () => {
156
+ resolve({
157
+ buffer: Buffer.concat(body),
158
+ mime: stream.headers["content-type"],
159
+ request: stream
160
+ });
152
161
  });
153
162
  });
154
163
  });
@@ -140,7 +140,7 @@ export class Field<T extends FieldValue> {
140
140
  await this.record.layout.database.apiRequest<null>(`${this.record.endpoint}/containers/${this.id}/1`, {
141
141
  method: "POST",
142
142
  // @ts-ignore
143
- headers: {"Authorization": "Bearer " + this.record.layout.database.token},
143
+ headers: {"Authorization": "Bearer " + this.record.layout.database.token, "content-type": "multipart/form-data"},
144
144
  body: form
145
145
  })
146
146
  }
@@ -1,11 +1,19 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { RecordGetOperation } from "./recordGetOperation.js";
5
14
  import { LayoutRecord } from "../layoutRecord.js";
6
15
  import { FMError } from "../../FMError.js";
7
16
  export class Find extends RecordGetOperation {
8
- queries;
9
17
  constructor(layout, start = 0, limit = 100) {
10
18
  super(layout);
11
19
  this.setOffset(start);
@@ -47,24 +55,26 @@ export class Find extends RecordGetOperation {
47
55
  run() {
48
56
  return this.fetch();
49
57
  }
50
- async fetch() {
51
- let trace = new Error();
52
- await this.layout.getLayoutMeta();
53
- let res = await this.layout.database.apiRequest(`${this.layout.endpoint}/_find`, {
54
- port: 443,
55
- method: "POST",
56
- body: JSON.stringify(this.toObject())
57
- });
58
- if (res.messages[0].code === "0") {
59
- // console.log("RESOLVING")
60
- if (!this.layout.metadata)
61
- await this.layout.getLayoutMeta();
62
- return res.response.data.map(item => {
63
- return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData);
58
+ fetch() {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ let trace = new Error();
61
+ yield this.layout.getLayoutMeta();
62
+ let res = yield this.layout.database.apiRequest(`${this.layout.endpoint}/_find`, {
63
+ port: 443,
64
+ method: "POST",
65
+ body: JSON.stringify(this.toObject())
64
66
  });
65
- }
66
- else {
67
- throw new FMError(res.messages[0].code, res.httpStatus, res, trace);
68
- }
67
+ if (res.messages[0].code === "0") {
68
+ // console.log("RESOLVING")
69
+ if (!this.layout.metadata)
70
+ yield this.layout.getLayoutMeta();
71
+ return res.response.data.map(item => {
72
+ return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData);
73
+ });
74
+ }
75
+ else {
76
+ throw new FMError(res.messages[0].code, res.httpStatus, res, trace);
77
+ }
78
+ });
69
79
  }
70
80
  }
@@ -2,13 +2,10 @@
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
4
  export class RecordGetOperation {
5
- layout;
6
- limit = 100;
7
- scripts;
8
- sort;
9
- limitPortals = [];
10
- offset = 0;
11
5
  constructor(layout) {
6
+ this.limit = 100;
7
+ this.limitPortals = [];
8
+ this.offset = 0;
12
9
  this.layout = layout;
13
10
  this.scripts = {
14
11
  "script": null,
@@ -1,6 +1,15 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { RecordGetOperation } from "./recordGetOperation.js";
5
14
  import { LayoutRecord } from "../layoutRecord.js";
6
15
  import { FMError } from "../../FMError.js";
@@ -54,24 +63,26 @@ export class RecordGetRange extends RecordGetOperation {
54
63
  run(extraBody = {}) {
55
64
  return this.fetch(extraBody);
56
65
  }
57
- async fetch(extraBody = {}) {
58
- let trace = new Error();
59
- let res = await this.layout.getLayoutMeta().then(() => {
60
- return this.layout.database.apiRequest(`${this.layout.endpoint}/records${this.generateQueryParams(extraBody)}`, {
61
- method: "GET"
66
+ fetch(extraBody = {}) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ let trace = new Error();
69
+ let res = yield this.layout.getLayoutMeta().then(() => {
70
+ return this.layout.database.apiRequest(`${this.layout.endpoint}/records${this.generateQueryParams(extraBody)}`, {
71
+ method: "GET"
72
+ });
62
73
  });
74
+ if (res.messages[0].code === "0") {
75
+ // console.log("RESOLVING")
76
+ if (!this.layout.metadata)
77
+ yield this.layout.getLayoutMeta();
78
+ let data = res.response.data.map(item => {
79
+ return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData);
80
+ });
81
+ return data;
82
+ }
83
+ else {
84
+ throw new FMError(res.messages[0].code, res.httpStatus, res, trace);
85
+ }
63
86
  });
64
- if (res.messages[0].code === "0") {
65
- // console.log("RESOLVING")
66
- if (!this.layout.metadata)
67
- await this.layout.getLayoutMeta();
68
- let data = res.response.data.map(item => {
69
- return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData);
70
- });
71
- return data;
72
- }
73
- else {
74
- throw new FMError(res.messages[0].code, res.httpStatus, res, trace);
75
- }
76
87
  }
77
88
  }
@@ -1,15 +1,24 @@
1
1
  /*
2
2
  * Copyright (c) 2023. See LICENSE file for more information
3
3
  */
4
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
+ return new (P || (P = Promise))(function (resolve, reject) {
7
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
11
+ });
12
+ };
4
13
  import { RecordBase } from "./recordBase.js";
5
14
  import { PortalRecord } from "./portalRecord.js";
6
15
  import { Portal } from "./portal.js";
7
16
  import { FMError } from "../FMError.js";
8
17
  export class LayoutRecord extends RecordBase {
9
- // @ts-ignore
10
- portals = {};
11
18
  constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
12
19
  super(layout, recordId, modId);
20
+ // @ts-ignore
21
+ this.portals = {};
13
22
  this.processFieldData(fieldData);
14
23
  if (portalData) {
15
24
  this.processPortalData(portalData);
@@ -18,61 +27,64 @@ export class LayoutRecord extends RecordBase {
18
27
  get portalsArray() {
19
28
  return Object.values(this.portals);
20
29
  }
21
- async commit(extraBody = {}) {
22
- let data = this.toObject();
23
- delete data.recordId;
24
- delete data.modId;
25
- if (extraBody.scripts?.after) {
26
- data["script"] = extraBody.scripts.after.name;
27
- if (extraBody.scripts.after.parameter)
28
- data["script.param"] = extraBody.scripts.after.parameter;
29
- }
30
- if (extraBody.scripts?.prerequest) {
31
- data["script.prerequest"] = extraBody.scripts.after.name;
32
- if (extraBody.scripts.prerequest.parameter)
33
- data["script.prerequest.param"] = extraBody.scripts.prerequest.parameter;
34
- }
35
- if (extraBody.scripts?.presort) {
36
- data["script.presort"] = extraBody.scripts.presort.name;
37
- if (extraBody.scripts.presort.parameter)
38
- data["script.presort.param"] = extraBody.scripts.presort.parameter;
39
- }
40
- if (this.recordId === -1) {
41
- // This is a new LayoutRecord
42
- let res = await this.layout.database.apiRequest(`${this.layout.endpoint}/records`, {
30
+ commit(extraBody = {}) {
31
+ var _a, _b, _c;
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ let data = this.toObject();
34
+ delete data.recordId;
35
+ delete data.modId;
36
+ if ((_a = extraBody.scripts) === null || _a === void 0 ? void 0 : _a.after) {
37
+ data["script"] = extraBody.scripts.after.name;
38
+ if (extraBody.scripts.after.parameter)
39
+ data["script.param"] = extraBody.scripts.after.parameter;
40
+ }
41
+ if ((_b = extraBody.scripts) === null || _b === void 0 ? void 0 : _b.prerequest) {
42
+ data["script.prerequest"] = extraBody.scripts.after.name;
43
+ if (extraBody.scripts.prerequest.parameter)
44
+ data["script.prerequest.param"] = extraBody.scripts.prerequest.parameter;
45
+ }
46
+ if ((_c = extraBody.scripts) === null || _c === void 0 ? void 0 : _c.presort) {
47
+ data["script.presort"] = extraBody.scripts.presort.name;
48
+ if (extraBody.scripts.presort.parameter)
49
+ data["script.presort.param"] = extraBody.scripts.presort.parameter;
50
+ }
51
+ if (this.recordId === -1) {
52
+ // This is a new LayoutRecord
53
+ let res = yield this.layout.database.apiRequest(`${this.layout.endpoint}/records`, {
54
+ port: 443,
55
+ method: "POST",
56
+ body: JSON.stringify(data)
57
+ });
58
+ if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
59
+ throw new FMError(res.response.scriptError, res.httpStatus, res);
60
+ }
61
+ else if (res.messages[0].code === "0") {
62
+ this.recordId = parseInt(res.response.recordId);
63
+ this.modId = parseInt(res.response.modId);
64
+ return this;
65
+ }
66
+ else {
67
+ throw new FMError(res.messages[0].code, res.httpStatus, res);
68
+ }
69
+ }
70
+ // for (let item of Object.keys(data)) extraBody[item] = data[item]
71
+ let res = yield this.layout.database.apiRequest(this.endpoint, {
43
72
  port: 443,
44
- method: "POST",
73
+ method: "PATCH",
45
74
  body: JSON.stringify(data)
46
75
  });
47
76
  if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
48
77
  throw new FMError(res.response.scriptError, res.httpStatus, res);
49
78
  }
50
79
  else if (res.messages[0].code === "0") {
51
- this.recordId = parseInt(res.response.recordId);
52
- this.modId = parseInt(res.response.modId);
80
+ this.modId = +res.response.modId;
81
+ this._onSave();
53
82
  return this;
54
83
  }
55
84
  else {
56
85
  throw new FMError(res.messages[0].code, res.httpStatus, res);
57
86
  }
58
- }
59
- // for (let item of Object.keys(data)) extraBody[item] = data[item]
60
- let res = await this.layout.database.apiRequest(this.endpoint, {
61
- port: 443,
62
- method: "PATCH",
63
- body: JSON.stringify(data)
64
87
  });
65
- if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
66
- throw new FMError(res.response.scriptError, res.httpStatus, res);
67
- }
68
- else if (res.messages[0].code === "0") {
69
- this.modId = +res.response.modId;
70
- this._onSave();
71
- return this;
72
- }
73
- else {
74
- throw new FMError(res.messages[0].code, res.httpStatus, res);
75
- }
76
88
  }
77
89
  processPortalData(portalData) {
78
90
  for (let portalName of Object.keys(portalData)) {
@@ -87,65 +99,72 @@ export class LayoutRecord extends RecordBase {
87
99
  this.portals[portalName] = _portal;
88
100
  }
89
101
  }
90
- async get() {
91
- let trace = new Error();
92
- if (this.recordId === -1) {
93
- throw "Cannot get this RecordBase until a commit() is done.";
94
- }
95
- if (!this.layout.metadata)
96
- await this.layout.getLayoutMeta();
97
- let res = await this.layout.database.apiRequest(this.endpoint, {
98
- port: 443,
99
- method: "GET"
102
+ get() {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ let trace = new Error();
105
+ if (this.recordId === -1) {
106
+ throw "Cannot get this RecordBase until a commit() is done.";
107
+ }
108
+ if (!this.layout.metadata)
109
+ yield this.layout.getLayoutMeta();
110
+ let res = yield this.layout.database.apiRequest(this.endpoint, {
111
+ port: 443,
112
+ method: "GET"
113
+ });
114
+ if (res.messages[0].code === "0") {
115
+ // console.log(res, res.response.data)
116
+ this.modId = +res.response.data[0].modId;
117
+ this.processFieldData(res.response.data[0].fieldData);
118
+ this.portalData = [];
119
+ if (res.response.data[0].portalData)
120
+ this.processPortalData(res.response.data[0].portalData);
121
+ return this;
122
+ }
100
123
  });
101
- if (res.messages[0].code === "0") {
102
- // console.log(res, res.response.data)
103
- this.modId = +res.response.data[0].modId;
104
- this.processFieldData(res.response.data[0].fieldData);
105
- this.portalData = [];
106
- if (res.response.data[0].portalData)
107
- this.processPortalData(res.response.data[0].portalData);
108
- return this;
109
- }
110
124
  }
111
125
  getPortal(portal) {
112
126
  return this.portalsArray.find(p => p.name === portal);
113
127
  }
114
- async duplicate() {
115
- let trace = new Error();
116
- let res = await this.layout.database.apiRequest(this.endpoint, {
117
- port: 443,
118
- method: "POST"
128
+ duplicate() {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ let trace = new Error();
131
+ let res = yield this.layout.database.apiRequest(this.endpoint, {
132
+ port: 443,
133
+ method: "POST"
134
+ });
135
+ if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
136
+ throw new FMError(res.response.scriptError, res.httpStatus, res, trace);
137
+ }
138
+ else if (res.messages[0].code === "0") {
139
+ let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false);
140
+ let _res = new LayoutRecord(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData);
141
+ this.emit("duplicated");
142
+ return _res;
143
+ }
144
+ else {
145
+ throw new FMError(res.messages[0].code, res.httpStatus, res, trace);
146
+ }
119
147
  });
120
- if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
121
- throw new FMError(res.response.scriptError, res.httpStatus, res, trace);
122
- }
123
- else if (res.messages[0].code === "0") {
124
- let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false);
125
- let _res = new LayoutRecord(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData);
126
- this.emit("duplicated");
127
- return _res;
128
- }
129
- else {
130
- throw new FMError(res.messages[0].code, res.httpStatus, res, trace);
131
- }
132
148
  }
133
- async delete() {
134
- let res = await this.layout.database.apiRequest(this.endpoint, {
135
- port: 443,
136
- method: "DELETE"
149
+ delete() {
150
+ var _a, _b;
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ let res = yield this.layout.database.apiRequest(this.endpoint, {
153
+ port: 443,
154
+ method: "DELETE"
155
+ });
156
+ console.log(res);
157
+ if (typeof ((_a = res.response) === null || _a === void 0 ? void 0 : _a.scriptError) !== "undefined" && ((_b = res.response) === null || _b === void 0 ? void 0 : _b.scriptError) !== '0') {
158
+ throw new FMError(res.response.scriptError, res.httpStatus, res);
159
+ }
160
+ else if (res.messages[0].code === "0") {
161
+ this.emit("deleted");
162
+ return;
163
+ }
164
+ else {
165
+ throw new FMError(res.messages[0].code, res.httpStatus, res);
166
+ }
137
167
  });
138
- console.log(res);
139
- if (typeof res.response?.scriptError !== "undefined" && res.response?.scriptError !== '0') {
140
- throw new FMError(res.response.scriptError, res.httpStatus, res);
141
- }
142
- else if (res.messages[0].code === "0") {
143
- this.emit("deleted");
144
- return;
145
- }
146
- else {
147
- throw new FMError(res.messages[0].code, res.httpStatus, res);
148
- }
149
168
  }
150
169
  toObject(filter = (a) => a.edited, portalFilter = (a) => a.records.find(record => record.edited), portalRowFilter = (a) => a.edited, portalFieldFilter = (a) => a.edited) {
151
170
  let obj = super.toObject(filter, portalFilter, portalRowFilter, portalFieldFilter);
@@ -3,9 +3,6 @@
3
3
  */
4
4
  import { PortalRecord } from "./portalRecord.js";
5
5
  export class Portal {
6
- record;
7
- name;
8
- records;
9
6
  constructor(record, name) {
10
7
  this.record = record;
11
8
  this.name = name;
@@ -3,10 +3,9 @@
3
3
  */
4
4
  import { RecordBase, RecordTypes } from "./recordBase.js";
5
5
  export class PortalRecord extends RecordBase {
6
- portal;
7
- type = RecordTypes.PORTAL;
8
6
  constructor(record, portal, recordId, modId = recordId, fieldData = {}) {
9
7
  super(record.layout, recordId, modId);
8
+ this.type = RecordTypes.PORTAL;
10
9
  this.portal = portal;
11
10
  this.processFieldData(fieldData);
12
11
  }
@@ -11,14 +11,9 @@ export var RecordTypes;
11
11
  RecordTypes[RecordTypes["PORTAL"] = 2] = "PORTAL";
12
12
  })(RecordTypes || (RecordTypes = {}));
13
13
  export class RecordBase extends EventEmitter {
14
- layout;
15
- type = RecordTypes.UNKNOWN;
16
- recordId;
17
- modId;
18
- fields;
19
- portalData;
20
14
  constructor(layout, recordId, modId = recordId) {
21
15
  super();
16
+ this.type = RecordTypes.UNKNOWN;
22
17
  this.layout = layout;
23
18
  this.recordId = recordId;
24
19
  this.modId = modId;