flamerest 1.0.46 → 1.0.49

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 +5 -5
  2. package/REST.js +54 -33
  3. package/package.json +1 -1
package/REST.d.ts CHANGED
@@ -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,15 @@ 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
+ if (appendTo === undefined) appendTo = null;
397
+ if (insertAfter === undefined) insertAfter = null;
398
+ if (insertFirst === undefined) insertFirst = null;
399
+
400
+ return this.request(this.SERVER + '/api/' + this.version + '/' + table + '/create?'
401
+ + (appendTo !== null ? '&appendTo=' + appendTo : '')
402
+ + (insertAfter !== null ? '&insertAfter=' + insertAfter : '')
403
+ + (insertFirst !== null ? '&insertFirst=' + insertFirst : '')
404
+ , JSON.stringify(values), 'POST');
392
405
 
393
406
  }
394
407
 
@@ -404,11 +417,11 @@ class FLAMEREST {
404
417
  table = table.replace(/_/g, "");
405
418
 
406
419
  let params = {};
407
- if(byFields instanceof Object) params = byFields;
408
-
420
+ if (byFields instanceof Object) params = byFields;
421
+
409
422
  let resp = await this.request(this.SERVER + '/api/' + this.version + '/' + table + '/delete?id=' + id, JSON.stringify(params), 'DELETE');
410
423
 
411
- if(resp.status === 204) return true;
424
+ if (resp.status === 204) return true;
412
425
 
413
426
  return resp;
414
427
 
@@ -420,7 +433,7 @@ class FLAMEREST {
420
433
  * @param ID
421
434
  * @param values
422
435
  */
423
- async edit(table, ID, values) {
436
+ async edit(table, ID, values, appendTo = null, insertAfter = null, insertFirst = null) {
424
437
 
425
438
  // Нормализуем имена таблиц
426
439
  table = table.replace(/_/g, "");
@@ -428,7 +441,15 @@ class FLAMEREST {
428
441
  // Подготовить значения
429
442
  await this.prepare(values);
430
443
 
431
- return this.request(this.SERVER + '/api/' + this.version + '/' + table + '/update?id=' + ID, JSON.stringify(values), 'PATCH');
444
+ if(appendTo === undefined) appendTo = null;
445
+ if (insertAfter === undefined) insertAfter = null;
446
+ if (insertFirst === undefined) insertFirst = null;
447
+
448
+ return this.request(this.SERVER + '/api/' + this.version + '/' + table + '/update?id=' + ID
449
+ + (appendTo!==null ? '&appendTo=' + appendTo : '')
450
+ + (insertAfter!==null ? '&insertAfter=' + insertAfter : '')
451
+ + (insertFirst!==null ? '&insertFirst=' + insertFirst : '')
452
+ , JSON.stringify(values), 'PATCH');
432
453
  }
433
454
 
434
455
  /**
@@ -450,9 +471,9 @@ class FLAMEREST {
450
471
  async auth(username, password) {
451
472
 
452
473
  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;}
474
+
475
+ if (resp.errors) return resp;
476
+ if (resp.data.length === 0) { resp.errors = []; return resp; }
456
477
 
457
478
  return resp.data;
458
479
 
@@ -461,9 +482,9 @@ class FLAMEREST {
461
482
  async signup(username, password) {
462
483
 
463
484
  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;}
485
+
486
+ if (resp.errors) return resp;
487
+ if (resp.data.length === 0) { resp.errors = []; return resp; }
467
488
 
468
489
  return resp.data;
469
490
 
@@ -482,20 +503,20 @@ class FLAMEREST {
482
503
 
483
504
  // Если в один из параметров передан FileList или input[type=file], т.е. нужно загрузить файлы
484
505
  for (let val in values) {
485
-
506
+
486
507
  // Преобразуем
487
508
  let value = values[val];
488
509
  let isRef = false;
489
- if (value instanceof Object
490
- && value.hasOwnProperty('_value')
510
+ if (value instanceof Object
511
+ && value.hasOwnProperty('_value')
491
512
  && (
492
- value._value instanceof Event ||
493
- value._value instanceof HTMLInputElement ||
494
- value._value instanceof ClipboardEvent ||
495
- value._value instanceof DataTransfer ||
513
+ value._value instanceof Event ||
514
+ value._value instanceof HTMLInputElement ||
515
+ value._value instanceof ClipboardEvent ||
516
+ value._value instanceof DataTransfer ||
496
517
  value._value instanceof FileList
497
- )
498
- ) {value = value.value; isRef = true}
518
+ )
519
+ ) { value = value.value; isRef = true }
499
520
 
500
521
  if (value instanceof Event && value.target instanceof HTMLInputElement && value.target.type === 'file') value = value.target.files;
501
522
  if (value instanceof HTMLInputElement && value.type === 'file') value = value.files;
@@ -529,13 +550,13 @@ class FLAMEREST {
529
550
  readFileAsync(file) {
530
551
  return new Promise((resolve, reject) => {
531
552
  let reader = new FileReader();
532
-
553
+
533
554
  reader.onloadend = () => {
534
555
  resolve(reader.result);
535
556
  };
536
-
557
+
537
558
  reader.onerror = reject;
538
-
559
+
539
560
  reader.readAsDataURL(file);
540
561
  })
541
562
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flamerest",
3
- "version": "1.0.46",
3
+ "version": "1.0.49",
4
4
  "description": "",
5
5
  "main": "REST.js",
6
6
  "typings": "./REST.d.ts",