@seamapi/http 0.3.0 → 0.4.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/dist/connect.cjs CHANGED
@@ -1,13 +1,98 @@
1
1
  'use strict';
2
2
 
3
3
  var axios = require('axios');
4
+ var axiosBetterStacktrace = require('axios-better-stacktrace');
4
5
  var axiosRetry = require('axios-retry');
5
6
 
6
7
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
8
 
8
9
  var axios__default = /*#__PURE__*/_interopDefault(axios);
10
+ var axiosBetterStacktrace__default = /*#__PURE__*/_interopDefault(axiosBetterStacktrace);
9
11
  var axiosRetry__default = /*#__PURE__*/_interopDefault(axiosRetry);
10
12
 
13
+ // src/lib/seam/connect/error-interceptor.ts
14
+
15
+ // src/lib/seam/connect/seam-http-error.ts
16
+ var SeamHttpApiError = class extends Error {
17
+ constructor(error, statusCode, requestId) {
18
+ const { type, message, data } = error;
19
+ super(message);
20
+ this.name = this.constructor.name;
21
+ Error.captureStackTrace(this, this.constructor);
22
+ this.code = type;
23
+ this.statusCode = statusCode;
24
+ this.requestId = requestId;
25
+ if (data != null)
26
+ this.data = data;
27
+ }
28
+ };
29
+ var isSeamHttpApiError = (error) => {
30
+ return error instanceof SeamHttpApiError;
31
+ };
32
+ var SeamHttpUnauthorizedError = class extends SeamHttpApiError {
33
+ constructor(requestId) {
34
+ const type = "unauthorized";
35
+ const status = 401;
36
+ super({ type, message: "Unauthorized" }, status, requestId);
37
+ this.name = this.constructor.name;
38
+ Error.captureStackTrace(this, this.constructor);
39
+ this.code = type;
40
+ this.statusCode = status;
41
+ this.requestId = requestId;
42
+ }
43
+ };
44
+ var isSeamHttpUnauthorizedError = (error) => {
45
+ return error instanceof SeamHttpUnauthorizedError;
46
+ };
47
+ var SeamHttpInvalidInputError = class extends SeamHttpApiError {
48
+ constructor(error, statusCode, requestId) {
49
+ super(error, statusCode, requestId);
50
+ this.name = this.constructor.name;
51
+ Error.captureStackTrace(this, this.constructor);
52
+ this.code = "invalid_input";
53
+ }
54
+ };
55
+ var isSeamHttpInvalidInputError = (error) => {
56
+ return error instanceof SeamHttpInvalidInputError;
57
+ };
58
+
59
+ // src/lib/seam/connect/error-interceptor.ts
60
+ var errorInterceptor = async (err) => {
61
+ if (!axios.isAxiosError(err))
62
+ throw err;
63
+ const { response } = err;
64
+ const status = response?.status;
65
+ const headers = response?.headers;
66
+ const requestId = headers?.["seam-request-id"] ?? "";
67
+ if (status == null)
68
+ throw err;
69
+ if (status === 401) {
70
+ throw new SeamHttpUnauthorizedError(requestId);
71
+ }
72
+ if (!isApiErrorResponse(response))
73
+ throw err;
74
+ const { type } = response.data.error;
75
+ const args = [response.data.error, status, requestId];
76
+ if (type === "invalid_input")
77
+ throw new SeamHttpInvalidInputError(...args);
78
+ throw new SeamHttpApiError(...args);
79
+ };
80
+ var isApiErrorResponse = (response) => {
81
+ if (response == null)
82
+ return false;
83
+ const { headers, data } = response;
84
+ if (headers == null)
85
+ return false;
86
+ const contentType = headers["content-type"];
87
+ if (typeof contentType === "string" && !contentType.startsWith("application/json")) {
88
+ return false;
89
+ }
90
+ if (typeof data === "object" && data != null) {
91
+ return "error" in data && typeof data.error === "object" && data.error != null && "type" in data.error && typeof data.error.type === "string";
92
+ }
93
+ return false;
94
+ };
95
+
11
96
  // src/lib/seam/connect/options.ts
