@jd-data-limited/easy-fm 0.0.7 → 0.0.9
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/.idea/easy-fm.iml +7 -7
- package/.idea/modules.xml +7 -7
- package/.idea/php.xml +18 -0
- package/.idea/vcs.xml +5 -5
- package/LICENSE +21 -21
- package/README.md +211 -192
- package/decs.d.ts +6 -6
- package/docs/classes/FileMakerConnection.md +86 -0
- package/docs/classes/layout.md +0 -0
- package/fmErrors.json +1009 -1009
- package/index.js +502 -503
- package/index.ts +583 -584
- package/package.json +1 -1
- package/tsconfig.json +8 -8
- package/.idea/copyright/JD_Data.xml +0 -6
- package/.idea/copyright/profiles_settings.xml +0 -7
- package/.idea/jsLibraryMappings.xml +0 -6
package/index.js
CHANGED
|
@@ -1,503 +1,502 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022. See LICENSE file for more information
|
|
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
|
-
};
|
|
13
|
-
// const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
|
|
14
|
-
import fetch, { File, FormData } from 'node-fetch';
|
|
15
|
-
import { EventEmitter } from "events";
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
import fs from "fs";
|
|
18
|
-
// @ts-ignore
|
|
19
|
-
import path from "path";
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
import { fileURLToPath } from "url";
|
|
22
|
-
import * as https from "https";
|
|
23
|
-
// import * as btoa from "btoa";
|
|
24
|
-
// @ts-ignore
|
|
25
|
-
const errs = JSON.parse(fs.readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), 'fmErrors.json')).toString());
|
|
26
|
-
export default class FileMakerConnection {
|
|
27
|
-
constructor() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
39
|
-
this.
|
|
40
|
-
this.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this.
|
|
64
|
-
this.
|
|
65
|
-
this.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
options.headers["
|
|
106
|
-
options.
|
|
107
|
-
|
|
108
|
-
let
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
this.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
let
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
this
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
this.
|
|
203
|
-
this.
|
|
204
|
-
this.
|
|
205
|
-
this.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
delete fieldData.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
this.
|
|
248
|
-
this.
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
delete data.
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
this.
|
|
280
|
-
this
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
this.
|
|
302
|
-
this
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
let
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
"
|
|
327
|
-
"
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
this.
|
|
346
|
-
this.
|
|
347
|
-
this.
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
this.
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
this.
|
|
400
|
-
this.
|
|
401
|
-
this.records
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
this.
|
|
433
|
-
this.
|
|
434
|
-
|
|
435
|
-
"script": null,
|
|
436
|
-
"script.
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
this
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
this.
|
|
495
|
-
this.
|
|
496
|
-
this
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
//
|
|
501
|
-
//
|
|
502
|
-
//
|
|
503
|
-
// }
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2022. See LICENSE file for more information
|
|
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
|
+
};
|
|
13
|
+
// const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
|
|
14
|
+
import fetch, { File, FormData } from 'node-fetch';
|
|
15
|
+
import { EventEmitter } from "events";
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
import fs from "fs";
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
import path from "path";
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
import { fileURLToPath } from "url";
|
|
22
|
+
import * as https from "https";
|
|
23
|
+
// import * as btoa from "btoa";
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
const errs = JSON.parse(fs.readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), 'fmErrors.json')).toString());
|
|
26
|
+
export default class FileMakerConnection {
|
|
27
|
+
constructor() { }
|
|
28
|
+
get endpoint() {
|
|
29
|
+
return `https://${this.hostname}/fmi/data/v2/databases/${encodeURI(this.database)}`;
|
|
30
|
+
}
|
|
31
|
+
login(hostname, database, username, password, rejectUnauthorized = true) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
if (this.token)
|
|
34
|
+
throw new Error("Already logged in. Run logout() first");
|
|
35
|
+
this.username = username;
|
|
36
|
+
this.password = password;
|
|
37
|
+
this.hostname = hostname;
|
|
38
|
+
this.database = database;
|
|
39
|
+
this.rejectUnauthroized = rejectUnauthorized;
|
|
40
|
+
return yield this.createSession();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
logout() {
|
|
44
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
if (!this.token)
|
|
46
|
+
reject(new Error("Not logged in"));
|
|
47
|
+
let _fetch = yield fetch(`${this.endpoint}/sessions/${this.token}`, {
|
|
48
|
+
method: "DELETE",
|
|
49
|
+
headers: {
|
|
50
|
+
"content-type": "application/json"
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
let data = yield _fetch.json();
|
|
54
|
+
// console.log(data)
|
|
55
|
+
this.token = null;
|
|
56
|
+
this.database = null;
|
|
57
|
+
this.hostname = null;
|
|
58
|
+
resolve();
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
importSession(hostname, database, token, rejectUnauthorized = true) {
|
|
62
|
+
this.token = token;
|
|
63
|
+
this.database = database;
|
|
64
|
+
this.hostname = hostname;
|
|
65
|
+
this.rejectUnauthroized = rejectUnauthorized;
|
|
66
|
+
}
|
|
67
|
+
createSession() {
|
|
68
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
fetch(`${this.endpoint}/sessions`, {
|
|
70
|
+
hostname: this.hostname,
|
|
71
|
+
port: 443,
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: {
|
|
74
|
+
"Content-Type": "application/json",
|
|
75
|
+
"Authorization": "Basic " + Buffer.from(this.username + ":" + this.password).toString("base64")
|
|
76
|
+
}
|
|
77
|
+
}).then(res => {
|
|
78
|
+
console.log(res);
|
|
79
|
+
return res.json();
|
|
80
|
+
})
|
|
81
|
+
.then(res => {
|
|
82
|
+
let _res = res;
|
|
83
|
+
if (_res.messages[0].code === "0") {
|
|
84
|
+
this.token = _res.response.token;
|
|
85
|
+
console.log(_res);
|
|
86
|
+
resolve(this.token);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
reject(new FMError(_res.messages[0].code, _res.status, res));
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
.catch(e => {
|
|
93
|
+
reject(e);
|
|
94
|
+
});
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
getLayout(name) {
|
|
98
|
+
return new layout(this, name);
|
|
99
|
+
}
|
|
100
|
+
apiRequest(url, options = {}) {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
if (!options.headers)
|
|
103
|
+
options.headers = {};
|
|
104
|
+
options.headers["content-type"] = options.headers["content-type"] ? options.headers["content-type"] : "application/json";
|
|
105
|
+
options.headers["authorization"] = "Bearer " + this.token;
|
|
106
|
+
options.rejectUnauthorized = this.rejectUnauthroized;
|
|
107
|
+
let _fetch = yield fetch(url, options);
|
|
108
|
+
let data = yield _fetch.json();
|
|
109
|
+
return data;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
setGlobals(globalFields) {
|
|
113
|
+
// console.log({globalFields})
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
this.apiRequest(`${this.endpoint}/globals`, {
|
|
116
|
+
method: "PATCH",
|
|
117
|
+
body: JSON.stringify({ globalFields })
|
|
118
|
+
}).then(res => {
|
|
119
|
+
// console.log(res)
|
|
120
|
+
if (res.messages[0].code === "0") {
|
|
121
|
+
resolve();
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
reject(new FMError(res.messages[0].code, res.status, res));
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
.catch(e => {
|
|
128
|
+
reject(e);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
script(name, parameter) {
|
|
133
|
+
return { name, parameter };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
class layout {
|
|
137
|
+
constructor(conn, name) {
|
|
138
|
+
this.conn = conn;
|
|
139
|
+
this.name = name;
|
|
140
|
+
}
|
|
141
|
+
get endpoint() {
|
|
142
|
+
return `${this.conn.endpoint}/layouts/${this.name}`;
|
|
143
|
+
}
|
|
144
|
+
getScript(script) {
|
|
145
|
+
return new script(this, script);
|
|
146
|
+
}
|
|
147
|
+
createRecord() {
|
|
148
|
+
return new Promise((resolve, reject) => {
|
|
149
|
+
// Get the layout's metadata
|
|
150
|
+
this.conn.apiRequest(`${this.endpoint}`).then(res => {
|
|
151
|
+
let fields = {};
|
|
152
|
+
for (let _field of res.response.fieldMetaData) {
|
|
153
|
+
fields[_field.name] = "";
|
|
154
|
+
}
|
|
155
|
+
resolve(new record(this, -1, 0, fields));
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
getRecord(recordId) {
|
|
160
|
+
return new record(this, recordId);
|
|
161
|
+
}
|
|
162
|
+
newFind() {
|
|
163
|
+
return new find(this);
|
|
164
|
+
}
|
|
165
|
+
runScript(script) {
|
|
166
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
let url = `${this.endpoint}/script/${encodeURI(script.name)}`;
|
|
168
|
+
if (script.parameter)
|
|
169
|
+
url += "?" + encodeURI(script.parameter);
|
|
170
|
+
this.conn.apiRequest(url, {
|
|
171
|
+
port: 443,
|
|
172
|
+
method: "GET"
|
|
173
|
+
})
|
|
174
|
+
.then(res => {
|
|
175
|
+
if (res.messages[0].code === "0") {
|
|
176
|
+
resolve(res.response);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
// console.log(res)
|
|
180
|
+
reject(new FMError(res.messages[0].code, res.status, res));
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
.catch(e => {
|
|
184
|
+
reject(e);
|
|
185
|
+
});
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
getLayoutMeta() {
|
|
189
|
+
return new Promise((resolve, reject) => {
|
|
190
|
+
this.conn.apiRequest(this.endpoint).then(res => {
|
|
191
|
+
this.metadata = res.response;
|
|
192
|
+
resolve(this);
|
|
193
|
+
})
|
|
194
|
+
.catch(e => reject(e));
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
class record extends EventEmitter {
|
|
199
|
+
constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
|
|
200
|
+
super();
|
|
201
|
+
this.layout = layout;
|
|
202
|
+
this.recordId = recordId;
|
|
203
|
+
this.modId = modId;
|
|
204
|
+
this.fields = this.processFieldData(fieldData);
|
|
205
|
+
this.portals = [];
|
|
206
|
+
if (portalData) {
|
|
207
|
+
this.processPortalData(portalData);
|
|
208
|
+
}
|
|
209
|
+
this.on("saved", () => {
|
|
210
|
+
for (let field of this.fields)
|
|
211
|
+
field.edited = false;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
get endpoint() {
|
|
215
|
+
return `${this.layout.endpoint}/records/${this.recordId}`;
|
|
216
|
+
}
|
|
217
|
+
get edited() {
|
|
218
|
+
return !!this.fields.find(i => i.edited);
|
|
219
|
+
}
|
|
220
|
+
processPortalData(portalData) {
|
|
221
|
+
for (let item of Object.keys(portalData)) {
|
|
222
|
+
this.portals.push(new portal(this, item, portalData[item].map(item => {
|
|
223
|
+
let fieldData = item;
|
|
224
|
+
delete fieldData.recordId;
|
|
225
|
+
delete fieldData.modId;
|
|
226
|
+
return new portalItem(this, item.recordId, item.modId, fieldData);
|
|
227
|
+
})));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
processFieldData(fieldData) {
|
|
231
|
+
return Object.keys(fieldData).map(item => {
|
|
232
|
+
return new field(this, item, fieldData[item]);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
get() {
|
|
236
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
237
|
+
if (!this.layout.metadata)
|
|
238
|
+
yield this.layout.getLayoutMeta();
|
|
239
|
+
this.layout.conn.apiRequest(this.endpoint, {
|
|
240
|
+
port: 443,
|
|
241
|
+
method: "GET"
|
|
242
|
+
})
|
|
243
|
+
.then(res => {
|
|
244
|
+
if (res.messages[0].code === "0") {
|
|
245
|
+
// console.log(res, res.response.data)
|
|
246
|
+
this.modId = res.response.data[0].modId;
|
|
247
|
+
this.fields = this.processFieldData(res.response.data[0].fieldData);
|
|
248
|
+
this.portalData = [];
|
|
249
|
+
if (res.response.data[0].portalData)
|
|
250
|
+
this.processPortalData(res.response.data[0].portalData);
|
|
251
|
+
resolve(this);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
reject(new FMError(res.messages[0].code, res.status, res));
|
|
255
|
+
}
|
|
256
|
+
})
|
|
257
|
+
.catch(e => {
|
|
258
|
+
reject(e);
|
|
259
|
+
});
|
|
260
|
+
}));
|
|
261
|
+
}
|
|
262
|
+
commit(extraBody = {}) {
|
|
263
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
264
|
+
let data = this.toObject();
|
|
265
|
+
delete data.recordId;
|
|
266
|
+
delete data.modId;
|
|
267
|
+
if (this.recordId === -1) {
|
|
268
|
+
// This is a new record
|
|
269
|
+
this.layout.conn.apiRequest(`${this.layout.endpoint}/records`, {
|
|
270
|
+
port: 443,
|
|
271
|
+
method: "POST",
|
|
272
|
+
body: JSON.stringify({
|
|
273
|
+
fieldData: data.fieldData
|
|
274
|
+
})
|
|
275
|
+
})
|
|
276
|
+
.then(res => {
|
|
277
|
+
if (res.messages[0].code === "0") {
|
|
278
|
+
this.recordId = res.response.recordId;
|
|
279
|
+
this.modId = res.response.modId;
|
|
280
|
+
resolve(this);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
reject(new FMError(res.messages[0].code, res.status, res));
|
|
284
|
+
}
|
|
285
|
+
})
|
|
286
|
+
.catch(e => {
|
|
287
|
+
reject(e);
|
|
288
|
+
});
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
for (let item of Object.keys(extraBody))
|
|
292
|
+
data[item] = extraBody[item];
|
|
293
|
+
this.layout.conn.apiRequest(this.endpoint, {
|
|
294
|
+
port: 443,
|
|
295
|
+
method: "PATCH",
|
|
296
|
+
body: JSON.stringify(data)
|
|
297
|
+
})
|
|
298
|
+
.then(res => {
|
|
299
|
+
if (res.messages[0].code === "0") {
|
|
300
|
+
this.modId = res.response.modId;
|
|
301
|
+
this.emit("saved");
|
|
302
|
+
resolve(this);
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
reject(new FMError(res.messages[0].code, res.status, res));
|
|
306
|
+
}
|
|
307
|
+
})
|
|
308
|
+
.catch(e => {
|
|
309
|
+
reject(e);
|
|
310
|
+
});
|
|
311
|
+
}));
|
|
312
|
+
}
|
|
313
|
+
getField(field) {
|
|
314
|
+
return this.fields.find(_field => _field.id === field);
|
|
315
|
+
}
|
|
316
|
+
getPortal(portal) {
|
|
317
|
+
return this.portals.find(p => p.name === portal);
|
|
318
|
+
}
|
|
319
|
+
toObject(filter = (a) => a.edited, portalFilter = (a) => a.records.find(record => record.edited), portalRowFilter = (a) => a.edited, portalFieldFilter = (a) => a.edited) {
|
|
320
|
+
let fields_processed = {};
|
|
321
|
+
for (let field of this.fields.filter(field => filter(field))) {
|
|
322
|
+
fields_processed[field.id] = field.value;
|
|
323
|
+
}
|
|
324
|
+
let obj = {
|
|
325
|
+
"recordId": this.recordId,
|
|
326
|
+
"modId": this.modId,
|
|
327
|
+
"fieldData": fields_processed
|
|
328
|
+
};
|
|
329
|
+
// Check if there's been any edited portal information
|
|
330
|
+
let portals = this.portals.filter(a => portalFilter(a));
|
|
331
|
+
if (portals) {
|
|
332
|
+
obj["portalData"] = {};
|
|
333
|
+
for (let portal of portals) {
|
|
334
|
+
obj["portalData"][portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
|
|
335
|
+
return record.toObject(portalFieldFilter);
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return obj;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
class field {
|
|
343
|
+
constructor(record, id, contents) {
|
|
344
|
+
this.record = record;
|
|
345
|
+
this.id = id;
|
|
346
|
+
this.value = contents;
|
|
347
|
+
this.edited = false;
|
|
348
|
+
}
|
|
349
|
+
set(content) {
|
|
350
|
+
if (this.dataType === "container")
|
|
351
|
+
throw "Cannot set container value using set(). Use upload() instead.";
|
|
352
|
+
this.value = content;
|
|
353
|
+
this.edited = true;
|
|
354
|
+
}
|
|
355
|
+
get dataType() {
|
|
356
|
+
if (!this.record.layout.metadata)
|
|
357
|
+
return "unknown";
|
|
358
|
+
if (this.record instanceof portalItem) {
|
|
359
|
+
return this.record.layout.metadata.portalMetaData.find(i => i.name === this.id).result || "unknown";
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
return this.record.layout.metadata.fieldMetaData.find(i => i.name === this.id).result || "unknown";
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
upload(buffer, filename, mime) {
|
|
366
|
+
if (this.dataType !== "container")
|
|
367
|
+
throw "Cannot upload a file to the field; " + this.id + " (not a container field)";
|
|
368
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
369
|
+
let form = new FormData();
|
|
370
|
+
form.append("upload", new File([buffer], filename, { type: mime }));
|
|
371
|
+
let _fetch = yield fetch(`${this.record.endpoint}/containers/${this.id}/1`, {
|
|
372
|
+
method: "POST",
|
|
373
|
+
headers: { "Authorization": "Bearer " + this.record.layout.conn.token },
|
|
374
|
+
body: form
|
|
375
|
+
}).then(res => res.json())
|
|
376
|
+
.then(data => {
|
|
377
|
+
let _res = data;
|
|
378
|
+
if (_res.messages[0].code === "0") {
|
|
379
|
+
resolve();
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
reject(new FMError(_res.messages[0].code, _res.status, data));
|
|
383
|
+
}
|
|
384
|
+
})
|
|
385
|
+
.catch(e => { reject(e); });
|
|
386
|
+
}));
|
|
387
|
+
}
|
|
388
|
+
download() {
|
|
389
|
+
return new Promise((resolve, reject) => {
|
|
390
|
+
https.get(this.value.toString(), (res) => {
|
|
391
|
+
resolve(res);
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
class portal {
|
|
397
|
+
constructor(record, name, records = []) {
|
|
398
|
+
this.record = record;
|
|
399
|
+
this.name = name;
|
|
400
|
+
this.records = records;
|
|
401
|
+
for (let item of this.records)
|
|
402
|
+
item.attachPortal(this);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
class portalItem extends record {
|
|
406
|
+
constructor(record, recordId, modId = recordId, fieldData = {}) {
|
|
407
|
+
super(record.layout, recordId, modId, fieldData);
|
|
408
|
+
record.on("saved", () => {
|
|
409
|
+
this.emit("saved");
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
attachPortal(portal) {
|
|
413
|
+
this.portal = portal;
|
|
414
|
+
}
|
|
415
|
+
commit(extraBody = {}) {
|
|
416
|
+
return this.portal.record.commit(extraBody);
|
|
417
|
+
}
|
|
418
|
+
toObject(fieldFilter) {
|
|
419
|
+
super.toObject();
|
|
420
|
+
let res = {
|
|
421
|
+
recordId: this.recordId, modId: this.modId
|
|
422
|
+
};
|
|
423
|
+
for (let field of this.fields.filter(a => fieldFilter(a)))
|
|
424
|
+
res[field.id] = field.value;
|
|
425
|
+
// console.log(res)
|
|
426
|
+
return res;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
class find {
|
|
430
|
+
constructor(layout) {
|
|
431
|
+
this.layout = layout;
|
|
432
|
+
this.queries = [];
|
|
433
|
+
this.scripts = {
|
|
434
|
+
"script": null,
|
|
435
|
+
"script.prerequeset": null,
|
|
436
|
+
"script.presort": null // Runs before the sort
|
|
437
|
+
};
|
|
438
|
+
this.sort = [];
|
|
439
|
+
}
|
|
440
|
+
toObject() {
|
|
441
|
+
let out = { query: this.queries, sort: undefined };
|
|
442
|
+
if (this.sort.length !== 0)
|
|
443
|
+
out.sort = this.sort;
|
|
444
|
+
for (let item of Object.keys(this.scripts)) {
|
|
445
|
+
if (this.scripts[item]) {
|
|
446
|
+
out[item] = this.scripts[item].name;
|
|
447
|
+
if (this.scripts[item].parameter)
|
|
448
|
+
out[item + ".param"] = this.scripts[item].parameter;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return out;
|
|
452
|
+
}
|
|
453
|
+
addRequests(...requests) {
|
|
454
|
+
for (let item of requests)
|
|
455
|
+
this.queries.push(item);
|
|
456
|
+
return this;
|
|
457
|
+
}
|
|
458
|
+
addSort(fieldName, sortOrder) {
|
|
459
|
+
this.sort.push({ fieldName, sortOrder });
|
|
460
|
+
return this;
|
|
461
|
+
}
|
|
462
|
+
find() {
|
|
463
|
+
return new Promise((resolve, reject) => {
|
|
464
|
+
// console.log(this.#toObject())
|
|
465
|
+
this.layout.conn.apiRequest(`${this.layout.endpoint}/_find`, {
|
|
466
|
+
port: 443,
|
|
467
|
+
method: "POST",
|
|
468
|
+
body: JSON.stringify(this.toObject())
|
|
469
|
+
}).then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
470
|
+
// // console.log(res)
|
|
471
|
+
if (res.messages[0].code === "0") {
|
|
472
|
+
// console.log("RESOLVING")
|
|
473
|
+
if (!this.layout.metadata)
|
|
474
|
+
yield this.layout.getLayoutMeta();
|
|
475
|
+
let data = res.response.data.map(item => {
|
|
476
|
+
return new record(this.layout, item.recordId, item.modId, item.fieldData, item.portalData);
|
|
477
|
+
});
|
|
478
|
+
resolve(data);
|
|
479
|
+
}
|
|
480
|
+
else {
|
|
481
|
+
reject(new FMError(res.messages[0].code, res.status, res));
|
|
482
|
+
}
|
|
483
|
+
}))
|
|
484
|
+
.catch(e => {
|
|
485
|
+
reject(e);
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
export class FMError extends Error {
|
|
491
|
+
constructor(code, httpStatus, res) {
|
|
492
|
+
super(errs.find(err => err.e === this.code) || "Unknown error");
|
|
493
|
+
this.httpStatus = httpStatus;
|
|
494
|
+
this.res = res;
|
|
495
|
+
this.code = typeof code === "string" ? parseInt(code) : code;
|
|
496
|
+
Error.captureStackTrace(this, FMError);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
//
|
|
500
|
+
// module.exports = {
|
|
501
|
+
// default: {FileMakerConnection}
|
|
502
|
+
// }
|