bkper-js 2.7.0 → 2.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/lib/index.d.ts +142 -144
  3. package/lib/model/Account.js +18 -24
  4. package/lib/model/App.js +70 -24
  5. package/lib/model/Bkper.js +42 -30
  6. package/lib/model/Book.js +107 -102
  7. package/lib/model/BotResponse.js +2 -2
  8. package/lib/model/Collaborator.js +10 -14
  9. package/lib/model/Collection.js +22 -22
  10. package/lib/model/Connection.js +26 -21
  11. package/lib/model/Conversation.js +18 -16
  12. package/lib/model/File.js +17 -20
  13. package/lib/model/Group.js +27 -24
  14. package/lib/model/Integration.js +15 -16
  15. package/lib/model/Message.js +18 -19
  16. package/lib/model/Query.js +10 -14
  17. package/lib/model/Resource.js +23 -0
  18. package/lib/model/Template.js +8 -10
  19. package/lib/model/Transaction.js +59 -46
  20. package/lib/model/User.js +12 -15
  21. package/lib/service/account-service.js +12 -12
  22. package/lib/service/app-service.js +8 -8
  23. package/lib/service/balances-service.js +8 -5
  24. package/lib/service/book-service.js +14 -14
  25. package/lib/service/collaborator-service.js +6 -6
  26. package/lib/service/collection-service.js +12 -12
  27. package/lib/service/connection-service.js +12 -15
  28. package/lib/service/conversation-service.js +10 -10
  29. package/lib/service/event-service.js +8 -8
  30. package/lib/service/file-service.js +4 -4
  31. package/lib/service/group-service.js +16 -16
  32. package/lib/service/http-api-request.js +78 -71
  33. package/lib/service/http-request.js +31 -9
  34. package/lib/service/integration-service.js +8 -8
  35. package/lib/service/query-service.js +8 -8
  36. package/lib/service/template-service.js +2 -2
  37. package/lib/service/transaction-service.js +32 -32
  38. package/lib/service/user-service.js +4 -4
  39. package/package.json +1 -2