12
97
  var isSeamHttpOptionsWithClient = (options) => {
13
98
  if (!("client" in options))
@@ -241,11 +326,13 @@ var createClient = (options) => {
241
326
  paramsSerializer,
242
327
  ...options.axiosOptions
243
328
  });
329
+ axiosBetterStacktrace__default.default(axios__default.default);
244
330
  axiosRetry__default.default(client, {
245
331
  retries: 2,
246
332
  retryDelay: axiosRetry.exponentialDelay,
247
333
  ...options.axiosRetryOptions
248
334
  });
335
+ client.interceptors.response.use(void 0, errorInterceptor);
249
336
  return client;
250
337
  };
251
338
 
@@ -1939,24 +2026,31 @@ exports.SeamHttpAcsCredentials = SeamHttpAcsCredentials;
1939
2026
  exports.SeamHttpAcsSystems = SeamHttpAcsSystems;
1940
2027
  exports.SeamHttpAcsUsers = SeamHttpAcsUsers;
1941
2028
  exports.SeamHttpActionAttempts = SeamHttpActionAttempts;
2029
+ exports.SeamHttpApiError = SeamHttpApiError;
1942
2030
  exports.SeamHttpClientSessions = SeamHttpClientSessions;
1943
2031
  exports.SeamHttpConnectWebviews = SeamHttpConnectWebviews;
1944
2032
  exports.SeamHttpConnectedAccounts = SeamHttpConnectedAccounts;
1945
2033
  exports.SeamHttpDevices = SeamHttpDevices;
1946
2034
  exports.SeamHttpDevicesUnmanaged = SeamHttpDevicesUnmanaged;
1947
2035
  exports.SeamHttpEvents = SeamHttpEvents;
2036
+ exports.SeamHttpInvalidInputError = SeamHttpInvalidInputError;
1948
2037
  exports.SeamHttpInvalidOptionsError = SeamHttpInvalidOptionsError;
1949
2038
  exports.SeamHttpLocks = SeamHttpLocks;
1950
2039
  exports.SeamHttpNoiseSensors = SeamHttpNoiseSensors;
1951
2040
  exports.SeamHttpNoiseSensorsNoiseThresholds = SeamHttpNoiseSensorsNoiseThresholds;
1952
2041
  exports.SeamHttpThermostats = SeamHttpThermostats;
1953
2042
  exports.SeamHttpThermostatsClimateSettingSchedules = SeamHttpThermostatsClimateSettingSchedules;
2043
+ exports.SeamHttpUnauthorizedError = SeamHttpUnauthorizedError;
1954
2044
  exports.SeamHttpWebhooks = SeamHttpWebhooks;
1955
2045
  exports.SeamHttpWorkspaces = SeamHttpWorkspaces;
1956
2046
  exports.UnserializableParamError = UnserializableParamError;
2047
+ exports.errorInterceptor = errorInterceptor;
2048
+ exports.isSeamHttpApiError = isSeamHttpApiError;
2049
+ exports.isSeamHttpInvalidInputError = isSeamHttpInvalidInputError;
1957
2050
  exports.isSeamHttpOptionsWithApiKey = isSeamHttpOptionsWithApiKey;
1958
2051
  exports.isSeamHttpOptionsWithClient = isSeamHttpOptionsWithClient;
1959
2052
  exports.isSeamHttpOptionsWithClientSessionToken = isSeamHttpOptionsWithClientSessionToken;
2053
+ exports.isSeamHttpUnauthorizedError = isSeamHttpUnauthorizedError;
1960
2054
  exports.paramsSerializer = paramsSerializer;
1961
2055
  //# sourceMappingURL=out.js.map
1962
2056
  //# sourceMappingURL=connect.cjs.map