@seamapi/http 0.1.0 → 0.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.
Files changed (122) hide show
  1. package/dist/connect.cjs +752 -366
  2. package/dist/connect.cjs.map +1 -1
  3. package/dist/connect.d.cts +186 -142
  4. package/lib/params-serializer.d.ts +5 -0
  5. package/lib/params-serializer.js +42 -0
  6. package/lib/params-serializer.js.map +1 -0
  7. package/lib/seam/connect/auth.d.ts +3 -2
  8. package/lib/seam/connect/auth.js +55 -4
  9. package/lib/seam/connect/auth.js.map +1 -1
  10. package/lib/seam/connect/client.d.ts +10 -22
  11. package/lib/seam/connect/client.js +17 -77
  12. package/lib/seam/connect/client.js.map +1 -1
  13. package/lib/seam/connect/index.d.ts +2 -1
  14. package/lib/seam/connect/index.js +2 -1
  15. package/lib/seam/connect/index.js.map +1 -1
  16. package/lib/seam/connect/{client-options.d.ts → options.d.ts} +7 -7
  17. package/lib/seam/connect/{client-options.js → options.js} +5 -10
  18. package/lib/seam/connect/options.js.map +1 -0
  19. package/lib/seam/connect/parse-options.d.ts +6 -2
  20. package/lib/seam/connect/parse-options.js +34 -16
  21. package/lib/seam/connect/parse-options.js.map +1 -1
  22. package/lib/seam/connect/routes/access-codes-unmanaged.d.ts +10 -9
  23. package/lib/seam/connect/routes/access-codes-unmanaged.js +26 -14
  24. package/lib/seam/connect/routes/access-codes-unmanaged.js.map +1 -1
  25. package/lib/seam/connect/routes/access-codes.d.ts +15 -14
  26. package/lib/seam/connect/routes/access-codes.js +29 -17
  27. package/lib/seam/connect/routes/access-codes.js.map +1 -1
  28. package/lib/seam/connect/routes/acs-access-groups.d.ts +13 -12
  29. package/lib/seam/connect/routes/acs-access-groups.js +26 -14
  30. package/lib/seam/connect/routes/acs-access-groups.js.map +1 -1
  31. package/lib/seam/connect/routes/acs-credentials.d.ts +9 -8
  32. package/lib/seam/connect/routes/acs-credentials.js +26 -14
  33. package/lib/seam/connect/routes/acs-credentials.js.map +1 -1
  34. package/lib/seam/connect/routes/acs-systems.d.ts +9 -8
  35. package/lib/seam/connect/routes/acs-systems.js +29 -17
  36. package/lib/seam/connect/routes/acs-systems.js.map +1 -1
  37. package/lib/seam/connect/routes/acs-users.d.ts +18 -11
  38. package/lib/seam/connect/routes/acs-users.js +40 -14
  39. package/lib/seam/connect/routes/acs-users.js.map +1 -1
  40. package/lib/seam/connect/routes/acs.d.ts +5 -4
  41. package/lib/seam/connect/routes/acs.js +26 -14
  42. package/lib/seam/connect/routes/acs.js.map +1 -1
  43. package/lib/seam/connect/routes/action-attempts.d.ts +7 -6
  44. package/lib/seam/connect/routes/action-attempts.js +26 -14
  45. package/lib/seam/connect/routes/action-attempts.js.map +1 -1
  46. package/lib/seam/connect/routes/client-sessions.d.ts +13 -9
  47. package/lib/seam/connect/routes/client-sessions.js +33 -14
  48. package/lib/seam/connect/routes/client-sessions.js.map +1 -1
  49. package/lib/seam/connect/routes/connect-webviews.d.ts +11 -10
  50. package/lib/seam/connect/routes/connect-webviews.js +29 -17
  51. package/lib/seam/connect/routes/connect-webviews.js.map +1 -1
  52. package/lib/seam/connect/routes/connected-accounts.d.ts +8 -7
  53. package/lib/seam/connect/routes/connected-accounts.js +26 -14
  54. package/lib/seam/connect/routes/connected-accounts.js.map +1 -1
  55. package/lib/seam/connect/routes/devices-unmanaged.d.ts +8 -7
  56. package/lib/seam/connect/routes/devices-unmanaged.js +26 -14
  57. package/lib/seam/connect/routes/devices-unmanaged.js.map +1 -1
  58. package/lib/seam/connect/routes/devices.d.ts +10 -9
  59. package/lib/seam/connect/routes/devices.js +26 -14
  60. package/lib/seam/connect/routes/devices.js.map +1 -1
  61. package/lib/seam/connect/routes/events.d.ts +7 -6
  62. package/lib/seam/connect/routes/events.js +26 -14
  63. package/lib/seam/connect/routes/events.js.map +1 -1
  64. package/lib/seam/connect/routes/locks.d.ts +9 -8
  65. package/lib/seam/connect/routes/locks.js +26 -14
  66. package/lib/seam/connect/routes/locks.js.map +1 -1
  67. package/lib/seam/connect/routes/noise-sensors-noise-thresholds.d.ts +10 -9
  68. package/lib/seam/connect/routes/noise-sensors-noise-thresholds.js +26 -14
  69. package/lib/seam/connect/routes/noise-sensors-noise-thresholds.js.map +1 -1
  70. package/lib/seam/connect/routes/noise-sensors.d.ts +5 -4
  71. package/lib/seam/connect/routes/noise-sensors.js +26 -14
  72. package/lib/seam/connect/routes/noise-sensors.js.map +1 -1
  73. package/lib/seam/connect/routes/thermostats-climate-setting-schedules.d.ts +10 -9
  74. package/lib/seam/connect/routes/thermostats-climate-setting-schedules.js +26 -14
  75. package/lib/seam/connect/routes/thermostats-climate-setting-schedules.js.map +1 -1
  76. package/lib/seam/connect/routes/thermostats.d.ts +13 -12
  77. package/lib/seam/connect/routes/thermostats.js +26 -14
  78. package/lib/seam/connect/routes/thermostats.js.map +1 -1
  79. package/lib/seam/connect/routes/webhooks.d.ts +9 -8
  80. package/lib/seam/connect/routes/webhooks.js +26 -14
  81. package/lib/seam/connect/routes/webhooks.js.map +1 -1
  82. package/lib/seam/connect/routes/workspaces.d.ts +8 -7
  83. package/lib/seam/connect/routes/workspaces.js +26 -14
  84. package/lib/seam/connect/routes/workspaces.js.map +1 -1
  85. package/lib/seam/connect/seam-http.d.ts +24 -0
  86. package/lib/seam/connect/seam-http.js +82 -0
  87. package/lib/seam/connect/seam-http.js.map +1 -0
  88. package/package.json +7 -4
  89. package/src/lib/params-serializer.ts +55 -0
  90. package/src/lib/seam/connect/auth.ts +94 -6
  91. package/src/lib/seam/connect/client.ts +22 -127
  92. package/src/lib/seam/connect/env.d.ts +11 -0
  93. package/src/lib/seam/connect/index.ts +2 -1
  94. package/src/lib/seam/connect/{client-options.ts → options.ts} +13 -19
  95. package/src/lib/seam/connect/parse-options.ts +50 -23
  96. package/src/lib/seam/connect/routes/access-codes-unmanaged.ts +44 -31
  97. package/src/lib/seam/connect/routes/access-codes.ts +48 -48
  98. package/src/lib/seam/connect/routes/acs-access-groups.ts +49 -40
  99. package/src/lib/seam/connect/routes/acs-credentials.ts +40 -28
  100. package/src/lib/seam/connect/routes/acs-systems.ts +40 -30
  101. package/src/lib/seam/connect/routes/acs-users.ts +70 -36
  102. package/src/lib/seam/connect/routes/acs.ts +33 -17
  103. package/src/lib/seam/connect/routes/action-attempts.ts +36 -22
  104. package/src/lib/seam/connect/routes/client-sessions.ts +58 -30
  105. package/src/lib/seam/connect/routes/connect-webviews.ts +45 -34
  106. package/src/lib/seam/connect/routes/connected-accounts.ts +40 -25
  107. package/src/lib/seam/connect/routes/devices-unmanaged.ts +39 -25
  108. package/src/lib/seam/connect/routes/devices.ts +39 -31
  109. package/src/lib/seam/connect/routes/events.ts +36 -22
  110. package/src/lib/seam/connect/routes/locks.ts +38 -28
  111. package/src/lib/seam/connect/routes/noise-sensors-noise-thresholds.ts +47 -31
  112. package/src/lib/seam/connect/routes/noise-sensors.ts +33 -17
  113. package/src/lib/seam/connect/routes/thermostats-climate-setting-schedules.ts +47 -31
  114. package/src/lib/seam/connect/routes/thermostats.ts +42 -40
  115. package/src/lib/seam/connect/routes/webhooks.ts +38 -28
  116. package/src/lib/seam/connect/routes/workspaces.ts +38 -25
  117. package/src/lib/seam/connect/seam-http.ts +141 -0
  118. package/lib/seam/connect/axios.d.ts +0 -3
  119. package/lib/seam/connect/axios.js +0 -19
  120. package/lib/seam/connect/axios.js.map +0 -1
  121. package/lib/seam/connect/client-options.js.map +0 -1
  122. package/src/lib/seam/connect/axios.ts +0 -25