@@ -8,12 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpBooksApiV5Request } from "./http-api-request.js";
11
- import axios from 'axios';
12
- export function getBalances(bookId, query) {
11
+ import { HttpRequest } from "./http-request.js";
12
+ export function getBalances(bookId, query, config) {
13
13
  return __awaiter(this, void 0, void 0, function* () {
14
- var response = yield new HttpBooksApiV5Request(`${bookId}/balances`).addParam('query', query).addParam('time', Date.now()).fetch();
15
- if (response.data.balancesUrl) {
16
- response = yield axios.get(response.data.balancesUrl);
14
+ var _a;
15
+ let response = yield new HttpBooksApiV5Request(`${bookId}/balances`, config).addParam('query', query).addParam('time', Date.now()).fetch();
16
+ const balancesUrl = (_a = response.data) === null || _a === void 0 ? void 0 : _a.balancesUrl; // Expected for large payloads
17
+ if (balancesUrl) {
18
+ const balancesResponse = yield new HttpRequest(balancesUrl).setMethod('GET').execute();
19
+ response = { data: balancesResponse.data, status: balancesResponse.status };
17
20
  }
18
21
  return response.data;
19
22
  });
@@ -8,9 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpBooksApiV5Request } from "./http-api-request.js";
11
- export function loadBooks(query) {
11
+ export function loadBooks(query, config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
- let request = new HttpBooksApiV5Request('');
13
+ let request = new HttpBooksApiV5Request('', config);
14
14
  if (query) {
15
15
  request.addParam('query', query);
16
16
  }
@@ -26,44 +26,44 @@ export function loadBooks(query) {
26
26
  return booksJson;
27
27
  });
28
28
  }
29
- export function loadBook(bookId, loadAccounts, loadGroups) {
29
+ export function loadBook(bookId, loadAccounts, loadGroups, config) {
30
30
  return __awaiter(this, void 0, void 0, function* () {
31
31
  if (bookId == null) {
32
32
  throw new Error("Book id null!");
33
33
  }
34
34
  loadAccounts = loadAccounts || false;
35
35
  loadGroups = loadGroups || false;
36
- let response = yield new HttpBooksApiV5Request(bookId).addParam('loadAccounts', loadAccounts).addParam('loadGroups', loadGroups).fetch();
36
+ let response = yield new HttpBooksApiV5Request(bookId, config).addParam('loadAccounts', loadAccounts).addParam('loadGroups', loadGroups).fetch();
37
37
  return response.data;
38
38
  });
39
39
  }
40
- export function createBook(book) {
40
+ export function createBook(book, config) {
41
41
  return __awaiter(this, void 0, void 0, function* () {
42
- let response = yield new HttpBooksApiV5Request('').setMethod('POST').setPayload(book).fetch();
42
+ let response = yield new HttpBooksApiV5Request('', config).setMethod('POST').setPayload(book).fetch();
43
43
  return response.data;
44
44
  });
45
45
  }
46
- export function updateBook(bookId, book) {
46
+ export function updateBook(bookId, book, config) {
47
47
  return __awaiter(this, void 0, void 0, function* () {
48
- var response = yield new HttpBooksApiV5Request(`${bookId}`).setMethod('PUT').setPayload(book).fetch();
48
+ var response = yield new HttpBooksApiV5Request(`${bookId}`, config).setMethod('PUT').setPayload(book).fetch();
49
49
  return response.data;
50
50
  });
51
51
  }
52
- export function audit(bookId) {
52
+ export function audit(bookId, config) {
53
53
  return __awaiter(this, void 0, void 0, function* () {
54
- new HttpBooksApiV5Request(`${bookId}/audit`).setMethod('PATCH').fetch();
54
+ new HttpBooksApiV5Request(`${bookId}/audit`, config).setMethod('PATCH').fetch();
55
55
  });
56
56
  }
57
- export function getApps(bookId) {
57
+ export function getApps(bookId, config) {
58
58
  return __awaiter(this, void 0, void 0, function* () {
59
59
  var _a;
60
- let response = yield new HttpBooksApiV5Request(`${bookId}/apps`).setMethod('GET').fetch();
60
+ let response = yield new HttpBooksApiV5Request(`${bookId}/apps`, config).setMethod('GET').fetch();
61
61
  return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
62
62
  });
63
63
  }
64
- export function copyBook(bookId, name, copyTransactions, fromDate) {
64
+ export function copyBook(bookId, name, copyTransactions, fromDate, config) {
65
65
  return __awaiter(this, void 0, void 0, function* () {
66
- const request = new HttpBooksApiV5Request(`${bookId}/copy`).setMethod('POST').addParam('name', name);
66
+ const request = new HttpBooksApiV5Request(`${bookId}/copy`, config).setMethod('POST').addParam('name', name);
67
67
  if (copyTransactions) {
68
68
  request.addParam('copyTransactions', copyTransactions);
69
69
  if (fromDate) {
@@ -8,16 +8,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpBooksApiV5Request } from './http-api-request.js';
11
- export function listCollaborators(bookId) {
11
+ export function listCollaborators(bookId, config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
13
  var _a;
14
- const response = yield new HttpBooksApiV5Request(`${bookId}/collaborators`).setMethod('GET').fetch();
14
+ const response = yield new HttpBooksApiV5Request(`${bookId}/collaborators`, config).setMethod('GET').fetch();
15
15
  return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
16
16
  });
17
17
  }
18
- export function addOrUpdateCollaborator(bookId, collaborator, message) {
18
+ export function addOrUpdateCollaborator(bookId, collaborator, message, config) {
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
- let request = new HttpBooksApiV5Request(`${bookId}/collaborators`).setMethod('POST').setPayload(collaborator);
20
+ let request = new HttpBooksApiV5Request(`${bookId}/collaborators`, config).setMethod('POST').setPayload(collaborator);
21
21
  if (message) {
22
22
  request = request.addParam('message', message);
23
23
  }
@@ -25,9 +25,9 @@ export function addOrUpdateCollaborator(bookId, collaborator, message) {
25
25
  return response.data;
26
26
  });
27
27
  }
28
- export function removeCollaborator(bookId, email) {
28
+ export function removeCollaborator(bookId, email, config) {
29
29
  return __awaiter(this, void 0, void 0, function* () {
30
- const response = yield new HttpBooksApiV5Request(`${bookId}/collaborators/${email}`).setMethod('DELETE').fetch();
30
+ const response = yield new HttpBooksApiV5Request(`${bookId}/collaborators/${email}`, config).setMethod('DELETE').fetch();
31
31
  return response.data;
32
32
  });
33
33
  }
@@ -8,43 +8,43 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpApiV5Request } from "./http-api-request.js";
11
- export function loadCollections() {
11
+ export function loadCollections(config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
13
  var _a;
14
- let response = yield new HttpApiV5Request('collections').fetch();
14
+ let response = yield new HttpApiV5Request('collections', config).fetch();
15
15
  return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
16
16
  });
17
17
  }
18
- export function createCollection(payload) {
18
+ export function createCollection(payload, config) {
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
- let response = yield new HttpApiV5Request('collections').setMethod('POST').setPayload(payload).fetch();
20
+ let response = yield new HttpApiV5Request('collections', config).setMethod('POST').setPayload(payload).fetch();
21
21
  return response.data;
22
22
  });
23
23
  }
24
- export function updateCollection(payload) {
24
+ export function updateCollection(payload, config) {
25
25
  return __awaiter(this, void 0, void 0, function* () {
26
- let response = yield new HttpApiV5Request('collections').setMethod('PUT').setPayload(payload).fetch();
26
+ let response = yield new HttpApiV5Request('collections', config).setMethod('PUT').setPayload(payload).fetch();
27
27
  return response.data;
28
28
  });
29
29
  }
30
- export function deleteCollection(payload) {
30
+ export function deleteCollection(payload, config) {
31
31
  return __awaiter(this, void 0, void 0, function* () {
32
32
  var _a;
33
- let response = yield new HttpApiV5Request(`collections/${payload.id}`).setMethod('DELETE').fetch();
33
+ let response = yield new HttpApiV5Request(`collections/${payload.id}`, config).setMethod('DELETE').fetch();
34
34
  return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
35
35
  });
36
36
  }
37
- export function addBooksToCollection(collectionId, payload) {
37
+ export function addBooksToCollection(collectionId, payload, config) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
39
  var _a;
40
- let response = yield new HttpApiV5Request(`collections/${collectionId}/books/add`).setMethod('PATCH').setPayload(payload).fetch();
40
+ let response = yield new HttpApiV5Request(`collections/${collectionId}/books/add`, config).setMethod('PATCH').setPayload(payload).fetch();
41
41
  return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
42
42
  });
43
43
  }
44
- export function removeBooksFromCollection(collectionId, payload) {
44
+ export function removeBooksFromCollection(collectionId, payload, config) {
45
45
  return __awaiter(this, void 0, void 0, function* () {
46
46
  var _a;
47
- let response = yield new HttpApiV5Request(`collections/${collectionId}/books/remove`).setMethod('PATCH').setPayload(payload).fetch();
47
+ let response = yield new HttpApiV5Request(`collections/${collectionId}/books/remove`, config).setMethod('PATCH').setPayload(payload).fetch();
48
48
  return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
49
49
  });
50
50
  }
@@ -8,56 +8,53 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpApiV5Request } from "./http-api-request.js";
11
- export function getConnection(id) {
11
+ export function getConnection(id, config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
- const res = yield new HttpApiV5Request(`user/connections/${id}`)
13
+ const res = yield new HttpApiV5Request(`user/connections/${id}`, config)
14
14
  .setMethod('GET')
15
15
  .fetch();
16
16
  return res.data;
17
17
  });
18
18
  }
19
- export function listConnections() {
19
+ export function listConnections(config) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
21
  var _a;
22
- const res = yield new HttpApiV5Request(`user/connections`)
22
+ const res = yield new HttpApiV5Request(`user/connections`, config)
23
23
  .setMethod('GET')
24
24
  .fetch();
25
25
  return ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.items) || [];
26
26
  });
27
27
  }
28
- export function createConnection(connection) {
28
+ export function createConnection(connection, config) {
29
29
  return __awaiter(this, void 0, void 0, function* () {
30
- const res = yield new HttpApiV5Request(`user/connections`)
30
+ const res = yield new HttpApiV5Request(`user/connections`, config)
31
31
  .setPayload(connection)
32
32
  .setMethod('POST')
33
33
  .fetch();
34
34
  return res.data;
35
35
  });
36
36
  }
37
- export function updateConnection(connection) {
37
+ export function updateConnection(connection, config) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
- const res = yield new HttpApiV5Request(`user/connections`)
39
+ const res = yield new HttpApiV5Request(`user/connections`, config)
40
40
  .setPayload(connection)
41
41
  .setMethod('PUT')
42
42
  .fetch();
43
43
  return res.data;
44
44
  });
45
45
  }
46
- export function deleteConnection(id) {
46
+ export function deleteConnection(id, config) {
47
47
  return __awaiter(this, void 0, void 0, function* () {
48
- const res = yield new HttpApiV5Request(`user/connections/${id}`)
48
+ const res = yield new HttpApiV5Request(`user/connections/${id}`, config)
49
49
  .setMethod('DELETE')
50
50
  .fetch();
51
51
  return res.data;
52
52
  });
53
53
  }
54
- export function listIntegrations(connectionId) {
54
+ export function listIntegrations(connectionId, config) {
55
55
  return __awaiter(this, void 0, void 0, function* () {
56
56
  var _a;
57
- if (!connectionId) {
58
- return [];
59
- }
60
- const res = yield new HttpApiV5Request(`user/connections/${connectionId}/integrations`)
57
+ const res = yield new HttpApiV5Request(`user/connections/${connectionId}/integrations`, config)
61
58
  .setMethod('GET')
62
59
  .fetch();
63
60
  return ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.items) || [];
@@ -8,35 +8,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpApiRequest } from "./http-api-request.js";
11
- export function getConversations() {
11
+ export function getConversations(config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
13
  var _a;
14
- const response = yield new HttpApiRequest(`v5/apps/conversations`).setMethod('GET').fetch();
14
+ const response = yield new HttpApiRequest(`v5/apps/conversations`, config).setMethod('GET').fetch();
15
15
  return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
16
16
  });
17
17
  }
18
- export function createConversation(agentId, conversation) {
18
+ export function createConversation(agentId, conversation, config) {
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
- const response = yield new HttpApiRequest(`v5/apps/${agentId}/conversations`).setMethod('POST').setPayload(conversation).fetch();
20
+ const response = yield new HttpApiRequest(`v5/apps/${agentId}/conversations`, config).setMethod('POST').setPayload(conversation).fetch();
21
21
  return response.data;
22
22
  });
23
23
  }
24
- export function getMessages(conversationId) {
24
+ export function getMessages(conversationId, config) {
25
25
  return __awaiter(this, void 0, void 0, function* () {
26
26
  var _a;
27
- const response = yield new HttpApiRequest(`v5/apps/conversations/${conversationId}/messages`).setMethod('GET').fetch();
27
+ const response = yield new HttpApiRequest(`v5/apps/conversations/${conversationId}/messages`, config).setMethod('GET').fetch();
28
28
  return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
29
29
  });
30
30
  }
31
- export function createMessage(conversationId, message) {
31
+ export function createMessage(conversationId, message, config) {
32
32
  return __awaiter(this, void 0, void 0, function* () {
33
- const response = yield new HttpApiRequest(`v5/apps/conversations/${conversationId}/messages`).setMethod('POST').setPayload(message).fetch();
33
+ const response = yield new HttpApiRequest(`v5/apps/conversations/${conversationId}/messages`, config).setMethod('POST').setPayload(message).fetch();
34
34
  return response.data;
35
35
  });
36
36
  }
37
- export function streamMessage(conversationId, message) {
37
+ export function streamMessage(conversationId, message, config) {
38
38
  return __awaiter(this, void 0, void 0, function* () {
39
- new HttpApiRequest(`v5/apps/conversations/${conversationId}/stream`).setMethod('POST').setPayload(message).fetch();
39
+ new HttpApiRequest(`v5/apps/conversations/${conversationId}/stream`, config).setMethod('POST').setPayload(message).fetch();
40
40
  });
41
41
  }
42
42
  //# sourceMappingURL=conversation-service.js.map
@@ -8,9 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpBooksApiV5Request } from "./http-api-request.js";
11
- export function listEvents(book, afterDate, beforeDate, onError, resourceId, limit, cursor) {
11
+ export function listEvents(book, afterDate, beforeDate, onError, resourceId, limit, cursor, config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
- let request = new HttpBooksApiV5Request(`${book.getId()}/events`);
13
+ let request = new HttpBooksApiV5Request(`${book.getId()}/events`, config);
14
14
  request.addParam('after', afterDate);
15
15
  request.addParam('before', beforeDate);
16
16
  request.addParam('error', onError);
@@ -23,21 +23,21 @@ export function listEvents(book, afterDate, beforeDate, onError, resourceId, lim
23
23
  return response.data;
24
24
  });
25
25
  }
26
- export function replayBotResponse(book, eventId, agentId) {
26
+ export function replayBotResponse(book, eventId, agentId, config) {
27
27
  return __awaiter(this, void 0, void 0, function* () {
28
- const response = yield new HttpBooksApiV5Request(`${book.getId()}/events/${eventId}/responses/${agentId}`).setMethod('PUT').fetch();
28
+ const response = yield new HttpBooksApiV5Request(`${book.getId()}/events/${eventId}/responses/${agentId}`, config).setMethod('PUT').fetch();
29
29
  return response.data;
30
30
  });
31
31
  }
32
- export function deleteBotResponse(book, eventId, agentId) {
32
+ export function deleteBotResponse(book, eventId, agentId, config) {
33
33
  return __awaiter(this, void 0, void 0, function* () {
34
- const response = yield new HttpBooksApiV5Request(`${book.getId()}/events/${eventId}/responses/${agentId}`).setMethod('DELETE').fetch();
34
+ const response = yield new HttpBooksApiV5Request(`${book.getId()}/events/${eventId}/responses/${agentId}`, config).setMethod('DELETE').fetch();
35
35
  return response.data;
36
36
  });
37
37
  }
38
- export function replayEventsBatch(book, eventList, errorsOnly) {
38
+ export function replayEventsBatch(book, eventList, errorsOnly, config) {
39
39
  return __awaiter(this, void 0, void 0, function* () {
40
- let request = new HttpBooksApiV5Request(`${book.getId()}/events/replay/batch`).setMethod('PATCH').setPayload(eventList);
40
+ let request = new HttpBooksApiV5Request(`${book.getId()}/events/replay/batch`, config).setMethod('PATCH').setPayload(eventList);
41
41
  if (errorsOnly) {
42
42
  request.addParam('errorsOnly', errorsOnly);
43
43
  }
@@ -8,15 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpBooksApiV5Request } from "./http-api-request.js";
11
- export function createFile(bookId, file) {
11
+ export function createFile(bookId, file, config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
- var response = yield new HttpBooksApiV5Request(`${bookId}/files`).setMethod('POST').setPayload(file).fetch();
13
+ var response = yield new HttpBooksApiV5Request(`${bookId}/files`, config).setMethod('POST').setPayload(file).fetch();
14
14
  return response.data;
15
15
  });
16
16
  }
17
- export function getFile(bookId, id) {
17
+ export function getFile(bookId, id, config) {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
- var response = yield new HttpBooksApiV5Request(`${bookId}/files/${id}`).setMethod('GET').fetch();
19
+ var response = yield new HttpBooksApiV5Request(`${bookId}/files/${id}`, config).setMethod('GET').fetch();
20
20
  return response.data;
21
21
  });
22
22
  }
@@ -8,41 +8,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { HttpBooksApiV5Request } from "./http-api-request.js";
11
- export function createGroup(bookId, group) {
11
+ export function createGroup(bookId, group, config) {
12
12
  return __awaiter(this, void 0, void 0, function* () {
13
- var response = yield new HttpBooksApiV5Request(`${bookId}/groups`).setMethod('POST').setPayload(group).fetch();
13
+ var response = yield new HttpBooksApiV5Request(`${bookId}/groups`, config).setMethod('POST').setPayload(group).fetch();
14
14
  return response.data;
15
15
  });
16
16
  }
17
- export function createGroups(bookId, payload) {
17
+ export function createGroups(bookId, payload, config) {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
19
  var _a;
20
- const response = yield new HttpBooksApiV5Request(`${bookId}/groups/batch`).setMethod('POST').setPayload(payload).fetch();
20
+ const response = yield new HttpBooksApiV5Request(`${bookId}/groups/batch`, config).setMethod('POST').setPayload(payload).fetch();
21
21
  return ((_a = response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
22
22
  });
23
23
  }
24
- export function updateGroup(bookId, group) {
24
+ export function updateGroup(bookId, group, config) {
25
25
  return __awaiter(this, void 0, void 0, function* () {
26
- var response = yield new HttpBooksApiV5Request(`${bookId}/groups`).setMethod('PUT').setPayload(group).fetch();
26
+ var response = yield new HttpBooksApiV5Request(`${bookId}/groups`, config).setMethod('PUT').setPayload(group).fetch();
27
27
  return response.data;
28
28
  });
29
29
  }
30
- export function deleteGroup(bookId, group) {
30
+ export function deleteGroup(bookId, group, config) {
31
31
  return __awaiter(this, void 0, void 0, function* () {
32
- var response = yield new HttpBooksApiV5Request(`${bookId}/groups/${group.id}`).setMethod('DELETE').fetch();
32
+ var response = yield new HttpBooksApiV5Request(`${bookId}/groups/${group.id}`, config).setMethod('DELETE').fetch();
33
33
  return response.data;
34
34
  });
35
35
  }
36
- export function getGroupsByAccountId(bookId, accountId) {
36
+ export function getGroupsByAccountId(bookId, accountId, config) {
37
37
  return __awaiter(this, void 0, void 0, function* () {
38
38
  var _a;
39
- var response = yield new HttpBooksApiV5Request(`${bookId}/accounts/${accountId}/groups`).setMethod('GET').fetch();
39
+ var response = yield new HttpBooksApiV5Request(`${bookId}/accounts/${accountId}/groups`, config).setMethod('GET').fetch();
40
40
  return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
41
41
  });
42
42
  }
43
- export function getGroups(bookId) {
43
+ export function getGroups(bookId, config) {
44
44
  return __awaiter(this, void 0, void 0, function* () {
45
- var response = yield new HttpBooksApiV5Request(`${bookId}/groups`).setMethod('GET').fetch();
45
+ var response = yield new HttpBooksApiV5Request(`${bookId}/groups`, config).setMethod('GET').fetch();
46
46
  var groupsPlain = response.data;
47
47
  if (!(groupsPlain === null || groupsPlain === void 0 ? void 0 : groupsPlain.items)) {
48
48
  return [];
@@ -50,18 +50,18 @@ export function getGroups(bookId) {
50
50
  return groupsPlain.items;
51
51
  });
52
52
  }
53
- export function getGroup(bookId, idOrName) {
53
+ export function getGroup(bookId, idOrName, config) {
54
54
  return __awaiter(this, void 0, void 0, function* () {
55
- var response = yield new HttpBooksApiV5Request(`${bookId}/groups/${encodeURIComponent(idOrName)}`).setMethod('GET').fetch();
55
+ var response = yield new HttpBooksApiV5Request(`${bookId}/groups/${encodeURIComponent(idOrName)}`, config).setMethod('GET').fetch();
56
56
  return response.data;
57
57
  });
58
58
  }
59
- export function getAccounts(bookId, idOrName) {
59
+ export function getAccounts(bookId, idOrName, config) {
60
60
  return __awaiter(this, void 0, void 0, function* () {
61
61
  if (!idOrName) {
62
62
  return [];
63
63
  }
64
- var response = yield new HttpBooksApiV5Request(`${bookId}/groups/${encodeURIComponent(idOrName)}/accounts`).setMethod('GET').fetch();
64
+ var response = yield new HttpBooksApiV5Request(`${bookId}/groups/${encodeURIComponent(idOrName)}/accounts`, config).setMethod('GET').fetch();
65
65
  var accountsPlain = response.data;
66
66
  if (!(accountsPlain === null || accountsPlain === void 0 ? void 0 : accountsPlain.items)) {
67
67
  return [];
@@ -7,11 +7,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { HttpRequest } from './http-request.js';
10
+ import { HttpRequest } from "./http-request.js";
11
11
  export class HttpApiRequest extends HttpRequest {
12
- constructor(path) {
13
- super(`${HttpApiRequest.config.apiBaseUrl || "https://app.bkper.com/_ah/api/bkper"}/${path}`);
12
+ constructor(path, config) {
13
+ const effectiveConfig = config;
14
+ super(`${effectiveConfig.apiBaseUrl || "https://app.bkper.com/_ah/api/bkper"}/${path}`);
14
15
  this.retry = 0;
16
+ this.config = config;
15
17
  }
16
18
  fetch() {
17
19
  const _super = Object.create(null, {
@@ -19,67 +21,70 @@ export class HttpApiRequest extends HttpRequest {
19
21
  });
20
22
  return __awaiter(this, void 0, void 0, function* () {
21
23
  this.addCustomHeaders();
22
- this.setHeader('Authorization', `Bearer ${yield getAccessToken()}`);
23
- this.addParam('key', yield getApiKey());
24
- // this.httpRequest.setMuteHttpExceptions(true);
24
+ this.setHeader("Authorization", `Bearer ${yield this.getAccessToken()}`);
25
+ this.addParam("key", yield this.getApiKey());
25
26
  try {
26
27
  let resp = yield _super.execute.call(this);
27
28
  if (resp.status >= 200 && resp.status < 300) {
28
29
  return resp;
29
30
  }
31
+ else if (resp.status == 404) {
32
+ return { data: null, status: resp.status };
33
+ }
34
+ else if (resp.status != 400 && this.retry <= 3) {
35
+ this.retry++;
36
+ const effectiveConfig = this.config;
37
+ if (effectiveConfig.requestRetryHandler) {
38
+ yield effectiveConfig.requestRetryHandler(resp.status, resp.data, this.retry);
39
+ }
40
+ else {
41
+ console.log(`${JSON.stringify(resp.data)} - Retrying... `);
42
+ }
43
+ return yield this.fetch();
44
+ }
30
45
  else {
31
- throw this.handleError(resp);
46
+ // Create an error object that matches axios error structure for compatibility
47
+ const errorObj = {
48
+ response: {
49
+ status: resp.status,
50
+ data: resp.data,
51
+ },
52
+ };
53
+ throw this.handleError(errorObj);
32
54
  }
33
55
  }
34
56
  catch (error) {
57
+ // If error already has response structure (from our code above), use it
35
58
  if (error.response) {
36
- let errorResp = error.response;
37
- // The request was made and the server responded with a status code
38
- // that falls out of the range of 2xx
39
- // console.log(error.response.data);
40
- // console.log(error.response.status);
41
- // console.log(error.response.headers);
42
- if (errorResp.status == 404) {
43
- return { data: null };
44
- }
45
- else if (errorResp.status != 400 && this.retry <= 3) {
46
- this.retry++;
47
- if (HttpApiRequest.config.requestRetryHandler) {
48
- yield HttpApiRequest.config.requestRetryHandler(errorResp.status, errorResp.data, this.retry);
49
- }
50
- else {
51
- console.log(`${JSON.stringify(errorResp.data)} - Retrying... `);
52
- }
53
- return yield this.fetch();
54
- }
55
- throw this.handleError(errorResp.data);
59
+ throw error;
56
60
  }
57
- else if (error.request) {
58
- // The request was made but no response was received
59
- // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
60
- // http.ClientRequest in node.js
61
+ // Network error or fetch failure
62
+ if (error instanceof TypeError && error.message.includes("fetch")) {
63
+ // Network error - retry if within retry limit
61
64
  if (this.retry <= 3) {
62
65
  this.retry++;
63
- if (HttpApiRequest.config.requestRetryHandler) {
64
- yield HttpApiRequest.config.requestRetryHandler(520, undefined, this.retry);
66
+ const effectiveConfig = this.config;
67
+ if (effectiveConfig.requestRetryHandler) {
68
+ yield effectiveConfig.requestRetryHandler(520, undefined, this.retry);
65
69
  }
66
70
  else {
67
- console.log(`No response received - Retrying... `);
71
+ console.log(`Network error - Retrying... `);
68
72
  }
69
73
  return yield this.fetch();
70
74
  }
71
75
  }
72
- else {
73
- // Something happened in setting up the request that triggered an Error
74
- console.log('Error', error.message);
75
- }
76
- throw this.handleError(error.toJSON ? error.toJSON() : error);
76
+ // Other errors
77
+ console.log("Error", error.message);
78
+ throw this.handleError(error);
77
79
  }
78
80
  });
79
81
  }
80
82
  handleError(err) {
81
83
  var _a, _b, _c;
82
- const customError = HttpApiRequest.config.requestErrorHandler ? HttpApiRequest.config.requestErrorHandler(err) : undefined;
84
+ const effectiveConfig = this.config;
85
+ const customError = effectiveConfig.requestErrorHandler
86
+ ? effectiveConfig.requestErrorHandler(err)
87
+ : undefined;
83
88
  if (customError) {
84
89
  return customError;
85
90
  }
@@ -96,46 +101,48 @@ export class HttpApiRequest extends HttpRequest {
96
101
  }
97
102
  addCustomHeaders() {
98
103
  return __awaiter(this, void 0, void 0, function* () {
99
- if (HttpApiRequest.config.requestHeadersProvider) {
100
- const headers = yield HttpApiRequest.config.requestHeadersProvider();
104
+ const effectiveConfig = this.config;
105
+ if (effectiveConfig.requestHeadersProvider) {
106
+ const headers = yield effectiveConfig.requestHeadersProvider();
101
107
  Object.entries(headers).forEach(([key, value]) => this.setHeader(key, value));
102
108
  }
103
109
  });
104
110
  }
105
- }
106
- HttpApiRequest.config = {};
107
- function getApiKey() {
108
- return __awaiter(this, void 0, void 0, function* () {
109
- if (HttpApiRequest.config.apiKeyProvider) {
110
- return yield HttpApiRequest.config.apiKeyProvider();
111
- }
112
- return null;
113
- });
114
- }
115
- function getAccessToken() {
116
- return __awaiter(this, void 0, void 0, function* () {
117
- let token = undefined;
118
- if (HttpApiRequest.config.oauthTokenProvider) {
119
- token = yield HttpApiRequest.config.oauthTokenProvider();
120
- }
121
- else {
122
- console.warn(`Token provider NOT configured!`);
123
- }
124
- if (token) {
125
- token = token.replace('Bearer ', '');
126
- token = token.replace('bearer ', '');
127
- }
128
- return token;
129
- });
111
+ getApiKey() {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ const effectiveConfig = this.config;
114
+ if (effectiveConfig.apiKeyProvider) {
115
+ return yield effectiveConfig.apiKeyProvider();
116
+ }
117
+ return null;
118
+ });
119
+ }
120
+ getAccessToken() {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ let token = undefined;
123
+ const effectiveConfig = this.config;
124
+ if (effectiveConfig.oauthTokenProvider) {
125
+ token = yield effectiveConfig.oauthTokenProvider();
126
+ }
127
+ else {
128
+ console.warn(`Token provider NOT configured!`);
129
+ }
130
+ if (token) {
131
+ token = token.replace("Bearer ", "");
132
+ token = token.replace("bearer ", "");
133
+ }
134
+ return token;
135
+ });
136
+ }
130
137
  }
131
138
  export class HttpBooksApiV5Request extends HttpApiRequest {
132
- constructor(service) {
133
- super(`v5/books/${service}`);
139
+ constructor(service, config) {
140
+ super(`v5/books/${service}`, config);
134
141
  }
135
142
  }
136
143
  export class HttpApiV5Request extends HttpApiRequest {
137
- constructor(service) {
138
- super(`v5/${service}`);
144
+ constructor(service, config) {
145
+ super(`v5/${service}`, config);
139
146
  }
140
147
  }
141
148
  //# sourceMappingURL=http-api-request.js.map