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