bkper-js 1.0.0 → 1.2.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.
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,116 +7,66 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.HttpApiV5Request = exports.HttpBooksApiV5Request = exports.HttpApiRequest = void 0;
16
- const gaxios_1 = require("gaxios");
17
- const https_1 = __importDefault(require("https"));
18
- const utils_1 = require("../utils");
19
- const httpsAgent = new https_1.default.Agent({ keepAlive: true });
20
- class HttpApiRequest {
10
+ import { HttpRequest } from './http-request.js';
11
+ export class HttpApiRequest extends HttpRequest {
21
12
  constructor(path) {
22
- this.params = [];
23
- this.headers = {};
24
- this.method = 'GET';
25
- this.payload = null;
26
- this.url = `${HttpApiRequest.config.apiBaseUrl || "https://app.bkper.com/_ah/api/bkper"}/${path}`;
27
- }
28
- setMethod(method) {
29
- this.method = method;
30
- return this;
31
- }
32
- setHeader(name, value) {
33
- if (value) {
34
- this.headers[name] = value;
35
- }
36
- return this;
37
- }
38
- addParam(name, value) {
39
- if (value) {
40
- this.params.push({ name, value });
41
- }
42
- return this;
43
- }
44
- setPayload(payload) {
45
- this.payload = typeof payload === "string" ? payload : JSON.stringify(payload);
46
- return this;
47
- }
48
- /**
49
- * Gets the result url, with query params appended.
50
- */
51
- getUrl() {
52
- let url = this.url;
53
- if (this.params != null) {
54
- let i = 0;
55
- if (url.indexOf('?') < 0) {
56
- url += '?';
57
- }
58
- else {
59
- i++;
60
- }
61
- for (const param of this.params) {
62
- if (i > 0) {
63
- url += "&";
64
- }
65
- var key = param.name;
66
- var value = param.value;
67
- if (value != null) {
68
- url += key + "=" + encodeURIComponent(value);
69
- i++;
70
- }
71
- }
72
- }
73
- return url;
13
+ super(`${HttpApiRequest.config.apiBaseUrl || "https://app.bkper.com/_ah/api/bkper"}/${path}`);
14
+ this.retry = 0;
74
15
  }
75
16
  fetch() {
17
+ const _super = Object.create(null, {
18
+ execute: { get: () => super.execute }
19
+ });
76
20
  return __awaiter(this, void 0, void 0, function* () {
77
- var _a;
78
21
  this.addCustomHeaders();
79
- this.headers['Authorization'] = `Bearer ${yield getAccessToken()}`;
22
+ this.setHeader('Authorization', `Bearer ${yield getAccessToken()}`);
80
23
  this.addParam('key', yield getApiKey());
81
24
  // this.httpRequest.setMuteHttpExceptions(true);
82
- const url = this.getUrl();
83
25
  try {
84
- return yield (0, gaxios_1.request)({
85
- url: url,
86
- method: this.method,
87
- headers: this.headers,
88
- body: this.payload,
89
- agent: url.startsWith('https') ? httpsAgent : null,
90
- retryConfig: {
91
- httpMethodsToRetry: ['GET', 'PUT', 'POST', 'PATCH', 'HEAD', 'OPTIONS', 'DELETE'],
92
- statusCodesToRetry: [[100, 199], [429, 429], [500, 599]],
93
- retry: process.env.NODE_ENV == utils_1.NODE_ENV_DEV ? 0 : 3,
94
- onRetryAttempt: (err) => { console.log(`${err.message} - Retrying... `); }
95
- }
96
- });
97
- }
98
- catch (e) {
99
- const customError = HttpApiRequest.config.requestErrorHandler ? HttpApiRequest.config.requestErrorHandler(e) : undefined;
100
- if (customError) {
101
- throw customError;
26
+ let resp = yield _super.execute.call(this);
27
+ if (resp.status >= 200 && resp.status < 300) {
28
+ console.log(resp.data);
29
+ return resp;
102
30
  }
103
- else {
104
- //Default error handler
105
- let error = (_a = e.response.data) === null || _a === void 0 ? void 0 : _a.error;
106
- if (error) {
107
- if (error.code == 404) {
108
- return { data: null };
109
- }
110
- else {
111
- throw error.message;
112
- }
31
+ else if (resp.status == 404) {
32
+ return { data: null };
33
+ }
34
+ else if (this.retry <= 3) {
35
+ this.retry++;
36
+ if (HttpApiRequest.config.requestRetryHandler) {
37
+ yield HttpApiRequest.config.requestRetryHandler(resp.status, resp.data, this.retry);
113
38
  }
114
39
  else {
115
- throw e.message;
40
+ console.log(`${resp.data} - Retrying... `);
116
41
  }
42
+ return yield this.fetch();
43
+ }
44
+ else {
45
+ throw this.handleError(resp.data);
117
46
  }
118
47
  }
48
+ catch (err) {
49
+ throw this.handleError(err.toJSON ? err.toJSON() : err);
50
+ }
119
51
  });
120
52
  }
53
+ handleError(err) {
54
+ var _a, _b;
55
+ const customError = HttpApiRequest.config.requestErrorHandler ? HttpApiRequest.config.requestErrorHandler(err) : undefined;
56
+ if (customError) {
57
+ return customError;
58
+ }
59
+ else {
60
+ //Default error handler
61
+ let error = (_b = (_a = err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error;
62
+ if (error) {
63
+ return error.message;
64
+ }
65
+ else {
66
+ return err.message || err;
67
+ }
68
+ }
69
+ }
121
70
  addCustomHeaders() {
122
71
  return __awaiter(this, void 0, void 0, function* () {
123
72
  if (HttpApiRequest.config.requestHeadersProvider) {
@@ -127,7 +76,6 @@ class HttpApiRequest {
127
76
  });
128
77
  }
129
78
  }
130
- exports.HttpApiRequest = HttpApiRequest;
131
79
  HttpApiRequest.config = {};
132
80
  function getApiKey() {
133
81
  return __awaiter(this, void 0, void 0, function* () {
@@ -139,10 +87,13 @@ function getApiKey() {
139
87
  }
140
88
  function getAccessToken() {
141
89
  return __awaiter(this, void 0, void 0, function* () {
142
- let token = null;
90
+ let token = undefined;
143
91
  if (HttpApiRequest.config.oauthTokenProvider) {
144
92
  token = yield HttpApiRequest.config.oauthTokenProvider();
145
93
  }
94
+ else {
95
+ console.warn(`Token provider NOT configured!`);
96
+ }
146
97
  if (token) {
147
98
  token = token.replace('Bearer ', '');
148
99
  token = token.replace('bearer ', '');
@@ -150,16 +101,14 @@ function getAccessToken() {
150
101
  return token;
151
102
  });
152
103
  }
153
- class HttpBooksApiV5Request extends HttpApiRequest {
104
+ export class HttpBooksApiV5Request extends HttpApiRequest {
154
105
  constructor(service) {
155
106
  super(`v5/books/${service}`);
156
107
  }
157
108
  }
158
- exports.HttpBooksApiV5Request = HttpBooksApiV5Request;
159
- class HttpApiV5Request extends HttpApiRequest {
109
+ export class HttpApiV5Request extends HttpApiRequest {
160
110
  constructor(service) {
161
111
  super(`v5/${service}`);
162
112
  }
163
113
  }
164
- exports.HttpApiV5Request = HttpApiV5Request;
165
114
  //# sourceMappingURL=http-api-request.js.map
@@ -0,0 +1,78 @@
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 https from 'https';
11
+ import axios from 'axios';
12
+ const httpsAgent = https && https.Agent ? new https.Agent({ keepAlive: true }) : undefined;
13
+ export class HttpRequest {
14
+ constructor(url) {
15
+ this.params = [];
16
+ this.headers = {};
17
+ this.method = 'GET';
18
+ this.payload = null;
19
+ this.url = url;
20
+ }
21
+ setMethod(method) {
22
+ this.method = method;
23
+ return this;
24
+ }
25
+ setHeader(name, value) {
26
+ this.headers[name] = value;
27
+ return this;
28
+ }
29
+ addParam(name, value) {
30
+ this.params.push({ name, value });
31
+ return this;
32
+ }
33
+ setPayload(payload) {
34
+ this.payload = payload;
35
+ return this;
36
+ }
37
+ /**
38
+ * Gets the result url, with query params appended.
39
+ */
40
+ getUrl() {
41
+ let url = this.url;
42
+ if (this.params != null) {
43
+ let i = 0;
44
+ if (url.indexOf('?') < 0) {
45
+ url += '?';
46
+ }
47
+ else {
48
+ i++;
49
+ }
50
+ for (const param of this.params) {
51
+ if (i > 0) {
52
+ url += "&";
53
+ }
54
+ var key = param.name;
55
+ var value = param.value;
56
+ if (value != null) {
57
+ url += key + "=" + encodeURIComponent(value);
58
+ i++;
59
+ }
60
+ }
61
+ }
62
+ return url;
63
+ }
64
+ execute() {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ const url = this.getUrl();
67
+ return axios.request({
68
+ url: url,
69
+ method: this.method,
70
+ headers: this.headers,
71
+ data: this.payload,
72
+ httpsAgent: url.startsWith('https') ? httpsAgent : undefined,
73
+ // withCredentials: true
74
+ });
75
+ });
76
+ }
77
+ }
78
+ //# sourceMappingURL=http-request.js.map
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,42 +7,37 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.listIntegrations = listIntegrations;
13
- exports.createIntegration = createIntegration;
14
- exports.updateIntegration = updateIntegration;
15
- exports.deleteIntegration = deleteIntegration;
16
- const http_api_request_1 = require("./http-api-request");
17
- function listIntegrations(bookId) {
10
+ import { HttpBooksApiV5Request } from "./http-api-request.js";
11
+ export function listIntegrations(bookId) {
18
12
  return __awaiter(this, void 0, void 0, function* () {
19
13
  var _a;
20
- const res = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/integrations`)
14
+ const res = yield new HttpBooksApiV5Request(`${bookId}/integrations`)
21
15
  .setMethod('GET')
22
16
  .fetch();
23
17
  return ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.items) || [];
24
18
  });
25
19
  }
26
- function createIntegration(bookId, integration) {
20
+ export function createIntegration(bookId, integration) {
27
21
  return __awaiter(this, void 0, void 0, function* () {
28
- const res = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/integrations`)
22
+ const res = yield new HttpBooksApiV5Request(`${bookId}/integrations`)
29
23
  .setPayload(integration)
30
24
  .setMethod('POST')
31
25
  .fetch();
32
26
  return res.data;
33
27
  });
34
28
  }
35
- function updateIntegration(bookId, integration) {
29
+ export function updateIntegration(bookId, integration) {
36
30
  return __awaiter(this, void 0, void 0, function* () {
37
- const res = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/integrations`)
31
+ const res = yield new HttpBooksApiV5Request(`${bookId}/integrations`)
38
32
  .setPayload(integration)
39
33
  .setMethod('PUT')
40
34
  .fetch();
41
35
  return res.data;
42
36
  });
43
37
  }
44
- function deleteIntegration(bookId, id) {
38
+ export function deleteIntegration(bookId, id) {
45
39
  return __awaiter(this, void 0, void 0, function* () {
46
- const res = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/integrations/${id}`)
40
+ const res = yield new HttpBooksApiV5Request(`${bookId}/integrations/${id}`)
47
41
  .setMethod('DELETE')
48
42
  .fetch();
49
43
  return res.data;
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,32 +7,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.createTransaction = createTransaction;
13
- exports.createTransactionsBatch = createTransactionsBatch;
14
- exports.trashTransactionsBatch = trashTransactionsBatch;
15
- exports.updateTransaction = updateTransaction;
16
- exports.postTransaction = postTransaction;
17
- exports.checkTransaction = checkTransaction;
18
- exports.uncheckTransaction = uncheckTransaction;
19
- exports.trashTransaction = trashTransaction;
20
- exports.restoreTransaction = restoreTransaction;
21
- exports.getTransaction = getTransaction;
22
- exports.searchTransactions = searchTransactions;
23
- const http_api_request_1 = require("./http-api-request");
24
- function createTransaction(bookId, transaction) {
10
+ import { HttpBooksApiV5Request } from "./http-api-request.js";
11
+ export function createTransaction(bookId, transaction) {
25
12
  return __awaiter(this, void 0, void 0, function* () {
26
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions`).setMethod('POST').setPayload(transaction).fetch();
13
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions`).setMethod('POST').setPayload(transaction).fetch();
27
14
  return response.data;
28
15
  });
29
16
  }
30
- function createTransactionsBatch(bookId, transactions) {
17
+ export function createTransactionsBatch(bookId, transactions) {
31
18
  return __awaiter(this, void 0, void 0, function* () {
32
19
  let transactionList = {
33
20
  items: transactions
34
21
  };
35
22
  var payload = JSON.stringify(transactionList);
36
- let response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/batch`)
23
+ let response = yield new HttpBooksApiV5Request(`${bookId}/transactions/batch`)
37
24
  .setMethod('POST')
38
25
  .setPayload(payload)
39
26
  .fetch();
@@ -41,67 +28,70 @@ function createTransactionsBatch(bookId, transactions) {
41
28
  return transactionList != null && transactionList.items != null ? transactionList.items : [];
42
29
  });
43
30
  }
44
- function trashTransactionsBatch(bookId, transactions) {
31
+ export function trashTransactionsBatch(bookId, transactions) {
45
32
  return __awaiter(this, void 0, void 0, function* () {
46
33
  let transactionList = {
47
34
  items: transactions
48
35
  };
49
36
  var payload = JSON.stringify(transactionList);
50
- let response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/trash/batch`)
37
+ let response = yield new HttpBooksApiV5Request(`${bookId}/transactions/trash/batch`)
51
38
  .setMethod('PATCH')
52
39
  .setPayload(payload)
53
40
  .fetch();
54
41
  transactionList = yield response.data;
55
42
  });
56
43
  }
57
- function updateTransaction(bookId, transaction) {
44
+ export function updateTransaction(bookId, transaction) {
58
45
  return __awaiter(this, void 0, void 0, function* () {
59
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions`).setMethod('PUT').setPayload(transaction).fetch();
46
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions`).setMethod('PUT').setPayload(transaction).fetch();
60
47
  return response.data;
61
48
  });
62
49
  }
63
- function postTransaction(bookId, transaction) {
50
+ export function postTransaction(bookId, transaction) {
64
51
  return __awaiter(this, void 0, void 0, function* () {
65
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/post`).setMethod('PATCH').setPayload(transaction).fetch();
52
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions/post`).setMethod('PATCH').setPayload(transaction).fetch();
66
53
  return response.data;
67
54
  });
68
55
  }
69
- function checkTransaction(bookId, transaction) {
56
+ export function checkTransaction(bookId, transaction) {
70
57
  return __awaiter(this, void 0, void 0, function* () {
71
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/check`).setMethod('PATCH').setPayload(transaction).fetch();
58
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions/check`).setMethod('PATCH').setPayload(transaction).fetch();
72
59
  return response.data;
73
60
  });
74
61
  }
75
- function uncheckTransaction(bookId, transaction) {
62
+ export function uncheckTransaction(bookId, transaction) {
76
63
  return __awaiter(this, void 0, void 0, function* () {
77
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/uncheck`).setMethod('PATCH').setPayload(transaction).fetch();
64
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions/uncheck`).setMethod('PATCH').setPayload(transaction).fetch();
78
65
  return response.data;
79
66
  });
80
67
  }
81
- function trashTransaction(bookId, transaction) {
68
+ export function trashTransaction(bookId, transaction) {
82
69
  return __awaiter(this, void 0, void 0, function* () {
83
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/trash`).setMethod('PATCH').setPayload(transaction).fetch();
70
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions/trash`).setMethod('PATCH').setPayload(transaction).fetch();
84
71
  return response.data;
85
72
  });
86
73
  }
87
- function restoreTransaction(bookId, transaction) {
74
+ export function restoreTransaction(bookId, transaction) {
88
75
  return __awaiter(this, void 0, void 0, function* () {
89
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/restore`).setMethod('PATCH').setPayload(transaction).fetch();
76
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions/restore`).setMethod('PATCH').setPayload(transaction).fetch();
90
77
  return response.data;
91
78
  });
92
79
  }
93
- function getTransaction(bookId, id) {
80
+ export function getTransaction(bookId, id) {
94
81
  return __awaiter(this, void 0, void 0, function* () {
95
- var response = yield new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions/${id}`).setMethod('GET').fetch();
82
+ var response = yield new HttpBooksApiV5Request(`${bookId}/transactions/${id}`).setMethod('GET').fetch();
96
83
  return response.data;
97
84
  });
98
85
  }
99
- function searchTransactions(bookId, query, limit, cursor) {
86
+ export function searchTransactions(bookId, query, limit, cursor) {
100
87
  return __awaiter(this, void 0, void 0, function* () {
101
- if (query == null) {
88
+ if (!query) {
102
89
  query = "";
103
90
  }
104
- var request = new http_api_request_1.HttpBooksApiV5Request(`${bookId}/transactions`);
91
+ if (!limit) {
92
+ limit = 100;
93
+ }
94
+ var request = new HttpBooksApiV5Request(`${bookId}/transactions`);
105
95
  request.addParam('query', query);
106
96
  request.addParam('limit', limit);
107
97
  if (cursor != null) {
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,12 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
8
  });
10
9
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getUser = getUser;
13
- const http_api_request_1 = require("./http-api-request");
14
- function getUser() {
10
+ import { HttpApiV5Request } from "./http-api-request.js";
11
+ export function getUser() {
15
12
  return __awaiter(this, void 0, void 0, function* () {
16
- const res = yield new http_api_request_1.HttpApiV5Request(`user`).setMethod('GET').fetch();
13
+ const res = yield new HttpApiV5Request(`user`).setMethod('GET').fetch();
17
14
  return res.data;
18
15
  });
19
16
  }