@jd-data-limited/easy-fm 3.2.8 → 4.0.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.
Files changed (72) hide show
  1. package/README.md +36 -24
  2. package/package.json +12 -9
  3. package/src/FMError.js +2 -2
  4. package/src/FMError.ts +2 -2
  5. package/src/connection/FMHost.d.ts +2 -1
  6. package/src/connection/FMHost.js +1 -2
  7. package/src/connection/FMHost.ts +4 -4
  8. package/src/connection/HostBase.d.ts +1 -1
  9. package/src/connection/HostBase.js +1 -1
  10. package/src/connection/HostBase.ts +2 -4
  11. package/src/connection/database.d.ts +9 -8
  12. package/src/connection/database.js +6 -7
  13. package/src/connection/database.ts +14 -13
  14. package/src/connection/databaseBase.d.ts +4 -4
  15. package/src/connection/databaseBase.js +1 -1
  16. package/src/connection/databaseBase.ts +4 -6
  17. package/src/databaseStructure.d.ts +6 -0
  18. package/src/databaseStructure.js +4 -0
  19. package/src/databaseStructure.ts +12 -0
  20. package/src/index.d.ts +3 -6
  21. package/src/index.js +3 -5
  22. package/src/index.ts +4 -9
  23. package/src/layouts/layout.d.ts +3 -17
  24. package/src/layouts/layout.js +2 -20
  25. package/src/layouts/layout.ts +5 -27
  26. package/src/layouts/layoutBase.d.ts +3 -3
  27. package/src/layouts/layoutBase.js +1 -1
  28. package/src/layouts/layoutBase.ts +4 -5
  29. package/src/layouts/layoutInterface.d.ts +1 -1
  30. package/src/layouts/layoutInterface.js +1 -1
  31. package/src/layouts/layoutInterface.ts +2 -2
  32. package/src/layouts/layoutRecordManager.d.ts +5 -6
  33. package/src/layouts/layoutRecordManager.js +5 -9
  34. package/src/layouts/layoutRecordManager.ts +9 -14
  35. package/src/layouts/recordFieldsMap.d.ts +2 -2
  36. package/src/layouts/recordFieldsMap.js +1 -1
  37. package/src/layouts/recordFieldsMap.ts +3 -3
  38. package/src/records/field.d.ts +3 -23
  39. package/src/records/field.js +3 -152
  40. package/src/records/field.ts +4 -169
  41. package/src/records/fieldBase.d.ts +30 -0
  42. package/src/records/fieldBase.js +154 -0
  43. package/src/records/fieldBase.ts +174 -0
  44. package/src/records/getOperations/recordGetOperation.d.ts +47 -11
  45. package/src/records/getOperations/recordGetOperation.js +142 -31
  46. package/src/records/getOperations/recordGetOperation.ts +172 -30
  47. package/src/records/layoutRecord.d.ts +8 -7
  48. package/src/records/layoutRecord.js +4 -2
  49. package/src/records/layoutRecord.ts +19 -11
  50. package/src/records/portal.d.ts +6 -5
  51. package/src/records/portal.js +1 -1
  52. package/src/records/portal.ts +7 -7
  53. package/src/records/portalBase.d.ts +5 -4
  54. package/src/records/portalBase.js +1 -1
  55. package/src/records/portalBase.ts +6 -6
  56. package/src/records/portalRecord.d.ts +6 -6
  57. package/src/records/portalRecord.js +3 -2
  58. package/src/records/portalRecord.ts +5 -5
  59. package/src/records/recordBase.d.ts +7 -12
  60. package/src/records/recordBase.js +6 -10
  61. package/src/records/recordBase.ts +10 -15
  62. package/src/types.d.ts +18 -15
  63. package/src/types.js +7 -1
  64. package/src/types.ts +20 -16
  65. package/src/records/getOperations/find.d.ts +0 -15
  66. package/src/records/getOperations/find.js +0 -70
  67. package/src/records/getOperations/find.ts +0 -77
  68. package/src/records/getOperations/recordGetRange.d.ts +0 -13
  69. package/src/records/getOperations/recordGetRange.js +0 -77
  70. package/src/records/getOperations/recordGetRange.ts +0 -77
  71. package/src/records/portalRecordBase.d.ts +0 -4
  72. package/src/records/portalRecordBase.js +0 -6
