qiscus-sdk-core 2.12.4 → 2.12.6-logger

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,35 +1,45 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports["default"] = void 0;
9
-
10
8
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
-
12
9
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
-
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
11
  var _superagent = _interopRequireDefault(require("superagent"));
15
-
16
- var HttpAdapter = /*#__PURE__*/function () {
12
+ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
13
+ /**
14
+ * @param {Object} options
15
+ * @param {string} options.baseURL
16
+ * @param {string} options.AppId
17
+ * @param {string} options.userId
18
+ * @param {string} options.version
19
+ * @param {() => Object} options.getCustomHeader
20
+ * @param {import('../../logger').Logger} options.logger
21
+ */
17
22
  function HttpAdapter(_ref) {
18
23
  var baseURL = _ref.baseURL,
19
- AppId = _ref.AppId,
20
- userId = _ref.userId,
21
- version = _ref.version,
22
- getCustomHeader = _ref.getCustomHeader;
24
+ AppId = _ref.AppId,
25
+ userId = _ref.userId,
26
+ version = _ref.version,
27
+ getCustomHeader = _ref.getCustomHeader,
28
+ logger = _ref.logger;
23
29
  (0, _classCallCheck2["default"])(this, HttpAdapter);
30
+ /**
31
+ * @type {import('pino').Logger}
32
+ */
33
+ (0, _defineProperty2["default"])(this, "logger", void 0);
24
34
  this.baseURL = baseURL;
25
35
  this.token = null;
26
36
  this.userId = userId;
27
37
  this.AppId = AppId;
28
38
  this.version = version;
29
39
  this.getCustomHeader = getCustomHeader;
40
+ this.logger = logger.child('HttpAdapter');
30
41
  }
31
-
32
- (0, _createClass2["default"])(HttpAdapter, [{
42
+ return (0, _createClass2["default"])(HttpAdapter, [{
33
43
  key: "setToken",
34
44
  value: function setToken(token) {
35
45
  this.token = token;
@@ -38,31 +48,32 @@ var HttpAdapter = /*#__PURE__*/function () {
38
48
  key: "get",
39
49
  value: function get(path) {
40
50
  var _this = this;
41
-
42
51
  var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
43
52
  var options = arguments.length > 2 ? arguments[2] : undefined;
44
53
  return new Promise(function (resolve, reject) {
54
+ _this.logger.debug("".concat(_this.baseURL, "/").concat(path), {
55
+ method: 'get'
56
+ });
45
57
  var req = _superagent["default"].get("".concat(_this.baseURL, "/").concat(path));
46
-
47
58
  if (options && options.baseURL) req = _superagent["default"].get("".concat(options.baseURL, "/").concat(path));
48
59
  req = _this.setupHeaders(req, headers);
49
60
  req.end(function (err, res) {
50
61
  if (err) {
51
62
  _this._rethrowIfExpiredToken(err);
52
-
53
63
  return reject(err);
54
64
  }
55
-
56
65
  return resolve(res);
57
66
  });
58
67
  });
59
- } // eslint-disable-next-line
60
-
68
+ }
69
+ // eslint-disable-next-line
61
70
  }, {
62
71
  key: "get_request",
63
72
  value: function get_request(path) {
73
+ this.logger.debug("".concat(this.baseURL, "/").concat(path), {
74
+ method: 'get_request'
75
+ });
64
76
  var req = _superagent["default"].get("".concat(this.baseURL, "/").concat(path));
65
-
66
77
  req = this.setupHeaders(req, {});
67
78
  return req;
68
79
  }
@@ -70,43 +81,42 @@ var HttpAdapter = /*#__PURE__*/function () {
70
81
  key: "post",
71
82
  value: function post(path) {
72
83
  var _this2 = this;
73
-
74
84
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
75
85
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
76
86
  return new Promise(function (resolve, reject) {
87
+ _this2.logger.debug("".concat(_this2.baseURL, "/").concat(path), {
88
+ method: 'post'
89
+ });
77
90
  var req = _superagent["default"].post("".concat(_this2.baseURL, "/").concat(path));
78
-
79
91
  req = _this2.setupHeaders(req, headers);
80
92
  req.send(body).set("Content-Type", "application/x-www-form-urlencoded").end(function (err, res) {
81
93
  if (err) {
82
94
  _this2._rethrowIfExpiredToken(err);
83
-
84
95
  return reject(err);
85
96
  }
86
-
87
97
  return resolve(res);
88
98
  });
89
99
  });
90
- } // eslint-disable-next-line
100
+ }
91
101
 
102
+ // eslint-disable-next-line
92
103
  }, {
93
104
  key: "post_json",
94
105
  value: function post_json(path) {
95
106
  var _this3 = this;
96
-
97
107
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
98
108
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
99
109
  return new Promise(function (resolve, reject) {
110
+ _this3.logger.debug("".concat(_this3.baseURL, "/").concat(path), {
111
+ method: 'post_json'
112
+ });
100
113
  var req = _superagent["default"].post("".concat(_this3.baseURL, "/").concat(path));
101
-
102
114
  req = _this3.setupHeaders(req, headers);
103
115
  req.send(body).set("Content-Type", "application/json").end(function (err, res) {
104
116
  if (err) {
105
117
  _this3._rethrowIfExpiredToken(err);
106
-
107
118
  return reject(err);
108
119
  }
109
-
110
120
  return resolve(res);
111
121
  });
112
122
  });
@@ -115,20 +125,19 @@ var HttpAdapter = /*#__PURE__*/function () {
115
125
  key: "put",
116
126
  value: function put(path) {
117
127
  var _this4 = this;
118
-
119
128
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
120
129
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
121
130
  return new Promise(function (resolve, reject) {
131
+ _this4.logger.debug("".concat(_this4.baseURL, "/").concat(path), {
132
+ method: 'put'
133
+ });
122
134
  var req = _superagent["default"].put("".concat(_this4.baseURL, "/").concat(path));
123
-
124
135
  req = _this4.setupHeaders(req, headers);
125
136
  req.send(body).set("Content-Type", "application/x-www-form-urlencoded").end(function (err, res) {
126
137
  if (err) {
127
138
  _this4._rethrowIfExpiredToken(err);
128
-
129
139
  return reject(err);
130
140
  }
131
-
132
141
  return resolve(res);
133
142
  });
134
143
  });
@@ -137,20 +146,19 @@ var HttpAdapter = /*#__PURE__*/function () {
137
146
  key: "patch",
138
147
  value: function patch(path) {
139
148
  var _this5 = this;
140
-
141
149
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
142
150
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
143
151
  return new Promise(function (resolve, reject) {
152
+ _this5.logger.debug("".concat(_this5.baseURL, "/").concat(path), {
153
+ method: 'patch'
154
+ });
144
155
  var req = _superagent["default"].patch("".concat(_this5.baseURL, "/").concat(path));
145
-
146
156
  req = _this5.setupHeaders(req, headers);
147
157
  req.send(body).set("Content-Type", "application/x-www-form-urlencoded").end(function (err, res) {
148
158
  if (err) {
149
159
  _this5._rethrowIfExpiredToken(err);
150
-
151
160
  return reject(err);
152
161
  }
153
-
154
162
  return resolve(res);
155
163
  });
156
164
  });
@@ -159,20 +167,19 @@ var HttpAdapter = /*#__PURE__*/function () {
159
167
  key: "del",
160
168
  value: function del(path) {
161
169
  var _this6 = this;
162
-
163
170
  var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
164
171
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
165
172
  return new Promise(function (resolve, reject) {
173
+ _this6.logger.debug("".concat(_this6.baseURL, "/").concat(path), {
174
+ method: 'del'
175
+ });
166
176
  var req = _superagent["default"].del("".concat(_this6.baseURL, "/").concat(path));
167
-
168
177
  req = _this6.setupHeaders(req, headers);
169
178
  req.send(body).set("Content-Type", "application/json").end(function (err, res) {
170
179
  if (err) {
171
180
  _this6._rethrowIfExpiredToken(err);
172
-
173
181
  return reject(err);
174
182
  }
175
-
176
183
  return resolve(res);
177
184
  });
178
185
  });
@@ -181,24 +188,19 @@ var HttpAdapter = /*#__PURE__*/function () {
181
188
  key: "setupHeaders",
182
189
  value: function setupHeaders(req) {
183
190
  var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
184
-
185
191
  // let's give this default Authorization Header
186
192
  if (this.AppId != null) {
187
193
  req.set("QISCUS-SDK-APP-ID", "".concat(this.AppId));
188
194
  }
189
-
190
195
  if (this.userId != null) {
191
196
  req.set("QISCUS-SDK-USER-ID", "".concat(this.userId));
192
197
  }
193
-
194
198
  if (this.token != null) {
195
199
  req.set("QISCUS-SDK-TOKEN", "".concat(this.token));
196
200
  }
197
-
198
201
  if (this.version != null) {
199
202
  req.set("QISCUS-SDK-VERSION", "".concat(this.version));
200
203
  }
201
-
202
204
  if (this.getCustomHeader != null) {
203
205
  var customHeaders = this.getCustomHeader();
204
206
  Object.keys(customHeaders).filter(function (key) {
@@ -206,32 +208,29 @@ var HttpAdapter = /*#__PURE__*/function () {
206
208
  }).forEach(function (key) {
207
209
  req.set(key, customHeaders[key]);
208
210
  });
209
- } // Return the req if no headers attached
210
-
211
-
212
- if (Object.keys(headers).length < 1) return req; // now let's process custom header
213
-
211
+ }
212
+ // Return the req if no headers attached
213
+ if (Object.keys(headers).length < 1) return req;
214
+ // now let's process custom header
214
215
  for (var key in headers) {
215
216
  if (headers.hasOwnProperty(key)) req.set(key, headers[key]);
216
217
  }
217
-
218
218
  return req;
219
219
  }
220
220
  }, {
221
221
  key: "_rethrowIfExpiredToken",
222
222
  value: function _rethrowIfExpiredToken(err) {
223
- var _err$response, _err$response2, _body$error, _body$error$message;
224
-
223
+ var _err$response, _err$response2, _body$error;
225
224
  var status = (_err$response = err.response) === null || _err$response === void 0 ? void 0 : _err$response.status;
226
225
  var body = (_err$response2 = err.response) === null || _err$response2 === void 0 ? void 0 : _err$response2.body;
227
-
228
- if (status === 403 && ((_body$error = body.error) === null || _body$error === void 0 ? void 0 : (_body$error$message = _body$error.message) === null || _body$error$message === void 0 ? void 0 : _body$error$message.toLowerCase()) === 'unauthorized. token is expired') {
226
+ if (status === 403 && ((_body$error = body.error) === null || _body$error === void 0 || (_body$error = _body$error.message) === null || _body$error === void 0 ? void 0 : _body$error.toLowerCase()) === 'unauthorized. token is expired') {
227
+ this.logger.debug('Token expired', {
228
+ status: status,
229
+ body: body
230
+ });
229
231
  throw new Error('Token expired');
230
232
  }
231
233
  }
232
234
  }]);
233
- return HttpAdapter;
234
235
  }();
235
-
236
- exports["default"] = HttpAdapter;
237
236
  module.exports = exports.default;