bkper-js 1.7.0 → 1.8.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/lib/index.d.ts CHANGED
@@ -408,10 +408,11 @@ export declare class Bkper {
408
408
  * Gets the [[Book]] with the specified bookId from url param.
409
409
  *
410
410
  * @param id - The universal book id - The same bookId param of URL you access at app.bkper.com
411
+ * @param includeAccounts - Optional parameter to include accounts in the retrieved Book
411
412
  *
412
413
  * @returns The retrieved Book, for chaining
413
414
  */
414
- static getBook(id: string): Promise<Book>;
415
+ static getBook(id: string, includeAccounts?: boolean): Promise<Book>;
415
416
  /**
416
417
  * Gets all [[Books]] the user has access to.
417
418
  *
@@ -729,21 +730,13 @@ export declare class Book {
729
730
  * @returns The updated Integration object
730
731
  */
731
732
  updateIntegration(integration: bkper.Integration): Promise<Integration>;
732
- /**
733
- * Resumes a transaction iteration using a continuation token from a previous iterator.
734
- *
735
- * @param continuationToken - continuation token from a previous transaction iterator
736
- *
737
- * @returns a collection of transactions that remained in a previous iterator when the continuation token was generated
738
- */
739
- continueTransactionIterator(query: string, continuationToken: string): TransactionIterator;
740
733
  /**
741
734
  * Instantiate a new [[Transaction]]
742
735
  *
743
736
  * Example:
744
737
  *
745
738
  * ```js
746
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
739
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
747
740
  *
748
741
  * book.newTransaction()
749
742
  * .setDate('2013-01-25')
@@ -762,7 +755,7 @@ export declare class Book {
762
755
  *
763
756
  * Example:
764
757
  * ```js
765
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
758
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
766
759
  *
767
760
  * book.newAccount()
768
761
  * .setName('Some New Account')
@@ -778,7 +771,7 @@ export declare class Book {
778
771
  *
779
772
  * Example:
780
773
  * ```js
781
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
774
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
782
775
  *
783
776
  * book.newGroup()
784
777
  * .setName('Some New Group')
@@ -796,7 +789,9 @@ export declare class Book {
796
789
  */
797
790
  getAccount(idOrName?: string): Promise<Account | undefined>;
798
791
 
799
- removeGroupCache(group: Group): void;
792
+
793
+
794
+
800
795
  /**
801
796
  * Gets a [[Group]] object
802
797
  *
@@ -809,33 +804,19 @@ export declare class Book {
809
804
  * Gets all [[Groups]] of this Book
810
805
  */
811
806
  getGroups(): Promise<Group[]>;
807
+ private mapGroups;
808
+ getAccounts(): Promise<Account[]>;
809
+ private mapAccounts;
812
810
  /**
813
- * Get the [[Groups]] of a given account.
814
- */
815
- getGroupsByAccount(accountIdOrName: string): Promise<Group[]>;
816
- /**
817
- * Get Book transactions based on a query.
818
- *
819
- * See [Query Guide](https://help.bkper.com/en/articles/2569178-search-query-guide) to learn more
820
- *
821
- * @param query - The query string.
811
+ * Lists transactions in the Book based on the provided query, limit, and cursor, for pagination.
822
812
  *
823
- * @returns The Transactions result as an iterator.
813
+ * @param query - The query string to filter transactions
814
+ * @param limit - The maximum number of transactions to return. Default to 100, max to 1000;
815
+ * @param cursor - The cursor for pagination
824
816
  *
825
- * Example:
826
- *
827
- * ```js
828
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
829
- *
830
- * var transactions = book.getTransactions("account:CreditCard after:28/01/2013 before:29/01/2013");
831
- *
832
- * while (transactions.hasNext()) {
833
- * var transaction = transactions.next();
834
- * Logger.log(transaction.getDescription());
835
- * }
836
- * ```
817
+ * @returns A TransactionPage object containing the list of transactions
837
818
  */
838
- getTransactions(query?: string): TransactionIterator;
819
+ listTransactions(query?: string, limit?: number, cursor?: string): Promise<TransactionPage>;
839
820
  /**
840
821
  * Retrieve a transaction by id
841
822
  */
@@ -845,7 +826,7 @@ export declare class Book {
845
826
  *
846
827
  * Example:
847
828
  * ```js
848
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
829
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
849
830
  *
850
831
  * book.newFile()
851
832
  * .setBlob(UrlFetchApp.fetch('https://bkper.com/images/index/integrations4.png').getBlob())
@@ -1545,6 +1526,10 @@ export declare class Transaction {
1545
1526
  * @returns The wrapped plain json object
1546
1527
  */
1547
1528
  json(): bkper.Transaction;
1529
+ /**
1530
+ * @returns The book of the Transaction.
1531
+ */
1532
+ getBook(): Book;
1548
1533
  /**
1549
1534
  * @returns The id of the Transaction.
1550
1535
  */
@@ -1862,59 +1847,37 @@ export declare class Transaction {
1862
1847
  }
1863
1848
 
1864
1849
  /**
1865
- *
1866
- * An iterator that allows scripts to iterate over a potentially large collection of transactions.
1867
- *
1868
- * Example:
1869
- *
1870
- * ```js
1871
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
1872
- *
1873
- * var transactionIterator = book.getTransactions("account:CreditCard after:28/01/2013 before:29/01/2013");
1874
- *
1875
- * while (transactionIterator.hasNext()) {
1876
- * var transaction = transactions.next();
1877
- * Logger.log(transaction.getDescription());
1878
- * }
1879
- * ```
1880
- *
1881
- * @public
1850
+ * A list associated with a transaction query.
1882
1851
  */
1883
- export declare class TransactionIterator {
1884
-
1885
-
1886
-
1887
-
1888
-
1889
-
1890
- /**
1891
- * Gets the Book that originate the iterator
1892
- */
1893
- getBook(): Book;
1852
+ export declare class TransactionPage {
1853
+ private book;
1854
+ private wrapped;
1855
+ constructor(book: Book, transactionList: bkper.TransactionList);
1894
1856
  /**
1895
- * Gets a token that can be used to resume this iteration at a later time.
1896
- *
1897
- * This method is useful if processing an iterator in one execution would exceed the maximum execution time.
1898
- *
1899
- * Continuation tokens are generally valid short period of time.
1857
+ * @returns The cursor associated with the query for pagination.
1900
1858
  */
1901
- getContinuationToken(): string | undefined;
1859
+ getCursor(): string | undefined;
1902
1860
  /**
1903
- * Sets a continuation token from previous paused iteration
1861
+ * Retrieves the account associated with the query, when filtering by account.
1904
1862
  */
1905
- setContinuationToken(continuationToken: string): Promise<void>;
1863
+ getAccount(): Promise<Account | undefined>;
1906
1864
  /**
1907
- * Determines whether calling next() will return a transaction.
1865
+ * @returns The first Transaction in the list.
1908
1866
  */
1909
- hasNext(): Promise<boolean>;
1867
+ getFirst(): Transaction | undefined;
1910
1868
  /**
1911
- * Gets the next transaction in the collection of transactions.
1869
+ *
1870
+ * Get the total number of transactions in the list.
1871
+ *
1872
+ * @returns The total number of transactions.
1912
1873
  */
1913
- next(): Promise<Transaction | undefined>;
1874
+ size(): number;
1914
1875
  /**
1915
- * @returns The account, when filtering by a single account.
1876
+ * Get the transactions in the list.
1877
+ *
1878
+ * @returns An array of Transaction objects.
1916
1879
  */
1917
- getAccount(): Promise<Account | undefined>;
1880
+ getItems(): Transaction[];
1918
1881
  }
1919
1882
 
1920
1883
  /**
package/lib/index.js CHANGED
@@ -17,7 +17,7 @@ export { Group } from './model/Group.js';
17
17
  export { Integration } from './model/Integration.js';
18
18
  export { Template } from './model/Template.js';
19
19
  export { Transaction } from './model/Transaction.js';
20
- export { TransactionIterator } from './model/TransactionIterator.js';
20
+ export { TransactionList as TransactionPage } from './model/TransactionList.js';
21
21
  export { User } from './model/User.js';
22
22
  export { Periodicity, DecimalSeparator, Permission, Visibility, AccountType, Period, Month } from './model/Enums.js';
23
23
  //# sourceMappingURL=index.js.map
@@ -54,12 +54,13 @@ export class Bkper {
54
54
  * Gets the [[Book]] with the specified bookId from url param.
55
55
  *
56
56
  * @param id - The universal book id - The same bookId param of URL you access at app.bkper.com
57
+ * @param includeAccounts - Optional parameter to include accounts in the retrieved Book
57
58
  *
58
59
  * @returns The retrieved Book, for chaining
59
60
  */
60
- static getBook(id) {
61
+ static getBook(id, includeAccounts) {
61
62
  return __awaiter(this, void 0, void 0, function* () {
62
- let book = yield BookService.loadBook(id);
63
+ let book = yield BookService.loadBook(id, includeAccounts);
63
64
  return new Book(book);
64
65
  });
65
66
  }
package/lib/model/Book.js CHANGED
@@ -14,14 +14,13 @@ import * as FileService from '../service/file-service.js';
14
14
  import * as TransactionService from '../service/transaction-service.js';
15
15
  import * as IntegrationService from '../service/integration-service.js';
16
16
  import * as Utils from '../utils.js';
17
- import { normalizeName } from '../utils.js';
18
17
  import { Account } from './Account.js';
19
18
  import { Collection } from './Collection.js';
20
19
  import { File } from './File.js';
21
20
  import { Group } from './Group.js';
22
21
  import { Transaction } from './Transaction.js';
23
- import { TransactionIterator } from './TransactionIterator.js';
24
22
  import { Integration } from './Integration.js';
23
+ import { TransactionList } from './TransactionList.js';
25
24
  /**
26
25
  *
27
26
  * A Book represents [General Ledger](https://en.wikipedia.org/wiki/General_ledger) for a company or business, but can also represent a [Ledger](https://en.wikipedia.org/wiki/Ledger) for a project or department
@@ -33,6 +32,8 @@ import { Integration } from './Integration.js';
33
32
  export class Book {
34
33
  constructor(json) {
35
34
  this.wrapped = json || {};
35
+ this.mapGroups();
36
+ this.mapAccounts();
36
37
  }
37
38
  /**
38
39
  * @returns The wrapped plain json object
@@ -452,25 +453,13 @@ export class Book {
452
453
  return new Integration(integration);
453
454
  });
454
455
  }
455
- /**
456
- * Resumes a transaction iteration using a continuation token from a previous iterator.
457
- *
458
- * @param continuationToken - continuation token from a previous transaction iterator
459
- *
460
- * @returns a collection of transactions that remained in a previous iterator when the continuation token was generated
461
- */
462
- continueTransactionIterator(query, continuationToken) {
463
- var transactionIterator = new TransactionIterator(this, query);
464
- transactionIterator.setContinuationToken(continuationToken);
465
- return transactionIterator;
466
- }
467
456
  /**
468
457
  * Instantiate a new [[Transaction]]
469
458
  *
470
459
  * Example:
471
460
  *
472
461
  * ```js
473
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
462
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
474
463
  *
475
464
  * book.newTransaction()
476
465
  * .setDate('2013-01-25')
@@ -492,7 +481,7 @@ export class Book {
492
481
  *
493
482
  * Example:
494
483
  * ```js
495
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
484
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
496
485
  *
497
486
  * book.newAccount()
498
487
  * .setName('Some New Account')
@@ -512,7 +501,7 @@ export class Book {
512
501
  *
513
502
  * Example:
514
503
  * ```js
515
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
504
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
516
505
  *
517
506
  * book.newGroup()
518
507
  * .setName('Some New Group')
@@ -533,16 +522,22 @@ export class Book {
533
522
  */
534
523
  getAccount(idOrName) {
535
524
  return __awaiter(this, void 0, void 0, function* () {
536
- if (!idOrName) {
525
+ if (!idOrName || idOrName.trim() == '') {
537
526
  return undefined;
538
527
  }
539
- idOrName = idOrName + '';
528
+ let account;
529
+ if (this.idAccountMap) {
530
+ account = this.idAccountMap.get(idOrName);
531
+ if (account) {
532
+ return account;
533
+ }
534
+ }
540
535
  const accountPlain = yield AccountService.getAccount(this.getId(), idOrName);
541
- if (!accountPlain) {
542
- return undefined;
536
+ if (accountPlain) {
537
+ account = new Account(this, accountPlain);
538
+ return account;
543
539
  }
544
- const account = new Account(this, accountPlain);
545
- return account;
540
+ return undefined;
546
541
  });
547
542
  }
548
543
  /** @internal */
@@ -553,16 +548,26 @@ export class Book {
553
548
  this.idGroupMap.set(id, group);
554
549
  }
555
550
  }
556
- if (this.nameGroupMap) {
557
- this.nameGroupMap.set(normalizeName(group.getName()), group);
558
- }
559
551
  }
552
+ /** @internal */
560
553
  removeGroupCache(group) {
561
554
  if (this.idGroupMap) {
562
555
  this.idGroupMap.delete(group.getId() || '');
563
556
  }
564
- if (this.nameGroupMap) {
565
- this.nameGroupMap.delete(normalizeName(group.getName()));
557
+ }
558
+ /** @internal */
559
+ updateAccountCache(account) {
560
+ if (this.idAccountMap) {
561
+ const id = account.getId();
562
+ if (id) {
563
+ this.idAccountMap.set(id, account);
564
+ }
565
+ }
566
+ }
567
+ /** @internal */
568
+ removeAccountCache(account) {
569
+ if (this.idAccountMap) {
570
+ this.idAccountMap.delete(account.getId() || '');
566
571
  }
567
572
  }
568
573
  /**
@@ -580,9 +585,6 @@ export class Book {
580
585
  idOrName = idOrName + '';
581
586
  if (this.idGroupMap) {
582
587
  let group = this.idGroupMap.get(idOrName);
583
- if (!group && this.nameGroupMap) {
584
- group = this.nameGroupMap.get(normalizeName(idOrName));
585
- }
586
588
  if (group) {
587
589
  return group;
588
590
  }
@@ -605,50 +607,54 @@ export class Book {
605
607
  return Array.from(this.idGroupMap.values());
606
608
  }
607
609
  let groups = yield GroupService.getGroups(this.getId());
608
- let groupsObj = groups.map(group => new Group(this, group));
609
- this.idGroupMap = new Map();
610
- this.nameGroupMap = new Map();
611
- for (var i = 0; i < groupsObj.length; i++) {
612
- var group = groupsObj[i];
613
- this.updateGroupCache(group);
614
- }
615
- return groupsObj;
610
+ return this.mapGroups(groups);
616
611
  });
617
612
  }
618
- /**
619
- * Get the [[Groups]] of a given account.
620
- */
621
- getGroupsByAccount(accountIdOrName) {
613
+ mapGroups(groups) {
614
+ if (!groups) {
615
+ return [];
616
+ }
617
+ let groupsObj = groups.map(group => new Group(this, group));
618
+ this.idGroupMap = new Map();
619
+ for (const group of groupsObj) {
620
+ this.updateGroupCache(group);
621
+ }
622
+ return groupsObj;
623
+ }
624
+ getAccounts() {
622
625
  return __awaiter(this, void 0, void 0, function* () {
623
- let groups = yield GroupService.getGroupsByAccountId(this.getId(), accountIdOrName);
624
- let groupsObj = groups.map(group => new Group(this, group));
625
- return groupsObj;
626
+ if (this.idAccountMap) {
627
+ return Array.from(this.idAccountMap.values());
628
+ }
629
+ let accounts = yield AccountService.getAccounts(this.getId());
630
+ return this.mapAccounts(accounts);
626
631
  });
627
632
  }
633
+ mapAccounts(accounts) {
634
+ if (!accounts) {
635
+ return [];
636
+ }
637
+ let accountsObj = accounts.map(account => new Account(this, account));
638
+ this.idAccountMap = new Map();
639
+ for (const account of accountsObj) {
640
+ this.updateAccountCache(account);
641
+ }
642
+ return accountsObj;
643
+ }
628
644
  /**
629
- * Get Book transactions based on a query.
645
+ * Lists transactions in the Book based on the provided query, limit, and cursor, for pagination.
630
646
  *
631
- * See [Query Guide](https://help.bkper.com/en/articles/2569178-search-query-guide) to learn more
647
+ * @param query - The query string to filter transactions
648
+ * @param limit - The maximum number of transactions to return. Default to 100, max to 1000;
649
+ * @param cursor - The cursor for pagination
632
650
  *
633
- * @param query - The query string.
634
- *
635
- * @returns The Transactions result as an iterator.
636
- *
637
- * Example:
638
- *
639
- * ```js
640
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
641
- *
642
- * var transactions = book.getTransactions("account:CreditCard after:28/01/2013 before:29/01/2013");
643
- *
644
- * while (transactions.hasNext()) {
645
- * var transaction = transactions.next();
646
- * Logger.log(transaction.getDescription());
647
- * }
648
- * ```
651
+ * @returns A TransactionPage object containing the list of transactions
649
652
  */
650
- getTransactions(query) {
651
- return new TransactionIterator(this, query);
653
+ listTransactions(query, limit, cursor) {
654
+ return __awaiter(this, void 0, void 0, function* () {
655
+ const transactionsList = yield TransactionService.listTransactions(this.getId(), query, limit, cursor);
656
+ return new TransactionList(this, transactionsList);
657
+ });
652
658
  }
653
659
  /**
654
660
  * Retrieve a transaction by id
@@ -668,7 +674,7 @@ export class Book {
668
674
  *
669
675
  * Example:
670
676
  * ```js
671
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
677
+ * var book = Bkper.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
672
678
  *
673
679
  * book.newFile()
674
680
  * .setBlob(UrlFetchApp.fetch('https://bkper.com/images/index/integrations4.png').getBlob())
@@ -32,6 +32,12 @@ export class Transaction {
32
32
  json() {
33
33
  return this.wrapped;
34
34
  }
35
+ /**
36
+ * @returns The book of the Transaction.
37
+ */
38
+ getBook() {
39
+ return this.book;
40
+ }
35
41
  /**
36
42
  * @returns The id of the Transaction.
37
43
  */
@@ -0,0 +1,67 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { Transaction } from "./Transaction.js";
11
+ /**
12
+ * A list associated with a transaction query.
13
+ */
14
+ export class TransactionList {
15
+ constructor(book, transactionList) {
16
+ this.book = book;
17
+ this.wrapped = transactionList;
18
+ }
19
+ /**
20
+ * @returns The cursor associated with the query for pagination.
21
+ */
22
+ getCursor() {
23
+ return this.wrapped.cursor;
24
+ }
25
+ /**
26
+ * Retrieves the account associated with the query, when filtering by account.
27
+ */
28
+ getAccount() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ if (!this.wrapped.account) {
31
+ return undefined;
32
+ }
33
+ return yield this.book.getAccount(this.wrapped.account);
34
+ });
35
+ }
36
+ /**
37
+ * @returns The first Transaction in the list.
38
+ */
39
+ getFirst() {
40
+ const transactions = this.getItems();
41
+ return transactions.length > 0 ? transactions[0] : undefined;
42
+ }
43
+ /**
44
+ *
45
+ * Get the total number of transactions in the list.
46
+ *
47
+ * @returns The total number of transactions.
48
+ */
49
+ size() {
50
+ var _a;
51
+ return ((_a = this.wrapped.items) === null || _a === void 0 ? void 0 : _a.length) || 0;
52
+ }
53
+ /**
54
+ * Get the transactions in the list.
55
+ *
56
+ * @returns An array of Transaction objects.
57
+ */
58
+ getItems() {
59
+ var _a;
60
+ let transactions = [];
61
+ for (let transaction of (_a = this.wrapped.items) !== null && _a !== void 0 ? _a : []) {
62
+ transactions.push(new Transaction(this.book, transaction));
63
+ }
64
+ return transactions;
65
+ }
66
+ }
67
+ //# sourceMappingURL=TransactionList.js.map
@@ -33,4 +33,10 @@ export function getAccount(bookId, idOrName) {
33
33
  return response.data;
34
34
  });
35
35
  }
36
+ export function getAccounts(bookId) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ var response = yield new HttpBooksApiV5Request(`${bookId}/accounts`).setMethod('GET').fetch();
39
+ return response.data;
40
+ });
41
+ }
36
42
  //# sourceMappingURL=account-service.js.map
