flamerest 1.0.45 → 1.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/REST.d.ts +6 -6
  2. package/REST.js +46 -33
  3. package/package.json +1 -1
package/REST.d.ts CHANGED
@@ -29,7 +29,7 @@ export function get<T>(table: string, where?: object | string | null, expand?: o
29
29
  * @param {object} params
30
30
  * @returns
31
31
  */
32
- export function all<T>(table: string, params?: { where?: object, fields?: object | Array<string>, sort?: Array<string>, page?: number, perPage?: number }): Promise<Rows<T>>;
32
+ export function all<T>(table: string, params?: { where?: object, fields?: object | Array<string>, sort?: Array<string>, page?: number, perPage?: number, tree?: number }): Promise<Rows<T>>;
33
33
 
34
34
  /**
35
35
  * Стандартный ответ от Request с несколькими строками
@@ -39,8 +39,8 @@ type Rows<T> = {
39
39
  status: Number,
40
40
  ok: boolean,
41
41
  data?: Array<T>,
42
- errors: Object|undefined,
43
- message: string|undefined,
42
+ errors: Object | undefined,
43
+ message: string | undefined,
44
44
  pages: {
45
45
  page: Number,
46
46
  perPage: Number,
@@ -63,7 +63,7 @@ export function one<T>(table: string, IDOrWhere: number | string | object, field
63
63
  * @param table
64
64
  * @param values
65
65
  */
66
- export function create<T>(table: string, values: object): Promise<SavedObject<T>>
66
+ export function create<T>(table: string, values: object, appendTo: number | string | null = null, insertAfter: number | string | null = null, insertFirst: number | string | null = null): Promise<SavedObject<T>>
67
67
 
68
68
  /**
69
69
  * Удалить запись
@@ -71,7 +71,7 @@ export function create<T>(table: string, values: object): Promise<SavedObject<T>
71
71
  * @param id
72
72
  * @param byFields Если указан, удаляет по этим параметрам
73
73
  */
74
- export function remove(table: string, id: number | string, byFields?: object): Promise<boolean|Array<any>>
74
+ export function remove(table: string, id: number | string, byFields?: object): Promise<boolean | Array<any>>
75
75
 
76
76
  /**
77
77
  * Редактировать значения
@@ -79,7 +79,7 @@ export function remove(table: string, id: number | string, byFields?: object): P
79
79
  * @param ID
80
80
  * @param values
81
81
  */
82
- export function edit<T>(table: string, ID: number | string, values: object): Promise<SavedObject<T>>
82
+ export function edit<T>(table: string, ID: number | string, values: object, appendTo: number | string | null = null, insertAfter: number | string | null = null, insertFirst: number | string | null = null): Promise<SavedObject<T>>
83
83
 
84
84
 
85
85
  /**
package/REST.js CHANGED
@@ -97,7 +97,7 @@ class FLAMEREST {
97
97
  // Ошибка валидации: Собираем все ошибки полей
98
98
  case 422:
99
99
  let Errs = {};
100
- for(let err of ResolveBody.errors) {
100
+ for (let err of ResolveBody.errors) {
101
101
  Errs[err['field']] = Errs[err['field']] === undefined ? err['message'] : Errs[err['field']] + ". " + err['message']
102
102
  }
103
103
  ResolveBody.errors = Errs;
@@ -295,9 +295,10 @@ class FLAMEREST {
295
295
  * @param RemoveDuplicates
296
296
  * @param format
297
297
  * @param titles Это чтобы мы могли контроллить какие названия полей мы будет загружать при экспорте, чтобы они были как в таблице
298
+ * @param tree дерево
298
299
  * @return Promise<>
299
300
  */
300
- get(table, where, expand, fields, sortfields, page, perPage, RemoveDuplicates, format, titles) {
301
+ get(table, where, expand, fields, sortfields, page, perPage, RemoveDuplicates, format, titles, tree) {
301
302
 
302
303
  // Нормализуем имена таблиц
303
304
  table = table.replace(/_/g, "");
@@ -313,6 +314,10 @@ class FLAMEREST {
313
314
  if (where !== undefined && where !== null)
314
315
  json.where = where;
315
316
 
317
+ // Генерим условия
318
+ if (tree !== undefined && tree !== null)
319
+ json.tree = tree;
320
+
316
321
  if (fields !== undefined && fields !== null)
317
322
  json.fields = fields;
318
323
 
@@ -362,15 +367,15 @@ class FLAMEREST {
362
367
  * @returns {object|null}
363
368
  */
364
369
  async one(table, IDOrWhere, fields = null, primaryKeyName = 'id') {
365
-
370
+
366
371
  let where = {};
367
- if(typeof IDOrWhere === 'string' || typeof IDOrWhere === 'number') where = {[primaryKeyName]:id};
368
- else if(typeof IDOrWhere === 'object') where = IDOrWhere;
372
+ if (typeof IDOrWhere === 'string' || typeof IDOrWhere === 'number') where = { [primaryKeyName]: id };
373
+ else if (typeof IDOrWhere === 'object') where = IDOrWhere;
369
374
  else throw "Нужно передавать ID или объект";
370
375
 
371
- let resp = await this.get(table, where , null, fields, null, 1, 1);
372
- if(resp.errors) return resp;
373
- if(resp.data.length === 0) return null;
376
+ let resp = await this.get(table, where, null, fields, null, 1, 1);
377
+ if (resp.errors) return resp;
378
+ if (resp.data.length === 0) return null;
374
379
 
375
380
  return resp.data[0];
376
381
  }
@@ -380,7 +385,7 @@ class FLAMEREST {
380
385
  * @param table
381
386
  * @param values
382
387
  */
383
- async create(table, values) {
388
+ async create(table, values, appendTo = null, insertAfter = null, insertFirst = null) {
384
389
 
385
390
  // Нормализуем имена таблиц
386
391
  table = table.replace(/_/g, "");
@@ -388,7 +393,11 @@ class FLAMEREST {
388
393
  // Подготовить значения
389
394
  await this.prepare(values);
390
395
 
391
- return this.request(this.SERVER + '/api/' + this.version + '/' + table + '/create', JSON.stringify(values), 'POST');
396
+ return this.request(this.SERVER + '/api/' + this.version + '/' + table + '/create?'
397
+ + (appendTo ? '&appendTo=' + appendTo : '')
398
+ + (insertAfter ? '&insertAfter=' + insertAfter : '')
399
+ + (insertFirst ? '&insertFirst=' + insertFirst : '')
400
+ , JSON.stringify(values), 'POST');
392
401
 
393
402
  }
394
403
 
@@ -404,11 +413,11 @@ class FLAMEREST {
404
413
  table = table.replace(/_/g, "");
405
414
 
406
415
  let params = {};
407
- if(byFields instanceof Object) params = byFields;
408
-
416
+ if (byFields instanceof Object) params = byFields;
417
+
409
418
  let resp = await this.request(this.SERVER + '/api/' + this.version + '/' + table + '/delete?id=' + id, JSON.stringify(params), 'DELETE');
410
419
 
411
- if(resp.status === 204) return true;
420
+ if (resp.status === 204) return true;
412
421
 
413
422
  return resp;
414
423
 
@@ -420,7 +429,7 @@ class FLAMEREST {
420
429
  * @param ID
421
430
  * @param values
422
431
  */
423
- async edit(table, ID, values) {
432
+ async edit(table, ID, values, appendTo = null, insertAfter = null, insertFirst = null) {
424
433
 
425
434
  // Нормализуем имена таблиц
426
435
  table = table.replace(/_/g, "");
@@ -428,7 +437,11 @@ class FLAMEREST {
428
437
  // Подготовить значения
429
438
  await this.prepare(values);
430
439
 
431
- return this.request(this.SERVER + '/api/' + this.version + '/' + table + '/update?id=' + ID, JSON.stringify(values), 'PATCH');
440
+ return this.request(this.SERVER + '/api/' + this.version + '/' + table + '/update?id=' + ID
441
+ + (appendTo ? '&appendTo=' + appendTo : '')
442
+ + (insertAfter ? '&insertAfter=' + insertAfter : '')
443
+ + (insertFirst ? '&insertFirst=' + insertFirst : '')
444
+ , JSON.stringify(values), 'PATCH');
432
445
  }
433
446
 
434
447
  /**
@@ -450,9 +463,9 @@ class FLAMEREST {
450
463
  async auth(username, password) {
451
464
 
452
465
  let resp = await this.request(this.SERVER + '/auth/auth', JSON.stringify({ login: username, password: password }), 'POST');
453
-
454
- if(resp.errors) return resp;
455
- if(resp.data.length === 0) {resp.errors = []; return resp;}
466
+
467
+ if (resp.errors) return resp;
468
+ if (resp.data.length === 0) { resp.errors = []; return resp; }
456
469
 
457
470
  return resp.data;
458
471
 
@@ -461,9 +474,9 @@ class FLAMEREST {
461
474
  async signup(username, password) {
462
475
 
463
476
  let resp = await this.request(this.SERVER + '/auth/signup', JSON.stringify({ login: username, password: password }), 'POST');
464
-
465
- if(resp.errors) return resp;
466
- if(resp.data.length === 0) {resp.errors = []; return resp;}
477
+
478
+ if (resp.errors) return resp;
479
+ if (resp.data.length === 0) { resp.errors = []; return resp; }
467
480
 
468
481
  return resp.data;
469
482
 
@@ -482,20 +495,20 @@ class FLAMEREST {
482
495
 
483
496
  // Если в один из параметров передан FileList или input[type=file], т.е. нужно загрузить файлы
484
497
  for (let val in values) {
485
-
498
+
486
499
  // Преобразуем
487
500
  let value = values[val];
488
501
  let isRef = false;
489
- if (value instanceof Object
490
- && value.hasOwnProperty('_value')
502
+ if (value instanceof Object
503
+ && value.hasOwnProperty('_value')
491
504
  && (
492
- value._value instanceof Event ||
493
- value._value instanceof HTMLInputElement ||
494
- value._value instanceof ClipboardEvent ||
495
- value._value instanceof DataTransfer ||
505
+ value._value instanceof Event ||
506
+ value._value instanceof HTMLInputElement ||
507
+ value._value instanceof ClipboardEvent ||
508
+ value._value instanceof DataTransfer ||
496
509
  value._value instanceof FileList
497
- )
498
- ) {value = value.value; isRef = true}
510
+ )
511
+ ) { value = value.value; isRef = true }
499
512
 
500
513
  if (value instanceof Event && value.target instanceof HTMLInputElement && value.target.type === 'file') value = value.target.files;
501
514
  if (value instanceof HTMLInputElement && value.type === 'file') value = value.files;
@@ -529,13 +542,13 @@ class FLAMEREST {
529
542
  readFileAsync(file) {
530
543
  return new Promise((resolve, reject) => {
531
544
  let reader = new FileReader();
532
-
545
+
533
546
  reader.onloadend = () => {
534
547
  resolve(reader.result);
535
548
  };
536
-
549
+
537
550
  reader.onerror = reject;
538
-
551
+
539
552
  reader.readAsDataURL(file);
540
553
  })
541
554
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flamerest",
3
- "version": "1.0.45",
3
+ "version": "1.0.48",
4
4
  "description": "",
5
5
  "main": "REST.js",
6
6
  "typings": "./REST.d.ts",