@seamapi/http 0.6.0 → 0.7.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/README.md CHANGED
@@ -38,7 +38,7 @@ $ npm install @seamapi/http
38
38
  ## Usage
39
39
 
40
40
  ```ts
41
- import { SeamHttp } from '@seamapi/http'
41
+ import { SeamHttp } from '@seamapi/http/connect'
42
42
 
43
43
  const seam = new SeamHttp('your-api-key')
44
44
  const devices = await seam.devices.list()
@@ -213,7 +213,7 @@ Options are deep merged with the default options.
213
213
  The Axios client is exposed and may be used or configured directly:
214
214
 
215
215
  ```ts
216
- import { SeamHttp, DevicesListResponse } from '@seamapi/http'
216
+ import { SeamHttp, DevicesListResponse } from '@seamapi/http/connect'
217
217
 
218
218
  const seam = new SeamHttp()
219
219
 
package/dist/connect.cjs CHANGED
@@ -10,90 +10,8 @@ var axios__default = /*#__PURE__*/_interopDefault(axios);
10
10
  var axiosBetterStacktrace__default = /*#__PURE__*/_interopDefault(axiosBetterStacktrace);
11
11
  var axiosRetry__default = /*#__PURE__*/_interopDefault(axiosRetry);
12
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
-
96
13
  // src/lib/seam/connect/options.ts
14
+ var isSeamHttpMultiWorkspaceOptionsWithClient = (options) => isSeamHttpOptionsWithClient(options);
97
15
  var isSeamHttpOptionsWithClient = (options) => {
98
16
  if (!("client" in options))
99
17
  return false;
@@ -153,16 +71,11 @@ var isSeamHttpOptionsWithClientSessionToken = (options) => {
153
71
  }
154
72
  return true;
155
73
  };
