@sixb/client 0.1.1 → 0.1.3
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 +2 -2
- package/dist/agent-streams.d.ts +26 -3
- package/dist/agent-streams.d.ts.map +1 -1
- package/dist/api.d.ts +6 -2
- package/dist/api.d.ts.map +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +2 -2
- package/dist/browser.js.map +1 -1
- package/dist/{chunk-2vsyqn32.js → chunk-6esh0dxx.js} +1 -1
- package/dist/{chunk-hkrfakyt.js → chunk-7ts981a8.js} +2 -2
- package/dist/{chunk-h8bd95xf.js → chunk-eyd61e4t.js} +55 -2
- package/dist/chunk-eyd61e4t.js.map +19 -0
- package/dist/{chunk-1mnf48s9.js → chunk-heqezbjc.js} +2 -2
- package/dist/{chunk-dkd2wgva.js → chunk-hfk246y4.js} +40 -28
- package/dist/{chunk-dkd2wgva.js.map → chunk-hfk246y4.js.map} +3 -3
- package/dist/{chunk-w527w02k.js → chunk-mpgzkmwc.js} +2 -74
- package/dist/chunk-mpgzkmwc.js.map +10 -0
- package/dist/{chunk-6w5w9e75.js → chunk-yjank04c.js} +27 -12
- package/dist/chunk-yjank04c.js.map +10 -0
- package/dist/client-provider.d.ts +8 -0
- package/dist/client-provider.d.ts.map +1 -0
- package/dist/connectors/connection.d.ts +50 -0
- package/dist/connectors/connection.d.ts.map +1 -0
- package/dist/connectors/index.d.ts +4 -0
- package/dist/connectors/index.d.ts.map +1 -0
- package/dist/connectors/mutations.d.ts +62 -0
- package/dist/connectors/mutations.d.ts.map +1 -0
- package/dist/connectors/queries.d.ts +536 -0
- package/dist/connectors/queries.d.ts.map +1 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts +5679 -809
- package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/sdk.gen.d.ts +41 -1
- package/dist/generated/sdk.gen.d.ts.map +1 -1
- package/dist/generated/types.gen.d.ts +2330 -218
- package/dist/generated/types.gen.d.ts.map +1 -1
- package/dist/hooks.d.ts +2 -0
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +1180 -360
- package/dist/hooks.js.map +11 -6
- package/dist/index.js +29 -11
- package/dist/index.js.map +1 -1
- package/dist/logs.js +3 -3
- package/dist/models.d.ts +0 -31
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +23 -7
- package/dist/models.js.map +1 -1
- package/dist/query-hooks.d.ts +2 -6
- package/dist/query-hooks.d.ts.map +1 -1
- package/dist/query.js +3 -3
- package/dist/useAgentActivityStream.d.ts +14 -0
- package/dist/useAgentActivityStream.d.ts.map +1 -0
- package/package.json +6 -2
- package/src/agent-streams.ts +68 -45
- package/src/api.ts +26 -2
- package/src/browser.ts +1 -1
- package/src/client-provider.ts +12 -0
- package/src/connectors/connection.ts +443 -0
- package/src/connectors/index.ts +12 -0
- package/src/connectors/mutations.ts +417 -0
- package/src/connectors/queries.ts +150 -0
- package/src/generated/@tanstack/react-query.gen.ts +346 -0
- package/src/generated/index.ts +60 -0
- package/src/generated/sdk.gen.ts +189 -0
- package/src/generated/types.gen.ts +3048 -254
- package/src/hooks.ts +6 -0
- package/src/models.ts +0 -103
- package/src/query-hooks.ts +8 -14
- package/src/useAgentActivityStream.ts +48 -0
- package/dist/chunk-6w5w9e75.js.map +0 -10
- package/dist/chunk-h8bd95xf.js.map +0 -19
- package/dist/chunk-w527w02k.js.map +0 -10
- /package/dist/{chunk-2vsyqn32.js.map → chunk-6esh0dxx.js.map} +0 -0
- /package/dist/{chunk-hkrfakyt.js.map → chunk-7ts981a8.js.map} +0 -0
- /package/dist/{chunk-1mnf48s9.js.map → chunk-heqezbjc.js.map} +0 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type QueryClient,
|
|
3
|
+
type UseMutationOptions,
|
|
4
|
+
type UseMutationResult,
|
|
5
|
+
useMutation,
|
|
6
|
+
useQueryClient,
|
|
7
|
+
} from "@tanstack/react-query"
|
|
8
|
+
import { useSixbProviderClient } from "../client-provider"
|
|
9
|
+
import {
|
|
10
|
+
getConnectorConnectionRunQueryKey,
|
|
11
|
+
listConnectorConnectionsQueryKey,
|
|
12
|
+
} from "../generated/@tanstack/react-query.gen"
|
|
13
|
+
import type { Client } from "../generated/client"
|
|
14
|
+
import {
|
|
15
|
+
addConnectorConnection,
|
|
16
|
+
disconnectConnectorConnection,
|
|
17
|
+
reauthorizeConnectorConnection,
|
|
18
|
+
revokeConnectorConnection,
|
|
19
|
+
selectConnectorConnectionRunAccount,
|
|
20
|
+
startConnectorConnectionRun,
|
|
21
|
+
} from "../generated/sdk.gen"
|
|
22
|
+
import type {
|
|
23
|
+
AddConnectorConnectionResponse,
|
|
24
|
+
DisconnectConnectorConnectionResponse,
|
|
25
|
+
ReauthorizeConnectorConnectionResponse,
|
|
26
|
+
RevokeConnectorConnectionResponse,
|
|
27
|
+
SelectConnectorConnectionRunAccountResponse,
|
|
28
|
+
StartConnectorConnectionRunResponse,
|
|
29
|
+
} from "../generated/types.gen"
|
|
30
|
+
|
|
31
|
+
export interface AddConnectorConnectionOptions<TContext = unknown>
|
|
32
|
+
extends Omit<
|
|
33
|
+
UseMutationOptions<AddConnectorConnectionResponse, Error, void, TContext>,
|
|
34
|
+
"mutationFn"
|
|
35
|
+
> {
|
|
36
|
+
readonly connectorId: string
|
|
37
|
+
readonly fromConnectionId: string
|
|
38
|
+
readonly slot: string
|
|
39
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
40
|
+
readonly client?: Client
|
|
41
|
+
/** Override used by tests and non-hook option factory consumers. */
|
|
42
|
+
readonly queryClient?: QueryClient
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function addConnectorConnectionMutationOptions<TContext = unknown>(
|
|
46
|
+
options: AddConnectorConnectionOptions<TContext>
|
|
47
|
+
): UseMutationOptions<AddConnectorConnectionResponse, Error, void, TContext> {
|
|
48
|
+
const {
|
|
49
|
+
connectorId,
|
|
50
|
+
fromConnectionId,
|
|
51
|
+
slot,
|
|
52
|
+
client,
|
|
53
|
+
queryClient,
|
|
54
|
+
onSuccess,
|
|
55
|
+
...mutationOptions
|
|
56
|
+
} = options
|
|
57
|
+
const normalizedConnectorId = nonblank(connectorId, "connectorId")
|
|
58
|
+
const path = {
|
|
59
|
+
connectorId: normalizedConnectorId,
|
|
60
|
+
connectionId: nonblank(fromConnectionId, "fromConnectionId"),
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
...mutationOptions,
|
|
65
|
+
mutationFn: async () => {
|
|
66
|
+
const { data } = await addConnectorConnection<true>({
|
|
67
|
+
client,
|
|
68
|
+
path,
|
|
69
|
+
body: { slot: nonblank(slot, "slot") },
|
|
70
|
+
throwOnError: true,
|
|
71
|
+
})
|
|
72
|
+
return data
|
|
73
|
+
},
|
|
74
|
+
onSuccess: async (run, variables, context, mutation) => {
|
|
75
|
+
queryClient?.setQueryData(
|
|
76
|
+
getConnectorConnectionRunQueryKey({
|
|
77
|
+
path: { connectorId: normalizedConnectorId, runId: run.id },
|
|
78
|
+
}),
|
|
79
|
+
run
|
|
80
|
+
)
|
|
81
|
+
await onSuccess?.(run, variables, context, mutation)
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function useAddConnectorConnection<TContext = unknown>(
|
|
87
|
+
options: AddConnectorConnectionOptions<TContext>
|
|
88
|
+
): UseMutationResult<AddConnectorConnectionResponse, Error, void, TContext> {
|
|
89
|
+
const providerClient = useSixbProviderClient()
|
|
90
|
+
const queryClient = useQueryClient()
|
|
91
|
+
return useMutation(
|
|
92
|
+
addConnectorConnectionMutationOptions({
|
|
93
|
+
...options,
|
|
94
|
+
client: options.client ?? providerClient,
|
|
95
|
+
queryClient: options.queryClient ?? queryClient,
|
|
96
|
+
})
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ConnectConnectorOptions<TContext = unknown>
|
|
101
|
+
extends Omit<
|
|
102
|
+
UseMutationOptions<StartConnectorConnectionRunResponse, Error, void, TContext>,
|
|
103
|
+
"mutationFn"
|
|
104
|
+
> {
|
|
105
|
+
readonly connectorId: string
|
|
106
|
+
readonly slot: string
|
|
107
|
+
readonly returnTo: string
|
|
108
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
109
|
+
readonly client?: Client
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function connectConnectorMutationOptions<TContext = unknown>(
|
|
113
|
+
options: ConnectConnectorOptions<TContext>
|
|
114
|
+
): UseMutationOptions<StartConnectorConnectionRunResponse, Error, void, TContext> {
|
|
115
|
+
const { connectorId, slot, returnTo, client, ...mutationOptions } = options
|
|
116
|
+
const normalizedConnectorId = nonblank(connectorId, "connectorId")
|
|
117
|
+
const normalizedSlot = nonblank(slot, "slot")
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
...mutationOptions,
|
|
121
|
+
mutationFn: async () => {
|
|
122
|
+
const { data } = await startConnectorConnectionRun<true>({
|
|
123
|
+
client,
|
|
124
|
+
path: { connectorId: normalizedConnectorId },
|
|
125
|
+
body: {
|
|
126
|
+
slot: normalizedSlot,
|
|
127
|
+
returnTo: resolveReturnTo(returnTo),
|
|
128
|
+
},
|
|
129
|
+
throwOnError: true,
|
|
130
|
+
})
|
|
131
|
+
return data
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function useConnectConnector<TContext = unknown>(
|
|
137
|
+
options: ConnectConnectorOptions<TContext>
|
|
138
|
+
): UseMutationResult<StartConnectorConnectionRunResponse, Error, void, TContext> {
|
|
139
|
+
const providerClient = useSixbProviderClient()
|
|
140
|
+
return useMutation(
|
|
141
|
+
connectConnectorMutationOptions({
|
|
142
|
+
...options,
|
|
143
|
+
client: options.client ?? providerClient,
|
|
144
|
+
})
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface SelectConnectorAccountInput {
|
|
149
|
+
readonly accountId: string
|
|
150
|
+
readonly replace?: boolean
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface SelectConnectorAccountOptions<TContext = unknown>
|
|
154
|
+
extends Omit<
|
|
155
|
+
UseMutationOptions<
|
|
156
|
+
SelectConnectorConnectionRunAccountResponse,
|
|
157
|
+
Error,
|
|
158
|
+
SelectConnectorAccountInput,
|
|
159
|
+
TContext
|
|
160
|
+
>,
|
|
161
|
+
"mutationFn"
|
|
162
|
+
> {
|
|
163
|
+
readonly connectorId: string
|
|
164
|
+
readonly runId: string
|
|
165
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
166
|
+
readonly client?: Client
|
|
167
|
+
/** Override used by tests and non-hook option factory consumers. */
|
|
168
|
+
readonly queryClient?: QueryClient
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function selectConnectorAccountMutationOptions<TContext = unknown>(
|
|
172
|
+
options: SelectConnectorAccountOptions<TContext>
|
|
173
|
+
): UseMutationOptions<
|
|
174
|
+
SelectConnectorConnectionRunAccountResponse,
|
|
175
|
+
Error,
|
|
176
|
+
SelectConnectorAccountInput,
|
|
177
|
+
TContext
|
|
178
|
+
> {
|
|
179
|
+
const { connectorId, runId, client, queryClient, onSuccess, ...mutationOptions } = options
|
|
180
|
+
const normalizedConnectorId = nonblank(connectorId, "connectorId")
|
|
181
|
+
const path = () => ({
|
|
182
|
+
connectorId: normalizedConnectorId,
|
|
183
|
+
runId: nonblank(runId, "runId"),
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
...mutationOptions,
|
|
188
|
+
mutationFn: async ({ accountId, replace }) => {
|
|
189
|
+
const resolvedPath = path()
|
|
190
|
+
const { data } = await selectConnectorConnectionRunAccount<true>({
|
|
191
|
+
client,
|
|
192
|
+
path: resolvedPath,
|
|
193
|
+
body: {
|
|
194
|
+
accountId: nonblank(accountId, "accountId"),
|
|
195
|
+
...(replace === undefined ? {} : { replace }),
|
|
196
|
+
},
|
|
197
|
+
throwOnError: true,
|
|
198
|
+
})
|
|
199
|
+
return data
|
|
200
|
+
},
|
|
201
|
+
onSuccess: async (run, variables, context, mutation) => {
|
|
202
|
+
const resolvedPath = path()
|
|
203
|
+
queryClient?.setQueryData(getConnectorConnectionRunQueryKey({ path: resolvedPath }), run)
|
|
204
|
+
await invalidateConnectorConnections(queryClient, resolvedPath.connectorId)
|
|
205
|
+
await onSuccess?.(run, variables, context, mutation)
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function useSelectConnectorAccount<TContext = unknown>(
|
|
211
|
+
options: SelectConnectorAccountOptions<TContext>
|
|
212
|
+
): UseMutationResult<
|
|
213
|
+
SelectConnectorConnectionRunAccountResponse,
|
|
214
|
+
Error,
|
|
215
|
+
SelectConnectorAccountInput,
|
|
216
|
+
TContext
|
|
217
|
+
> {
|
|
218
|
+
const providerClient = useSixbProviderClient()
|
|
219
|
+
const queryClient = useQueryClient()
|
|
220
|
+
return useMutation(
|
|
221
|
+
selectConnectorAccountMutationOptions({
|
|
222
|
+
...options,
|
|
223
|
+
client: options.client ?? providerClient,
|
|
224
|
+
queryClient: options.queryClient ?? queryClient,
|
|
225
|
+
})
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
interface ConnectorConnectionMutationOptions<TResponse, TContext>
|
|
230
|
+
extends Omit<UseMutationOptions<TResponse, Error, void, TContext>, "mutationFn"> {
|
|
231
|
+
readonly connectorId: string
|
|
232
|
+
readonly connectionId: string
|
|
233
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
234
|
+
readonly client?: Client
|
|
235
|
+
/** Override used by tests and non-hook option factory consumers. */
|
|
236
|
+
readonly queryClient?: QueryClient
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export type DisconnectConnectorOptions<TContext = unknown> = ConnectorConnectionMutationOptions<
|
|
240
|
+
DisconnectConnectorConnectionResponse,
|
|
241
|
+
TContext
|
|
242
|
+
>
|
|
243
|
+
|
|
244
|
+
export function disconnectConnectorMutationOptions<TContext = unknown>(
|
|
245
|
+
options: DisconnectConnectorOptions<TContext>
|
|
246
|
+
): UseMutationOptions<DisconnectConnectorConnectionResponse, Error, void, TContext> {
|
|
247
|
+
const { connectorId, connectionId, client, queryClient, onSuccess, ...mutationOptions } = options
|
|
248
|
+
const normalizedConnectorId = nonblank(connectorId, "connectorId")
|
|
249
|
+
const path = () => ({
|
|
250
|
+
connectorId: normalizedConnectorId,
|
|
251
|
+
connectionId: nonblank(connectionId, "connectionId"),
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
...mutationOptions,
|
|
256
|
+
mutationFn: async () => {
|
|
257
|
+
const resolvedPath = path()
|
|
258
|
+
const { data } = await disconnectConnectorConnection<true>({
|
|
259
|
+
client,
|
|
260
|
+
path: resolvedPath,
|
|
261
|
+
throwOnError: true,
|
|
262
|
+
})
|
|
263
|
+
return data
|
|
264
|
+
},
|
|
265
|
+
onSuccess: async (result, variables, context, mutation) => {
|
|
266
|
+
await invalidateConnectorConnections(queryClient, normalizedConnectorId)
|
|
267
|
+
await onSuccess?.(result, variables, context, mutation)
|
|
268
|
+
},
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function useDisconnectConnector<TContext = unknown>(
|
|
273
|
+
options: DisconnectConnectorOptions<TContext>
|
|
274
|
+
): UseMutationResult<DisconnectConnectorConnectionResponse, Error, void, TContext> {
|
|
275
|
+
const providerClient = useSixbProviderClient()
|
|
276
|
+
const queryClient = useQueryClient()
|
|
277
|
+
return useMutation(
|
|
278
|
+
disconnectConnectorMutationOptions({
|
|
279
|
+
...options,
|
|
280
|
+
client: options.client ?? providerClient,
|
|
281
|
+
queryClient: options.queryClient ?? queryClient,
|
|
282
|
+
})
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export type RevokeConnectorOptions<TContext = unknown> = ConnectorConnectionMutationOptions<
|
|
287
|
+
RevokeConnectorConnectionResponse,
|
|
288
|
+
TContext
|
|
289
|
+
>
|
|
290
|
+
|
|
291
|
+
export function revokeConnectorMutationOptions<TContext = unknown>(
|
|
292
|
+
options: RevokeConnectorOptions<TContext>
|
|
293
|
+
): UseMutationOptions<RevokeConnectorConnectionResponse, Error, void, TContext> {
|
|
294
|
+
const { connectorId, connectionId, client, queryClient, onSuccess, ...mutationOptions } = options
|
|
295
|
+
const normalizedConnectorId = nonblank(connectorId, "connectorId")
|
|
296
|
+
const path = () => ({
|
|
297
|
+
connectorId: normalizedConnectorId,
|
|
298
|
+
connectionId: nonblank(connectionId, "connectionId"),
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
return {
|
|
302
|
+
...mutationOptions,
|
|
303
|
+
mutationFn: async () => {
|
|
304
|
+
const resolvedPath = path()
|
|
305
|
+
const { data } = await revokeConnectorConnection<true>({
|
|
306
|
+
client,
|
|
307
|
+
path: resolvedPath,
|
|
308
|
+
throwOnError: true,
|
|
309
|
+
})
|
|
310
|
+
return data
|
|
311
|
+
},
|
|
312
|
+
onSuccess: async (result, variables, context, mutation) => {
|
|
313
|
+
await invalidateConnectorConnections(queryClient, normalizedConnectorId)
|
|
314
|
+
await onSuccess?.(result, variables, context, mutation)
|
|
315
|
+
},
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function useRevokeConnector<TContext = unknown>(
|
|
320
|
+
options: RevokeConnectorOptions<TContext>
|
|
321
|
+
): UseMutationResult<RevokeConnectorConnectionResponse, Error, void, TContext> {
|
|
322
|
+
const providerClient = useSixbProviderClient()
|
|
323
|
+
const queryClient = useQueryClient()
|
|
324
|
+
return useMutation(
|
|
325
|
+
revokeConnectorMutationOptions({
|
|
326
|
+
...options,
|
|
327
|
+
client: options.client ?? providerClient,
|
|
328
|
+
queryClient: options.queryClient ?? queryClient,
|
|
329
|
+
})
|
|
330
|
+
)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export interface ReauthorizeConnectorOptions<TContext = unknown>
|
|
334
|
+
extends Omit<
|
|
335
|
+
UseMutationOptions<ReauthorizeConnectorConnectionResponse, Error, void, TContext>,
|
|
336
|
+
"mutationFn"
|
|
337
|
+
> {
|
|
338
|
+
readonly connectorId: string
|
|
339
|
+
readonly connectionId: string
|
|
340
|
+
readonly returnTo: string
|
|
341
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
342
|
+
readonly client?: Client
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function reauthorizeConnectorMutationOptions<TContext = unknown>(
|
|
346
|
+
options: ReauthorizeConnectorOptions<TContext>
|
|
347
|
+
): UseMutationOptions<ReauthorizeConnectorConnectionResponse, Error, void, TContext> {
|
|
348
|
+
const { connectorId, connectionId, returnTo, client, ...mutationOptions } = options
|
|
349
|
+
const normalizedConnectorId = nonblank(connectorId, "connectorId")
|
|
350
|
+
|
|
351
|
+
return {
|
|
352
|
+
...mutationOptions,
|
|
353
|
+
mutationFn: async () => {
|
|
354
|
+
const { data } = await reauthorizeConnectorConnection<true>({
|
|
355
|
+
client,
|
|
356
|
+
path: {
|
|
357
|
+
connectorId: normalizedConnectorId,
|
|
358
|
+
connectionId: nonblank(connectionId, "connectionId"),
|
|
359
|
+
},
|
|
360
|
+
body: { returnTo: resolveReturnTo(returnTo) },
|
|
361
|
+
throwOnError: true,
|
|
362
|
+
})
|
|
363
|
+
return data
|
|
364
|
+
},
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function useReauthorizeConnector<TContext = unknown>(
|
|
369
|
+
options: ReauthorizeConnectorOptions<TContext>
|
|
370
|
+
): UseMutationResult<ReauthorizeConnectorConnectionResponse, Error, void, TContext> {
|
|
371
|
+
const providerClient = useSixbProviderClient()
|
|
372
|
+
return useMutation(
|
|
373
|
+
reauthorizeConnectorMutationOptions({
|
|
374
|
+
...options,
|
|
375
|
+
client: options.client ?? providerClient,
|
|
376
|
+
})
|
|
377
|
+
)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function nonblank(value: string, field: string): string {
|
|
381
|
+
const normalized = value.trim()
|
|
382
|
+
if (!normalized) {
|
|
383
|
+
throw new Error(`[SixbClient] ${field} must be a non-empty string.`)
|
|
384
|
+
}
|
|
385
|
+
return normalized
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function resolveReturnTo(value: string): string {
|
|
389
|
+
const returnTo = nonblank(value, "returnTo")
|
|
390
|
+
|
|
391
|
+
try {
|
|
392
|
+
return new URL(returnTo).toString()
|
|
393
|
+
} catch {
|
|
394
|
+
const currentUrl = globalThis.location?.href
|
|
395
|
+
if (!currentUrl) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
"[SixbClient] A relative connector returnTo can only be resolved in a browser."
|
|
398
|
+
)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
try {
|
|
402
|
+
return new URL(returnTo, currentUrl).toString()
|
|
403
|
+
} catch (error) {
|
|
404
|
+
throw new Error("[SixbClient] Connector returnTo must be a valid URL.", { cause: error })
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async function invalidateConnectorConnections(
|
|
410
|
+
queryClient: QueryClient | undefined,
|
|
411
|
+
connectorId: string
|
|
412
|
+
): Promise<void> {
|
|
413
|
+
if (!queryClient) return
|
|
414
|
+
await queryClient.invalidateQueries({
|
|
415
|
+
queryKey: listConnectorConnectionsQueryKey({ path: { connectorId } }),
|
|
416
|
+
})
|
|
417
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import {
|
|
2
|
+
queryOptions,
|
|
3
|
+
type UseQueryOptions,
|
|
4
|
+
type UseQueryResult,
|
|
5
|
+
useQuery,
|
|
6
|
+
} from "@tanstack/react-query"
|
|
7
|
+
import { useSixbProviderClient } from "../client-provider"
|
|
8
|
+
import {
|
|
9
|
+
getConnectorConnectionRunQueryKey,
|
|
10
|
+
listConnectorConnectionsQueryKey,
|
|
11
|
+
} from "../generated/@tanstack/react-query.gen"
|
|
12
|
+
import type { Client } from "../generated/client"
|
|
13
|
+
import { getConnectorConnectionRun, listConnectorConnections } from "../generated/sdk.gen"
|
|
14
|
+
import type {
|
|
15
|
+
GetConnectorConnectionRunResponse,
|
|
16
|
+
ListConnectorConnectionsResponse,
|
|
17
|
+
} from "../generated/types.gen"
|
|
18
|
+
|
|
19
|
+
export type ConnectorConnection = ListConnectorConnectionsResponse[number]
|
|
20
|
+
export type ConnectorAccount = ConnectorConnection["account"]
|
|
21
|
+
export type ConnectorConnectionRun = GetConnectorConnectionRunResponse
|
|
22
|
+
|
|
23
|
+
export type ConnectorConnectionsQueryKey = ReturnType<typeof listConnectorConnectionsQueryKey>
|
|
24
|
+
|
|
25
|
+
export interface ConnectorConnectionsOptions
|
|
26
|
+
extends Omit<
|
|
27
|
+
UseQueryOptions<
|
|
28
|
+
ListConnectorConnectionsResponse,
|
|
29
|
+
Error,
|
|
30
|
+
ListConnectorConnectionsResponse,
|
|
31
|
+
ConnectorConnectionsQueryKey
|
|
32
|
+
>,
|
|
33
|
+
"queryKey" | "queryFn"
|
|
34
|
+
> {
|
|
35
|
+
readonly connectorId: string
|
|
36
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
37
|
+
readonly client?: Client
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function connectorConnectionsQueryOptions(options: ConnectorConnectionsOptions) {
|
|
41
|
+
const { connectorId, client, ...queryOptionsInput } = options
|
|
42
|
+
const path = { connectorId: nonblank(connectorId, "connectorId") }
|
|
43
|
+
|
|
44
|
+
return queryOptions<
|
|
45
|
+
ListConnectorConnectionsResponse,
|
|
46
|
+
Error,
|
|
47
|
+
ListConnectorConnectionsResponse,
|
|
48
|
+
ConnectorConnectionsQueryKey
|
|
49
|
+
>({
|
|
50
|
+
...queryOptionsInput,
|
|
51
|
+
queryKey: listConnectorConnectionsQueryKey({ path }),
|
|
52
|
+
queryFn: async ({ signal }) => {
|
|
53
|
+
const { data } = await listConnectorConnections<true>({
|
|
54
|
+
client,
|
|
55
|
+
path,
|
|
56
|
+
signal,
|
|
57
|
+
throwOnError: true,
|
|
58
|
+
})
|
|
59
|
+
return data
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function useConnectorConnections(
|
|
65
|
+
options: ConnectorConnectionsOptions
|
|
66
|
+
): UseQueryResult<ListConnectorConnectionsResponse, Error> {
|
|
67
|
+
const providerClient = useSixbProviderClient()
|
|
68
|
+
return useQuery(
|
|
69
|
+
connectorConnectionsQueryOptions({
|
|
70
|
+
...options,
|
|
71
|
+
client: options.client ?? providerClient,
|
|
72
|
+
})
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type ConnectorConnectionRunQueryKey = ReturnType<typeof getConnectorConnectionRunQueryKey>
|
|
77
|
+
|
|
78
|
+
export interface ConnectorConnectionRunOptions
|
|
79
|
+
extends Omit<
|
|
80
|
+
UseQueryOptions<
|
|
81
|
+
GetConnectorConnectionRunResponse,
|
|
82
|
+
Error,
|
|
83
|
+
GetConnectorConnectionRunResponse,
|
|
84
|
+
ConnectorConnectionRunQueryKey
|
|
85
|
+
>,
|
|
86
|
+
"queryKey" | "queryFn"
|
|
87
|
+
> {
|
|
88
|
+
readonly connectorId: string
|
|
89
|
+
/** A missing callback query parameter disables the query. */
|
|
90
|
+
readonly runId: string | null | undefined
|
|
91
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
92
|
+
readonly client?: Client
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function connectorConnectionRunQueryOptions(options: ConnectorConnectionRunOptions) {
|
|
96
|
+
const { connectorId, runId, client, ...queryOptionsInput } = options
|
|
97
|
+
const normalizedRunId = optionalNonblank(runId, "runId")
|
|
98
|
+
const path = {
|
|
99
|
+
connectorId: nonblank(connectorId, "connectorId"),
|
|
100
|
+
runId: normalizedRunId ?? "",
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return queryOptions<
|
|
104
|
+
GetConnectorConnectionRunResponse,
|
|
105
|
+
Error,
|
|
106
|
+
GetConnectorConnectionRunResponse,
|
|
107
|
+
ConnectorConnectionRunQueryKey
|
|
108
|
+
>({
|
|
109
|
+
...queryOptionsInput,
|
|
110
|
+
enabled: normalizedRunId === undefined ? false : queryOptionsInput.enabled,
|
|
111
|
+
queryKey: getConnectorConnectionRunQueryKey({ path }),
|
|
112
|
+
queryFn: async ({ signal }) => {
|
|
113
|
+
if (normalizedRunId === undefined) {
|
|
114
|
+
throw new Error("[SixbClient] runId is required to read a connector connection run.")
|
|
115
|
+
}
|
|
116
|
+
const { data } = await getConnectorConnectionRun<true>({
|
|
117
|
+
client,
|
|
118
|
+
path,
|
|
119
|
+
signal,
|
|
120
|
+
throwOnError: true,
|
|
121
|
+
})
|
|
122
|
+
return data
|
|
123
|
+
},
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function useConnectorConnectionRun(
|
|
128
|
+
options: ConnectorConnectionRunOptions
|
|
129
|
+
): UseQueryResult<GetConnectorConnectionRunResponse, Error> {
|
|
130
|
+
const providerClient = useSixbProviderClient()
|
|
131
|
+
return useQuery(
|
|
132
|
+
connectorConnectionRunQueryOptions({
|
|
133
|
+
...options,
|
|
134
|
+
client: options.client ?? providerClient,
|
|
135
|
+
})
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function nonblank(value: string, field: string): string {
|
|
140
|
+
const normalized = value.trim()
|
|
141
|
+
if (!normalized) {
|
|
142
|
+
throw new Error(`[SixbClient] ${field} must be a non-empty string.`)
|
|
143
|
+
}
|
|
144
|
+
return normalized
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function optionalNonblank(value: string | null | undefined, field: string): string | undefined {
|
|
148
|
+
if (value === null || value === undefined) return undefined
|
|
149
|
+
return nonblank(value, field)
|
|
150
|
+
}
|