totalum-api-sdk 2.0.20 → 2.0.22
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 +520 -520
- package/dist/common/endpoints.js +44 -44
- package/dist/common/interfaces.js +3 -3
- package/dist/index.js +58 -58
- package/dist/services/CrudService.js +93 -93
- package/dist/services/FilesService.js +61 -61
- package/dist/services/FilterService.js +49 -49
- package/dist/services/GoogleService.js +53 -53
- package/dist/services/OpenaiService.js +62 -62
- package/dist/totalum-styles.css +3069 -0
- package/dist/utils.js +13 -13
- package/package.json +28 -28
- package/dist/common/endpoints.d.ts +0 -40
- package/dist/common/interfaces.d.ts +0 -73
- package/dist/index.d.ts +0 -18
- package/dist/services/CrudService.d.ts +0 -35
- package/dist/services/FilesService.d.ts +0 -20
- package/dist/services/FilterService.d.ts +0 -8
- package/dist/services/GoogleService.d.ts +0 -15
- package/dist/services/OpenaiService.d.ts +0 -38
- package/dist/utils.d.ts +0 -3
package/README.MD
CHANGED
|
@@ -1,520 +1,520 @@
|
|
|
1
|
-
## Totalum api sdk wrapper
|
|
2
|
-
|
|
3
|
-
This library wraps the totalum public api, so you can easy call all endpoints.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
## Installation:
|
|
7
|
-
|
|
8
|
-
```bash
|
|
9
|
-
npm i totalum-api-sdk
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
# Usage of TotalumApiSdk
|
|
14
|
-
|
|
15
|
-
Totalum allows you to use the SDK to perform actions such as creating users, creating items, creating pdfs, etc.
|
|
16
|
-
|
|
17
|
-
Basically the Totalum sdk is a wrapper of the Totalum api, so if you want you can use the api directly if you prefer.
|
|
18
|
-
|
|
19
|
-
### What happens if you are not programming in javascript?
|
|
20
|
-
|
|
21
|
-
If you are not programming in javascript, you can use the api directly, see <a href="https://docs.totalum.app/totalum_openapi.html" target="_blank">TOTALUM API DOCUMENTATION</a>
|
|
22
|
-
|
|
23
|
-
## Authentication
|
|
24
|
-
|
|
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.getItems('your_item', {})
|
|
26
|
-
|
|
27
|
-
You can choose to use one of the two authentication methods offered by Totalum Sdk:
|
|
28
|
-
|
|
29
|
-
- **Token**: You can use an access token to authenticate. This token can be obtained from the localStorage of your browser from the web https://web.totalum.app
|
|
30
|
-
|
|
31
|
-
- **ApiKey**: (RECOMMENDED OPTION) You can use an ApiKey to authenticate. This ApiKey can be obtained in the **Api Keys** section of the **Configuration** section of Totalum.
|
|
32
|
-
|
|
33
|
-
**ES Module Import:**
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
|
|
37
|
-
import {AuthOptions, TotalumApiSdk} from 'totalum-api-sdk';
|
|
38
|
-
|
|
39
|
-
// CHOICE FROM USE accessToken OR apiKey (API KEY IS RECOMMENDED)
|
|
40
|
-
|
|
41
|
-
// the auth using token
|
|
42
|
-
const options: AuthOptions = {
|
|
43
|
-
token:{
|
|
44
|
-
accessToken: 'YOUR TOKEN' // get it from totalum project web localStorage
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// the auth using api key
|
|
49
|
-
const options: AuthOptions = {
|
|
50
|
-
apiKey:{
|
|
51
|
-
'api-key': 'your_api_key', //the api key secret that is shown only once, example: sk_23r23r23r...
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const totalumClient = new TotalumApiSdk(options);
|
|
56
|
-
|
|
57
|
-
// execute some TotalumApiSdk function
|
|
58
|
-
const result = await totalumClient.crud.getItems('your_element_table_name', {});
|
|
59
|
-
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
**CommonJS Require:**
|
|
63
|
-
|
|
64
|
-
```javascript
|
|
65
|
-
|
|
66
|
-
const totalum = require('totalum-api-sdk');
|
|
67
|
-
|
|
68
|
-
// CHOICE FROM USE accessToken OR apiKey (API KEY IS RECOMMENDED)
|
|
69
|
-
|
|
70
|
-
// the auth using token
|
|
71
|
-
const options = {
|
|
72
|
-
token:{
|
|
73
|
-
accessToken: 'YOUR TOKEN' // get it from totalum project web localStorage
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// the auth using api key
|
|
78
|
-
const options = {
|
|
79
|
-
apiKey:{
|
|
80
|
-
'api-key': 'your_api_key', //the api key secret that is shown only once, example: sk_23r23r23r...
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const totalumClient = new totalum.TotalumApiSdk(options);
|
|
85
|
-
|
|
86
|
-
// execute some TotalumApiSdk function
|
|
87
|
-
const result = await totalumClient.crud.getItems('your_element_table_name', {});
|
|
88
|
-
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
**HTML script import:** (Use this way if you are using standalone html, and you cannot import npm packages)
|
|
92
|
-
|
|
93
|
-
```html
|
|
94
|
-
<head>
|
|
95
|
-
<script src="https://cdn.jsdelivr.net/npm/totalum-api-sdk@2.0.19/dist/totalum-sdk.min.js"></script>
|
|
96
|
-
</head>
|
|
97
|
-
<script>
|
|
98
|
-
//Example of use TotalumSdk in your custom html page
|
|
99
|
-
const token=localStorage.getItem('token');
|
|
100
|
-
var totalumClient = new totalumSdk.TotalumApiSdk({
|
|
101
|
-
token:{
|
|
102
|
-
accessToken: token
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
const tableElementName = 'your-table-name'; // replace with your table name
|
|
107
|
-
|
|
108
|
-
//example of endpoint execution
|
|
109
|
-
const result = totalumClient.crud.getItems(tableElementName, {}).then((result) => {
|
|
110
|
-
const items = result.data.data;
|
|
111
|
-
console.log("THE ITEMS", items);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
</script>
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
## Functions for create, read, filter, update, delete. (CRUD)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
### Get item by id
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
```javascript
|
|
125
|
-
|
|
126
|
-
// get item by id from your_element_table_name
|
|
127
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
128
|
-
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item object
|
|
129
|
-
const result = await totalumClient.crud.getItemById(tableElementName, your_item_id);
|
|
130
|
-
const item = result.data.data;
|
|
131
|
-
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### Get items
|
|
135
|
-
|
|
136
|
-
```javascript
|
|
137
|
-
|
|
138
|
-
// get items from your_element_table_name (for default 50 items per page)
|
|
139
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
140
|
-
const result = await totalumClient.crud.getItems(tableElementName, {});
|
|
141
|
-
const items = result.data.data;
|
|
142
|
-
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### delete item by id
|
|
146
|
-
|
|
147
|
-
```javascript
|
|
148
|
-
// delete item by id from your_element_table_name
|
|
149
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
150
|
-
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item object
|
|
151
|
-
const result = await totalumClient.crud.deleteItemById(tableElementName, your_item_id);
|
|
152
|
-
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
### edit item by id
|
|
156
|
-
|
|
157
|
-
```javascript
|
|
158
|
-
// edit item by id from your_element_table_name, you can edit 1 or multiple properties at the same time (like a patch)
|
|
159
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
160
|
-
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item object
|
|
161
|
-
const result = await totalumClient.crud.editItemById(tableElementName, your_item_id, {your_item_property: 'new value'});
|
|
162
|
-
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### create item
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
// create item from your_element_table_name, you need to pass at least the required properties
|
|
169
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
170
|
-
const result = await totalumClient.crud.createItem(tableElementName, {your_item_property: 'new value'});
|
|
171
|
-
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### add many to many reference item (add a reference to another item that have a many to many relationship)
|
|
175
|
-
|
|
176
|
-
```javascript
|
|
177
|
-
|
|
178
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
179
|
-
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item
|
|
180
|
-
const propertyName = 'your_property_name'; // replace 'your_property_name' with the name of the property that have a many to many relationship
|
|
181
|
-
const referenceId = 'your_reference_item_id'; // replace 'your_reference_item_id' with the id of the item that you want to add as a many to many reference
|
|
182
|
-
const result = await totalumClient.crud.addManyToManyReferenceItem(tableElementName, your_item_id, propertyName, referenceId);
|
|
183
|
-
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
### drop many to many reference item (drop a reference to another item that have a many to many relationship)
|
|
187
|
-
|
|
188
|
-
```javascript
|
|
189
|
-
|
|
190
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
191
|
-
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item
|
|
192
|
-
const propertyName = 'your_property_name'; // replace 'your_property_name' with the name of the property that have a many to many relationship
|
|
193
|
-
const referenceId = 'your_reference_item_id'; // replace 'your_reference_item_id' with the id of the item that you want to drop as a many to many reference
|
|
194
|
-
const result = await totalumClient.crud.dropManyToManyReferenceItem(tableElementName, your_item_id, propertyName, referenceId);
|
|
195
|
-
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
### get many to many references items (get all items that have a many to many relationship with the item)
|
|
199
|
-
|
|
200
|
-
```javascript
|
|
201
|
-
|
|
202
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
203
|
-
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item
|
|
204
|
-
const propertyName = 'your_property_name'; // replace 'your_property_name' with the name of the property that have a many to many relationship
|
|
205
|
-
|
|
206
|
-
// the query is optional, you can use it to filter and sort the results
|
|
207
|
-
const query = {
|
|
208
|
-
filter: [
|
|
209
|
-
{
|
|
210
|
-
'your_property_name': 'value' // add your custom filters here
|
|
211
|
-
},
|
|
212
|
-
],
|
|
213
|
-
sort:{
|
|
214
|
-
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
215
|
-
},
|
|
216
|
-
pagination: {
|
|
217
|
-
limit: 50,
|
|
218
|
-
page: 0,
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
const result = await totalumClient.crud.getManyToManyReferencesItems(tableElementName, your_item_id, propertyName, query);
|
|
222
|
-
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
## Functions for filter data
|
|
227
|
-
|
|
228
|
-
### Filter data using totalum AND filter
|
|
229
|
-
|
|
230
|
-
```javascript
|
|
231
|
-
|
|
232
|
-
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
233
|
-
|
|
234
|
-
// get items from your_element_table_name (for default 50 items per page) applying a filter AND query (all conditions must be true)
|
|
235
|
-
const filter: FilterSearchQueryI = {
|
|
236
|
-
filter: [
|
|
237
|
-
{
|
|
238
|
-
'your_property_name': 'value' // it matches the value exactly
|
|
239
|
-
},
|
|
240
|
-
{
|
|
241
|
-
'your_other_property_name': {regex: 'your regex query', options: 'i'} // it matches a value using a regex query and options: i for case insensitive (ignore if it is uppercase or lowercase)
|
|
242
|
-
},
|
|
243
|
-
// PD: gte and lte operators are only allowed for properties of type date or number
|
|
244
|
-
{
|
|
245
|
-
'your_other_property_name': {gte: new Date('your date')} // it matches a value greater than or equal to the date
|
|
246
|
-
},
|
|
247
|
-
{
|
|
248
|
-
'your_other_property_name': {lte: new Date('your date')} // it matches a value less than or equal to the date
|
|
249
|
-
},
|
|
250
|
-
{
|
|
251
|
-
'your_other_property_name': {gte: 10} // it matches a value greater than or equal to 10
|
|
252
|
-
},
|
|
253
|
-
{
|
|
254
|
-
'your_other_property_name': {lte: 100} // it matches a value less than or equal to 100
|
|
255
|
-
}
|
|
256
|
-
],
|
|
257
|
-
sort:{
|
|
258
|
-
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
259
|
-
},
|
|
260
|
-
pagination: {
|
|
261
|
-
limit: 50,
|
|
262
|
-
page: 0,
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
const result = await totalumClient.crud.getItems(tableElementName, filter);
|
|
267
|
-
const items = result.data.data;
|
|
268
|
-
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
### Filter data using totalum OR filter
|
|
272
|
-
|
|
273
|
-
```javascript
|
|
274
|
-
|
|
275
|
-
// get items from your_element_table_name (for default 50 items per page) applying a filter OR query (at least one condition must be true)
|
|
276
|
-
const filter: FilterSearchQueryI = {
|
|
277
|
-
filter: [
|
|
278
|
-
{
|
|
279
|
-
or: [
|
|
280
|
-
{
|
|
281
|
-
'your_property_name': 'value' // it matches the value exactly
|
|
282
|
-
},
|
|
283
|
-
{
|
|
284
|
-
'your_other_property_name': {regex: 'your regex query', options: 'i'} // it matches a value using a regex query and options: i for case insensitive (ignore if it is uppercase or lowercase)
|
|
285
|
-
},
|
|
286
|
-
{
|
|
287
|
-
'your_other_property_name': {gte: new Date('your date')} // it matches a value greater than or equal to the date
|
|
288
|
-
},
|
|
289
|
-
]
|
|
290
|
-
}
|
|
291
|
-
],
|
|
292
|
-
sort: {
|
|
293
|
-
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
294
|
-
},
|
|
295
|
-
pagination: {
|
|
296
|
-
limit: 50,
|
|
297
|
-
page: 0,
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
const result = await totalumClient.crud.getItems(tableElementName, filter);
|
|
302
|
-
const items = result.data.data;
|
|
303
|
-
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
### Filter data using totalum AND and OR filter
|
|
307
|
-
|
|
308
|
-
```javascript
|
|
309
|
-
|
|
310
|
-
// get items from your_element_table_name (for default 50 items per page) applying a filter OR and AND
|
|
311
|
-
const filter: FilterSearchQueryI = {
|
|
312
|
-
filter: [
|
|
313
|
-
{
|
|
314
|
-
or: [
|
|
315
|
-
{
|
|
316
|
-
'your_property_name_in_or': 'value' // it matches the value exactly
|
|
317
|
-
},
|
|
318
|
-
{
|
|
319
|
-
'your_other_property_name_in_or': {regex: 'your regex query', options: 'i'} // it matches a value using a regex query and options: i for case insensitive (ignore if it is uppercase or lowercase)
|
|
320
|
-
},
|
|
321
|
-
],
|
|
322
|
-
{
|
|
323
|
-
'your_other_property_name': 'value' // it matches the value exactly
|
|
324
|
-
},
|
|
325
|
-
}
|
|
326
|
-
],
|
|
327
|
-
sort: {
|
|
328
|
-
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
329
|
-
},
|
|
330
|
-
pagination: {
|
|
331
|
-
limit: 50,
|
|
332
|
-
page: 0,
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
const result = await totalumClient.crud.getItems(tableElementName, filter);
|
|
337
|
-
const items = result.data.data;
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
### Filter using your custom mongoDb aggregation query
|
|
343
|
-
|
|
344
|
-
```javascript
|
|
345
|
-
|
|
346
|
-
// filter results from your_element_table_name applying a filter query (a custom mongodb aggregation query)
|
|
347
|
-
const customMongoDbAggregationQueryInString = `
|
|
348
|
-
|
|
349
|
-
your custom mongo aggregation query in string, for more info:
|
|
350
|
-
https://docs.mongodb.com/manual/aggregation/
|
|
351
|
-
|
|
352
|
-
or ask to chatgpt, he is very good writing mongo aggregation queries ;)
|
|
353
|
-
|
|
354
|
-
`;
|
|
355
|
-
|
|
356
|
-
const result = await totalumClient.filter.runCustomMongoAggregationQuery(tableElementName, customMongoDbAggregationQueryInString);
|
|
357
|
-
|
|
358
|
-
```
|
|
359
|
-
|
|
360
|
-
## Functions for create download and manipulate files
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
### Upload a file to Totalum (you can upload any file type)
|
|
364
|
-
|
|
365
|
-
```javascript
|
|
366
|
-
|
|
367
|
-
// this example is using axios for get a file, and then transform it to a blob before upload it to totalum
|
|
368
|
-
// you can get the file from any source, like a file input, or a url, or a base64 string, etc. Then you need to transform it to a blob before upload it to totalum
|
|
369
|
-
let imageUrl = 'https://www.totalum.app/img/logoT.png'; //an example test file
|
|
370
|
-
const response = await modules.axios.get(imageUrl, { responseType: 'blob' });
|
|
371
|
-
// The response is a blob
|
|
372
|
-
const yourFileBlob = new Blob([response.data], { type: response.headers['content-type'] });
|
|
373
|
-
|
|
374
|
-
const fileName = 'your_file_name.png'; // replace 'your_file_name' with the name of your file, replace .png with the extension of your file
|
|
375
|
-
const file = yourFileBlob // replace yourFile with your file object binary blob (in blob format)
|
|
376
|
-
const formData = new FormData();
|
|
377
|
-
formData.append('file', file, fileName);
|
|
378
|
-
const result = await totalumClient.files.uploadFile(formData);
|
|
379
|
-
const fileNameId = result.data.data;
|
|
380
|
-
|
|
381
|
-
//if you want to link this file to an item, you need to add the fileNameId to the item property of type file
|
|
382
|
-
const result2 = await totalumClient.crud.editItemById('your_element_table_name', 'your_item_id', {'your_file_property_name': {name: fileNameId}});
|
|
383
|
-
// now the file is linked to the item property, and you can see it in the totalum panel.
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
### Get the download url of a file
|
|
389
|
-
|
|
390
|
-
```javascript
|
|
391
|
-
|
|
392
|
-
// you can get the fileNameId from the result of the upload file function
|
|
393
|
-
const fileNameId = 'your_file_name.png'; // replace 'your_file_name' with the name id of your file, replace .png with the extension of your file
|
|
394
|
-
|
|
395
|
-
//optional options
|
|
396
|
-
const options = {
|
|
397
|
-
// the default expiration time is 128 hours, but you can set it to whatever you want, after the expiration time the url will not return the file
|
|
398
|
-
expirationTime: Date.now() + (128 * 60 * 60 * 1000); // Set to expire in 128 hours, how it works is, set the current date in miliseconds + the miliseconds you want the url to expire
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
const result = await totalumClient.files.getDownloadUrl(fileNameId, options);
|
|
402
|
-
let [fileUrl] = result.data.data;
|
|
403
|
-
// the result will be a full url that you can use to download the file
|
|
404
|
-
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
### Generate a pdf from a template
|
|
408
|
-
|
|
409
|
-
```javascript
|
|
410
|
-
|
|
411
|
-
const templateId = 'your_template_id'; // replace 'your_template_id' with the id of your template id, go to totalum -> configuration -> pdf templates
|
|
412
|
-
|
|
413
|
-
// the variables will be replaced in your template with handlebars
|
|
414
|
-
const variablesExample = { // replace the variables with your variables, can contain nested objects of n deep, can contain strings, numbers, dates, etc.
|
|
415
|
-
'your_variable_name': 'your_variable_value',
|
|
416
|
-
'your_other_variable_name': {
|
|
417
|
-
'your_other_variable_name': 'your_other_variable_value'
|
|
418
|
-
'your_other_variable_name': 10,
|
|
419
|
-
'your_other_variable_name': new Date('your date'),
|
|
420
|
-
},
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
const fileName = 'your_pdf_name.pdf'; // replace 'your_pdf_name' with the name of your pdf
|
|
424
|
-
//CAUTION: if you use the same name for multiple pdfs, the pdfs will be overwritten
|
|
425
|
-
const result = await totalumClient.files.generatePdfByTemplate(templateId, variablesExample, name);
|
|
426
|
-
const fileUrl = result.data.data;
|
|
427
|
-
// with the fileUrl you can download the pdf
|
|
428
|
-
|
|
429
|
-
//if you want to link this pdf to an item, you need to add the fileName to the item property of type file
|
|
430
|
-
const result2 = await totalumClient.crud.editItemById('your_element_table_name', 'your_item_id', {'your_pdf_property_name': {name: fileName}});
|
|
431
|
-
|
|
432
|
-
```
|
|
433
|
-
|
|
434
|
-
### get OCR of an image (get the text of an image)
|
|
435
|
-
|
|
436
|
-
```javascript
|
|
437
|
-
|
|
438
|
-
// you can only do ocr of a file that is previously uploaded to totalum (see the upload file function)
|
|
439
|
-
// then you can do ocr of the file using the file name of the file uploaded
|
|
440
|
-
const fileNameId = 'your_file_name_id.png'; // replace 'your_file_name' with the name of your file, replace .png with the extension of your image
|
|
441
|
-
const result = await totalumClient.files.ocrOfImage(fileNameId);
|
|
442
|
-
const ocrResult = result.data.data;
|
|
443
|
-
// ocrResult.text will contain all text of the image
|
|
444
|
-
// ocrResult.fullDetails will contain all details of the image, like the language, the position of the text, etc.
|
|
445
|
-
|
|
446
|
-
```
|
|
447
|
-
|
|
448
|
-
### get OCR of a pdf (get the text of a pdf)
|
|
449
|
-
|
|
450
|
-
```javascript
|
|
451
|
-
|
|
452
|
-
// you can only do ocr of a file that is previously uploaded to totalum (see the upload file function)
|
|
453
|
-
// then you can do ocr of the file using the file name of the file uploaded
|
|
454
|
-
const fileName = 'your_file_name_id.pdf'; // replace 'your_file_name' with the name of your file
|
|
455
|
-
const result = await totalumClient.files.ocrOfPdf(fileName);
|
|
456
|
-
const ocrResult = result.data.data;
|
|
457
|
-
// ocrResult.text will contain all text of the pdf
|
|
458
|
-
// ocrResult.fullDetails will contain all details of the pdf, like the language, in which page is the text, the position of the text, etc.
|
|
459
|
-
|
|
460
|
-
```
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
## Functions for call OpenAI API without need to have an OpenAI account
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
### Create a completion (deprecated, use createChatCompletion instead)
|
|
467
|
-
|
|
468
|
-
```javascript
|
|
469
|
-
|
|
470
|
-
//body the openai completion body, more info here: https://platform.openai.com/docs/api-reference/completions/create
|
|
471
|
-
const body={
|
|
472
|
-
// see the openai api docs for more info
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
const result = await totalumClient.openai.createCompletion(body);
|
|
476
|
-
const gptCompletion = result.data.data;
|
|
477
|
-
|
|
478
|
-
// returns the completion provided by openai api
|
|
479
|
-
|
|
480
|
-
```
|
|
481
|
-
|
|
482
|
-
### Create a chat completion
|
|
483
|
-
|
|
484
|
-
```javascript
|
|
485
|
-
|
|
486
|
-
//body the openai chat completion body, more info here: https://platform.openai.com/docs/api-reference/chat/create
|
|
487
|
-
const bodyExample={
|
|
488
|
-
// see the openai api docs for more info
|
|
489
|
-
messages: [
|
|
490
|
-
{content: 'You are a math specialist assistant', role: 'system'},
|
|
491
|
-
{content: 'how I can resolve a matrix', role: 'user'}
|
|
492
|
-
//etc...
|
|
493
|
-
],
|
|
494
|
-
model: 'gpt-3.5-turbo',
|
|
495
|
-
max_tokens: 30,
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
const result = await totalumClient.openai.createChatCompletion(bodyExample);
|
|
499
|
-
const chatCompletion = result.data.data;
|
|
500
|
-
// returns the completion provided by openai api
|
|
501
|
-
|
|
502
|
-
```
|
|
503
|
-
|
|
504
|
-
### Generate an image
|
|
505
|
-
|
|
506
|
-
```javascript
|
|
507
|
-
|
|
508
|
-
//body the openai chat completion body, more info here: https://platform.openai.com/docs/api-reference/chat/create
|
|
509
|
-
const bodyExample={
|
|
510
|
-
prompt:'your prompt',
|
|
511
|
-
size: '256x256' | '512x512' | '1024x1024',
|
|
512
|
-
fileName: 'file-name-of-file-saved.png' // CAUTION: if you use the same name for multiple images, the images will be overwritten
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
const result = await totalumClient.openai.generateImage(bodyExample);
|
|
516
|
-
const imageCompletion = result.data.data;
|
|
517
|
-
// returns the file name with the file generated by openai, if is not an url, you can download it using files.getDownloadUrl(imageCompletion);
|
|
518
|
-
|
|
519
|
-
```
|
|
520
|
-
|
|
1
|
+
## Totalum api sdk wrapper
|
|
2
|
+
|
|
3
|
+
This library wraps the totalum public api, so you can easy call all endpoints.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Installation:
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm i totalum-api-sdk
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Usage of TotalumApiSdk
|
|
14
|
+
|
|
15
|
+
Totalum allows you to use the SDK to perform actions such as creating users, creating items, creating pdfs, etc.
|
|
16
|
+
|
|
17
|
+
Basically the Totalum sdk is a wrapper of the Totalum api, so if you want you can use the api directly if you prefer.
|
|
18
|
+
|
|
19
|
+
### What happens if you are not programming in javascript?
|
|
20
|
+
|
|
21
|
+
If you are not programming in javascript, you can use the api directly, see <a href="https://docs.totalum.app/totalum_openapi.html" target="_blank">TOTALUM API DOCUMENTATION</a>
|
|
22
|
+
|
|
23
|
+
## Authentication
|
|
24
|
+
|
|
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.getItems('your_item', {})
|
|
26
|
+
|
|
27
|
+
You can choose to use one of the two authentication methods offered by Totalum Sdk:
|
|
28
|
+
|
|
29
|
+
- **Token**: You can use an access token to authenticate. This token can be obtained from the localStorage of your browser from the web https://web.totalum.app
|
|
30
|
+
|
|
31
|
+
- **ApiKey**: (RECOMMENDED OPTION) You can use an ApiKey to authenticate. This ApiKey can be obtained in the **Api Keys** section of the **Configuration** section of Totalum.
|
|
32
|
+
|
|
33
|
+
**ES Module Import:**
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
|
|
37
|
+
import {AuthOptions, TotalumApiSdk} from 'totalum-api-sdk';
|
|
38
|
+
|
|
39
|
+
// CHOICE FROM USE accessToken OR apiKey (API KEY IS RECOMMENDED)
|
|
40
|
+
|
|
41
|
+
// the auth using token
|
|
42
|
+
const options: AuthOptions = {
|
|
43
|
+
token:{
|
|
44
|
+
accessToken: 'YOUR TOKEN' // get it from totalum project web localStorage
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// the auth using api key
|
|
49
|
+
const options: AuthOptions = {
|
|
50
|
+
apiKey:{
|
|
51
|
+
'api-key': 'your_api_key', //the api key secret that is shown only once, example: sk_23r23r23r...
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const totalumClient = new TotalumApiSdk(options);
|
|
56
|
+
|
|
57
|
+
// execute some TotalumApiSdk function
|
|
58
|
+
const result = await totalumClient.crud.getItems('your_element_table_name', {});
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**CommonJS Require:**
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
|
|
66
|
+
const totalum = require('totalum-api-sdk');
|
|
67
|
+
|
|
68
|
+
// CHOICE FROM USE accessToken OR apiKey (API KEY IS RECOMMENDED)
|
|
69
|
+
|
|
70
|
+
// the auth using token
|
|
71
|
+
const options = {
|
|
72
|
+
token:{
|
|
73
|
+
accessToken: 'YOUR TOKEN' // get it from totalum project web localStorage
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// the auth using api key
|
|
78
|
+
const options = {
|
|
79
|
+
apiKey:{
|
|
80
|
+
'api-key': 'your_api_key', //the api key secret that is shown only once, example: sk_23r23r23r...
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const totalumClient = new totalum.TotalumApiSdk(options);
|
|
85
|
+
|
|
86
|
+
// execute some TotalumApiSdk function
|
|
87
|
+
const result = await totalumClient.crud.getItems('your_element_table_name', {});
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**HTML script import:** (Use this way if you are using standalone html, and you cannot import npm packages)
|
|
92
|
+
|
|
93
|
+
```html
|
|
94
|
+
<head>
|
|
95
|
+
<script src="https://cdn.jsdelivr.net/npm/totalum-api-sdk@2.0.19/dist/totalum-sdk.min.js"></script>
|
|
96
|
+
</head>
|
|
97
|
+
<script>
|
|
98
|
+
//Example of use TotalumSdk in your custom html page
|
|
99
|
+
const token=localStorage.getItem('token');
|
|
100
|
+
var totalumClient = new totalumSdk.TotalumApiSdk({
|
|
101
|
+
token:{
|
|
102
|
+
accessToken: token
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const tableElementName = 'your-table-name'; // replace with your table name
|
|
107
|
+
|
|
108
|
+
//example of endpoint execution
|
|
109
|
+
const result = totalumClient.crud.getItems(tableElementName, {}).then((result) => {
|
|
110
|
+
const items = result.data.data;
|
|
111
|
+
console.log("THE ITEMS", items);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
</script>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
## Functions for create, read, filter, update, delete. (CRUD)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
### Get item by id
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
|
|
126
|
+
// get item by id from your_element_table_name
|
|
127
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
128
|
+
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item object
|
|
129
|
+
const result = await totalumClient.crud.getItemById(tableElementName, your_item_id);
|
|
130
|
+
const item = result.data.data;
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Get items
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
|
|
138
|
+
// get items from your_element_table_name (for default 50 items per page)
|
|
139
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
140
|
+
const result = await totalumClient.crud.getItems(tableElementName, {});
|
|
141
|
+
const items = result.data.data;
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### delete item by id
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
// delete item by id from your_element_table_name
|
|
149
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
150
|
+
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item object
|
|
151
|
+
const result = await totalumClient.crud.deleteItemById(tableElementName, your_item_id);
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### edit item by id
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
// edit item by id from your_element_table_name, you can edit 1 or multiple properties at the same time (like a patch)
|
|
159
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
160
|
+
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item object
|
|
161
|
+
const result = await totalumClient.crud.editItemById(tableElementName, your_item_id, {your_item_property: 'new value'});
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### create item
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
// create item from your_element_table_name, you need to pass at least the required properties
|
|
169
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
170
|
+
const result = await totalumClient.crud.createItem(tableElementName, {your_item_property: 'new value'});
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### add many to many reference item (add a reference to another item that have a many to many relationship)
|
|
175
|
+
|
|
176
|
+
```javascript
|
|
177
|
+
|
|
178
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
179
|
+
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item
|
|
180
|
+
const propertyName = 'your_property_name'; // replace 'your_property_name' with the name of the property that have a many to many relationship
|
|
181
|
+
const referenceId = 'your_reference_item_id'; // replace 'your_reference_item_id' with the id of the item that you want to add as a many to many reference
|
|
182
|
+
const result = await totalumClient.crud.addManyToManyReferenceItem(tableElementName, your_item_id, propertyName, referenceId);
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### drop many to many reference item (drop a reference to another item that have a many to many relationship)
|
|
187
|
+
|
|
188
|
+
```javascript
|
|
189
|
+
|
|
190
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
191
|
+
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item
|
|
192
|
+
const propertyName = 'your_property_name'; // replace 'your_property_name' with the name of the property that have a many to many relationship
|
|
193
|
+
const referenceId = 'your_reference_item_id'; // replace 'your_reference_item_id' with the id of the item that you want to drop as a many to many reference
|
|
194
|
+
const result = await totalumClient.crud.dropManyToManyReferenceItem(tableElementName, your_item_id, propertyName, referenceId);
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### get many to many references items (get all items that have a many to many relationship with the item)
|
|
199
|
+
|
|
200
|
+
```javascript
|
|
201
|
+
|
|
202
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
203
|
+
let your_item_id = 'your_item_id'; // replace 'your_item_id' with the id of the item
|
|
204
|
+
const propertyName = 'your_property_name'; // replace 'your_property_name' with the name of the property that have a many to many relationship
|
|
205
|
+
|
|
206
|
+
// the query is optional, you can use it to filter and sort the results
|
|
207
|
+
const query = {
|
|
208
|
+
filter: [
|
|
209
|
+
{
|
|
210
|
+
'your_property_name': 'value' // add your custom filters here
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
sort:{
|
|
214
|
+
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
215
|
+
},
|
|
216
|
+
pagination: {
|
|
217
|
+
limit: 50,
|
|
218
|
+
page: 0,
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const result = await totalumClient.crud.getManyToManyReferencesItems(tableElementName, your_item_id, propertyName, query);
|
|
222
|
+
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
## Functions for filter data
|
|
227
|
+
|
|
228
|
+
### Filter data using totalum AND filter
|
|
229
|
+
|
|
230
|
+
```javascript
|
|
231
|
+
|
|
232
|
+
const tableElementName = 'your_element_table_name'; // replace 'your_element_table_name' with the name of your element table
|
|
233
|
+
|
|
234
|
+
// get items from your_element_table_name (for default 50 items per page) applying a filter AND query (all conditions must be true)
|
|
235
|
+
const filter: FilterSearchQueryI = {
|
|
236
|
+
filter: [
|
|
237
|
+
{
|
|
238
|
+
'your_property_name': 'value' // it matches the value exactly
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
'your_other_property_name': {regex: 'your regex query', options: 'i'} // it matches a value using a regex query and options: i for case insensitive (ignore if it is uppercase or lowercase)
|
|
242
|
+
},
|
|
243
|
+
// PD: gte and lte operators are only allowed for properties of type date or number
|
|
244
|
+
{
|
|
245
|
+
'your_other_property_name': {gte: new Date('your date')} // it matches a value greater than or equal to the date
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
'your_other_property_name': {lte: new Date('your date')} // it matches a value less than or equal to the date
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
'your_other_property_name': {gte: 10} // it matches a value greater than or equal to 10
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
'your_other_property_name': {lte: 100} // it matches a value less than or equal to 100
|
|
255
|
+
}
|
|
256
|
+
],
|
|
257
|
+
sort:{
|
|
258
|
+
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
259
|
+
},
|
|
260
|
+
pagination: {
|
|
261
|
+
limit: 50,
|
|
262
|
+
page: 0,
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const result = await totalumClient.crud.getItems(tableElementName, filter);
|
|
267
|
+
const items = result.data.data;
|
|
268
|
+
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Filter data using totalum OR filter
|
|
272
|
+
|
|
273
|
+
```javascript
|
|
274
|
+
|
|
275
|
+
// get items from your_element_table_name (for default 50 items per page) applying a filter OR query (at least one condition must be true)
|
|
276
|
+
const filter: FilterSearchQueryI = {
|
|
277
|
+
filter: [
|
|
278
|
+
{
|
|
279
|
+
or: [
|
|
280
|
+
{
|
|
281
|
+
'your_property_name': 'value' // it matches the value exactly
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
'your_other_property_name': {regex: 'your regex query', options: 'i'} // it matches a value using a regex query and options: i for case insensitive (ignore if it is uppercase or lowercase)
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
'your_other_property_name': {gte: new Date('your date')} // it matches a value greater than or equal to the date
|
|
288
|
+
},
|
|
289
|
+
]
|
|
290
|
+
}
|
|
291
|
+
],
|
|
292
|
+
sort: {
|
|
293
|
+
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
294
|
+
},
|
|
295
|
+
pagination: {
|
|
296
|
+
limit: 50,
|
|
297
|
+
page: 0,
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const result = await totalumClient.crud.getItems(tableElementName, filter);
|
|
302
|
+
const items = result.data.data;
|
|
303
|
+
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### Filter data using totalum AND and OR filter
|
|
307
|
+
|
|
308
|
+
```javascript
|
|
309
|
+
|
|
310
|
+
// get items from your_element_table_name (for default 50 items per page) applying a filter OR and AND
|
|
311
|
+
const filter: FilterSearchQueryI = {
|
|
312
|
+
filter: [
|
|
313
|
+
{
|
|
314
|
+
or: [
|
|
315
|
+
{
|
|
316
|
+
'your_property_name_in_or': 'value' // it matches the value exactly
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
'your_other_property_name_in_or': {regex: 'your regex query', options: 'i'} // it matches a value using a regex query and options: i for case insensitive (ignore if it is uppercase or lowercase)
|
|
320
|
+
},
|
|
321
|
+
],
|
|
322
|
+
{
|
|
323
|
+
'your_other_property_name': 'value' // it matches the value exactly
|
|
324
|
+
},
|
|
325
|
+
}
|
|
326
|
+
],
|
|
327
|
+
sort: {
|
|
328
|
+
'your_property_name': 1 // 1 for asc, -1 for desc
|
|
329
|
+
},
|
|
330
|
+
pagination: {
|
|
331
|
+
limit: 50,
|
|
332
|
+
page: 0,
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const result = await totalumClient.crud.getItems(tableElementName, filter);
|
|
337
|
+
const items = result.data.data;
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### Filter using your custom mongoDb aggregation query
|
|
343
|
+
|
|
344
|
+
```javascript
|
|
345
|
+
|
|
346
|
+
// filter results from your_element_table_name applying a filter query (a custom mongodb aggregation query)
|
|
347
|
+
const customMongoDbAggregationQueryInString = `
|
|
348
|
+
|
|
349
|
+
your custom mongo aggregation query in string, for more info:
|
|
350
|
+
https://docs.mongodb.com/manual/aggregation/
|
|
351
|
+
|
|
352
|
+
or ask to chatgpt, he is very good writing mongo aggregation queries ;)
|
|
353
|
+
|
|
354
|
+
`;
|
|
355
|
+
|
|
356
|
+
const result = await totalumClient.filter.runCustomMongoAggregationQuery(tableElementName, customMongoDbAggregationQueryInString);
|
|
357
|
+
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Functions for create download and manipulate files
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
### Upload a file to Totalum (you can upload any file type)
|
|
364
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
|
|
367
|
+
// this example is using axios for get a file, and then transform it to a blob before upload it to totalum
|
|
368
|
+
// you can get the file from any source, like a file input, or a url, or a base64 string, etc. Then you need to transform it to a blob before upload it to totalum
|
|
369
|
+
let imageUrl = 'https://www.totalum.app/img/logoT.png'; //an example test file
|
|
370
|
+
const response = await modules.axios.get(imageUrl, { responseType: 'blob' });
|
|
371
|
+
// The response is a blob
|
|
372
|
+
const yourFileBlob = new Blob([response.data], { type: response.headers['content-type'] });
|
|
373
|
+
|
|
374
|
+
const fileName = 'your_file_name.png'; // replace 'your_file_name' with the name of your file, replace .png with the extension of your file
|
|
375
|
+
const file = yourFileBlob // replace yourFile with your file object binary blob (in blob format)
|
|
376
|
+
const formData = new FormData();
|
|
377
|
+
formData.append('file', file, fileName);
|
|
378
|
+
const result = await totalumClient.files.uploadFile(formData);
|
|
379
|
+
const fileNameId = result.data.data;
|
|
380
|
+
|
|
381
|
+
//if you want to link this file to an item, you need to add the fileNameId to the item property of type file
|
|
382
|
+
const result2 = await totalumClient.crud.editItemById('your_element_table_name', 'your_item_id', {'your_file_property_name': {name: fileNameId}});
|
|
383
|
+
// now the file is linked to the item property, and you can see it in the totalum panel.
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### Get the download url of a file
|
|
389
|
+
|
|
390
|
+
```javascript
|
|
391
|
+
|
|
392
|
+
// you can get the fileNameId from the result of the upload file function
|
|
393
|
+
const fileNameId = 'your_file_name.png'; // replace 'your_file_name' with the name id of your file, replace .png with the extension of your file
|
|
394
|
+
|
|
395
|
+
//optional options
|
|
396
|
+
const options = {
|
|
397
|
+
// the default expiration time is 128 hours, but you can set it to whatever you want, after the expiration time the url will not return the file
|
|
398
|
+
expirationTime: Date.now() + (128 * 60 * 60 * 1000); // Set to expire in 128 hours, how it works is, set the current date in miliseconds + the miliseconds you want the url to expire
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const result = await totalumClient.files.getDownloadUrl(fileNameId, options);
|
|
402
|
+
let [fileUrl] = result.data.data;
|
|
403
|
+
// the result will be a full url that you can use to download the file
|
|
404
|
+
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### Generate a pdf from a template
|
|
408
|
+
|
|
409
|
+
```javascript
|
|
410
|
+
|
|
411
|
+
const templateId = 'your_template_id'; // replace 'your_template_id' with the id of your template id, go to totalum -> configuration -> pdf templates
|
|
412
|
+
|
|
413
|
+
// the variables will be replaced in your template with handlebars
|
|
414
|
+
const variablesExample = { // replace the variables with your variables, can contain nested objects of n deep, can contain strings, numbers, dates, etc.
|
|
415
|
+
'your_variable_name': 'your_variable_value',
|
|
416
|
+
'your_other_variable_name': {
|
|
417
|
+
'your_other_variable_name': 'your_other_variable_value'
|
|
418
|
+
'your_other_variable_name': 10,
|
|
419
|
+
'your_other_variable_name': new Date('your date'),
|
|
420
|
+
},
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const fileName = 'your_pdf_name.pdf'; // replace 'your_pdf_name' with the name of your pdf
|
|
424
|
+
//CAUTION: if you use the same name for multiple pdfs, the pdfs will be overwritten
|
|
425
|
+
const result = await totalumClient.files.generatePdfByTemplate(templateId, variablesExample, name);
|
|
426
|
+
const fileUrl = result.data.data;
|
|
427
|
+
// with the fileUrl you can download the pdf
|
|
428
|
+
|
|
429
|
+
//if you want to link this pdf to an item, you need to add the fileName to the item property of type file
|
|
430
|
+
const result2 = await totalumClient.crud.editItemById('your_element_table_name', 'your_item_id', {'your_pdf_property_name': {name: fileName}});
|
|
431
|
+
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### get OCR of an image (get the text of an image)
|
|
435
|
+
|
|
436
|
+
```javascript
|
|
437
|
+
|
|
438
|
+
// you can only do ocr of a file that is previously uploaded to totalum (see the upload file function)
|
|
439
|
+
// then you can do ocr of the file using the file name of the file uploaded
|
|
440
|
+
const fileNameId = 'your_file_name_id.png'; // replace 'your_file_name' with the name of your file, replace .png with the extension of your image
|
|
441
|
+
const result = await totalumClient.files.ocrOfImage(fileNameId);
|
|
442
|
+
const ocrResult = result.data.data;
|
|
443
|
+
// ocrResult.text will contain all text of the image
|
|
444
|
+
// ocrResult.fullDetails will contain all details of the image, like the language, the position of the text, etc.
|
|
445
|
+
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### get OCR of a pdf (get the text of a pdf)
|
|
449
|
+
|
|
450
|
+
```javascript
|
|
451
|
+
|
|
452
|
+
// you can only do ocr of a file that is previously uploaded to totalum (see the upload file function)
|
|
453
|
+
// then you can do ocr of the file using the file name of the file uploaded
|
|
454
|
+
const fileName = 'your_file_name_id.pdf'; // replace 'your_file_name' with the name of your file
|
|
455
|
+
const result = await totalumClient.files.ocrOfPdf(fileName);
|
|
456
|
+
const ocrResult = result.data.data;
|
|
457
|
+
// ocrResult.text will contain all text of the pdf
|
|
458
|
+
// ocrResult.fullDetails will contain all details of the pdf, like the language, in which page is the text, the position of the text, etc.
|
|
459
|
+
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
## Functions for call OpenAI API without need to have an OpenAI account
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
### Create a completion (deprecated, use createChatCompletion instead)
|
|
467
|
+
|
|
468
|
+
```javascript
|
|
469
|
+
|
|
470
|
+
//body the openai completion body, more info here: https://platform.openai.com/docs/api-reference/completions/create
|
|
471
|
+
const body={
|
|
472
|
+
// see the openai api docs for more info
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const result = await totalumClient.openai.createCompletion(body);
|
|
476
|
+
const gptCompletion = result.data.data;
|
|
477
|
+
|
|
478
|
+
// returns the completion provided by openai api
|
|
479
|
+
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### Create a chat completion
|
|
483
|
+
|
|
484
|
+
```javascript
|
|
485
|
+
|
|
486
|
+
//body the openai chat completion body, more info here: https://platform.openai.com/docs/api-reference/chat/create
|
|
487
|
+
const bodyExample={
|
|
488
|
+
// see the openai api docs for more info
|
|
489
|
+
messages: [
|
|
490
|
+
{content: 'You are a math specialist assistant', role: 'system'},
|
|
491
|
+
{content: 'how I can resolve a matrix', role: 'user'}
|
|
492
|
+
//etc...
|
|
493
|
+
],
|
|
494
|
+
model: 'gpt-3.5-turbo',
|
|
495
|
+
max_tokens: 30,
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const result = await totalumClient.openai.createChatCompletion(bodyExample);
|
|
499
|
+
const chatCompletion = result.data.data;
|
|
500
|
+
// returns the completion provided by openai api
|
|
501
|
+
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
### Generate an image
|
|
505
|
+
|
|
506
|
+
```javascript
|
|
507
|
+
|
|
508
|
+
//body the openai chat completion body, more info here: https://platform.openai.com/docs/api-reference/chat/create
|
|
509
|
+
const bodyExample={
|
|
510
|
+
prompt:'your prompt',
|
|
511
|
+
size: '256x256' | '512x512' | '1024x1024',
|
|
512
|
+
fileName: 'file-name-of-file-saved.png' // CAUTION: if you use the same name for multiple images, the images will be overwritten
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
const result = await totalumClient.openai.generateImage(bodyExample);
|
|
516
|
+
const imageCompletion = result.data.data;
|
|
517
|
+
// returns the file name with the file generated by openai, if is not an url, you can download it using files.getDownloadUrl(imageCompletion);
|
|
518
|
+
|
|
519
|
+
```
|
|
520
|
+
|