156
- var isSeamHttpOptionsWithConsoleSessionToken = (options) => {
74
+ var isSeamHttpMultiWorkspaceOptionsWithConsoleSessionToken = (options) => {
157
75
  if (!("consoleSessionToken" in options))
158
76
  return false;
159
77
  if (options.consoleSessionToken == null)
160
78
  return false;
161
- if (!("workspaceId" in options) || options.workspaceId == null) {
162
- throw new SeamHttpInvalidOptionsError(
163
- "Must pass a workspaceId when using a consoleSessionToken"
164
- );
165
- }
166
79
  if ("apiKey" in options && options.apiKey != null) {
167
80
  throw new SeamHttpInvalidOptionsError(
168
81
  "The apiKey option cannot be used with the consoleSessionToken option"
@@ -180,16 +93,22 @@ var isSeamHttpOptionsWithConsoleSessionToken = (options) => {
180
93
  }
181
94
  return true;
182
95
  };
183
- var isSeamHttpOptionsWithPersonalAccessToken = (options) => {
184
- if (!("personalAccessToken" in options))
185
- return false;
186
- if (options.personalAccessToken == null)
96
+ var isSeamHttpOptionsWithConsoleSessionToken = (options) => {
97
+ if (!isSeamHttpMultiWorkspaceOptionsWithConsoleSessionToken(options)) {
187
98
  return false;
99
+ }
188
100
  if (!("workspaceId" in options) || options.workspaceId == null) {
189
101
  throw new SeamHttpInvalidOptionsError(
190
- "Must pass a workspaceId when using a personalAccessToken"
102
+ "Must pass a workspaceId when using a consoleSessionToken"
191
103
  );
192
104
  }
105
+ return true;
106
+ };
107
+ var isSeamHttpMultiWorkspaceOptionsWithPersonalAccessToken = (options) => {
108
+ if (!("personalAccessToken" in options))
109
+ return false;
110
+ if (options.personalAccessToken == null)
111
+ return false;
193
112
  if ("apiKey" in options && options.apiKey != null) {
194
113
  throw new SeamHttpInvalidOptionsError(
195
114
  "The apiKey option cannot be used with the personalAccessToken option"
@@ -207,6 +126,17 @@ var isSeamHttpOptionsWithPersonalAccessToken = (options) => {
207
126
  }
208
127
  return true;
209
128
  };
129
+ var isSeamHttpOptionsWithPersonalAccessToken = (options) => {
130
+ if (!isSeamHttpMultiWorkspaceOptionsWithPersonalAccessToken(options)) {
131
+ return false;
132
+ }
133
+ if (!("workspaceId" in options) || options.workspaceId == null) {
134
+ throw new SeamHttpInvalidOptionsError(
135
+ "Must pass a workspaceId when using a personalAccessToken"
136
+ );
137
+ }
138
+ return true;
139
+ };
210
140
  var SeamHttpInvalidOptionsError = class extends Error {
211
141
  constructor(message) {
212
142
  super(`SeamHttp received invalid options: ${message}`);
@@ -214,6 +144,8 @@ var SeamHttpInvalidOptionsError = class extends Error {
214
144
  Error.captureStackTrace(this, this.constructor);
215
145
  }
216
146
  };
147
+ var SeamHttpMultiWorkspaceInvalidOptionsError = class extends SeamHttpInvalidOptionsError {
148
+ };
217
149
 
218
150
  // src/lib/seam/connect/auth.ts
219
151
  var getAuthHeaders = (options) => {
@@ -226,10 +158,10 @@ var getAuthHeaders = (options) => {
226
158
  if (isSeamHttpOptionsWithClientSessionToken(options)) {
227
159
  return getAuthHeadersForClientSessionToken(options);
228
160
  }
229
- if (isSeamHttpOptionsWithConsoleSessionToken(options)) {
161
+ if (isSeamHttpMultiWorkspaceOptionsWithConsoleSessionToken(options) || isSeamHttpOptionsWithConsoleSessionToken(options)) {
230
162
  return getAuthHeadersForConsoleSessionToken(options);
231
163
  }
232
- if (isSeamHttpOptionsWithPersonalAccessToken(options)) {
164
+ if (isSeamHttpMultiWorkspaceOptionsWithPersonalAccessToken(options) || isSeamHttpOptionsWithPersonalAccessToken(options)) {
233
165
  return getAuthHeadersForPersonalAccessToken(options);
234
166
  }
235
167
  throw new SeamHttpInvalidOptionsError(
@@ -238,8 +170,8 @@ var getAuthHeaders = (options) => {
238
170
  "an apiKey,",
239
171
  "clientSessionToken,",
240
172
  "publishableKey,",
241
- "consoleSessionToken with a workspaceId",
242
- "or personalAccessToken with a workspaceId"
173
+ "consoleSessionToken",
174
+ "or personalAccessToken"
243
175
  ].join(" ")
244
176
  );
245
177
  };
@@ -303,8 +235,9 @@ var getAuthHeadersForClientSessionToken = ({
303
235
  };
304
236
  var getAuthHeadersForConsoleSessionToken = ({
305
237
  consoleSessionToken,
306
- workspaceId
238
+ ...options
307
239
  }) => {
240
+ const workspaceId = "workspaceId" in options ? options.workspaceId : void 0;
308
241
  if (isAccessToken(consoleSessionToken)) {
309
242
  throw new SeamHttpInvalidTokenError(
310
243
  "An Access Token cannot be used as a consoleSessionToken"
@@ -327,13 +260,14 @@ var getAuthHeadersForConsoleSessionToken = ({
327
260
  }
328
261
  return {
329
262
  authorization: `Bearer ${consoleSessionToken}`,
330
- "seam-workspace-id": workspaceId
263
+ ...workspaceId != null ? { "seam-workspace-id": workspaceId } : {}
331
264
  };
332
265
  };
333
266
  var getAuthHeadersForPersonalAccessToken = ({
334
267
  personalAccessToken,
335
- workspaceId
268
+ ...options
336
269
  }) => {
270
+ const workspaceId = "workspaceId" in options ? options.workspaceId : void 0;
337
271
  if (isJwt(personalAccessToken)) {
338
272
  throw new SeamHttpInvalidTokenError(
339
273
  "A JWT cannot be used as a personalAccessToken"
@@ -356,7 +290,7 @@ var getAuthHeadersForPersonalAccessToken = ({
356
290
  }
357
291
  return {
358
292
  authorization: `Bearer ${personalAccessToken}`,
359
- "seam-workspace-id": workspaceId
293
+ ...workspaceId != null ? { "seam-workspace-id": workspaceId } : {}
360
294
  };
361
295
  };
362
296
  var getAuthHeadersForPublishableKey = (publishableKey) => {
@@ -416,6 +350,87 @@ var isSeamToken = (token) => token.startsWith(tokenPrefix);
416
350
  var isPublishableKey = (token) => token.startsWith(publishableKeyTokenPrefix);
417
351
  var isEmail = (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
418
352
 
353
+ // src/lib/seam/connect/seam-http-error.ts
354
+ var SeamHttpApiError = class extends Error {
355
+ constructor(error, statusCode, requestId) {
356
+ const { type, message, data } = error;
357
+ super(message);
358
+ this.name = this.constructor.name;
359
+ Error.captureStackTrace(this, this.constructor);
360
+ this.code = type;
361
+ this.statusCode = statusCode;
362
+ this.requestId = requestId;
363
+ if (data != null)
364
+ this.data = data;
365
+ }
366
+ };
367
+ var isSeamHttpApiError = (error) => {
368
+ return error instanceof SeamHttpApiError;
369
+ };
370
+ var SeamHttpUnauthorizedError = class extends SeamHttpApiError {
371
+ constructor(requestId) {
372
+ const type = "unauthorized";
373
+ const status = 401;
374
+ super({ type, message: "Unauthorized" }, status, requestId);
375
+ this.name = this.constructor.name;
376
+ Error.captureStackTrace(this, this.constructor);
377
+ this.code = type;
378
+ this.statusCode = status;
379
+ this.requestId = requestId;
380
+ }
381
+ };
382
+ var isSeamHttpUnauthorizedError = (error) => {
383
+ return error instanceof SeamHttpUnauthorizedError;
384
+ };
385
+ var SeamHttpInvalidInputError = class extends SeamHttpApiError {
386
+ constructor(error, statusCode, requestId) {
387
+ super(error, statusCode, requestId);
388
+ this.name = this.constructor.name;
389
+ Error.captureStackTrace(this, this.constructor);
390
+ this.code = "invalid_input";
391
+ }
392
+ };
393
+ var isSeamHttpInvalidInputError = (error) => {
394
+ return error instanceof SeamHttpInvalidInputError;
395
+ };
396
+
397
+ // src/lib/seam/connect/error-interceptor.ts
398
+ var errorInterceptor = async (err) => {
399
+ if (!axios.isAxiosError(err))
400
+ throw err;
401
+ const { response } = err;
402
+ const status = response?.status;
403
+ const headers = response?.headers;
404
+ const requestId = headers?.["seam-request-id"] ?? "";
405
+ if (status == null)
406
+ throw err;
407
+ if (status === 401) {
408
+ throw new SeamHttpUnauthorizedError(requestId);
409
+ }
410
+ if (!isApiErrorResponse(response))
411
+ throw err;
412
+ const { type } = response.data.error;
413
+ const args = [response.data.error, status, requestId];
414
+ if (type === "invalid_input")
415
+ throw new SeamHttpInvalidInputError(...args);
416
+ throw new SeamHttpApiError(...args);
417
+ };
418
+ var isApiErrorResponse = (response) => {
419
+ if (response == null)
420
+ return false;
421
+ const { headers, data } = response;
422
+ if (headers == null)
423
+ return false;
424
+ const contentType = headers["content-type"];
425
+ if (typeof contentType === "string" && !contentType.startsWith("application/json")) {
426
+ return false;
427
+ }
428
+ if (typeof data === "object" && data != null) {
429
+ return "error" in data && typeof data.error === "object" && data.error != null && "type" in data.error && typeof data.error.type === "string";
430
+ }
431
+ return false;
432
+ };
433
+
419
434
  // src/lib/params-serializer.ts
420
435
  var paramsSerializer = (params) => {
421
436
  const searchParams = new URLSearchParams();
@@ -497,6 +512,8 @@ var parseOptions = (apiKeyOrOptions) => {
497
512
  const options = getNormalizedOptions(apiKeyOrOptions);
498
513
  if (isSeamHttpOptionsWithClient(options))
499
514
  return options;
515
+ if (isSeamHttpMultiWorkspaceOptionsWithClient(options))
516
+ return options;
500
517
  return {
501
518
  axiosOptions: {
502
519
  baseURL: options.endpoint ?? getEndpointFromEnv() ?? defaultEndpoint,
@@ -2679,6 +2696,46 @@ var SeamHttp = class _SeamHttp {
2679
2696
  }
2680
2697
  };
2681
2698
 
2699
+ // src/lib/seam/connect/seam-http-multi-workspace.ts
2700
+ var SeamHttpMultiWorkspace = class _SeamHttpMultiWorkspace {
2701
+ constructor(options) {
2702
+ const clientOptions = parseOptions(options);
2703
+ this.client = createClient(clientOptions);
2704
+ }
2705
+ static fromClient(client, options = {}) {
2706
+ const constructorOptions = { ...options, client };
2707
+ if (!isSeamHttpMultiWorkspaceOptionsWithClient(constructorOptions)) {
2708
+ throw new SeamHttpMultiWorkspaceInvalidOptionsError("Missing client");
2709
+ }
2710
+ return new _SeamHttpMultiWorkspace(constructorOptions);
2711
+ }
2712
+ static fromConsoleSessionToken(consoleSessionToken, options = {}) {
2713
+ const constructorOptions = { ...options, consoleSessionToken };
2714
+ if (!isSeamHttpMultiWorkspaceOptionsWithConsoleSessionToken(
2715
+ constructorOptions
2716
+ )) {
2717
+ throw new SeamHttpMultiWorkspaceInvalidOptionsError(
2718
+ "Missing consoleSessionToken"
2719
+ );
2720
+ }
2721
+ return new _SeamHttpMultiWorkspace(constructorOptions);
2722
+ }
2723
+ static fromPersonalAccessToken(personalAccessToken, options = {}) {
2724
+ const constructorOptions = { ...options, personalAccessToken };
2725
+ if (!isSeamHttpMultiWorkspaceOptionsWithPersonalAccessToken(
2726
+ constructorOptions
2727
+ )) {
2728
+ throw new SeamHttpMultiWorkspaceInvalidOptionsError(
2729
+ "Missing personalAccessToken"
2730
+ );
2731
+ }
2732
+ return new _SeamHttpMultiWorkspace(constructorOptions);
2733
+ }
2734
+ get workspaces() {
2735
+ return SeamHttpWorkspaces.fromClient(this.client);
2736
+ }
2737
+ };
2738
+
2682
2739
  exports.SeamHttp = SeamHttp;
2683
2740
  exports.SeamHttpAccessCodes = SeamHttpAccessCodes;
2684
2741
  exports.SeamHttpAccessCodesUnmanaged = SeamHttpAccessCodesUnmanaged;
@@ -2697,7 +2754,10 @@ exports.SeamHttpDevicesUnmanaged = SeamHttpDevicesUnmanaged;
2697
2754
  exports.SeamHttpEvents = SeamHttpEvents;
2698
2755
  exports.SeamHttpInvalidInputError = SeamHttpInvalidInputError;
2699
2756
  exports.SeamHttpInvalidOptionsError = SeamHttpInvalidOptionsError;
2757
+ exports.SeamHttpInvalidTokenError = SeamHttpInvalidTokenError;
2700
2758
  exports.SeamHttpLocks = SeamHttpLocks;
2759
+ exports.SeamHttpMultiWorkspace = SeamHttpMultiWorkspace;
2760
+ exports.SeamHttpMultiWorkspaceInvalidOptionsError = SeamHttpMultiWorkspaceInvalidOptionsError;
2701
2761
  exports.SeamHttpNoiseSensors = SeamHttpNoiseSensors;
2702
2762
  exports.SeamHttpNoiseSensorsNoiseThresholds = SeamHttpNoiseSensorsNoiseThresholds;
2703
2763
  exports.SeamHttpThermostats = SeamHttpThermostats;
@@ -2710,6 +2770,9 @@ exports.UnserializableParamError = UnserializableParamError;
2710
2770
  exports.errorInterceptor = errorInterceptor;
2711
2771
  exports.isSeamHttpApiError = isSeamHttpApiError;
2712
2772
  exports.isSeamHttpInvalidInputError = isSeamHttpInvalidInputError;
2773
+ exports.isSeamHttpMultiWorkspaceOptionsWithClient = isSeamHttpMultiWorkspaceOptionsWithClient;
2774
+ exports.isSeamHttpMultiWorkspaceOptionsWithConsoleSessionToken = isSeamHttpMultiWorkspaceOptionsWithConsoleSessionToken;
2775
+ exports.isSeamHttpMultiWorkspaceOptionsWithPersonalAccessToken = isSeamHttpMultiWorkspaceOptionsWithPersonalAccessToken;
2713
2776
  exports.isSeamHttpOptionsWithApiKey = isSeamHttpOptionsWithApiKey;
2714
2777
  exports.isSeamHttpOptionsWithClient = isSeamHttpOptionsWithClient;
2715
2778
  exports.isSeamHttpOptionsWithClientSessionToken = isSeamHttpOptionsWithClientSessionToken;