@@ -1,19 +1,21 @@
1
- import type { Axios, AxiosRequestConfig } from 'axios'
1
+ import type { Client, ClientOptions } from './client.js'
2
2
 
3
3
  export type SeamHttpOptions =
4
+ | SeamHttpOptionsFromEnv
4
5
  | SeamHttpOptionsWithClient
5
6
  | SeamHttpOptionsWithApiKey
6
7
  | SeamHttpOptionsWithClientSessionToken
7
8
 
8
- interface SeamHttpCommonOptions {
9
+ interface SeamHttpCommonOptions extends ClientOptions {
9
10
  endpoint?: string
10
- axiosOptions?: AxiosRequestConfig
11
- enableLegacyMethodBehaivor?: boolean
12
11
  }
13
12
 
14
- export interface SeamHttpOptionsWithClient
15
- extends Pick<SeamHttpCommonOptions, 'enableLegacyMethodBehaivor'> {
16
- client: Axios
13
+ export type SeamHttpFromPublishableKeyOptions = SeamHttpCommonOptions
14
+
15
+ export type SeamHttpOptionsFromEnv = SeamHttpCommonOptions
16
+
17
+ export interface SeamHttpOptionsWithClient {
18
+ client: Client
17
19
  }
18
20
 
19
21
  export const isSeamHttpOptionsWithClient = (
@@ -22,12 +24,10 @@ export const isSeamHttpOptionsWithClient = (
22
24
  if (!('client' in options)) return false
23
25
  if (options.client == null) return false
24
26
 
25
- const keys = Object.keys(options).filter(
26
- (k) => !['client', 'enableLegacyMethodBehaivor'].includes(k),
27
- )
27
+ const keys = Object.keys(options).filter((k) => k !== 'client')
28
28
  if (keys.length > 0) {
29
29
  throw new SeamHttpInvalidOptionsError(
30
- `The client option cannot be used with any other option except enableLegacyMethodBehaivor, but received: ${keys.join(
30
+ `The client option cannot be used with any other option, but received: ${keys.join(
31
31
  ', ',
32
32
  )}`,
33
33
  )
@@ -48,7 +48,7 @@ export const isSeamHttpOptionsWithApiKey = (
48
48
 
49
49
  if ('clientSessionToken' in options && options.clientSessionToken != null) {
50
50
  throw new SeamHttpInvalidOptionsError(
51
- 'The clientSessionToken option cannot be used with the apiKey option.',
51
+ 'The clientSessionToken option cannot be used with the apiKey option',
52
52
  )
53
53
  }
54
54
 
@@ -68,7 +68,7 @@ export const isSeamHttpOptionsWithClientSessionToken = (
68
68
 
69
69
  if ('apiKey' in options && options.apiKey != null) {
70
70
  throw new SeamHttpInvalidOptionsError(
71
- 'The clientSessionToken option cannot be used with the apiKey option.',
71
+ 'The clientSessionToken option cannot be used with the apiKey option',
72
72
  )
73
73
  }
74
74
 
@@ -82,9 +82,3 @@ export class SeamHttpInvalidOptionsError extends Error {
82
82
  Error.captureStackTrace(this, this.constructor)
83
83
  }
84
84
  }
85
-
86
- // TODO: withSessionToken { sessionToken } or withMultiWorkspaceApiKey { apiKey }?
87
- // export interface SeamHttpOptionsWithSessionToken extends SeamHttpCommonOptions {
88
- // workspaceId: string
89
- // apiKey: string
90
- // }
@@ -1,42 +1,69 @@
1
+ import { getAuthHeaders } from './auth.js'
2
+ import type { ClientOptions } from './client.js'
1
3
  import {
2
4
  isSeamHttpOptionsWithClient,
5
+ isSeamHttpOptionsWithClientSessionToken,
3
6
  type SeamHttpOptions,
4
- } from './client-options.js'
7
+ } from './options.js'
5
8
 
6
- const enableLegacyMethodBehaivorDefault = true
9
+ const defaultEndpoint = 'https://connect.getseam.com'
10
+
11
+ export type Options = SeamHttpOptions & { publishableKey?: string }
7
12
 
8
13
  export const parseOptions = (
9
- apiKeyOrOptions: string | SeamHttpOptions,
10
- ): Required<SeamHttpOptions> => {
14
+ apiKeyOrOptions: string | Options,
15
+ ): ClientOptions => {
16
+ const options = getNormalizedOptions(apiKeyOrOptions)
17
+
18
+ if (isSeamHttpOptionsWithClient(options)) return options
19
+
20
+ return {
21
+ axiosOptions: {
22
+ baseURL: options.endpoint ?? getEndpointFromEnv() ?? defaultEndpoint,
23
+ withCredentials: isSeamHttpOptionsWithClientSessionToken(options),
24
+ ...options.axiosOptions,
25
+ headers: {
26
+ ...getAuthHeaders(options),
27
+ ...options.axiosOptions?.headers,
28
+ },
29
+ },
30
+ axiosRetryOptions: {
31
+ ...options.axiosRetryOptions,
32
+ },
33
+ }
34
+ }
35
+
36
+ const getNormalizedOptions = (
37
+ apiKeyOrOptions: string | Options,
38
+ ): SeamHttpOptions => {
11
39
  const options =
12
40
  typeof apiKeyOrOptions === 'string'
13
41
  ? { apiKey: apiKeyOrOptions }
14
42
  : apiKeyOrOptions
15
43
 
16
- if (isSeamHttpOptionsWithClient(options))
17
- return {
18
- ...options,
19
- enableLegacyMethodBehaivor:
20
- options.enableLegacyMethodBehaivor ?? enableLegacyMethodBehaivorDefault,
21
- }
22
-
23
- const endpoint =
24
- options.endpoint ??
25
- globalThis.process?.env?.['SEAM_ENDPOINT'] ??
26
- globalThis.process?.env?.['SEAM_API_URL'] ??
27
- 'https://connect.getseam.com'
44
+ if (isSeamHttpOptionsWithClient(options)) return options
28
45
 
29
46
  const apiKey =
30
- 'apiKey' in options
31
- ? options.apiKey
32
- : globalThis.process?.env?.['SEAM_API_KEY']
47
+ 'apiKey' in options ? options.apiKey : getApiKeyFromEnv(options)
33
48
 
34
49
  return {
35
50
  ...options,
36
51
  ...(apiKey != null ? { apiKey } : {}),
37
- endpoint,
38
- axiosOptions: options.axiosOptions ?? {},
39
- enableLegacyMethodBehaivor:
40
- options.enableLegacyMethodBehaivor ?? enableLegacyMethodBehaivorDefault,
41
52
  }
42
53
  }
54
+
55
+ const getApiKeyFromEnv = (
56
+ options: SeamHttpOptions,
57
+ ): string | null | undefined => {
58
+ if ('clientSessionToken' in options && options.clientSessionToken != null) {
59
+ return null
60
+ }
61
+ return globalThis.process?.env?.SEAM_API_KEY
62
+ }
63
+
64
+ const getEndpointFromEnv = (): string | null | undefined => {
65
+ return (
66
+ globalThis.process?.env?.SEAM_ENDPOINT ??
67
+ globalThis.process?.env?.SEAM_API_URL
68
+ )
69
+ }
@@ -4,50 +4,53 @@
4
4
  */
5
5
 
6
6
  import type { RouteRequestBody, RouteResponse } from '@seamapi/types/connect'
7
- import type { Axios } from 'axios'
8
7
  import type { SetNonNullable } from 'type-fest'
9
8
 
10
- import { createAxiosClient } from 'lib/seam/connect/axios.js'
9
+ import { warnOnInsecureuserIdentifierKey } from 'lib/seam/connect/auth.js'
10
+ import { type Client, createClient } from 'lib/seam/connect/client.js'
11
11
  import {
12
12
  isSeamHttpOptionsWithApiKey,
13
13
  isSeamHttpOptionsWithClient,
14
14
  isSeamHttpOptionsWithClientSessionToken,
15
+ type SeamHttpFromPublishableKeyOptions,
15
16
  SeamHttpInvalidOptionsError,
16
17
  type SeamHttpOptions,
17
18
  type SeamHttpOptionsWithApiKey,
18
19
  type SeamHttpOptionsWithClient,
19
20
  type SeamHttpOptionsWithClientSessionToken,
20
- } from 'lib/seam/connect/client-options.js'
21
+ } from 'lib/seam/connect/options.js'
21
22
  import { parseOptions } from 'lib/seam/connect/parse-options.js'
22
23
 
24
+ import { SeamHttpClientSessions } from './client-sessions.js'
25
+
23
26
  export class SeamHttpAccessCodesUnmanaged {
24
- client: Axios
27
+ client: Client
25
28
 
26
- constructor(apiKeyOrOptions: string | SeamHttpOptions) {
27
- const options = parseOptions(apiKeyOrOptions)
28
- this.client = createAxiosClient(options)
29
+ constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) {
30
+ const clientOptions = parseOptions(apiKeyOrOptions)
31
+ this.client = createClient(clientOptions)
29
32
  }
30
33
 
31
34
  static fromClient(
32
35
  client: SeamHttpOptionsWithClient['client'],
33
36
  options: Omit<SeamHttpOptionsWithClient, 'client'> = {},
34
37
  ): SeamHttpAccessCodesUnmanaged {
35
- const opts = { ...options, client }
36
- if (!isSeamHttpOptionsWithClient(opts)) {
38
+ const constructorOptions = { ...options, client }
39
+ if (!isSeamHttpOptionsWithClient(constructorOptions)) {
37
40
  throw new SeamHttpInvalidOptionsError('Missing client')
38
41
  }
39
- return new SeamHttpAccessCodesUnmanaged(opts)
42
+ return new SeamHttpAccessCodesUnmanaged(constructorOptions)
40
43
  }
41
44
 
42
45
  static fromApiKey(
43
46
  apiKey: SeamHttpOptionsWithApiKey['apiKey'],
44
47
  options: Omit<SeamHttpOptionsWithApiKey, 'apiKey'> = {},
45
48
  ): SeamHttpAccessCodesUnmanaged {
46
- const opts = { ...options, apiKey }
47
- if (!isSeamHttpOptionsWithApiKey(opts)) {
49
+ const constructorOptions = { ...options, apiKey }
50
+ if (!isSeamHttpOptionsWithApiKey(constructorOptions)) {
48
51
  throw new SeamHttpInvalidOptionsError('Missing apiKey')
49
52
  }
50
- return new SeamHttpAccessCodesUnmanaged(opts)
53
+ return new SeamHttpAccessCodesUnmanaged(constructorOptions)
51
54
  }
52
55
 
53
56
  static fromClientSessionToken(
@@ -57,11 +60,26 @@ export class SeamHttpAccessCodesUnmanaged {
57
60
  'clientSessionToken'
58
61
  > = {},
59
62
  ): SeamHttpAccessCodesUnmanaged {
60
- const opts = { ...options, clientSessionToken }
61
- if (!isSeamHttpOptionsWithClientSessionToken(opts)) {
63
+ const constructorOptions = { ...options, clientSessionToken }
64
+ if (!isSeamHttpOptionsWithClientSessionToken(constructorOptions)) {
62
65
  throw new SeamHttpInvalidOptionsError('Missing clientSessionToken')
63
66
  }
64
- return new SeamHttpAccessCodesUnmanaged(opts)
67
+ return new SeamHttpAccessCodesUnmanaged(constructorOptions)
68
+ }
69
+
70
+ static async fromPublishableKey(
71
+ publishableKey: string,
72
+ userIdentifierKey: string,
73
+ options: SeamHttpFromPublishableKeyOptions = {},
74
+ ): Promise<SeamHttpAccessCodesUnmanaged> {
75
+ warnOnInsecureuserIdentifierKey(userIdentifierKey)
76
+ const clientOptions = parseOptions({ ...options, publishableKey })
77
+ const client = createClient(clientOptions)
78
+ const clientSessions = SeamHttpClientSessions.fromClient(client)
79
+ const { token } = await clientSessions.getOrCreate({
80
+ user_identifier_key: userIdentifierKey,
81
+ })
82
+ return SeamHttpAccessCodesUnmanaged.fromClientSessionToken(token, options)
65
83
  }
66
84
 
67
85
  async convertToManaged(
@@ -116,41 +134,36 @@ export class SeamHttpAccessCodesUnmanaged {
116
134
  }
117
135
  }
118
136
 
119
- export type AccessCodesUnmanagedConvertToManagedBody = SetNonNullable<
120
- Required<RouteRequestBody<'/access_codes/unmanaged/convert_to_managed'>>
121
- >
137
+ export type AccessCodesUnmanagedConvertToManagedBody =
138
+ RouteRequestBody<'/access_codes/unmanaged/convert_to_managed'>
122
139
 
123
140
  export type AccessCodesUnmanagedConvertToManagedResponse = SetNonNullable<
124
141
  Required<RouteResponse<'/access_codes/unmanaged/convert_to_managed'>>
125
142
  >
126
143
 
127
- export type AccessCodesUnmanagedDeleteBody = SetNonNullable<
128
- Required<RouteRequestBody<'/access_codes/unmanaged/delete'>>
129
- >
144
+ export type AccessCodesUnmanagedDeleteBody =
145
+ RouteRequestBody<'/access_codes/unmanaged/delete'>
130
146
 
131
147
  export type AccessCodesUnmanagedDeleteResponse = SetNonNullable<
132
148
  Required<RouteResponse<'/access_codes/unmanaged/delete'>>
133
149
  >
134
150
 
135
- export type AccessCodesUnmanagedGetBody = SetNonNullable<
136
- Required<RouteRequestBody<'/access_codes/unmanaged/get'>>
137
- >
151
+ export type AccessCodesUnmanagedGetBody =
152
+ RouteRequestBody<'/access_codes/unmanaged/get'>
138
153
 
139
154
  export type AccessCodesUnmanagedGetResponse = SetNonNullable<
140
155
  Required<RouteResponse<'/access_codes/unmanaged/get'>>
141
156
  >
142
157
 
143
- export type AccessCodesUnmanagedListBody = SetNonNullable<
144
- Required<RouteRequestBody<'/access_codes/unmanaged/list'>>
145
- >
158
+ export type AccessCodesUnmanagedListBody =
159
+ RouteRequestBody<'/access_codes/unmanaged/list'>
146
160
 
147
161
  export type AccessCodesUnmanagedListResponse = SetNonNullable<
148
162
  Required<RouteResponse<'/access_codes/unmanaged/list'>>
149
163
  >
150
164
 
151
- export type AccessCodesUnmanagedUpdateBody = SetNonNullable<
152
- Required<RouteRequestBody<'/access_codes/unmanaged/update'>>
153
- >
165
+ export type AccessCodesUnmanagedUpdateBody =
166
+ RouteRequestBody<'/access_codes/unmanaged/update'>
154
167
 
155
168
  export type AccessCodesUnmanagedUpdateResponse = SetNonNullable<
156
169
  Required<RouteResponse<'/access_codes/unmanaged/update'>>
@@ -3,57 +3,55 @@
3
3
  * Do not edit this file or add other files to this directory.
4
4
  */
5
5
 
6
- import type {
7
- RouteRequestBody,
8
- RouteRequestParams,
9
- RouteResponse,
10
- } from '@seamapi/types/connect'
11
- import type { Axios } from 'axios'
6
+ import type { RouteRequestBody, RouteResponse } from '@seamapi/types/connect'
12
7
  import type { SetNonNullable } from 'type-fest'
13
8
 
14
- import { createAxiosClient } from 'lib/seam/connect/axios.js'
9
+ import { warnOnInsecureuserIdentifierKey } from 'lib/seam/connect/auth.js'
10
+ import { type Client, createClient } from 'lib/seam/connect/client.js'
15
11
  import {
16
12
  isSeamHttpOptionsWithApiKey,
17
13
  isSeamHttpOptionsWithClient,
18
14
  isSeamHttpOptionsWithClientSessionToken,
15
+ type SeamHttpFromPublishableKeyOptions,
19
16
  SeamHttpInvalidOptionsError,
20
17
  type SeamHttpOptions,
21
18
  type SeamHttpOptionsWithApiKey,
22
19
  type SeamHttpOptionsWithClient,
23
20
  type SeamHttpOptionsWithClientSessionToken,
24
- } from 'lib/seam/connect/client-options.js'
21
+ } from 'lib/seam/connect/options.js'
25
22
  import { parseOptions } from 'lib/seam/connect/parse-options.js'
26
23
 
27
24
  import { SeamHttpAccessCodesUnmanaged } from './access-codes-unmanaged.js'
25
+ import { SeamHttpClientSessions } from './client-sessions.js'
28
26
 
29
27
  export class SeamHttpAccessCodes {
30
- client: Axios
28
+ client: Client
31
29
 
32
- constructor(apiKeyOrOptions: string | SeamHttpOptions) {
33
- const options = parseOptions(apiKeyOrOptions)
34
- this.client = createAxiosClient(options)
30
+ constructor(apiKeyOrOptions: string | SeamHttpOptions = {}) {
31
+ const clientOptions = parseOptions(apiKeyOrOptions)
32
+ this.client = createClient(clientOptions)
35
33
  }
36
34
 
37
35
  static fromClient(
38
36
  client: SeamHttpOptionsWithClient['client'],
39
37
  options: Omit<SeamHttpOptionsWithClient, 'client'> = {},
40
38
  ): SeamHttpAccessCodes {
41
- const opts = { ...options, client }
42
- if (!isSeamHttpOptionsWithClient(opts)) {
39
+ const constructorOptions = { ...options, client }
40
+ if (!isSeamHttpOptionsWithClient(constructorOptions)) {
43
41
  throw new SeamHttpInvalidOptionsError('Missing client')
44
42
  }
45
- return new SeamHttpAccessCodes(opts)
43
+ return new SeamHttpAccessCodes(constructorOptions)
46
44
  }
47
45
 
48
46
  static fromApiKey(
49
47
  apiKey: SeamHttpOptionsWithApiKey['apiKey'],
50
48
  options: Omit<SeamHttpOptionsWithApiKey, 'apiKey'> = {},
51
49
  ): SeamHttpAccessCodes {
52
- const opts = { ...options, apiKey }
53
- if (!isSeamHttpOptionsWithApiKey(opts)) {
50
+ const constructorOptions = { ...options, apiKey }
51
+ if (!isSeamHttpOptionsWithApiKey(constructorOptions)) {
54
52
  throw new SeamHttpInvalidOptionsError('Missing apiKey')
55
53
  }
56
- return new SeamHttpAccessCodes(opts)
54
+ return new SeamHttpAccessCodes(constructorOptions)
57
55
  }
58
56
 
59
57
  static fromClientSessionToken(
@@ -63,11 +61,26 @@ export class SeamHttpAccessCodes {
63
61
  'clientSessionToken'
64
62
  > = {},
65
63
  ): SeamHttpAccessCodes {
66
- const opts = { ...options, clientSessionToken }
67
- if (!isSeamHttpOptionsWithClientSessionToken(opts)) {
64
+ const constructorOptions = { ...options, clientSessionToken }
65
+ if (!isSeamHttpOptionsWithClientSessionToken(constructorOptions)) {
68
66
  throw new SeamHttpInvalidOptionsError('Missing clientSessionToken')
69
67
  }
70
- return new SeamHttpAccessCodes(opts)
68
+ return new SeamHttpAccessCodes(constructorOptions)
69
+ }
70
+
71
+ static async fromPublishableKey(
72
+ publishableKey: string,
73
+ userIdentifierKey: string,
74
+ options: SeamHttpFromPublishableKeyOptions = {},
75
+ ): Promise<SeamHttpAccessCodes> {
76
+ warnOnInsecureuserIdentifierKey(userIdentifierKey)
77
+ const clientOptions = parseOptions({ ...options, publishableKey })
78
+ const client = createClient(clientOptions)
79
+ const clientSessions = SeamHttpClientSessions.fromClient(client)
80
+ const { token } = await clientSessions.getOrCreate({
81
+ user_identifier_key: userIdentifierKey,
82
+ })
83
+ return SeamHttpAccessCodes.fromClientSessionToken(token, options)
71
84
  }
72
85
 
73
86
  get unmanaged(): SeamHttpAccessCodesUnmanaged {
@@ -106,13 +119,13 @@ export class SeamHttpAccessCodes {
106
119
  }
107
120
 
108
121
  async generateCode(
109
- params?: AccessCodesGenerateCodeParams,
122
+ body: AccessCodesGenerateCodeBody,
110
123
  ): Promise<AccessCodesGenerateCodeResponse['generated_code']> {
111
124
  const { data } = await this.client.request<AccessCodesGenerateCodeResponse>(
112
125
  {
113
126
  url: '/access_codes/generate_code',
114
- method: 'get',
115
- params,
127
+ method: 'post',
128
+ data: body,
116
129
  },
117
130
  )
118
131
  return data.generated_code
@@ -161,65 +174,52 @@ export class SeamHttpAccessCodes {
161
174
  }
162
175
  }
163
176
 
164
- export type AccessCodesCreateBody = SetNonNullable<
165
- Required<RouteRequestBody<'/access_codes/create'>>
166
- >
177
+ export type AccessCodesCreateBody = RouteRequestBody<'/access_codes/create'>
167
178
 
168
179
  export type AccessCodesCreateResponse = SetNonNullable<
169
180
  Required<RouteResponse<'/access_codes/create'>>
170
181
  >
171
182
 
172
- export type AccessCodesCreateMultipleBody = SetNonNullable<
173
- Required<RouteRequestBody<'/access_codes/create_multiple'>>
174
- >
183
+ export type AccessCodesCreateMultipleBody =
184
+ RouteRequestBody<'/access_codes/create_multiple'>
175
185
 
176
186
  export type AccessCodesCreateMultipleResponse = SetNonNullable<
177
187
  Required<RouteResponse<'/access_codes/create_multiple'>>
178
188
  >
179
189
 
180
- export type AccessCodesDeleteBody = SetNonNullable<
181
- Required<RouteRequestBody<'/access_codes/delete'>>
182
- >
190
+ export type AccessCodesDeleteBody = RouteRequestBody<'/access_codes/delete'>
183
191
 
184
192
  export type AccessCodesDeleteResponse = SetNonNullable<
185
193
  Required<RouteResponse<'/access_codes/delete'>>
186
194
  >
187
195
 
188
- export type AccessCodesGenerateCodeParams = SetNonNullable<
189
- Required<RouteRequestParams<'/access_codes/generate_code'>>
190
- >
196
+ export type AccessCodesGenerateCodeBody =
197
+ RouteRequestBody<'/access_codes/generate_code'>
191
198
 
192
199
  export type AccessCodesGenerateCodeResponse = SetNonNullable<
193
200
  Required<RouteResponse<'/access_codes/generate_code'>>
194
201
  >
195
202
 
196
- export type AccessCodesGetBody = SetNonNullable<
197
- Required<RouteRequestBody<'/access_codes/get'>>
198
- >
203
+ export type AccessCodesGetBody = RouteRequestBody<'/access_codes/get'>
199
204
 
200
205
  export type AccessCodesGetResponse = SetNonNullable<
201
206
  Required<RouteResponse<'/access_codes/get'>>
202
207
  >
203
208
 
204
- export type AccessCodesListBody = SetNonNullable<
205
- Required<RouteRequestBody<'/access_codes/list'>>
206
- >
209
+ export type AccessCodesListBody = RouteRequestBody<'/access_codes/list'>
207
210
 
208
211
  export type AccessCodesListResponse = SetNonNullable<
209
212
  Required<RouteResponse<'/access_codes/list'>>
210
213
  >
211
214
 
212
- export type AccessCodesPullBackupAccessCodeBody = SetNonNullable<
213
- Required<RouteRequestBody<'/access_codes/pull_backup_access_code'>>
214
- >
215
+ export type AccessCodesPullBackupAccessCodeBody =
216
+ RouteRequestBody<'/access_codes/pull_backup_access_code'>
215
217
 
216
218
  export type AccessCodesPullBackupAccessCodeResponse = SetNonNullable<
217
219
  Required<RouteResponse<'/access_codes/pull_backup_access_code'>>
218
220
  >
219
221
 
220
- export type AccessCodesUpdateBody = SetNonNullable<
221
- Required<RouteRequestBody<'/access_codes/update'>>
222
- >
222
+ export type AccessCodesUpdateBody = RouteRequestBody<'/access_codes/update'>
223
223
 
224
224
  export type AccessCodesUpdateResponse = SetNonNullable<
225
225
  Required<RouteResponse<'/access_codes/update'>>