airtable-ts 1.3.2 → 1.4.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.
|
@@ -6,27 +6,6 @@ const fallbackMapperPair = (toFallback, fromFallback) => ({
|
|
|
6
6
|
toAirtable: (value) => value ?? toFallback,
|
|
7
7
|
fromAirtable: (value) => value ?? fromFallback,
|
|
8
8
|
});
|
|
9
|
-
const dateTimeMapperPair = {
|
|
10
|
-
// Number assumed to be unix time in seconds
|
|
11
|
-
toAirtable: (value) => {
|
|
12
|
-
if (value === null)
|
|
13
|
-
return null;
|
|
14
|
-
const date = new Date(typeof value === 'number' ? value * 1000 : value);
|
|
15
|
-
if (Number.isNaN(date.getTime())) {
|
|
16
|
-
throw new Error('[airtable-ts] Invalid dateTime');
|
|
17
|
-
}
|
|
18
|
-
return date.toJSON();
|
|
19
|
-
},
|
|
20
|
-
fromAirtable: (value) => {
|
|
21
|
-
if (value === null || value === undefined)
|
|
22
|
-
return null;
|
|
23
|
-
const date = new Date(value);
|
|
24
|
-
if (Number.isNaN(date.getTime())) {
|
|
25
|
-
throw new Error('[airtable-ts] Invalid dateTime');
|
|
26
|
-
}
|
|
27
|
-
return date.toJSON();
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
9
|
const readonly = (airtableType) => () => { throw new Error(`[airtable-ts] ${airtableType} type field is readonly`); };
|
|
31
10
|
const coerce = (airtableType, tsType) => (value) => {
|
|
32
11
|
const parsedType = (0, typeUtils_1.parseType)(tsType);
|
|
@@ -50,6 +29,71 @@ const coerce = (airtableType, tsType) => (value) => {
|
|
|
50
29
|
}
|
|
51
30
|
throw new Error(`[airtable-ts] Can't coerce ${airtableType} to a ${tsType}, as it was of type ${typeof value}`);
|
|
52
31
|
};
|
|
32
|
+
const dateTimeMapperPair = {
|
|
33
|
+
// Number assumed to be unix time in seconds
|
|
34
|
+
toAirtable: (value) => {
|
|
35
|
+
if (value === null)
|
|
36
|
+
return null;
|
|
37
|
+
const date = new Date(typeof value === 'number' ? value * 1000 : value);
|
|
38
|
+
if (Number.isNaN(date.getTime())) {
|
|
39
|
+
throw new Error('[airtable-ts] Invalid dateTime');
|
|
40
|
+
}
|
|
41
|
+
return date.toJSON();
|
|
42
|
+
},
|
|
43
|
+
fromAirtable: (value) => {
|
|
44
|
+
if (value === null || value === undefined)
|
|
45
|
+
return null;
|
|
46
|
+
const date = new Date(value);
|
|
47
|
+
if (Number.isNaN(date.getTime())) {
|
|
48
|
+
throw new Error('[airtable-ts] Invalid dateTime');
|
|
49
|
+
}
|
|
50
|
+
return date.toJSON();
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
const aiTextMapperPair = {
|
|
54
|
+
toAirtable: readonly('aiText'),
|
|
55
|
+
fromAirtable: (obj) => {
|
|
56
|
+
if (!obj || typeof obj !== 'object' || !('value' in obj) || typeof obj.value !== 'string')
|
|
57
|
+
return null;
|
|
58
|
+
return obj.value;
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
const barcodeMapperPair = {
|
|
62
|
+
toAirtable: (value) => ({ text: value }),
|
|
63
|
+
fromAirtable: (obj) => {
|
|
64
|
+
if (!obj || typeof obj !== 'object' || !('text' in obj) || typeof obj.text !== 'string')
|
|
65
|
+
return null;
|
|
66
|
+
return obj.text;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
const collaboratorMapperPair = {
|
|
70
|
+
toAirtable: (value) => ({ id: value }),
|
|
71
|
+
fromAirtable: (obj) => {
|
|
72
|
+
if (!obj || typeof obj !== 'object' || !('id' in obj) || typeof obj.id !== 'string')
|
|
73
|
+
return null;
|
|
74
|
+
return obj.id;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
const multipleCollaboratorsMapperPair = {
|
|
78
|
+
toAirtable: (value) => value,
|
|
79
|
+
fromAirtable: (obj) => {
|
|
80
|
+
return obj?.map((v) => ('id' in v && typeof v.id === 'string' ? v.id : null)).filter(Boolean) ?? null;
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
const multipleAttachmentsMapperPair = {
|
|
84
|
+
toAirtable: readonly('multipleAttachments'),
|
|
85
|
+
fromAirtable: (obj) => {
|
|
86
|
+
return obj?.map((v) => ('url' in v && typeof v.url === 'string' ? v.url : null)).filter(Boolean) ?? null;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
const buttonMapperPair = {
|
|
90
|
+
toAirtable: readonly('button'),
|
|
91
|
+
fromAirtable: (obj) => {
|
|
92
|
+
if (!obj || typeof obj !== 'object' || !('label' in obj) || typeof obj.label !== 'string')
|
|
93
|
+
return null;
|
|
94
|
+
return obj.label;
|
|
95
|
+
},
|
|
96
|
+
};
|
|
53
97
|
const stringOrNull = {
|
|
54
98
|
'string | null': {
|
|
55
99
|
url: fallbackMapperPair(null, null),
|
|
@@ -59,6 +103,26 @@ const stringOrNull = {
|
|
|
59
103
|
multilineText: fallbackMapperPair(null, null),
|
|
60
104
|
richText: fallbackMapperPair(null, null),
|
|
61
105
|
singleSelect: fallbackMapperPair(null, null),
|
|
106
|
+
aiText: aiTextMapperPair,
|
|
107
|
+
barcode: barcodeMapperPair,
|
|
108
|
+
button: buttonMapperPair,
|
|
109
|
+
singleCollaborator: collaboratorMapperPair,
|
|
110
|
+
multipleCollaborators: {
|
|
111
|
+
toAirtable: (value) => (value ? multipleCollaboratorsMapperPair.toAirtable([value]) : []),
|
|
112
|
+
fromAirtable: (value) => coerce('multipleCollaborators', 'string | null')(multipleCollaboratorsMapperPair.fromAirtable(value)),
|
|
113
|
+
},
|
|
114
|
+
createdBy: {
|
|
115
|
+
toAirtable: readonly('createdBy'),
|
|
116
|
+
fromAirtable: collaboratorMapperPair.fromAirtable,
|
|
117
|
+
},
|
|
118
|
+
lastModifiedBy: {
|
|
119
|
+
toAirtable: readonly('lastModifiedBy'),
|
|
120
|
+
fromAirtable: collaboratorMapperPair.fromAirtable,
|
|
121
|
+
},
|
|
122
|
+
multipleAttachments: {
|
|
123
|
+
toAirtable: readonly('multipleAttachments'),
|
|
124
|
+
fromAirtable: (value) => coerce('multipleAttachments', 'string | null')(multipleAttachmentsMapperPair.fromAirtable(value)),
|
|
125
|
+
},
|
|
62
126
|
multipleSelects: {
|
|
63
127
|
toAirtable: (value) => (value ? [value] : []),
|
|
64
128
|
fromAirtable: coerce('multipleSelects', 'string | null'),
|
|
@@ -198,6 +262,8 @@ const stringArrayOrNull = {
|
|
|
198
262
|
'string[] | null': {
|
|
199
263
|
multipleSelects: fallbackMapperPair([], []),
|
|
200
264
|
multipleRecordLinks: fallbackMapperPair([], []),
|
|
265
|
+
multipleCollaborators: multipleCollaboratorsMapperPair,
|
|
266
|
+
multipleAttachments: multipleAttachmentsMapperPair,
|
|
201
267
|
multipleLookupValues: {
|
|
202
268
|
toAirtable: () => { throw new Error('[airtable-ts] lookup type field is readonly'); },
|
|
203
269
|
fromAirtable: coerce('multipleLookupValues', 'string[] | null'),
|
|
@@ -3,7 +3,7 @@ type NonNullToString<T> = T extends string ? 'string' : T extends number ? 'numb
|
|
|
3
3
|
export type ToTsTypeString<T> = null extends T ? `${NonNullToString<T>} | null` : NonNullToString<T>;
|
|
4
4
|
export type FromTsTypeString<T> = T extends 'string' ? string : T extends 'string | null' ? string | null : T extends 'number' ? number : T extends 'number | null' ? number | null : T extends 'boolean' ? boolean : T extends 'boolean | null' ? boolean | null : T extends 'string[]' ? string[] : T extends 'string[] | null' ? string[] | null : T extends 'number[]' ? number[] : T extends 'number[] | null' ? number[] | null : T extends 'boolean[]' ? boolean[] : T extends 'boolean[] | null' ? boolean[] | null : never;
|
|
5
5
|
export type AirtableTypeString = 'aiText' | 'autoNumber' | 'barcode' | 'button' | 'checkbox' | 'count' | 'createdBy' | 'createdTime' | 'currency' | 'date' | 'dateTime' | 'duration' | 'email' | 'externalSyncSource' | 'formula' | 'lastModifiedBy' | 'lastModifiedTime' | 'lookup' | 'multipleLookupValues' | 'multilineText' | 'multipleAttachments' | 'multipleCollaborators' | 'multipleRecordLinks' | 'multipleSelects' | 'number' | 'percent' | 'phoneNumber' | 'rating' | 'richText' | 'rollup' | 'singleCollaborator' | 'singleLineText' | 'singleSelect' | 'url';
|
|
6
|
-
export type FromAirtableTypeString<T extends AirtableTypeString | 'unknown'> = null | (T extends 'url' | 'email' | 'phoneNumber' | 'singleLineText' | 'multilineText' | 'richText' | 'singleSelect' | 'externalSyncSource' | 'date' | 'dateTime' | 'createdTime' | 'lastModifiedTime' ? string : T extends 'multipleRecordLinks' | 'multipleSelects' ? string[] : T extends 'number' | 'rating' | 'duration' | 'currency' | 'percent' | 'count' | 'autoNumber' ? number : T extends 'checkbox' ? boolean : T extends 'lookup' | 'multipleLookupValues' | 'rollup' | 'formula' ? FromAirtableTypeString<any>[] : T extends 'unknown' ? unknown : never);
|
|
6
|
+
export type FromAirtableTypeString<T extends AirtableTypeString | 'unknown'> = null | (T extends 'url' | 'email' | 'phoneNumber' | 'singleLineText' | 'multilineText' | 'richText' | 'singleSelect' | 'externalSyncSource' | 'date' | 'dateTime' | 'createdTime' | 'lastModifiedTime' ? string : T extends 'multipleRecordLinks' | 'multipleSelects' ? string[] : T extends 'number' | 'rating' | 'duration' | 'currency' | 'percent' | 'count' | 'autoNumber' ? number : T extends 'checkbox' ? boolean : T extends 'lookup' | 'multipleLookupValues' | 'rollup' | 'formula' ? FromAirtableTypeString<any>[] : T extends 'aiText' | 'barcode' | 'singleCollaborator' | 'createdBy' | 'modifiedBy' | 'button' ? object : T extends 'multipleCollaborators' | 'multipleAttachments' ? object[] : T extends 'unknown' ? unknown : never);
|
|
7
7
|
interface TypeDef {
|
|
8
8
|
single: 'string' | 'number' | 'boolean';
|
|
9
9
|
array: boolean;
|