@@ -1,21 +1,19 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
5
  import {LayoutRecordManager} from "./layoutRecordManager.js";
6
- import {LayoutRecord} from "../records/layoutRecord.js";
7
6
  import {Script, ScriptResult} from "../types.js";
8
7
  import {LayoutInterface} from "./layoutInterface.js";
9
- import {Find} from "../records/getOperations/find.js";
10
8
  import {FMError} from "../FMError.js";
11
9
  import {LayoutBase} from "./layoutBase.js"
12
- import {DatabaseBase} from "../connection/databaseBase";
13
- import {ApiLayoutMetadata, ApiScriptResult} from "../models/apiResults";
10
+ import {DatabaseBase} from "../connection/databaseBase.js";
11
+ import {ApiLayoutMetadata, ApiScriptResult} from "../models/apiResults.js";
14
12
 
15
13
  export class Layout<T extends LayoutInterface> implements LayoutBase {
16
14
  readonly database: DatabaseBase;
17
- readonly records = new LayoutRecordManager<T>(this)
18
15
  readonly name: string;
16
+ readonly records = new LayoutRecordManager<T>(this)
19
17
  metadata: ApiLayoutMetadata;
20
18
 
21
19
  constructor(database: DatabaseBase, name: string) {
@@ -23,31 +21,11 @@ export class Layout<T extends LayoutInterface> implements LayoutBase {
23
21
  this.name = name
24
22
  }
25
23
 
24
+
26
25
  get endpoint() {
27
26
  return `${this.database.endpoint}/layouts/${this.name}`
28
27
  }
29
28
 
30
- /**
31
- * @deprecated use layout.records.create() instead
32
- */
33
- createRecord(): Promise<LayoutRecord<T["fields"], T["portals"]>> {
34
- return this.records.create()
35
- }
36
-
37
- /**
38
- * @deprecated use layout.records.get() instead
39
- */
40
- getRecord(recordId): Promise<LayoutRecord<T["fields"], T["portals"]>> {
41
- return this.records.get(recordId)
42
- }
43
-
44
- /**
45
- * @deprecated use layout.records.find() instead
46
- */
47
- newFind(): Find<T> {
48
- return this.records.find()
49
- }
50
-
51
29
  async runScript(script: Script): Promise<ScriptResult> {
52
30
  let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`
53
31
  if (script.parameter) url += "?script.param=" + encodeURIComponent(script.parameter)
@@ -1,6 +1,6 @@
1
- import { Script, ScriptResult } from "../types";
2
- import { DatabaseBase } from "../connection/databaseBase";
3
- import { ApiLayoutMetadata } from "../models/apiResults";
1
+ import { Script, ScriptResult } from "../types.js";
2
+ import { DatabaseBase } from "../connection/databaseBase.js";
3
+ import { ApiLayoutMetadata } from "../models/apiResults.js";
4
4
  export interface LayoutBase {
5
5
  readonly name: string;
6
6
  metadata: any;
@@ -1,4 +1,4 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  export {};
@@ -1,11 +1,10 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
- import {Script, ScriptResult} from "../types";
6
- import {FMError} from "../FMError";
7
- import {DatabaseBase} from "../connection/databaseBase";
8
- import {ApiLayoutMetadata} from "../models/apiResults";
5
+ import {Script, ScriptResult} from "../types.js";
6
+ import {DatabaseBase} from "../connection/databaseBase.js";
7
+ import {ApiLayoutMetadata} from "../models/apiResults.js";
9
8
 
10
9
  export interface LayoutBase {
11
10
  readonly name: string
@@ -1,5 +1,5 @@
1
1
  import { Portal } from "../records/portal.js";
2
- import { RecordFieldsMap } from "./recordFieldsMap";
2
+ import { RecordFieldsMap } from "./recordFieldsMap.js";
3
3
  export interface LayoutInterface {
4
4
  fields: RecordFieldsMap;
5
5
  portals: PortalInterface;
@@ -1,4 +1,4 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  export {};
@@ -1,9 +1,9 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
5
  import {Portal} from "../records/portal.js";
6
- import {RecordFieldsMap} from "./recordFieldsMap";
6
+ import {RecordFieldsMap} from "./recordFieldsMap.js";
7
7
 
8
8
  export interface LayoutInterface {
9
9
  fields: RecordFieldsMap,
@@ -1,13 +1,12 @@
1
1
  import { LayoutRecord } from "../records/layoutRecord.js";
2
2
  import { LayoutInterface } from "./layoutInterface.js";
3
- import { RecordGetRange } from "../records/getOperations/recordGetRange.js";
4
- import { Find } from "../records/getOperations/find.js";
5
3
  import { LayoutBase } from "./layoutBase.js";
4
+ import { GetOperationOptions, RecordGetOperation } from "../records/getOperations/recordGetOperation.js";
5
+ import { RecordFetchOptions } from "../types.js";
6
6
  export declare class LayoutRecordManager<T extends LayoutInterface> {
7
7
  readonly layout: LayoutBase;
8
8
  constructor(layout: LayoutBase);
9
- create(): Promise<LayoutRecord<T["fields"], T["portals"]>>;
10
- get(recordId: number): Promise<LayoutRecord<T["fields"], T["portals"]>>;
11
- range(start?: number, limit?: number): RecordGetRange<T>;
12
- find(start?: number, limit?: number): Find<T>;
9
+ create<OPTIONS extends RecordFetchOptions>(options: OPTIONS): Promise<LayoutRecord<T, OPTIONS["portals"][number]>>;
10
+ get(recordId: number): Promise<LayoutRecord<T>>;
11
+ list<OPTIONS extends GetOperationOptions<T>>(options: OPTIONS): RecordGetOperation<T, OPTIONS>;
13
12
  }
@@ -1,15 +1,14 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  import { LayoutRecord } from "../records/layoutRecord.js";
5
- import { RecordGetRange } from "../records/getOperations/recordGetRange.js";
6
- import { Find } from "../records/getOperations/find.js";
5
+ import { RecordGetOperation } from "../records/getOperations/recordGetOperation.js";
7
6
  export class LayoutRecordManager {
8
7
  layout;
9
8
  constructor(layout) {
10
9
  this.layout = layout;
11
10
  }
12
- async create() {
11
+ async create(options) {
13
12
  await this.layout.getLayoutMeta();
14
13
  let fields = {};
15
14
  for (let _field of this.layout.metadata.fieldMetaData) {
@@ -26,10 +25,7 @@ export class LayoutRecordManager {
26
25
  await record.get();
27
26
  return record;
28
27
  }
29
- range(start = 0, limit = 100) {
30
- return new RecordGetRange(this.layout, start, limit);
31
- }
32
- find(start = 0, limit = 100) {
33
- return new Find(this.layout, start, limit);
28
+ list(options) {
29
+ return new RecordGetOperation(this.layout, options);
34
30
  }
35
31
  }
@@ -1,21 +1,20 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
5
  import {LayoutRecord} from "../records/layoutRecord.js";
6
6
  import {LayoutInterface} from "./layoutInterface.js";
7
- import {RecordGetRange} from "../records/getOperations/recordGetRange.js";
8
- import {Find} from "../records/getOperations/find.js";
9
7
  import {LayoutBase} from "./layoutBase.js"
8
+ import {GetOperationOptions, RecordGetOperation} from "../records/getOperations/recordGetOperation.js";
9
+ import {RecordFetchOptions} from "../types.js";
10
10
 
11
11
  export class LayoutRecordManager<T extends LayoutInterface> {
12
- readonly layout: LayoutBase
13
-
12
+ readonly layout: LayoutBase;
14
13
  constructor(layout: LayoutBase) {
15
14
  this.layout = layout
16
15
  }
17
16
 
18
- async create(): Promise<LayoutRecord<T["fields"], T["portals"]>> {
17
+ async create<OPTIONS extends RecordFetchOptions>(options: OPTIONS): Promise<LayoutRecord<T, OPTIONS["portals"][number]>> {
19
18
  await this.layout.getLayoutMeta()
20
19
  let fields = {}
21
20
  for (let _field of this.layout.metadata.fieldMetaData) {
@@ -26,18 +25,14 @@ export class LayoutRecordManager<T extends LayoutInterface> {
26
25
  return new LayoutRecord(this.layout, -1, 0, fields, portals)
27
26
  }
28
27
 
29
- async get(recordId: number): Promise<LayoutRecord<T["fields"], T["portals"]>> {
28
+ async get(recordId: number): Promise<LayoutRecord<T>> {
30
29
  await this.layout.getLayoutMeta()
31
- let record = new LayoutRecord(this.layout, recordId)
30
+ let record = new LayoutRecord<T>(this.layout, recordId)
32
31
  await record.get()
33
32
  return record
34
33
  }
35
34
 
36
- range(start = 0, limit = 100) {
37
- return new RecordGetRange<T>(this.layout, start, limit)
38
- }
39
-
40
- find(start = 0, limit = 100): Find<T> {
41
- return new Find<T>(this.layout, start, limit)
35
+ list<OPTIONS extends GetOperationOptions<T>>(options: OPTIONS) {
36
+ return new RecordGetOperation<T, OPTIONS>(this.layout, options)
42
37
  }
43
38
  }
@@ -1,4 +1,4 @@
1
- import { Field, FieldValue } from "../records/field";
1
+ import { FieldBase, FieldValue } from "../records/fieldBase.js";
2
2
  export interface RecordFieldsMap {
3
- [fieldName: string]: Field<FieldValue>;
3
+ [fieldName: string]: FieldBase<FieldValue>;
4
4
  }
@@ -1,4 +1,4 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  export {};
@@ -1,9 +1,9 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
- import {Field, FieldValue} from "../records/field";
5
+ import {FieldBase, FieldValue} from "../records/fieldBase.js";
6
6
 
7
7
  export interface RecordFieldsMap {
8
- [fieldName: string]: Field<FieldValue>
8
+ [fieldName: string]: FieldBase<FieldValue>
9
9
  }
@@ -1,25 +1,5 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { RecordBase } from "./recordBase.js";
4
- import * as http from "http";
5
- import { ContainerBufferResult, DOWNLOAD_MODES } from "../types.js";
6
- import { ApiFieldMetadata } from "../models/apiResults.js";
7
- export type FieldValue = string | number | Date | Container;
8
- export type Container = null;
9
- export declare class Field<T extends FieldValue> {
1
+ import { RecordBase } from "./recordBase";
2
+ import { FieldBase, FieldValue } from "./fieldBase";
3
+ export declare class Field<T extends FieldValue> extends FieldBase<T> {
10
4
  record: RecordBase<any>;
11
- id: string;
12
- protected _value: T;
13
- edited: boolean;
14
- constructor(record: any, id: any, contents: any);
15
- set(content: string | number | Date | undefined | null): void;
16
- get metadata(): ApiFieldMetadata;
17
- get value(): T;
18
- set value(value: T);
19
- get string(): string;
20
- get date(): Date;
21
- get number(): number;
22
- upload(buffer: Buffer, filename: string, mime: string): Promise<void>;
23
- download(mode: DOWNLOAD_MODES.Stream): Promise<http.IncomingMessage>;
24
- download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerBufferResult>;
25
5
  }
@@ -1,156 +1,7 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2024. See LICENSE file for more information
3
3
  */
4
- import { RecordTypes } from "./recordBase.js";
5
- import { File, FormData } from "node-fetch";
6
- import { DOWNLOAD_MODES } from "../types.js";
7
- import { ApiFieldDisplayTypes, ApiFieldResultTypes, ApiFieldTypes } from "../models/apiResults.js";
8
- export class Field {
4
+ import { FieldBase } from "./fieldBase";
5
+ export class Field extends FieldBase {
9
6
  record;
10
- id;
11
- _value;
12
- edited;
13
- constructor(record, id, contents) {
14
- this.record = record;
15
- this.id = id;
16
- this._value = contents;
17
- this.edited = false;
18
- }
19
- set(content) {
20
- if (this.metadata.result === "container")
21
- throw "Cannot set container value using set(). Use upload() instead.";
22
- if ((this.metadata.result === "timeStamp" ||
23
- this.metadata.result === "date" ||
24
- this.metadata.result === "time")
25
- && !(content instanceof Date) && !!content) {
26
- throw "Value was not an instance of Date: " + content;
27
- }
28
- // @ts-ignore
29
- if (!content)
30
- this._value = "";
31
- // @ts-ignore
32
- else
33
- this._value = content;
34
- this.edited = true;
35
- }
36
- get metadata() {
37
- if (!this.record.layout.metadata) {
38
- // Default to a regular text field
39
- return {
40
- name: this.id.toString(),
41
- type: ApiFieldTypes.NORMAL,
42
- displayType: ApiFieldDisplayTypes.EDIT_TEXT,
43
- result: ApiFieldResultTypes.TEXT,
44
- global: false,
45
- autoEnter: true,
46
- fourDigitYear: false,
47
- maxRepeat: 1,
48
- maxCharacters: 0,
49
- notEmpty: false,
50
- numeric: false,
51
- timeOfDay: false,
52
- repetitions: 1,
53
- valueList: ""
54
- };
55
- }
56
- if (this.record.type === RecordTypes.PORTAL) {
57
- // @ts-ignore
58
- return this.record.layout.metadata.portalMetaData[this.record.portal.name || "portal not attached"].find(i => i.name === this.id) || {
59
- name: this.id.toString(),
60
- type: 'normal',
61
- displayType: 'editText',
62
- result: 'text',
63
- global: false,
64
- autoEnter: true,
65
- fourDigitYear: false,
66
- maxRepeat: 1,
67
- maxCharacters: 0,
68
- notEmpty: false,
69
- numeric: false,
70
- timeOfDay: false,
71
- repetitionStart: 1,
72
- repetitionEnd: 1
73
- };
74
- }
75
- else {
76
- return this.record.layout.metadata.fieldMetaData.find(i => i.name === this.id) || {
77
- name: this.id.toString(),
78
- type: ApiFieldTypes.NORMAL,
79
- displayType: ApiFieldDisplayTypes.EDIT_TEXT,
80
- result: ApiFieldResultTypes.TEXT,
81
- global: false,
82
- autoEnter: true,
83
- fourDigitYear: false,
84
- maxRepeat: 1,
85
- maxCharacters: 0,
86
- notEmpty: false,
87
- numeric: false,
88
- timeOfDay: false,
89
- repetitions: 1,
90
- valueList: ""
91
- };
92
- }
93
- }
94
- get value() {
95
- // if (this.metadata.result === "container") throw "Use await field.stream() to get the contents of a container field, instead of field.value"
96
- return this._value;
97
- }
98
- set value(value) {
99
- this._value = value;
100
- }
101
- get string() {
102
- if (typeof this._value === "string") {
103
- return this._value;
104
- }
105
- throw "Field value is not a string";
106
- }
107
- get date() {
108
- if (this._value instanceof Date) {
109
- return this._value;
110
- }
111
- throw "Field value is not a date";
112
- }
113
- get number() {
114
- if (typeof this._value === "number") {
115
- return this._value;
116
- }
117
- throw "Field value is not a number";
118
- }
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
130
- });
131
- }
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
152
- });
153
- });
154
- });
155
- }
156
7
  }
@@ -1,175 +1,10 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2024. See LICENSE file for more information
3
3
  */
4
4
 
5
- import {RecordBase, RecordTypes} from "./recordBase.js";
6
- import fetch, {File, FormData} from "node-fetch";
7
- import * as http from "http";
8
- import {ContainerBufferResult, DOWNLOAD_MODES, FieldMetaData} from "../types.js";
9
- import {FMError} from "../FMError.js";
10
- import {ApiFieldDisplayTypes, ApiFieldMetadata, ApiFieldResultTypes, ApiFieldTypes} from "../models/apiResults.js";
5
+ import {RecordBase} from "./recordBase";
6
+ import {FieldBase, FieldValue} from "./fieldBase";
11
7
 
12
- export type FieldValue = string | number | Date | Container
13
- export type Container = null
14
-
15
- export class Field<T extends FieldValue> {
8
+ export class Field<T extends FieldValue> extends FieldBase<T> {
16
9
  record: RecordBase<any>;
17
- id: string;
18
- protected _value: T;
19
- edited: boolean;
20
-
21
- constructor(record, id, contents) {
22
- this.record = record
23
- this.id = id
24
- this._value = contents
25
- this.edited = false
26
- }
27
-
28
- set(content: string | number | Date | undefined | null) {
29
- if (this.metadata.result === "container") throw "Cannot set container value using set(). Use upload() instead."
30
- if (
31
- (this.metadata.result === "timeStamp" ||
32
- this.metadata.result === "date" ||
33
- this.metadata.result === "time")
34
- && !(content instanceof Date) && !!content
35
- ) {
36
- throw "Value was not an instance of Date: " + content
37
- }
38
- // @ts-ignore
39
- if (!content) this._value = ""
40
- // @ts-ignore
41
- else this._value = content
42
- this.edited = true
43
- }
44
-
45
- get metadata(): ApiFieldMetadata {
46
- if (!this.record.layout.metadata) {
47
- // Default to a regular text field
48
- return {
49
- name: this.id.toString(),
50
- type: ApiFieldTypes.NORMAL,
51
- displayType: ApiFieldDisplayTypes.EDIT_TEXT,
52
- result: ApiFieldResultTypes.TEXT,
53
- global: false,
54
- autoEnter: true,
55
- fourDigitYear: false,
56
- maxRepeat: 1,
57
- maxCharacters: 0,
58
- notEmpty: false,
59
- numeric: false,
60
- timeOfDay: false,
61
- repetitions: 1,
62
- valueList: ""
63
- }
64
- }
65
- if (this.record.type === RecordTypes.PORTAL) {
66
- // @ts-ignore
67
- return this.record.layout.metadata.portalMetaData[this.record.portal.name || "portal not attached"].find(i => i.name === this.id) as FieldMetaData || {
68
- name: this.id.toString(),
69
- type: 'normal',
70
- displayType: 'editText',
71
- result: 'text',
72
- global: false,
73
- autoEnter: true,
74
- fourDigitYear: false,
75
- maxRepeat: 1,
76
- maxCharacters: 0,
77
- notEmpty: false,
78
- numeric: false,
79
- timeOfDay: false,
80
- repetitionStart: 1,
81
- repetitionEnd: 1
82
- } as FieldMetaData
83
- }
84
- else {
85
- return this.record.layout.metadata.fieldMetaData.find(i => i.name === this.id) || {
86
- name: this.id.toString(),
87
- type: ApiFieldTypes.NORMAL,
88
- displayType: ApiFieldDisplayTypes.EDIT_TEXT,
89
- result: ApiFieldResultTypes.TEXT,
90
- global: false,
91
- autoEnter: true,
92
- fourDigitYear: false,
93
- maxRepeat: 1,
94
- maxCharacters: 0,
95
- notEmpty: false,
96
- numeric: false,
97
- timeOfDay: false,
98
- repetitions: 1,
99
- valueList: ""
100
- }
101
- }
102
- }
103
-
104
- get value(): T {
105
- // if (this.metadata.result === "container") throw "Use await field.stream() to get the contents of a container field, instead of field.value"
106
- return this._value
107
- }
108
-
109
- set value(value: T) {
110
- this._value = value
111
- }
112
-
113
- get string(): string {
114
- if (typeof this._value === "string") {
115
- return this._value
116
- }
117
- throw "Field value is not a string"
118
- }
119
-
120
- get date(): Date {
121
- if (this._value instanceof Date) {
122
- return this._value
123
- }
124
- throw "Field value is not a date"
125
- }
126
-
127
- get number(): number {
128
- if (typeof this._value === "number") {
129
- return this._value
130
- }
131
- throw "Field value is not a number"
132
- }
133
-
134
- async upload(buffer: Buffer, filename: string, mime: string): Promise<void> {
135
- let trace = new Error()
136
- if (this.metadata.result !== "container") throw "Cannot upload a file to the field; " + this.id + " (not a container field)"
137
- let form = new FormData()
138
- form.append("upload", new File([buffer], filename, {type: mime}))
139
-
140
- await this.record.layout.database.apiRequest<null>(`${this.record.endpoint}/containers/${this.id}/1`, {
141
- method: "POST",
142
- // @ts-ignore
143
- headers: {"Authorization": "Bearer " + this.record.layout.database.token},
144
- body: form
145
- })
146
- }
147
-
148
- download(mode: DOWNLOAD_MODES.Stream): Promise<http.IncomingMessage>
149
- download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerBufferResult>
150
- async download(mode: DOWNLOAD_MODES = DOWNLOAD_MODES.Stream): Promise<http.IncomingMessage | ContainerBufferResult> {
151
- if (this.metadata.result !== ApiFieldResultTypes.CONTAINER) throw new Error("Cannot perform download() on a non-container field")
152
-
153
- let stream = await this.record.layout.database.streamURL(this.string)
154
- if (mode === DOWNLOAD_MODES.Stream) {
155
- return stream
156
- }
157
-
158
- let body = []
159
- return new Promise((resolve, reject) => {
160
- stream.on("data", chunk => {
161
- body.push(chunk)
162
- })
163
- stream.on("error", (e) => {
164
- reject(e)
165
- })
166
- stream.on("end", () => {
167
- resolve({
168
- buffer: Buffer.concat(body),
169
- mime: stream.headers["content-type"],
170
- request: stream
171
- } as ContainerBufferResult)
172
- })
173
- })
174
- }
175
10
  }
@@ -0,0 +1,30 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import * as http from "http";
4
+ import { ContainerBufferResult, DOWNLOAD_MODES, RecordTypes } from "../types.js";
5
+ import { ApiFieldMetadata } from "../models/apiResults.js";
6
+ import { LayoutBase } from "../layouts/layoutBase.js";
7
+ export type FieldValue = string | number | Date | Container;
8
+ export type Container = null;
9
+ export type Parentable = {
10
+ layout: LayoutBase;
11
+ type: RecordTypes;
12
+ endpoint: string;
13
+ };
14
+ export declare class FieldBase<T extends FieldValue> {
15
+ parent: Parentable;
16
+ id: string;
17
+ protected _value: T;
18
+ edited: boolean;
19
+ constructor(record: Parentable, id: string, contents: T);
20
+ set(content: string | number | Date | undefined | null): void;
21
+ get metadata(): ApiFieldMetadata;
22
+ get value(): T;
23
+ set value(value: T);
24
+ get string(): string;
25
+ get date(): Date;
26
+ get number(): number;
27
+ upload(buffer: Buffer, filename: string, mime: string): Promise<void>;
28
+ download(mode: DOWNLOAD_MODES.Stream): Promise<http.IncomingMessage>;
29
+ download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerBufferResult>;
30
+ }