@jd-data-limited/easy-fm 3.0.9 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/compiler.xml +7 -0
- package/.idea/discord.xml +7 -0
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.LICENSE.txt +3 -0
- package/examples/test.js +7 -0
- package/img.png +0 -0
- package/package.json +1 -1
- package/src/FMError.d.ts +12 -0
- package/src/FMError.js +1028 -0
- package/src/FMError.ts +1035 -0
- package/src/connection/FMHost.d.ts +12 -0
- package/src/connection/FMHost.js +68 -0
- package/src/connection/FMHost.ts +75 -0
- package/src/connection/database.d.ts +26 -0
- package/src/connection/database.js +195 -0
- package/src/connection/database.ts +216 -0
- package/src/connection/generateAuthorizationHeaders.d.ts +2 -0
- package/src/connection/generateAuthorizationHeaders.js +23 -0
- package/src/connection/generateAuthorizationHeaders.ts +32 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +5 -2073
- package/src/index.ts +3 -2323
- package/src/layouts/layout.d.ts +29 -0
- package/src/layouts/layout.js +84 -0
- package/src/layouts/layout.ts +92 -0
- package/src/layouts/layoutInterface.d.ts +13 -0
- package/src/layouts/layoutInterface.js +4 -0
- package/src/layouts/layoutInterface.ts +21 -0
- package/src/layouts/layoutRecordManager.d.ts +13 -0
- package/src/layouts/layoutRecordManager.js +50 -0
- package/src/layouts/layoutRecordManager.ts +59 -0
- package/src/records/field.d.ts +21 -0
- package/src/records/field.js +177 -0
- package/src/records/field.ts +189 -0
- package/src/records/getOperations/find.d.ts +15 -0
- package/src/records/getOperations/find.js +90 -0
- package/src/records/getOperations/find.ts +88 -0
- package/src/records/getOperations/recordGetOperation.d.ts +17 -0
- package/src/records/getOperations/recordGetOperation.js +43 -0
- package/src/records/getOperations/recordGetOperation.ts +54 -0
- package/src/records/getOperations/recordGetRange.d.ts +13 -0
- package/src/records/getOperations/recordGetRange.js +95 -0
- package/src/records/getOperations/recordGetRange.ts +84 -0
- package/src/records/layoutRecord.d.ts +16 -0
- package/src/records/layoutRecord.js +211 -0
- package/src/records/layoutRecord.ts +220 -0
- package/src/records/portal.d.ts +10 -0
- package/src/records/portal.js +19 -0
- package/src/records/portal.ts +28 -0
- package/src/records/portalRecord.d.ts +12 -0
- package/src/records/portalRecord.js +32 -0
- package/src/records/portalRecord.ts +43 -0
- package/src/records/recordBase.d.ts +20 -0
- package/src/records/recordBase.js +95 -0
- package/src/records/recordBase.ts +121 -0
- package/src/types.d.ts +117 -0
- package/src/types.js +8 -0
- package/src/types.ts +151 -0
- package/test.d.ts +1 -0
- package/test.js +4 -0
- package/tsconfig.json +4 -0
- package/TEMP/FILEMAKER.txt +0 -218
- package/TEMP/WEBAPP.txt +0 -92
- package/TEMP/config.yaml +0 -40
- package/TEMP/data.json +0 -19837
- package/temp2/1.19.2-forge-43.2.8.json +0 -347
- package/temp2/Quinn-1.19.2-forge-43.2.8.json +0 -347
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { LayoutRecordManager } from "./layoutRecordManager";
|
|
2
|
+
import { LayoutRecord } from "../records/layoutRecord";
|
|
3
|
+
import { ScriptResult } from "../types.js";
|
|
4
|
+
import { LayoutInterface } from "./layoutInterface";
|
|
5
|
+
import { Find } from "../records/getOperations/find";
|
|
6
|
+
import { FMError } from "../FMError";
|
|
7
|
+
import { Database } from "../connection/database";
|
|
8
|
+
export declare class Layout<T extends LayoutInterface> {
|
|
9
|
+
readonly database: Database;
|
|
10
|
+
readonly records: LayoutRecordManager<T>;
|
|
11
|
+
protected name: string;
|
|
12
|
+
metadata: any;
|
|
13
|
+
constructor(database: Database, name: string);
|
|
14
|
+
get endpoint(): string;
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated use layout.records.create() instead
|
|
17
|
+
*/
|
|
18
|
+
createRecord(): Promise<LayoutRecord<T["fields"], T["portals"]>>;
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated use layout.records.get() instead
|
|
21
|
+
*/
|
|
22
|
+
getRecord(recordId: any): Promise<LayoutRecord<T["fields"], T["portals"]>>;
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated use layout.records.find() instead
|
|
25
|
+
*/
|
|
26
|
+
newFind(): Find<T>;
|
|
27
|
+
runScript(script: any): Promise<ScriptResult>;
|
|
28
|
+
getLayoutMeta(): Promise<this | FMError>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023. 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
|
+
import { LayoutRecordManager } from "./layoutRecordManager";
|
|
14
|
+
import { FMError } from "../FMError";
|
|
15
|
+
export class Layout {
|
|
16
|
+
constructor(database, name) {
|
|
17
|
+
this.records = new LayoutRecordManager(this);
|
|
18
|
+
this.database = database;
|
|
19
|
+
this.name = name;
|
|
20
|
+
}
|
|
21
|
+
get endpoint() {
|
|
22
|
+
return `${this.database.endpoint}/layouts/${this.name}`;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated use layout.records.create() instead
|
|
26
|
+
*/
|
|
27
|
+
createRecord() {
|
|
28
|
+
return this.records.create();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated use layout.records.get() instead
|
|
32
|
+
*/
|
|
33
|
+
getRecord(recordId) {
|
|
34
|
+
return this.records.get(recordId);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated use layout.records.find() instead
|
|
38
|
+
*/
|
|
39
|
+
newFind() {
|
|
40
|
+
return this.records.find();
|
|
41
|
+
}
|
|
42
|
+
runScript(script) {
|
|
43
|
+
let trace = new Error();
|
|
44
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`;
|
|
46
|
+
if (script.parameter)
|
|
47
|
+
url += "?script.param=" + encodeURIComponent(script.parameter);
|
|
48
|
+
this.database.apiRequest(url, {
|
|
49
|
+
port: 443,
|
|
50
|
+
method: "GET"
|
|
51
|
+
})
|
|
52
|
+
.then(res => {
|
|
53
|
+
if (res.messages[0].code === "0") {
|
|
54
|
+
let error = parseInt(res.response.scriptError);
|
|
55
|
+
let msg = {
|
|
56
|
+
scriptError: error ? new FMError(error, 200, res, trace) : undefined,
|
|
57
|
+
scriptResult: res.response.scriptResult
|
|
58
|
+
};
|
|
59
|
+
resolve(msg);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
// console.log(res)
|
|
63
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
.catch(e => {
|
|
67
|
+
reject(e);
|
|
68
|
+
});
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
getLayoutMeta() {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
if (this.metadata) {
|
|
74
|
+
resolve(this.metadata);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.database.apiRequest(this.endpoint).then(res => {
|
|
78
|
+
this.metadata = res.response;
|
|
79
|
+
resolve(this);
|
|
80
|
+
})
|
|
81
|
+
.catch(e => reject(e));
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023. See LICENSE file for more information
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {LayoutRecordManager} from "./layoutRecordManager";
|
|
6
|
+
import {LayoutRecord} from "../records/layoutRecord";
|
|
7
|
+
import {ScriptResult} from "../types.js";
|
|
8
|
+
import {LayoutInterface} from "./layoutInterface";
|
|
9
|
+
import {Find} from "../records/getOperations/find";
|
|
10
|
+
import {FMError} from "../FMError";
|
|
11
|
+
import {Database} from "../connection/database";
|
|
12
|
+
|
|
13
|
+
export class Layout<T extends LayoutInterface> {
|
|
14
|
+
readonly database: Database;
|
|
15
|
+
readonly records = new LayoutRecordManager<T>(this)
|
|
16
|
+
protected name: string;
|
|
17
|
+
metadata: any;
|
|
18
|
+
|
|
19
|
+
constructor(database: Database, name: string) {
|
|
20
|
+
this.database = database
|
|
21
|
+
this.name = name
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get endpoint() {
|
|
25
|
+
return `${this.database.endpoint}/layouts/${this.name}`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @deprecated use layout.records.create() instead
|
|
30
|
+
*/
|
|
31
|
+
createRecord(): Promise<LayoutRecord<T["fields"], T["portals"]>> {
|
|
32
|
+
return this.records.create()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated use layout.records.get() instead
|
|
37
|
+
*/
|
|
38
|
+
getRecord(recordId): Promise<LayoutRecord<T["fields"], T["portals"]>> {
|
|
39
|
+
return this.records.get(recordId)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated use layout.records.find() instead
|
|
44
|
+
*/
|
|
45
|
+
newFind(): Find<T> {
|
|
46
|
+
return this.records.find()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
runScript(script): Promise<ScriptResult> {
|
|
50
|
+
let trace = new Error()
|
|
51
|
+
return new Promise(async (resolve, reject) => {
|
|
52
|
+
let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`
|
|
53
|
+
if (script.parameter) url += "?script.param=" + encodeURIComponent(script.parameter)
|
|
54
|
+
this.database.apiRequest(url, {
|
|
55
|
+
port: 443,
|
|
56
|
+
method: "GET"
|
|
57
|
+
})
|
|
58
|
+
.then(res => {
|
|
59
|
+
if (res.messages[0].code === "0") {
|
|
60
|
+
let error = parseInt(res.response.scriptError)
|
|
61
|
+
let msg: ScriptResult = {
|
|
62
|
+
scriptError: error ? new FMError(error, 200, res, trace) : undefined,
|
|
63
|
+
scriptResult: res.response.scriptResult
|
|
64
|
+
}
|
|
65
|
+
resolve(msg)
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// console.log(res)
|
|
69
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace))
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.catch(e => {
|
|
73
|
+
reject(e)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public getLayoutMeta(): Promise<this | FMError> {
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
if (this.metadata) {
|
|
81
|
+
resolve(this.metadata)
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.database.apiRequest(this.endpoint).then(res => {
|
|
86
|
+
this.metadata = res.response
|
|
87
|
+
resolve(this)
|
|
88
|
+
})
|
|
89
|
+
.catch(e => reject(e))
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Portal } from "../records/portal";
|
|
2
|
+
import { Field } from "../records/field";
|
|
3
|
+
export interface LayoutInterface {
|
|
4
|
+
fields: RecordFieldsMap;
|
|
5
|
+
portals: PortalInterface;
|
|
6
|
+
}
|
|
7
|
+
export interface PortalInterface {
|
|
8
|
+
[key: string]: Portal<RecordFieldsMap>;
|
|
9
|
+
}
|
|
10
|
+
export interface RecordFieldsMap {
|
|
11
|
+
[fieldName: string]: Field<FieldValue>;
|
|
12
|
+
}
|
|
13
|
+
export type FieldValue = string | number | Date;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023. See LICENSE file for more information
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {Portal} from "../records/portal";
|
|
6
|
+
import {Field} from "../records/field";
|
|
7
|
+
|
|
8
|
+
export interface LayoutInterface {
|
|
9
|
+
fields: RecordFieldsMap,
|
|
10
|
+
portals: PortalInterface
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface PortalInterface {
|
|
14
|
+
[key: string]: Portal<RecordFieldsMap>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RecordFieldsMap {
|
|
18
|
+
[fieldName: string]: Field<FieldValue>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type FieldValue = string | number | Date
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LayoutRecord } from "../records/layoutRecord";
|
|
2
|
+
import { Layout } from "./layout";
|
|
3
|
+
import { LayoutInterface } from "./layoutInterface";
|
|
4
|
+
import { RecordGetRange } from "../records/getOperations/recordGetRange";
|
|
5
|
+
import { Find } from "../records/getOperations/find";
|
|
6
|
+
export declare class LayoutRecordManager<T extends LayoutInterface> {
|
|
7
|
+
readonly layout: Layout<T>;
|
|
8
|
+
constructor(layout: Layout<T>);
|
|
9
|
+
create(): Promise<LayoutRecord<T["fields"], T["portals"]>>;
|
|
10
|
+
get(recordId: any): Promise<LayoutRecord<T["fields"], T["portals"]>>;
|
|
11
|
+
range(start?: number, limit?: number): RecordGetRange<LayoutInterface>;
|
|
12
|
+
find(start?: number, limit?: number): Find<T>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023. See LICENSE file for more information
|
|
3
|
+
*/
|
|
4
|
+
import { LayoutRecord } from "../records/layoutRecord";
|
|
5
|
+
import { RecordGetRange } from "../records/getOperations/recordGetRange";
|
|
6
|
+
import { Find } from "../records/getOperations/find";
|
|
7
|
+
export class LayoutRecordManager {
|
|
8
|
+
constructor(layout) {
|
|
9
|
+
this.layout = layout;
|
|
10
|
+
}
|
|
11
|
+
create() {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
// Get the layout's metadata
|
|
14
|
+
this.layout.getLayoutMeta().then(layout => {
|
|
15
|
+
let fields = {};
|
|
16
|
+
for (let _field of this.layout.metadata.fieldMetaData) {
|
|
17
|
+
fields[_field.name] = "";
|
|
18
|
+
}
|
|
19
|
+
let portals = {};
|
|
20
|
+
for (let _portal of Object.keys(this.layout.metadata.portalMetaData))
|
|
21
|
+
portals[_portal] = [];
|
|
22
|
+
resolve(new LayoutRecord(this.layout, -1, 0, fields, portals));
|
|
23
|
+
}).catch(e => {
|
|
24
|
+
reject(e);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
get(recordId) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
let record;
|
|
31
|
+
this.layout.getLayoutMeta()
|
|
32
|
+
.then(layout => {
|
|
33
|
+
record = new LayoutRecord(this.layout, recordId);
|
|
34
|
+
return record.get();
|
|
35
|
+
})
|
|
36
|
+
.then(() => {
|
|
37
|
+
resolve(record);
|
|
38
|
+
})
|
|
39
|
+
.catch(e => {
|
|
40
|
+
reject(e);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
range(start = 0, limit = 100) {
|
|
45
|
+
return new RecordGetRange(this.layout, start, limit);
|
|
46
|
+
}
|
|
47
|
+
find(start = 0, limit = 100) {
|
|
48
|
+
return new Find(this.layout, start, limit);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023. See LICENSE file for more information
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {LayoutRecord} from "../records/layoutRecord";
|
|
6
|
+
import {Layout} from "./layout";
|
|
7
|
+
import {LayoutInterface} from "./layoutInterface";
|
|
8
|
+
import {RecordGetRange} from "../records/getOperations/recordGetRange";
|
|
9
|
+
import {Find} from "../records/getOperations/find";
|
|
10
|
+
|
|
11
|
+
export class LayoutRecordManager<T extends LayoutInterface> {
|
|
12
|
+
readonly layout: Layout<T>
|
|
13
|
+
|
|
14
|
+
constructor(layout: Layout<T>) {
|
|
15
|
+
this.layout = layout
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
create(): Promise<LayoutRecord<T["fields"], T["portals"]>> {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
// Get the layout's metadata
|
|
21
|
+
this.layout.getLayoutMeta().then(layout => {
|
|
22
|
+
let fields = {}
|
|
23
|
+
for (let _field of this.layout.metadata.fieldMetaData) {
|
|
24
|
+
fields[_field.name] = ""
|
|
25
|
+
}
|
|
26
|
+
let portals = {}
|
|
27
|
+
for (let _portal of Object.keys(this.layout.metadata.portalMetaData)) portals[_portal] = []
|
|
28
|
+
resolve(new LayoutRecord(this.layout, -1, 0, fields, portals))
|
|
29
|
+
}).catch(e => {
|
|
30
|
+
reject(e)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get(recordId): Promise<LayoutRecord<T["fields"], T["portals"]>> {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
let record
|
|
38
|
+
this.layout.getLayoutMeta()
|
|
39
|
+
.then(layout => {
|
|
40
|
+
record = new LayoutRecord(this.layout, recordId)
|
|
41
|
+
return record.get()
|
|
42
|
+
})
|
|
43
|
+
.then(() => {
|
|
44
|
+
resolve(record)
|
|
45
|
+
})
|
|
46
|
+
.catch(e => {
|
|
47
|
+
reject(e)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
range(start = 0, limit = 100) {
|
|
53
|
+
return new RecordGetRange(this.layout, start, limit)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
find(start = 0, limit = 100): Find<T> {
|
|
57
|
+
return new Find(this.layout, start, limit)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { RecordBase } from "./recordBase";
|
|
2
|
+
import * as http from "http";
|
|
3
|
+
import { ContainerBufferResult, DOWNLOAD_MODES, FieldMetaData } from "../types.js";
|
|
4
|
+
import { FieldValue } from "../layouts/layoutInterface";
|
|
5
|
+
export declare class Field<T extends FieldValue> {
|
|
6
|
+
record: RecordBase<any>;
|
|
7
|
+
id: string;
|
|
8
|
+
protected _value: T;
|
|
9
|
+
edited: boolean;
|
|
10
|
+
constructor(record: any, id: any, contents: any);
|
|
11
|
+
set(content: string | number | Date | undefined | null): void;
|
|
12
|
+
get metadata(): FieldMetaData;
|
|
13
|
+
get value(): T;
|
|
14
|
+
set value(value: T);
|
|
15
|
+
get string(): string;
|
|
16
|
+
get date(): Date;
|
|
17
|
+
get number(): number;
|
|
18
|
+
upload(buffer: Buffer, filename: string, mime: string): Promise<void>;
|
|
19
|
+
download(mode: DOWNLOAD_MODES.Stream): Promise<http.IncomingMessage>;
|
|
20
|
+
download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerBufferResult>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023. 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
|
+
import fetch, { File, FormData } from "node-fetch";
|
|
14
|
+
import { DOWNLOAD_MODES } from "../types.js";
|
|
15
|
+
import { PortalRecord } from "./portalRecord";
|
|
16
|
+
import { FMError } from "../FMError";
|
|
17
|
+
export class Field {
|
|
18
|
+
constructor(record, id, contents) {
|
|
19
|
+
this.record = record;
|
|
20
|
+
this.id = id;
|
|
21
|
+
this._value = contents;
|
|
22
|
+
this.edited = false;
|
|
23
|
+
}
|
|
24
|
+
set(content) {
|
|
25
|
+
if (this.metadata.result === "container")
|
|
26
|
+
throw "Cannot set container value using set(). Use upload() instead.";
|
|
27
|
+
if ((this.metadata.result === "timeStamp" ||
|
|
28
|
+
this.metadata.result === "date" ||
|
|
29
|
+
this.metadata.result === "time")
|
|
30
|
+
&& !(content instanceof Date) && !!content) {
|
|
31
|
+
throw "Value was not an instance of Date: " + content;
|
|
32
|
+
}
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
if (!content)
|
|
35
|
+
this._value = "";
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
else
|
|
38
|
+
this._value = content;
|
|
39
|
+
this.edited = true;
|
|
40
|
+
}
|
|
41
|
+
get metadata() {
|
|
42
|
+
if (!this.record.layout.metadata) {
|
|
43
|
+
// Default to a regular text field
|
|
44
|
+
return {
|
|
45
|
+
name: this.id.toString(),
|
|
46
|
+
type: 'normal',
|
|
47
|
+
displayType: 'editText',
|
|
48
|
+
result: 'text',
|
|
49
|
+
global: false,
|
|
50
|
+
autoEnter: true,
|
|
51
|
+
fourDigitYear: false,
|
|
52
|
+
maxRepeat: 1,
|
|
53
|
+
maxCharacters: 0,
|
|
54
|
+
notEmpty: false,
|
|
55
|
+
numeric: false,
|
|
56
|
+
timeOfDay: false,
|
|
57
|
+
repetitionStart: 1,
|
|
58
|
+
repetitionEnd: 1
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (this.record instanceof PortalRecord) {
|
|
62
|
+
return this.record.layout.metadata.portalMetaData[this.record.portal.name || "portal not attached"].find(i => i.name === this.id) || {
|
|
63
|
+
name: this.id.toString(),
|
|
64
|
+
type: 'normal',
|
|
65
|
+
displayType: 'editText',
|
|
66
|
+
result: 'text',
|
|
67
|
+
global: false,
|
|
68
|
+
autoEnter: true,
|
|
69
|
+
fourDigitYear: false,
|
|
70
|
+
maxRepeat: 1,
|
|
71
|
+
maxCharacters: 0,
|
|
72
|
+
notEmpty: false,
|
|
73
|
+
numeric: false,
|
|
74
|
+
timeOfDay: false,
|
|
75
|
+
repetitionStart: 1,
|
|
76
|
+
repetitionEnd: 1
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return this.record.layout.metadata.fieldMetaData.find(i => i.name === this.id) || {
|
|
81
|
+
name: this.id.toString(),
|
|
82
|
+
type: 'normal',
|
|
83
|
+
displayType: 'editText',
|
|
84
|
+
result: 'text',
|
|
85
|
+
global: false,
|
|
86
|
+
autoEnter: true,
|
|
87
|
+
fourDigitYear: false,
|
|
88
|
+
maxRepeat: 1,
|
|
89
|
+
maxCharacters: 0,
|
|
90
|
+
notEmpty: false,
|
|
91
|
+
numeric: false,
|
|
92
|
+
timeOfDay: false,
|
|
93
|
+
repetitionStart: 1,
|
|
94
|
+
repetitionEnd: 1
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
get value() {
|
|
99
|
+
// if (this.metadata.result === "container") throw "Use await field.stream() to get the contents of a container field, instead of field.value"
|
|
100
|
+
return this._value;
|
|
101
|
+
}
|
|
102
|
+
set value(value) {
|
|
103
|
+
this._value = value;
|
|
104
|
+
}
|
|
105
|
+
get string() {
|
|
106
|
+
if (typeof this._value === "string") {
|
|
107
|
+
return this._value;
|
|
108
|
+
}
|
|
109
|
+
throw "Field value is not a string";
|
|
110
|
+
}
|
|
111
|
+
get date() {
|
|
112
|
+
if (this._value instanceof Date) {
|
|
113
|
+
return this._value;
|
|
114
|
+
}
|
|
115
|
+
throw "Field value is not a date";
|
|
116
|
+
}
|
|
117
|
+
get number() {
|
|
118
|
+
if (typeof this._value === "number") {
|
|
119
|
+
return this._value;
|
|
120
|
+
}
|
|
121
|
+
throw "Field value is not a number";
|
|
122
|
+
}
|
|
123
|
+
upload(buffer, filename, mime) {
|
|
124
|
+
let trace = new Error();
|
|
125
|
+
if (this.metadata.result !== "container")
|
|
126
|
+
throw "Cannot upload a file to the field; " + this.id + " (not a container field)";
|
|
127
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
let form = new FormData();
|
|
129
|
+
form.append("upload", new File([buffer], filename, { type: mime }));
|
|
130
|
+
let _fetch = yield fetch(`${this.record.endpoint}/containers/${this.id}/1`, {
|
|
131
|
+
method: "POST",
|
|
132
|
+
headers: { "Authorization": "Bearer " + this.record.layout.database.token },
|
|
133
|
+
body: form
|
|
134
|
+
}).then(res => res.json())
|
|
135
|
+
.then(data => {
|
|
136
|
+
let _res = data;
|
|
137
|
+
if (_res.messages[0].code === "0") {
|
|
138
|
+
resolve();
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
reject(new FMError(_res.messages[0].code, _res.status, data, trace));
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
.catch(e => {
|
|
145
|
+
reject(e);
|
|
146
|
+
});
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
download(mode = DOWNLOAD_MODES.Stream) {
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
this.record.layout.database.streamContainer(this, this._value)
|
|
152
|
+
.then(stream => {
|
|
153
|
+
if (mode === DOWNLOAD_MODES.Stream) {
|
|
154
|
+
resolve(stream);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
let body = [];
|
|
158
|
+
stream.on("data", chunk => {
|
|
159
|
+
body.push(chunk);
|
|
160
|
+
});
|
|
161
|
+
stream.on("error", (e) => {
|
|
162
|
+
reject(e);
|
|
163
|
+
});
|
|
164
|
+
stream.on("end", () => {
|
|
165
|
+
resolve({
|
|
166
|
+
buffer: Buffer.concat(body),
|
|
167
|
+
mime: stream.headers["content-type"],
|
|
168
|
+
request: stream
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
})
|
|
172
|
+
.catch(e => {
|
|
173
|
+
reject(e);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|