@@ -22,12 +22,13 @@ export function loadBooks() {
22
22
  return booksJson;
23
23
  });
24
24
  }
25
- export function loadBook(bookId) {
25
+ export function loadBook(bookId, loadAccounts) {
26
26
  return __awaiter(this, void 0, void 0, function* () {
27
27
  if (bookId == null) {
28
28
  throw new Error("Book id null!");
29
29
  }
30
- let response = yield new HttpBooksApiV5Request(bookId).fetch();
30
+ loadAccounts = loadAccounts || false;
31
+ let response = yield new HttpBooksApiV5Request(bookId).addParam('loadAccounts', loadAccounts).fetch();
31
32
  return response.data;
32
33
  });
33
34
  }
@@ -83,7 +83,7 @@ export function getTransaction(bookId, id) {
83
83
  return response.data;
84
84
  });
85
85
  }
86
- export function searchTransactions(bookId, query, limit, cursor) {
86
+ export function listTransactions(bookId, query, limit, cursor) {
87
87
  return __awaiter(this, void 0, void 0, function* () {
88
88
  if (!query) {
89
89
  query = "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",
@@ -1,150 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import { TransactionPage } from "./TransactionPage.js";
11
- /**
12
- *
13
- * An iterator that allows scripts to iterate over a potentially large collection of transactions.
14
- *
15
- * Example:
16
- *
17
- * ```js
18
- * var book = BkperApp.getBook("agtzfmJrcGVyLWhyZHITCxIGTGVkZ2VyGICAgIDggqALDA");
19
- *
20
- * var transactionIterator = book.getTransactions("account:CreditCard after:28/01/2013 before:29/01/2013");
21
- *
22
- * while (transactionIterator.hasNext()) {
23
- * var transaction = transactions.next();
24
- * Logger.log(transaction.getDescription());
25
- * }
26
- * ```
27
- *
28
- * @public
29
- */
30
- export class TransactionIterator {
31
- /** @internal */
32
- constructor(book, query) {
33
- this.book = book;
34
- this.query = query;
35
- if (this.query == null) {
36
- this.query = "";
37
- }
38
- this.currentPage = undefined;
39
- this.nextPage = undefined;
40
- this.lastCursor = undefined;
41
- }
42
- /**
43
- * Gets the Book that originate the iterator
44
- */
45
- getBook() {
46
- return this.book;
47
- }
48
- /**
49
- * Gets a token that can be used to resume this iteration at a later time.
50
- *
51
- * This method is useful if processing an iterator in one execution would exceed the maximum execution time.
52
- *
53
- * Continuation tokens are generally valid short period of time.
54
- */
55
- getContinuationToken() {
56
- if (this.currentPage == null) {
57
- return undefined;
58
- }
59
- var cursor = this.lastCursor;
60
- if (cursor == null) {
61
- cursor = "null";
62
- }
63
- var continuationToken = cursor + "_bkperpageindex_" + this.currentPage.getIndex();
64
- return continuationToken;
65
- }
66
- /**
67
- * Sets a continuation token from previous paused iteration
68
- */
69
- setContinuationToken(continuationToken) {
70
- return __awaiter(this, void 0, void 0, function* () {
71
- if (continuationToken == null) {
72
- return;
73
- }
74
- var cursorIndexArray = continuationToken.split("_bkperpageindex_");
75
- if (cursorIndexArray.length != 2) {
76
- return;
77
- }
78
- var cursor = cursorIndexArray[0];
79
- var index = cursorIndexArray[1];
80
- if ("null" != cursor) {
81
- this.lastCursor = cursor;
82
- }
83
- let indexNum = new Number(index).valueOf();
84
- this.currentPage = yield new TransactionPage().init(this.book, this.query, this.lastCursor);
85
- this.currentPage.setIndex(indexNum);
86
- });
87
- }
88
- /**
89
- * Determines whether calling next() will return a transaction.
90
- */
91
- hasNext() {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- if (this.currentPage == null) {
94
- this.currentPage = yield new TransactionPage().init(this.book, this.query, this.lastCursor);
95
- }
96
- if (this.currentPage.hasNext()) {
97
- return true;
98
- }
99
- else if (!this.currentPage.hasReachEnd()) {
100
- this.lastCursor = this.currentPage.getCursor();
101
- if (this.nextPage == null) {
102
- this.nextPage = yield new TransactionPage().init(this.book, this.query, this.lastCursor);
103
- }
104
- return this.nextPage.hasNext();
105
- }
106
- else {
107
- return false;
108
- }
109
- });
110
- }
111
- /**
112
- * Gets the next transaction in the collection of transactions.
113
- */
114
- next() {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- if (this.currentPage == null) {
117
- this.currentPage = yield new TransactionPage().init(this.book, this.query, this.lastCursor);
118
- }
119
- if (this.currentPage.hasNext()) {
120
- return this.currentPage.next();
121
- }
122
- else if (!this.currentPage.hasReachEnd()) {
123
- this.lastCursor = this.currentPage.getCursor();
124
- if (this.nextPage) {
125
- this.currentPage = this.nextPage;
126
- this.nextPage = undefined;
127
- }
128
- else {
129
- this.currentPage = yield new TransactionPage().init(this.book, this.query, this.lastCursor);
130
- }
131
- return this.currentPage.next();
132
- }
133
- else {
134
- return undefined;
135
- }
136
- });
137
- }
138
- /**
139
- * @returns The account, when filtering by a single account.
140
- */
141
- getAccount() {
142
- return __awaiter(this, void 0, void 0, function* () {
143
- if (this.currentPage == null) {
144
- this.currentPage = yield new TransactionPage().init(this.book, this.query, this.lastCursor);
145
- }
146
- return this.currentPage.getAccount();
147
- });
148
- }
149
- }
150
- //# sourceMappingURL=TransactionIterator.js.map
@@ -1,71 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import { Transaction } from "./Transaction.js";
11
- import * as TransactionService from '../service/transaction-service.js';
12
- export class TransactionPage {
13
- constructor() {
14
- this.transactions = [];
15
- }
16
- init(book, query, lastCursor) {
17
- return __awaiter(this, void 0, void 0, function* () {
18
- var transactionList = yield TransactionService.searchTransactions(book.getId(), query, 1000, lastCursor);
19
- if (!transactionList.items) {
20
- transactionList.items = [];
21
- }
22
- this.transactions = transactionList.items.map(tx => new Transaction(book, tx));
23
- this.cursor = transactionList.cursor;
24
- if (transactionList.account) {
25
- this.account = yield book.getAccount(transactionList.account);
26
- }
27
- this.index = 0;
28
- if (this.transactions == null || this.transactions.length == 0 || this.cursor == null || this.cursor == "") {
29
- this.reachEnd = true;
30
- }
31
- else {
32
- this.reachEnd = false;
33
- }
34
- return this;
35
- });
36
- }
37
- getCursor() {
38
- return this.cursor;
39
- }
40
- hasNext() {
41
- return (this.index != null && this.index < this.transactions.length) || false;
42
- }
43
- hasReachEnd() {
44
- return this.reachEnd || false;
45
- }
46
- getIndex() {
47
- if (this.index != null && this.index >= this.transactions.length) {
48
- return 0;
49
- }
50
- else {
51
- return this.index || 0;
52
- }
53
- }
54
- setIndex(index) {
55
- this.index = index;
56
- }
57
- getAccount() {
58
- return this.account;
59
- }
60
- next() {
61
- if (this.index != null && this.index < this.transactions.length) {
62
- var transaction = this.transactions[this.index];
63
- this.index++;
64
- return transaction;
65
- }
66
- else {
67
- return undefined;
68
- }
69
- }
70
- }
71
- //# sourceMappingURL=TransactionPage.js.map