flamerest 1.0.21 → 1.0.24

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 CHANGED
@@ -17,3 +17,7 @@ or
17
17
  import FLAMEREST from "flamerest";
18
18
  window.REST = new FLAMEREST('localhost');
19
19
  ```
20
+
21
+ # Create and Update
22
+
23
+ Just field list, but any field can be HTML Input, or FileList (automatically restify that)
package/REST.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @param {string} type Тип
6
6
  * @param {string} responseType Тип ответа: json или blob
7
7
  */
8
- export function request(url: string, params: object | string, type: string = 'GET', responseType: string = 'json'): object
8
+ export function request(url: string, params: object | string, type: string, responseType: string): object
9
9
 
10
10
  /**
11
11
  * Получить выборку из таблицы через REST
@@ -21,7 +21,7 @@ export function request(url: string, params: object | string, type: string = 'GE
21
21
  * @param titles Это чтобы мы могли контроллить какие названия полей мы будет загружать при экспорте, чтобы они были как в таблице
22
22
  * @return Promise<object>
23
23
  */
24
- export function get(table: string, where: object | string | null, expand: object | string | null, fields: object | Array | string | null, sortfields: object | Array | string | null, page: number, perPage: number, RemoveDuplicates, format, titles): object
24
+ export function get(table: string, where: object | string | null, expand: object | string | null, fields: object | Array<string> | string | null, sortfields: object | Array<string> | string | null, page: number, perPage: number, RemoveDuplicates, format, titles): object
25
25
 
26
26
  /**
27
27
  * Получить все записи по запросу [постранично]
@@ -29,7 +29,7 @@ export function get(table: string, where: object | string | null, expand: object
29
29
  * @param {object} params
30
30
  * @returns
31
31
  */
32
- export function all(table: string, params: {where: object, fields: object|Array, sort: Array, page: number, perPage: number}): object;
32
+ export function all(table: string, params: {where: object, fields: object|Array<string>, sort: Array<string>, page: number, perPage: number}): object;
33
33
 
34
34
  /**
35
35
  * Получить одну запись по её ID
@@ -38,7 +38,7 @@ export function all(table: string, params: {where: object, fields: object|Array,
38
38
  * @param fields
39
39
  * @param primaryKeyName
40
40
  */
41
- export function one(table: string, id: number|string, fields: object|Array, primaryKeyName: string): object;
41
+ export function one(table: string, id: number|string, fields: object|Array<string>, primaryKeyName: string): object;
42
42
 
43
43
  /**
44
44
  * Создать новую запись
package/REST.js CHANGED
@@ -368,23 +368,27 @@ class FLAMEREST {
368
368
  * @param table
369
369
  * @param values
370
370
  */
371
- create(table, values) {
371
+ async create(table, values) {
372
372
 
373
373
  // Нормализуем имена таблиц
374
374
  table = table.replace(/_/g, "");
375
375
 
376
- // Если в один из параметров передан FileList, т.е. нужно загрузить файлы
377
- for (let val in values)
378
- if (val instanceof FileList) {
376
+ // Если в один из параметров передан FileList или input[type=file], т.е. нужно загрузить файлы
377
+ for (let val in values) {
378
+ // Преобразуем
379
+ if(values[val] instanceof HTMLInputElement && values[val].type === 'file') values[val] = values[val].files;
380
+ if (values[val] instanceof FileList) {
379
381
  let newValues = [];
380
- for (let file in val) {
382
+ values[val] = Array.from(values[val]);
383
+ for (let file in values[val]) {
381
384
  newValues.push({
382
- 'mime': 'jpg',
383
- 'size': 23,
384
- 'data': btoa(val)
385
+ 'name': values[val][file].name,
386
+ 'data': await this.readFileAsync(values[val][file])
385
387
  });
386
388
  }
389
+ values[val] = newValues;
387
390
  }
391
+ }
388
392
 
389
393
  return this.request(this.SERVER + '/api/' + table + '/create', JSON.stringify(values), 'POST');
390
394
 
@@ -410,11 +414,28 @@ class FLAMEREST {
410
414
  * @param ID
411
415
  * @param values
412
416
  */
413
- edit(table, ID, values) {
417
+ async edit(table, ID, values) {
414
418
 
415
419
  // Нормализуем имена таблиц
416
420
  table = table.replace(/_/g, "");
417
421
 
422
+ // Если в один из параметров передан FileList или input[type=file], т.е. нужно загрузить файлы
423
+ for (let val in values) {
424
+ // Преобразуем
425
+ if(values[val] instanceof HTMLInputElement && values[val].type === 'file') values[val] = values[val].files;
426
+ if (values[val] instanceof FileList) {
427
+ let newValues = [];
428
+ values[val] = Array.from(values[val]);
429
+ for (let file in values[val]) {
430
+ newValues.push({
431
+ 'name': values[val][file].name,
432
+ 'data': await this.readFileAsync(values[val][file])
433
+ });
434
+ }
435
+ values[val] = newValues;
436
+ }
437
+ }
438
+
418
439
  return this.request(this.SERVER + '/api/' + table + '/update?id=' + ID, JSON.stringify(values), 'PATCH');
419
440
  }
420
441
 
@@ -446,6 +467,25 @@ class FLAMEREST {
446
467
  return this.request(this.SERVER + '/auth/logout', '{}', 'POST');
447
468
  }
448
469
 
470
+ /**
471
+ * Прочесть файл асинхронно
472
+ * @param {File} file
473
+ * @returns {Promise<string>}
474
+ */
475
+ readFileAsync(file) {
476
+ return new Promise((resolve, reject) => {
477
+ let reader = new FileReader();
478
+
479
+ reader.onloadend = () => {
480
+ resolve(reader.result);
481
+ };
482
+
483
+ reader.onerror = reject;
484
+
485
+ reader.readAsDataURL(file);
486
+ })
487
+ }
488
+
449
489
  }
450
490
 
451
491
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flamerest",
3
- "version": "1.0.21",
3
+ "version": "1.0.24",
4
4
  "description": "",
5
5
  "main": "REST.js",
6
6
  "typings": "./REST.d.ts",