totalum-api-sdk 2.0.41 → 3.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.
- package/README.MD +736 -205
- package/dist/common/endpoints.d.ts +1 -0
- package/dist/common/endpoints.js +2 -1
- package/dist/common/fetch-client.d.ts +24 -0
- package/dist/common/fetch-client.js +127 -0
- package/dist/common/interfaces.d.ts +26 -5
- package/dist/common/response-types.d.ts +210 -0
- package/dist/common/response-types.js +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +23 -17
- package/dist/services/CrudService.d.ts +77 -41
- package/dist/services/CrudService.js +88 -56
- package/dist/services/EmailService.d.ts +11 -9
- package/dist/services/EmailService.js +28 -18
- package/dist/services/FilesService.d.ts +70 -20
- package/dist/services/FilesService.js +86 -31
- package/dist/services/FilterService.d.ts +23 -20
- package/dist/services/FilterService.js +32 -36
- package/dist/services/NotificationService.d.ts +9 -9
- package/dist/services/NotificationService.js +7 -13
- package/dist/services/OpenaiService.d.ts +21 -24
- package/dist/services/OpenaiService.js +21 -26
- package/dist/services/StatisticService.d.ts +11 -5
- package/dist/services/StatisticService.js +10 -9
- package/dist/totalum-sdk.min.js +1 -1
- package/package.json +6 -5
package/README.MD
CHANGED
|
@@ -1,20 +1,106 @@
|
|
|
1
|
-
## Totalum
|
|
1
|
+
## Totalum API SDK (v3.0.0)
|
|
2
2
|
|
|
3
|
-
This library
|
|
3
|
+
Official TypeScript/JavaScript SDK for the Totalum API. This library provides a fully typed, modern interface to interact with all Totalum endpoints.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
## Installation:
|
|
5
|
+
## Installation
|
|
7
6
|
|
|
8
7
|
```bash
|
|
9
8
|
npm i totalum-api-sdk
|
|
10
9
|
```
|
|
11
10
|
|
|
11
|
+
## What's New in v3.0
|
|
12
|
+
|
|
13
|
+
- **Native Fetch**: Removed `axios` dependency, now using native `fetch` API (works in Node.js 18+ and browsers)
|
|
14
|
+
- **Typed Responses**: All methods return strongly-typed `TotalumApiResponse<T>` with proper TypeScript support
|
|
15
|
+
- **Better Error Handling**: Introduced `TotalumError` class for structured error handling
|
|
16
|
+
- **Renamed Methods**: More consistent naming (`getItems` → `getRecords`, `getItemById` → `getRecordById`, etc.)
|
|
17
|
+
- **Improved Type Safety**: Full TypeScript support with detailed response types
|
|
18
|
+
- **No Breaking Changes in API**: Same Totalum API, just better developer experience
|
|
19
|
+
|
|
20
|
+
## Migration Guide (v2.x → v3.0)
|
|
21
|
+
|
|
22
|
+
### Breaking Changes
|
|
23
|
+
|
|
24
|
+
#### 1. Method Names (CRUD Service)
|
|
25
|
+
```javascript
|
|
26
|
+
// v2.x
|
|
27
|
+
await totalumClient.crud.getItemById(table, id);
|
|
28
|
+
await totalumClient.crud.getItems(table, query);
|
|
29
|
+
await totalumClient.crud.createItem(table, data);
|
|
30
|
+
await totalumClient.crud.editItemById(table, id, data);
|
|
31
|
+
await totalumClient.crud.deleteItemById(table, id);
|
|
32
|
+
await totalumClient.crud.addManyToManyReferenceItem(table, id, prop, refId);
|
|
33
|
+
await totalumClient.crud.dropManyToManyReferenceItem(table, id, prop, refId);
|
|
34
|
+
await totalumClient.crud.getManyToManyReferencesItems(table, id, prop, query);
|
|
35
|
+
|
|
36
|
+
// v3.0
|
|
37
|
+
await totalumClient.crud.getRecordById(table, id);
|
|
38
|
+
await totalumClient.crud.getRecords(table, query);
|
|
39
|
+
await totalumClient.crud.createRecord(table, data);
|
|
40
|
+
await totalumClient.crud.editRecordById(table, id, data);
|
|
41
|
+
await totalumClient.crud.deleteRecordById(table, id);
|
|
42
|
+
await totalumClient.crud.addManyToManyReferenceRecord(table, id, prop, refId);
|
|
43
|
+
await totalumClient.crud.dropManyToManyReferenceRecord(table, id, prop, refId);
|
|
44
|
+
await totalumClient.crud.getManyToManyReferencesRecords(table, id, prop, query);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### 2. Response Structure
|
|
48
|
+
```javascript
|
|
49
|
+
// v2.x (using axios)
|
|
50
|
+
const result = await totalumClient.crud.getItems(table, {});
|
|
51
|
+
const items = result.data.data; // Double .data because of axios wrapper
|
|
52
|
+
|
|
53
|
+
// v3.0 (using fetch)
|
|
54
|
+
const result = await totalumClient.crud.getRecords(table, {});
|
|
55
|
+
if (result.errors) {
|
|
56
|
+
console.error('Error:', result.errors.errorMessage);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const records = result.data; // Direct access to data
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### 3. Error Handling
|
|
63
|
+
```javascript
|
|
64
|
+
// v2.x (axios throws errors)
|
|
65
|
+
try {
|
|
66
|
+
const result = await totalumClient.crud.getItems(table, {});
|
|
67
|
+
const items = result.data.data;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error('Error:', error.message);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// v3.0 (errors in response object)
|
|
73
|
+
const result = await totalumClient.crud.getRecords(table, {});
|
|
74
|
+
if (result.errors) {
|
|
75
|
+
console.error('Error:', result.errors.errorMessage);
|
|
76
|
+
console.error('Error Code:', result.errors.errorCode);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const records = result.data;
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Dependencies
|
|
83
|
+
```json
|
|
84
|
+
// v2.x
|
|
85
|
+
{
|
|
86
|
+
"dependencies": {
|
|
87
|
+
"axios": "^1.x.x"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// v3.0 (no dependencies!)
|
|
92
|
+
{
|
|
93
|
+
"dependencies": {}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Note:** v3.0 requires Node.js 18+ (for native fetch support). If you're using an older Node.js version, you'll need to polyfill `fetch` or upgrade to Node.js 18+.
|
|
12
98
|
|
|
13
99
|
# Usage of TotalumApiSdk
|
|
14
100
|
|
|
15
|
-
Totalum allows you to use the SDK to perform actions such as creating
|
|
101
|
+
Totalum allows you to use the SDK to perform actions such as creating records, managing files, creating PDFs, sending emails, and more.
|
|
16
102
|
|
|
17
|
-
|
|
103
|
+
The SDK is a fully-typed wrapper around the Totalum REST API.
|
|
18
104
|
|
|
19
105
|
### What happens if you are not programming in javascript?
|
|
20
106
|
|
|
@@ -22,7 +108,7 @@ If you are not programming in javascript, you can use the api directly, see <a h
|
|
|
22
108
|
|
|
23
109
|
## Authentication
|
|
24
110
|
|
|
25
|
-
**Note:** If you use totalumSdk inside a totalum plugin, you don't need to authenticate, you can start using totalum like this: modules.totalumSdk.your.function();-> example: modules.totalumSdk.crud.
|
|
111
|
+
**Note:** If you use totalumSdk inside a totalum plugin, you don't need to authenticate, you can start using totalum like this: modules.totalumSdk.your.function();-> example: modules.totalumSdk.crud.getRecords('your_table', {})
|
|
26
112
|
|
|
27
113
|
You can choose to use one of the two authentication methods offered by Totalum Sdk:
|
|
28
114
|
|
|
@@ -55,8 +141,8 @@ const options: AuthOptions = {
|
|
|
55
141
|
const totalumClient = new TotalumApiSdk(options);
|
|
56
142
|
|
|
57
143
|
// execute some TotalumApiSdk function
|
|
58
|
-
const result = await totalumClient.crud.
|
|
59
|
-
|
|
144
|
+
const result = await totalumClient.crud.getRecords('your_table_name', {});
|
|
145
|
+
|
|
60
146
|
```
|
|
61
147
|
|
|
62
148
|
**CommonJS Require:**
|
|
@@ -84,15 +170,15 @@ const options = {
|
|
|
84
170
|
const totalumClient = new totalum.TotalumApiSdk(options);
|
|
85
171
|
|
|
86
172
|
// execute some TotalumApiSdk function
|
|
87
|
-
const result = await totalumClient.crud.
|
|
88
|
-
|
|
173
|
+
const result = await totalumClient.crud.getRecords('your_table_name', {});
|
|
174
|
+
|
|
89
175
|
```
|
|
90
176
|
|
|
91
177
|
**HTML script import:** (Use this way if you are using standalone html, and you cannot import npm packages)
|
|
92
178
|
|
|
93
179
|
```html
|
|
94
180
|
<head>
|
|
95
|
-
<script src="https://cdn.jsdelivr.net/npm/totalum-api-sdk@
|
|
181
|
+
<script src="https://cdn.jsdelivr.net/npm/totalum-api-sdk@3.0.0/dist/totalum-sdk.min.js"></script>
|
|
96
182
|
</head>
|
|
97
183
|
<script>
|
|
98
184
|
//Example of use TotalumSdk in your custom html page
|
|
@@ -103,42 +189,119 @@ const result = await totalumClient.crud.getItems('your_element_table_name', {});
|
|
|
103
189
|
}
|
|
104
190
|
});
|
|
105
191
|
|
|
106
|
-
const
|
|
192
|
+
const tableName = 'your-table-name'; // replace with your table name
|
|
107
193
|
|
|
108
194
|
//example of endpoint execution
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
195
|
+
totalumClient.crud.getRecords(tableName, {}).then((result) => {
|
|
196
|
+
if (result.errors) {
|
|
197
|
+
console.error("ERROR:", result.errors);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const records = result.data;
|
|
201
|
+
console.log("THE RECORDS", records);
|
|
112
202
|
});
|
|
113
|
-
|
|
203
|
+
|
|
114
204
|
</script>
|
|
115
205
|
```
|
|
116
206
|
|
|
207
|
+
## Response Structure
|
|
117
208
|
|
|
118
|
-
|
|
209
|
+
All SDK methods return a `TotalumApiResponse<T>` object with the following structure:
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
interface TotalumApiResponse<T> {
|
|
213
|
+
data?: T; // The successful response data
|
|
214
|
+
errors?: ErrorResult; // Error information if request failed
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
interface ErrorResult {
|
|
218
|
+
errorCode: string;
|
|
219
|
+
errorMessage: string;
|
|
220
|
+
hasManyErrors?: boolean;
|
|
221
|
+
multipleErrors?: Array<{ errorCode: string; errorMessage: string }>;
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
**Example:**
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
const result = await totalumClient.crud.getRecordById('users', 'user-id-123');
|
|
119
229
|
|
|
230
|
+
// Always check for errors first
|
|
231
|
+
if (result.errors) {
|
|
232
|
+
console.error('Error:', result.errors.errorMessage);
|
|
233
|
+
console.error('Error Code:', result.errors.errorCode);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Now you can safely use result.data
|
|
238
|
+
const user = result.data;
|
|
239
|
+
console.log('User:', user);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Error Handling
|
|
243
|
+
|
|
244
|
+
The SDK provides a `TotalumError` class for structured error handling:
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
import { TotalumError } from 'totalum-api-sdk';
|
|
120
248
|
|
|
121
|
-
|
|
249
|
+
const result = await totalumClient.crud.getRecordById('users', 'invalid-id');
|
|
122
250
|
|
|
251
|
+
if (result.errors) {
|
|
252
|
+
// You can create a TotalumError for better error handling
|
|
253
|
+
const error = new TotalumError(result.errors);
|
|
254
|
+
console.error('Error Code:', error.errorCode);
|
|
255
|
+
console.error('Error Message:', error.errorMessage);
|
|
256
|
+
|
|
257
|
+
// Check for multiple errors
|
|
258
|
+
if (error.hasManyErrors && error.multipleErrors) {
|
|
259
|
+
error.multipleErrors.forEach(err => {
|
|
260
|
+
console.error(`- ${err.errorCode}: ${err.errorMessage}`);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Functions for create, read, filter, update, delete. (CRUD)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
### Get record by id
|
|
123
270
|
|
|
124
271
|
```javascript
|
|
125
272
|
|
|
126
|
-
//
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
273
|
+
// Get a single record by id
|
|
274
|
+
const tableName = 'users'; // replace with your table name
|
|
275
|
+
const recordId = 'record-id-123'; // replace with your record id
|
|
276
|
+
|
|
277
|
+
const result = await totalumClient.crud.getRecordById(tableName, recordId);
|
|
278
|
+
|
|
279
|
+
if (result.errors) {
|
|
280
|
+
console.error('Error:', result.errors.errorMessage);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const record = result.data;
|
|
285
|
+
console.log('Record:', record);
|
|
131
286
|
|
|
132
287
|
```
|
|
133
288
|
|
|
134
|
-
### Get
|
|
289
|
+
### Get records
|
|
135
290
|
|
|
136
291
|
```javascript
|
|
137
292
|
|
|
138
|
-
//
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
const
|
|
293
|
+
// Get records from a table (default: first 50 records)
|
|
294
|
+
const tableName = 'users'; // replace with your table name
|
|
295
|
+
|
|
296
|
+
const result = await totalumClient.crud.getRecords(tableName, {});
|
|
297
|
+
|
|
298
|
+
if (result.errors) {
|
|
299
|
+
console.error('Error:', result.errors.errorMessage);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const records = result.data;
|
|
304
|
+
console.log('Records:', records);
|
|
142
305
|
|
|
143
306
|
```
|
|
144
307
|
|
|
@@ -305,120 +468,185 @@ const products = result.data.data;
|
|
|
305
468
|
You can do the same approach to get the orders.
|
|
306
469
|
|
|
307
470
|
|
|
308
|
-
###
|
|
471
|
+
### Delete record by id
|
|
309
472
|
|
|
310
473
|
```javascript
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
const
|
|
474
|
+
|
|
475
|
+
// Delete a record by id
|
|
476
|
+
const tableName = 'users';
|
|
477
|
+
const recordId = 'record-id-123';
|
|
478
|
+
|
|
479
|
+
const result = await totalumClient.crud.deleteRecordById(tableName, recordId);
|
|
480
|
+
|
|
481
|
+
if (result.errors) {
|
|
482
|
+
console.error('Error:', result.errors.errorMessage);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
console.log('Deleted count:', result.data.deletedCount);
|
|
487
|
+
console.log('Acknowledged:', result.data.acknowledged);
|
|
315
488
|
|
|
316
489
|
```
|
|
317
490
|
|
|
318
|
-
###
|
|
491
|
+
### Edit record by id
|
|
319
492
|
|
|
320
493
|
```javascript
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const
|
|
494
|
+
|
|
495
|
+
// Edit a record by id (you can edit one or multiple properties at the same time)
|
|
496
|
+
const tableName = 'users';
|
|
497
|
+
const recordId = 'record-id-123';
|
|
498
|
+
const updates = {
|
|
499
|
+
name: 'John Doe',
|
|
500
|
+
email: 'john@example.com',
|
|
501
|
+
age: 30
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const result = await totalumClient.crud.editRecordById(tableName, recordId, updates);
|
|
505
|
+
|
|
506
|
+
if (result.errors) {
|
|
507
|
+
console.error('Error:', result.errors.errorMessage);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
console.log('Modified count:', result.data.modifiedCount);
|
|
512
|
+
console.log('Matched count:', result.data.matchedCount);
|
|
325
513
|
|
|
326
514
|
```
|
|
327
515
|
|
|
328
|
-
###
|
|
516
|
+
### Create record
|
|
329
517
|
|
|
330
518
|
```javascript
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
const
|
|
519
|
+
|
|
520
|
+
// Create a new record (you need to pass at least the required properties)
|
|
521
|
+
const tableName = 'users';
|
|
522
|
+
const newRecord = {
|
|
523
|
+
name: 'Jane Doe',
|
|
524
|
+
email: 'jane@example.com',
|
|
525
|
+
age: 25
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
const result = await totalumClient.crud.createRecord(tableName, newRecord);
|
|
529
|
+
|
|
530
|
+
if (result.errors) {
|
|
531
|
+
console.error('Error:', result.errors.errorMessage);
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
console.log('Created record ID:', result.data.insertedId);
|
|
536
|
+
console.log('Acknowledged:', result.data.acknowledged);
|
|
334
537
|
|
|
335
538
|
```
|
|
336
539
|
|
|
337
|
-
###
|
|
540
|
+
### Add many-to-many reference (add a reference to another record with a many-to-many relationship)
|
|
338
541
|
|
|
339
542
|
```javascript
|
|
340
543
|
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
const propertyName = '
|
|
344
|
-
const referenceId = '
|
|
345
|
-
|
|
544
|
+
const tableName = 'students';
|
|
545
|
+
const recordId = 'student-id-123';
|
|
546
|
+
const propertyName = 'courses'; // the property that defines the many-to-many relationship
|
|
547
|
+
const referenceId = 'course-id-456'; // the id of the record to link
|
|
548
|
+
|
|
549
|
+
const result = await totalumClient.crud.addManyToManyReferenceRecord(tableName, recordId, propertyName, referenceId);
|
|
550
|
+
|
|
551
|
+
if (result.errors) {
|
|
552
|
+
console.error('Error:', result.errors.errorMessage);
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
console.log('Reference added successfully');
|
|
346
557
|
|
|
347
558
|
```
|
|
348
559
|
|
|
349
|
-
###
|
|
560
|
+
### Drop many-to-many reference (remove a reference from a many-to-many relationship)
|
|
350
561
|
|
|
351
562
|
```javascript
|
|
352
563
|
|
|
353
|
-
const
|
|
354
|
-
|
|
355
|
-
const propertyName = '
|
|
356
|
-
const referenceId = '
|
|
357
|
-
|
|
564
|
+
const tableName = 'students';
|
|
565
|
+
const recordId = 'student-id-123';
|
|
566
|
+
const propertyName = 'courses';
|
|
567
|
+
const referenceId = 'course-id-456'; // the id of the record to unlink
|
|
568
|
+
|
|
569
|
+
const result = await totalumClient.crud.dropManyToManyReferenceRecord(tableName, recordId, propertyName, referenceId);
|
|
570
|
+
|
|
571
|
+
if (result.errors) {
|
|
572
|
+
console.error('Error:', result.errors.errorMessage);
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
console.log('Reference removed successfully');
|
|
358
577
|
|
|
359
578
|
```
|
|
360
579
|
|
|
361
|
-
###
|
|
580
|
+
### Get many-to-many references (get all records with a many-to-many relationship)
|
|
362
581
|
|
|
363
582
|
```javascript
|
|
364
583
|
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
const propertyName = '
|
|
584
|
+
const tableName = 'students';
|
|
585
|
+
const recordId = 'student-id-123';
|
|
586
|
+
const propertyName = 'courses';
|
|
368
587
|
|
|
369
|
-
//
|
|
588
|
+
// Optional query for filtering and sorting
|
|
370
589
|
const query = {
|
|
371
590
|
filter: [
|
|
372
591
|
{
|
|
373
|
-
'
|
|
592
|
+
'courseName': { regex: 'Math', options: 'i' }
|
|
374
593
|
},
|
|
375
594
|
],
|
|
376
|
-
sort:{
|
|
377
|
-
'
|
|
595
|
+
sort: {
|
|
596
|
+
'courseName': 1 // 1 for asc, -1 for desc
|
|
378
597
|
},
|
|
379
598
|
pagination: {
|
|
380
599
|
limit: 50,
|
|
381
600
|
page: 0,
|
|
382
601
|
}
|
|
383
602
|
};
|
|
384
|
-
|
|
603
|
+
|
|
604
|
+
const result = await totalumClient.crud.getManyToManyReferencesRecords(tableName, recordId, propertyName, query);
|
|
605
|
+
|
|
606
|
+
if (result.errors) {
|
|
607
|
+
console.error('Error:', result.errors.errorMessage);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const relatedRecords = result.data;
|
|
612
|
+
console.log('Related records:', relatedRecords);
|
|
385
613
|
|
|
386
614
|
```
|
|
387
615
|
|
|
388
616
|
|
|
389
617
|
## Functions for filter data
|
|
390
618
|
|
|
391
|
-
### Filter data using
|
|
619
|
+
### Filter data using AND filter
|
|
392
620
|
|
|
393
621
|
```javascript
|
|
394
622
|
|
|
395
|
-
const
|
|
623
|
+
const tableName = 'users';
|
|
396
624
|
|
|
397
|
-
//
|
|
398
|
-
const filter
|
|
625
|
+
// Get records applying AND filter (all conditions must be true)
|
|
626
|
+
const filter = {
|
|
399
627
|
filter: [
|
|
400
628
|
{
|
|
401
|
-
'
|
|
629
|
+
'name': 'John' // exact match
|
|
402
630
|
},
|
|
403
631
|
{
|
|
404
|
-
'
|
|
632
|
+
'email': { regex: '@example.com', options: 'i' } // regex with case insensitive
|
|
405
633
|
},
|
|
406
|
-
//
|
|
634
|
+
// Note: gte and lte operators are only for date or number properties
|
|
407
635
|
{
|
|
408
|
-
'
|
|
636
|
+
'age': { gte: 18 } // greater than or equal to 18
|
|
409
637
|
},
|
|
410
638
|
{
|
|
411
|
-
'
|
|
639
|
+
'age': { lte: 65 } // less than or equal to 65
|
|
412
640
|
},
|
|
413
641
|
{
|
|
414
|
-
'
|
|
642
|
+
'createdAt': { gte: new Date('2024-01-01') } // created after date
|
|
415
643
|
},
|
|
416
644
|
{
|
|
417
|
-
'
|
|
645
|
+
'createdAt': { lte: new Date('2024-12-31') } // created before date
|
|
418
646
|
}
|
|
419
647
|
],
|
|
420
|
-
sort:{
|
|
421
|
-
'
|
|
648
|
+
sort: {
|
|
649
|
+
'name': 1 // 1 for ascending, -1 for descending
|
|
422
650
|
},
|
|
423
651
|
pagination: {
|
|
424
652
|
limit: 50,
|
|
@@ -426,34 +654,43 @@ const filter: FilterSearchQueryI = {
|
|
|
426
654
|
}
|
|
427
655
|
};
|
|
428
656
|
|
|
429
|
-
const result = await totalumClient.crud.
|
|
430
|
-
|
|
657
|
+
const result = await totalumClient.crud.getRecords(tableName, filter);
|
|
658
|
+
|
|
659
|
+
if (result.errors) {
|
|
660
|
+
console.error('Error:', result.errors.errorMessage);
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const records = result.data;
|
|
665
|
+
console.log('Filtered records:', records);
|
|
431
666
|
|
|
432
667
|
```
|
|
433
668
|
|
|
434
|
-
### Filter data using
|
|
669
|
+
### Filter data using OR filter
|
|
435
670
|
|
|
436
671
|
```javascript
|
|
437
672
|
|
|
438
|
-
|
|
439
|
-
|
|
673
|
+
const tableName = 'users';
|
|
674
|
+
|
|
675
|
+
// Get records applying OR filter (at least one condition must be true)
|
|
676
|
+
const filter = {
|
|
440
677
|
filter: [
|
|
441
678
|
{
|
|
442
679
|
or: [
|
|
443
680
|
{
|
|
444
|
-
'
|
|
681
|
+
'name': 'John'
|
|
445
682
|
},
|
|
446
683
|
{
|
|
447
|
-
'
|
|
684
|
+
'email': { regex: '@example.com', options: 'i' }
|
|
448
685
|
},
|
|
449
686
|
{
|
|
450
|
-
'
|
|
687
|
+
'age': { gte: 18 }
|
|
451
688
|
},
|
|
452
689
|
]
|
|
453
690
|
}
|
|
454
691
|
],
|
|
455
692
|
sort: {
|
|
456
|
-
|
|
693
|
+
'name': 1 // 1 for ascending, -1 for descending
|
|
457
694
|
},
|
|
458
695
|
pagination: {
|
|
459
696
|
limit: 50,
|
|
@@ -461,35 +698,43 @@ const filter: FilterSearchQueryI = {
|
|
|
461
698
|
}
|
|
462
699
|
};
|
|
463
700
|
|
|
464
|
-
const result = await totalumClient.crud.
|
|
465
|
-
|
|
701
|
+
const result = await totalumClient.crud.getRecords(tableName, filter);
|
|
702
|
+
|
|
703
|
+
if (result.errors) {
|
|
704
|
+
console.error('Error:', result.errors.errorMessage);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
const records = result.data;
|
|
709
|
+
console.log('Filtered records:', records);
|
|
466
710
|
|
|
467
711
|
```
|
|
468
712
|
|
|
469
|
-
### Filter data using
|
|
713
|
+
### Filter data using AND and OR combined
|
|
470
714
|
|
|
471
715
|
```javascript
|
|
472
716
|
|
|
473
|
-
|
|
474
|
-
|
|
717
|
+
const tableName = 'users';
|
|
718
|
+
|
|
719
|
+
// Get records applying both OR and AND filters
|
|
720
|
+
const filter = {
|
|
475
721
|
filter: [
|
|
476
722
|
{
|
|
477
723
|
or: [
|
|
478
724
|
{
|
|
479
|
-
'
|
|
725
|
+
'role': 'admin'
|
|
480
726
|
},
|
|
481
727
|
{
|
|
482
|
-
'
|
|
728
|
+
'role': 'moderator'
|
|
483
729
|
},
|
|
484
730
|
],
|
|
485
731
|
},
|
|
486
732
|
{
|
|
487
|
-
'
|
|
733
|
+
'status': 'active' // AND this condition must be true
|
|
488
734
|
}
|
|
489
|
-
|
|
490
735
|
],
|
|
491
736
|
sort: {
|
|
492
|
-
'
|
|
737
|
+
'name': 1
|
|
493
738
|
},
|
|
494
739
|
pagination: {
|
|
495
740
|
limit: 50,
|
|
@@ -497,27 +742,40 @@ const filter: FilterSearchQueryI = {
|
|
|
497
742
|
}
|
|
498
743
|
};
|
|
499
744
|
|
|
500
|
-
const result = await totalumClient.crud.
|
|
501
|
-
|
|
745
|
+
const result = await totalumClient.crud.getRecords(tableName, filter);
|
|
746
|
+
|
|
747
|
+
if (result.errors) {
|
|
748
|
+
console.error('Error:', result.errors.errorMessage);
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
502
751
|
|
|
752
|
+
const records = result.data;
|
|
753
|
+
console.log('Filtered records:', records);
|
|
503
754
|
|
|
504
755
|
```
|
|
505
756
|
|
|
506
|
-
### Filter using
|
|
757
|
+
### Filter using custom MongoDB aggregation query
|
|
507
758
|
|
|
508
759
|
```javascript
|
|
509
760
|
|
|
510
|
-
|
|
761
|
+
const tableName = 'users';
|
|
762
|
+
|
|
763
|
+
// Custom MongoDB aggregation query as string
|
|
511
764
|
const customMongoDbAggregationQueryInString = `
|
|
512
|
-
|
|
513
|
-
your custom mongo aggregation query in string, for more info:
|
|
514
|
-
https://docs.mongodb.com/manual/aggregation/
|
|
515
765
|
|
|
516
|
-
|
|
766
|
+
Your custom MongoDB aggregation query in string format
|
|
767
|
+
For more info: https://docs.mongodb.com/manual/aggregation/
|
|
517
768
|
|
|
518
769
|
`;
|
|
519
770
|
|
|
520
|
-
const result = await totalumClient.filter.runCustomMongoAggregationQuery(
|
|
771
|
+
const result = await totalumClient.filter.runCustomMongoAggregationQuery(tableName, customMongoDbAggregationQueryInString);
|
|
772
|
+
|
|
773
|
+
if (result.errors) {
|
|
774
|
+
console.error('Error:', result.errors.errorMessage);
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const results = result.data;
|
|
521
779
|
|
|
522
780
|
```
|
|
523
781
|
|
|
@@ -525,9 +783,17 @@ const result = await totalumClient.filter.runCustomMongoAggregationQuery(tableEl
|
|
|
525
783
|
|
|
526
784
|
```javascript
|
|
527
785
|
|
|
528
|
-
const
|
|
786
|
+
const recordId = 'record-id-123';
|
|
787
|
+
|
|
788
|
+
const result = await totalumClient.crud.getHistoricRecordUpdatesById(recordId);
|
|
789
|
+
|
|
790
|
+
if (result.errors) {
|
|
791
|
+
console.error('Error:', result.errors.errorMessage);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
529
794
|
|
|
530
|
-
const historicUpdates = result.data
|
|
795
|
+
const historicUpdates = result.data;
|
|
796
|
+
console.log('Historic updates:', historicUpdates);
|
|
531
797
|
|
|
532
798
|
```
|
|
533
799
|
|
|
@@ -609,11 +875,18 @@ Depending on the platform you are using, you will need to transform the file to
|
|
|
609
875
|
const FormData = require('form-data'); // if you are using node.js
|
|
610
876
|
|
|
611
877
|
const fileName = 'your_file_name.png'; // replace 'your_file_name' with the name of your file, replace .png with the extension of your file
|
|
612
|
-
const file = yourFileBlob // your blob file created in the previous step
|
|
878
|
+
const file = yourFileBlob // your blob file created in the previous step
|
|
613
879
|
const formData = new FormData();
|
|
614
880
|
formData.append('file', file, fileName);
|
|
881
|
+
|
|
615
882
|
const result = await totalumClient.files.uploadFile(formData);
|
|
616
|
-
|
|
883
|
+
|
|
884
|
+
if (result.errors) {
|
|
885
|
+
console.error('Error:', result.errors.errorMessage);
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
const fileNameId = result.data;
|
|
617
890
|
```
|
|
618
891
|
|
|
619
892
|
**Using Totalum API**
|
|
@@ -638,9 +911,19 @@ Depending on the platform you are using, you will need to transform the file to
|
|
|
638
911
|
|
|
639
912
|
``` javascript
|
|
640
913
|
|
|
641
|
-
//
|
|
642
|
-
const
|
|
643
|
-
|
|
914
|
+
// If you want to link this file to a record, you need to add the fileNameId to the record property of type file
|
|
915
|
+
const tableName = 'documents';
|
|
916
|
+
const recordId = 'record-id-123';
|
|
917
|
+
const result2 = await totalumClient.crud.editRecordById(tableName, recordId, {
|
|
918
|
+
'attachmentFile': { name: fileNameId }
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
if (result2.errors) {
|
|
922
|
+
console.error('Error:', result2.errors.errorMessage);
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
console.log('File linked to record successfully');
|
|
644
927
|
|
|
645
928
|
```
|
|
646
929
|
|
|
@@ -648,141 +931,365 @@ const result2 = await totalumClient.crud.editItemById('your_element_table_name',
|
|
|
648
931
|
|
|
649
932
|
```javascript
|
|
650
933
|
|
|
651
|
-
//
|
|
652
|
-
const fileNameId = 'your_file_name.png';
|
|
934
|
+
// Remove a file from Totalum using the file name id
|
|
935
|
+
const fileNameId = 'your_file_name.png';
|
|
653
936
|
|
|
654
937
|
const result = await totalumClient.files.deleteFile(fileNameId);
|
|
655
938
|
|
|
939
|
+
if (result.errors) {
|
|
940
|
+
console.error('Error:', result.errors.errorMessage);
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
console.log('File deleted successfully');
|
|
945
|
+
|
|
656
946
|
```
|
|
657
947
|
|
|
658
|
-
### Get the download
|
|
948
|
+
### Get the download URL of a file
|
|
659
949
|
|
|
660
950
|
```javascript
|
|
661
951
|
|
|
662
|
-
//
|
|
663
|
-
const fileNameId = 'your_file_name.png';
|
|
952
|
+
// Get a signed download URL for a file
|
|
953
|
+
const fileNameId = 'your_file_name.png';
|
|
664
954
|
|
|
665
|
-
//
|
|
955
|
+
// Optional: set expiration time
|
|
666
956
|
const options = {
|
|
667
|
-
//
|
|
668
|
-
expirationTime: Date.now() + (128 * 60 * 60 * 1000)
|
|
669
|
-
}
|
|
957
|
+
// Default expiration is 128 hours. Set custom expiration in milliseconds from now
|
|
958
|
+
expirationTime: Date.now() + (128 * 60 * 60 * 1000) // 128 hours
|
|
959
|
+
};
|
|
670
960
|
|
|
671
961
|
const result = await totalumClient.files.getDownloadUrl(fileNameId, options);
|
|
672
|
-
|
|
673
|
-
|
|
962
|
+
|
|
963
|
+
if (result.errors) {
|
|
964
|
+
console.error('Error:', result.errors.errorMessage);
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
const fileUrl = result.data;
|
|
969
|
+
console.log('Download URL:', fileUrl);
|
|
674
970
|
|
|
675
971
|
```
|
|
676
972
|
|
|
677
|
-
### Generate a
|
|
973
|
+
### Generate a PDF from a template
|
|
678
974
|
|
|
679
975
|
```javascript
|
|
680
976
|
|
|
681
|
-
|
|
977
|
+
// Go to Totalum -> Configuration -> PDF Templates to get your template ID
|
|
978
|
+
const templateId = 'template-id-123';
|
|
682
979
|
|
|
683
|
-
//
|
|
684
|
-
const
|
|
685
|
-
'
|
|
686
|
-
'
|
|
687
|
-
'
|
|
688
|
-
'
|
|
689
|
-
'
|
|
980
|
+
// Variables will be replaced in your template using Handlebars
|
|
981
|
+
const variables = {
|
|
982
|
+
'customerName': 'John Doe',
|
|
983
|
+
'orderDetails': {
|
|
984
|
+
'orderId': 'ORD-12345',
|
|
985
|
+
'amount': 150.50,
|
|
986
|
+
'date': new Date(),
|
|
690
987
|
},
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
const fileName = 'invoice_12345.pdf';
|
|
991
|
+
// CAUTION: if you use the same name, the previous PDF will be overwritten
|
|
992
|
+
|
|
993
|
+
const result = await totalumClient.files.generatePdfByTemplate(templateId, variables, fileName);
|
|
994
|
+
|
|
995
|
+
if (result.errors) {
|
|
996
|
+
console.error('Error:', result.errors.errorMessage);
|
|
997
|
+
return;
|
|
691
998
|
}
|
|
692
999
|
|
|
693
|
-
const
|
|
694
|
-
|
|
695
|
-
const result = await totalumClient.files.generatePdfByTemplate(templateId, variablesExample, name);
|
|
696
|
-
const fileResult = result.data.data;
|
|
697
|
-
// with the fileUrl you can download the pdf
|
|
1000
|
+
const generatedFileName = result.data;
|
|
1001
|
+
console.log('PDF generated:', generatedFileName);
|
|
698
1002
|
|
|
699
|
-
//
|
|
700
|
-
const
|
|
1003
|
+
// Link this PDF to a record
|
|
1004
|
+
const tableName = 'invoices';
|
|
1005
|
+
const recordId = 'invoice-record-123';
|
|
1006
|
+
const result2 = await totalumClient.crud.editRecordById(tableName, recordId, {
|
|
1007
|
+
'pdfFile': { name: generatedFileName }
|
|
1008
|
+
});
|
|
701
1009
|
|
|
702
1010
|
```
|
|
703
1011
|
|
|
704
|
-
###
|
|
1012
|
+
### Get OCR of an image (extract text from image)
|
|
705
1013
|
|
|
706
1014
|
```javascript
|
|
707
1015
|
|
|
708
|
-
//
|
|
709
|
-
|
|
710
|
-
|
|
1016
|
+
// First upload the image to Totalum, then extract text from it
|
|
1017
|
+
const fileNameId = 'uploaded_image.png';
|
|
1018
|
+
|
|
711
1019
|
const result = await totalumClient.files.ocrOfImage(fileNameId);
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
1020
|
+
|
|
1021
|
+
if (result.errors) {
|
|
1022
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
const ocrResult = result.data;
|
|
1027
|
+
console.log('Extracted text:', ocrResult.text);
|
|
1028
|
+
console.log('Full details:', ocrResult.fullDetails);
|
|
715
1029
|
|
|
716
1030
|
```
|
|
717
1031
|
|
|
718
|
-
###
|
|
1032
|
+
### Get OCR of a PDF (extract text from PDF)
|
|
719
1033
|
|
|
720
1034
|
```javascript
|
|
721
1035
|
|
|
722
|
-
//
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
const result = await totalumClient.files.ocrOfPdf(
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
1036
|
+
// First upload the PDF to Totalum, then extract text from it
|
|
1037
|
+
const fileNameId = 'uploaded_document.pdf';
|
|
1038
|
+
|
|
1039
|
+
const result = await totalumClient.files.ocrOfPdf(fileNameId);
|
|
1040
|
+
|
|
1041
|
+
if (result.errors) {
|
|
1042
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
const ocrResult = result.data;
|
|
1047
|
+
console.log('Extracted text:', ocrResult.text);
|
|
1048
|
+
console.log('Full details:', ocrResult.fullDetails);
|
|
729
1049
|
|
|
730
1050
|
```
|
|
731
1051
|
|
|
732
|
-
###
|
|
1052
|
+
### Scan an invoice (extract structured data from invoice)
|
|
733
1053
|
|
|
734
1054
|
```javascript
|
|
735
1055
|
|
|
736
|
-
//
|
|
737
|
-
|
|
738
|
-
const
|
|
739
|
-
const file = yourFileBlob // replace yourFile with your file object binary blob (in blob format)
|
|
1056
|
+
// First upload the invoice file to Totalum
|
|
1057
|
+
const fileName = 'invoice.png';
|
|
1058
|
+
const file = yourFileBlob;
|
|
740
1059
|
const formData = new FormData();
|
|
741
1060
|
formData.append('file', file, fileName);
|
|
742
|
-
const result = await totalumClient.files.uploadFile(formData);
|
|
743
|
-
const fileNameId = result.data.data;
|
|
744
1061
|
|
|
1062
|
+
const uploadResult = await totalumClient.files.uploadFile(formData);
|
|
1063
|
+
|
|
1064
|
+
if (uploadResult.errors) {
|
|
1065
|
+
console.error('Error:', uploadResult.errors.errorMessage);
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
const fileNameId = uploadResult.data;
|
|
1070
|
+
|
|
1071
|
+
// Scan the invoice to extract structured data
|
|
745
1072
|
const result = await totalumClient.files.scanInvoice(fileNameId);
|
|
746
|
-
|
|
1073
|
+
|
|
1074
|
+
if (result.errors) {
|
|
1075
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
const invoiceData = result.data.data;
|
|
1080
|
+
const metadata = result.data.metadata;
|
|
1081
|
+
|
|
1082
|
+
console.log('Invoice data:', invoiceData);
|
|
1083
|
+
console.log('Total amount:', invoiceData.totalAmountIncludingTaxes);
|
|
1084
|
+
console.log('Invoice number:', invoiceData.complete_invoice_number);
|
|
1085
|
+
console.log('Usage units:', metadata.usageUnits);
|
|
747
1086
|
|
|
748
1087
|
```
|
|
749
1088
|
|
|
750
|
-
###
|
|
1089
|
+
### Scan a document (extract custom fields from document)
|
|
751
1090
|
|
|
752
1091
|
```javascript
|
|
753
1092
|
|
|
754
|
-
//
|
|
755
|
-
const fileName = '
|
|
756
|
-
const file = yourFileBlob
|
|
1093
|
+
// First upload the document
|
|
1094
|
+
const fileName = 'document.png';
|
|
1095
|
+
const file = yourFileBlob;
|
|
757
1096
|
const formData = new FormData();
|
|
758
1097
|
formData.append('file', file, fileName);
|
|
759
|
-
const result = await totalumClient.files.uploadFile(formData);
|
|
760
|
-
const fileNameId = result.data.data;
|
|
761
1098
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
1099
|
+
const uploadResult = await totalumClient.files.uploadFile(formData);
|
|
1100
|
+
|
|
1101
|
+
if (uploadResult.errors) {
|
|
1102
|
+
console.error('Error:', uploadResult.errors.errorMessage);
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
const fileNameId = uploadResult.data;
|
|
1107
|
+
|
|
1108
|
+
// Define the JSON schema for properties you want to extract
|
|
1109
|
+
const properties = {
|
|
765
1110
|
"fecha": {
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
1111
|
+
"type": "string",
|
|
1112
|
+
"format": "date",
|
|
1113
|
+
"description": "The date of the document"
|
|
769
1114
|
},
|
|
770
1115
|
"import": {
|
|
771
|
-
|
|
772
|
-
|
|
1116
|
+
"type": "number",
|
|
1117
|
+
"description": "The total amount"
|
|
773
1118
|
},
|
|
774
1119
|
"currency": {
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
1120
|
+
"type": "string",
|
|
1121
|
+
"enum": ["EUR", "USD", "GBP", "OTHER"],
|
|
1122
|
+
"description": "Currency code"
|
|
778
1123
|
}
|
|
779
|
-
}
|
|
1124
|
+
};
|
|
780
1125
|
|
|
781
1126
|
const result = await totalumClient.files.scanDocument(fileNameId, properties);
|
|
782
|
-
|
|
1127
|
+
|
|
1128
|
+
if (result.errors) {
|
|
1129
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
const documentData = result.data.data;
|
|
1134
|
+
const metadata = result.data.metadata;
|
|
1135
|
+
|
|
1136
|
+
console.log('Extracted data:', documentData);
|
|
1137
|
+
console.log('OCR text:', metadata.ocrFullResultText);
|
|
1138
|
+
console.log('Usage units:', metadata.usageUnits);
|
|
1139
|
+
|
|
1140
|
+
```
|
|
1141
|
+
|
|
1142
|
+
### Create PDF from HTML
|
|
1143
|
+
|
|
1144
|
+
Create a PDF directly from custom HTML:
|
|
1145
|
+
|
|
1146
|
+
```javascript
|
|
1147
|
+
|
|
1148
|
+
const htmlContent = `
|
|
1149
|
+
<html>
|
|
1150
|
+
<body>
|
|
1151
|
+
<h1>Hello World</h1>
|
|
1152
|
+
<p>This is a PDF created from HTML</p>
|
|
1153
|
+
</body>
|
|
1154
|
+
</html>
|
|
1155
|
+
`;
|
|
1156
|
+
|
|
1157
|
+
const fileName = 'my-generated-pdf.pdf';
|
|
1158
|
+
|
|
1159
|
+
const result = await totalumClient.files.createPdfFromHtml({
|
|
1160
|
+
html: htmlContent,
|
|
1161
|
+
name: fileName
|
|
1162
|
+
});
|
|
1163
|
+
|
|
1164
|
+
if (result.errors) {
|
|
1165
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
const generatedFileName = result.data;
|
|
1170
|
+
console.log('PDF generated:', generatedFileName);
|
|
1171
|
+
|
|
1172
|
+
// Link this PDF to a record
|
|
1173
|
+
const tableName = 'documents';
|
|
1174
|
+
const recordId = 'doc-record-123';
|
|
1175
|
+
const result2 = await totalumClient.crud.editRecordById(tableName, recordId, {
|
|
1176
|
+
'pdfFile': { name: generatedFileName }
|
|
1177
|
+
});
|
|
1178
|
+
|
|
1179
|
+
```
|
|
1180
|
+
|
|
1181
|
+
**Note:** The HTML is automatically encoded to base64 by the SDK before sending it to the API. Works in both Node.js and browser environments.
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
## Functions for send emails
|
|
1185
|
+
|
|
1186
|
+
### Send a basic email
|
|
1187
|
+
|
|
1188
|
+
```javascript
|
|
1189
|
+
|
|
1190
|
+
const emailPayload = {
|
|
1191
|
+
to: ['recipient@example.com'],
|
|
1192
|
+
subject: 'Your email subject',
|
|
1193
|
+
html: '<h1>Hello</h1><p>This is the email content in HTML</p>',
|
|
1194
|
+
};
|
|
1195
|
+
|
|
1196
|
+
const result = await totalumClient.email.sendEmail(emailPayload);
|
|
1197
|
+
|
|
1198
|
+
if (result.errors) {
|
|
1199
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
console.log('Email sent successfully');
|
|
1204
|
+
console.log('Message ID:', result.data.messageId);
|
|
783
1205
|
|
|
784
1206
|
```
|
|
785
1207
|
|
|
1208
|
+
### Send an email with all options
|
|
1209
|
+
|
|
1210
|
+
```javascript
|
|
1211
|
+
|
|
1212
|
+
const emailPayload = {
|
|
1213
|
+
to: ['recipient1@example.com', 'recipient2@example.com'], // array of recipients
|
|
1214
|
+
subject: 'Your email subject',
|
|
1215
|
+
html: '<h1>Hello</h1><p>This is the email content in HTML</p>',
|
|
1216
|
+
fromName: 'Your Company Name', // optional: custom sender name
|
|
1217
|
+
cc: ['cc@example.com'], // optional: carbon copy recipients
|
|
1218
|
+
bcc: ['bcc@example.com'], // optional: blind carbon copy recipients
|
|
1219
|
+
replyTo: 'reply@example.com', // optional: reply-to address
|
|
1220
|
+
attachments: [ // optional: array of attachments (max 10, each up to 15MB)
|
|
1221
|
+
{
|
|
1222
|
+
filename: 'document.pdf',
|
|
1223
|
+
url: 'https://example.com/path/to/document.pdf',
|
|
1224
|
+
contentType: 'application/pdf' // optional
|
|
1225
|
+
},
|
|
1226
|
+
{
|
|
1227
|
+
filename: 'image.png',
|
|
1228
|
+
url: 'https://example.com/path/to/image.png',
|
|
1229
|
+
contentType: 'image/png' // optional
|
|
1230
|
+
}
|
|
1231
|
+
]
|
|
1232
|
+
};
|
|
1233
|
+
|
|
1234
|
+
const result = await totalumClient.email.sendEmail(emailPayload);
|
|
1235
|
+
|
|
1236
|
+
if (result.errors) {
|
|
1237
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
console.log('Email sent successfully');
|
|
1242
|
+
console.log('Message ID:', result.data.messageId);
|
|
1243
|
+
|
|
1244
|
+
```
|
|
1245
|
+
|
|
1246
|
+
### Send an email with files from Totalum storage
|
|
1247
|
+
|
|
1248
|
+
```javascript
|
|
1249
|
+
|
|
1250
|
+
// First, get the download URL of the file uploaded to Totalum
|
|
1251
|
+
const fileNameId = 'your_file_name.pdf';
|
|
1252
|
+
|
|
1253
|
+
const fileUrlResult = await totalumClient.files.getDownloadUrl(fileNameId);
|
|
1254
|
+
|
|
1255
|
+
if (fileUrlResult.errors) {
|
|
1256
|
+
console.error('Error:', fileUrlResult.errors.errorMessage);
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
const fileUrl = fileUrlResult.data;
|
|
1261
|
+
|
|
1262
|
+
// Then, send the email with the attachment
|
|
1263
|
+
const emailPayload = {
|
|
1264
|
+
to: ['recipient@example.com'],
|
|
1265
|
+
subject: 'Email with attachment from Totalum',
|
|
1266
|
+
html: '<h1>Hello</h1><p>Please find the attached document</p>',
|
|
1267
|
+
attachments: [
|
|
1268
|
+
{
|
|
1269
|
+
filename: 'document.pdf',
|
|
1270
|
+
url: fileUrl,
|
|
1271
|
+
contentType: 'application/pdf'
|
|
1272
|
+
}
|
|
1273
|
+
]
|
|
1274
|
+
};
|
|
1275
|
+
|
|
1276
|
+
const result = await totalumClient.email.sendEmail(emailPayload);
|
|
1277
|
+
|
|
1278
|
+
if (result.errors) {
|
|
1279
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
console.log('Email sent successfully');
|
|
1284
|
+
|
|
1285
|
+
```
|
|
1286
|
+
|
|
1287
|
+
**Important notes:**
|
|
1288
|
+
- Maximum attachments: 10 per email
|
|
1289
|
+
- Maximum size per attachment: 15MB
|
|
1290
|
+
- All attachments must be provided as valid HTTP/HTTPS URLs
|
|
1291
|
+
- The SDK validates attachments before sending
|
|
1292
|
+
|
|
786
1293
|
|
|
787
1294
|
## Functions for call OpenAI API without need to have an OpenAI account
|
|
788
1295
|
|
|
@@ -791,15 +1298,22 @@ const documentResult = result.data.data;
|
|
|
791
1298
|
|
|
792
1299
|
```javascript
|
|
793
1300
|
|
|
794
|
-
//
|
|
795
|
-
const body={
|
|
796
|
-
|
|
797
|
-
|
|
1301
|
+
// See OpenAI API docs: https://platform.openai.com/docs/api-reference/completions/create
|
|
1302
|
+
const body = {
|
|
1303
|
+
model: 'gpt-3.5-turbo-instruct',
|
|
1304
|
+
prompt: 'Say hello in Spanish',
|
|
1305
|
+
max_tokens: 50
|
|
1306
|
+
};
|
|
798
1307
|
|
|
799
1308
|
const result = await totalumClient.openai.createCompletion(body);
|
|
800
|
-
const gptCompletion = result.data.data;
|
|
801
1309
|
|
|
802
|
-
|
|
1310
|
+
if (result.errors) {
|
|
1311
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
const completion = result.data;
|
|
1316
|
+
console.log('Completion:', completion);
|
|
803
1317
|
|
|
804
1318
|
```
|
|
805
1319
|
|
|
@@ -807,21 +1321,26 @@ const gptCompletion = result.data.data;
|
|
|
807
1321
|
|
|
808
1322
|
```javascript
|
|
809
1323
|
|
|
810
|
-
//
|
|
811
|
-
const
|
|
812
|
-
// see the openai api docs for more info
|
|
1324
|
+
// See OpenAI API docs: https://platform.openai.com/docs/api-reference/chat/create
|
|
1325
|
+
const body = {
|
|
813
1326
|
messages: [
|
|
814
|
-
{content: 'You are a math specialist assistant', role: 'system'},
|
|
815
|
-
{content: '
|
|
816
|
-
//etc...
|
|
1327
|
+
{ content: 'You are a math specialist assistant', role: 'system' },
|
|
1328
|
+
{ content: 'How do I solve a matrix?', role: 'user' }
|
|
817
1329
|
],
|
|
818
1330
|
model: 'gpt-3.5-turbo',
|
|
819
|
-
max_tokens:
|
|
1331
|
+
max_tokens: 150,
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1334
|
+
const result = await totalumClient.openai.createChatCompletion(body);
|
|
1335
|
+
|
|
1336
|
+
if (result.errors) {
|
|
1337
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1338
|
+
return;
|
|
820
1339
|
}
|
|
821
1340
|
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
|
|
1341
|
+
const chatCompletion = result.data;
|
|
1342
|
+
console.log('Response:', chatCompletion.choices[0].message.content);
|
|
1343
|
+
console.log('Tokens used:', chatCompletion.usage.total_tokens);
|
|
825
1344
|
|
|
826
1345
|
```
|
|
827
1346
|
|
|
@@ -829,16 +1348,28 @@ const chatCompletion = result.data.data;
|
|
|
829
1348
|
|
|
830
1349
|
```javascript
|
|
831
1350
|
|
|
832
|
-
//
|
|
833
|
-
const
|
|
834
|
-
prompt:'
|
|
835
|
-
size: '256x256' | '512x512' | '1024x1024'
|
|
836
|
-
fileName: '
|
|
1351
|
+
// See OpenAI API docs: https://platform.openai.com/docs/api-reference/images
|
|
1352
|
+
const body = {
|
|
1353
|
+
prompt: 'A beautiful sunset over the mountains',
|
|
1354
|
+
size: '1024x1024', // '256x256' | '512x512' | '1024x1024'
|
|
1355
|
+
fileName: 'sunset_image.png' // CAUTION: reusing names will overwrite files
|
|
1356
|
+
};
|
|
1357
|
+
|
|
1358
|
+
const result = await totalumClient.openai.generateImage(body);
|
|
1359
|
+
|
|
1360
|
+
if (result.errors) {
|
|
1361
|
+
console.error('Error:', result.errors.errorMessage);
|
|
1362
|
+
return;
|
|
837
1363
|
}
|
|
838
1364
|
|
|
839
|
-
const
|
|
840
|
-
|
|
841
|
-
|
|
1365
|
+
const imageFileName = result.data;
|
|
1366
|
+
console.log('Image generated:', imageFileName);
|
|
1367
|
+
|
|
1368
|
+
// Get download URL to view/download the image
|
|
1369
|
+
const urlResult = await totalumClient.files.getDownloadUrl(imageFileName);
|
|
1370
|
+
if (!urlResult.errors) {
|
|
1371
|
+
console.log('Image URL:', urlResult.data);
|
|
1372
|
+
}
|
|
842
1373
|
|
|
843
1374
|
```
|
|
844
1375
|
|