@sixb/client 0.0.1
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/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/actions.d.ts +61 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/agent-streams.d.ts +72 -0
- package/dist/agent-streams.d.ts.map +1 -0
- package/dist/api.d.ts +54 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/browser.d.ts +41 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +174 -0
- package/dist/browser.js.map +10 -0
- package/dist/chunk-05yz32g0.js +240 -0
- package/dist/chunk-05yz32g0.js.map +10 -0
- package/dist/chunk-2d7239yx.js +1181 -0
- package/dist/chunk-2d7239yx.js.map +17 -0
- package/dist/chunk-3g6ad4jc.js +147 -0
- package/dist/chunk-3g6ad4jc.js.map +10 -0
- package/dist/chunk-aw8rcsq3.js +145 -0
- package/dist/chunk-aw8rcsq3.js.map +10 -0
- package/dist/chunk-ctsdm59b.js +1477 -0
- package/dist/chunk-ctsdm59b.js.map +19 -0
- package/dist/chunk-fxrh83wy.js +180 -0
- package/dist/chunk-fxrh83wy.js.map +12 -0
- package/dist/chunk-pey7nh0d.js +123 -0
- package/dist/chunk-pey7nh0d.js.map +10 -0
- package/dist/chunk-trpg18dp.js +3 -0
- package/dist/chunk-trpg18dp.js.map +9 -0
- package/dist/events-builder.d.ts +218 -0
- package/dist/events-builder.d.ts.map +1 -0
- package/dist/events-hooks.d.ts +50 -0
- package/dist/events-hooks.d.ts.map +1 -0
- package/dist/events-model.d.ts +12 -0
- package/dist/events-model.d.ts.map +1 -0
- package/dist/events-provider.d.ts +56 -0
- package/dist/events-provider.d.ts.map +1 -0
- package/dist/events-transport.d.ts +62 -0
- package/dist/events-transport.d.ts.map +1 -0
- package/dist/events.d.ts +5 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/file.d.ts +27 -0
- package/dist/file.d.ts.map +1 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts +17064 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -0
- package/dist/generated/client/client.gen.d.ts +3 -0
- package/dist/generated/client/client.gen.d.ts.map +1 -0
- package/dist/generated/client/index.d.ts +9 -0
- package/dist/generated/client/index.d.ts.map +1 -0
- package/dist/generated/client/types.gen.d.ts +118 -0
- package/dist/generated/client/types.gen.d.ts.map +1 -0
- package/dist/generated/client/utils.gen.d.ts +34 -0
- package/dist/generated/client/utils.gen.d.ts.map +1 -0
- package/dist/generated/client.gen.d.ts +13 -0
- package/dist/generated/client.gen.d.ts.map +1 -0
- package/dist/generated/core/auth.gen.d.ts +19 -0
- package/dist/generated/core/auth.gen.d.ts.map +1 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +26 -0
- package/dist/generated/core/bodySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/params.gen.d.ts +44 -0
- package/dist/generated/core/params.gen.d.ts.map +1 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +34 -0
- package/dist/generated/core/pathSerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +19 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +72 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts.map +1 -0
- package/dist/generated/core/types.gen.d.ts +79 -0
- package/dist/generated/core/types.gen.d.ts.map +1 -0
- package/dist/generated/core/utils.gen.d.ts +20 -0
- package/dist/generated/core/utils.gen.d.ts.map +1 -0
- package/dist/generated/index.d.ts +3 -0
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/sdk.gen.d.ts +464 -0
- package/dist/generated/sdk.gen.d.ts.map +1 -0
- package/dist/generated/types.gen.d.ts +7672 -0
- package/dist/generated/types.gen.d.ts.map +1 -0
- package/dist/hooks.d.ts +123 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +3179 -0
- package/dist/hooks.js.map +16 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +329 -0
- package/dist/index.js.map +9 -0
- package/dist/logs-builder.d.ts +59 -0
- package/dist/logs-builder.d.ts.map +1 -0
- package/dist/logs-hooks.d.ts +30 -0
- package/dist/logs-hooks.d.ts.map +1 -0
- package/dist/logs-transport.d.ts +38 -0
- package/dist/logs-transport.d.ts.map +1 -0
- package/dist/logs.d.ts +7 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +22 -0
- package/dist/logs.js.map +9 -0
- package/dist/models.d.ts +126 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +252 -0
- package/dist/models.js.map +9 -0
- package/dist/query-hooks.d.ts +365 -0
- package/dist/query-hooks.d.ts.map +1 -0
- package/dist/query.d.ts +27 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +15 -0
- package/dist/query.js.map +9 -0
- package/dist/sha256.d.ts +15 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/telemetry-events.d.ts +25 -0
- package/dist/telemetry-events.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/useAgentRunStream.d.ts +21 -0
- package/dist/useAgentRunStream.d.ts.map +1 -0
- package/dist/ws-socket.d.ts +45 -0
- package/dist/ws-socket.d.ts.map +1 -0
- package/package.json +105 -0
- package/src/actions.ts +310 -0
- package/src/agent-streams.ts +275 -0
- package/src/api.ts +229 -0
- package/src/browser.ts +267 -0
- package/src/events-builder.ts +735 -0
- package/src/events-hooks.ts +233 -0
- package/src/events-model.ts +40 -0
- package/src/events-provider.ts +161 -0
- package/src/events-transport.ts +193 -0
- package/src/events.ts +6 -0
- package/src/file.ts +367 -0
- package/src/generated/@tanstack/react-query.gen.ts +3876 -0
- package/src/generated/client/client.gen.ts +288 -0
- package/src/generated/client/index.ts +25 -0
- package/src/generated/client/types.gen.ts +209 -0
- package/src/generated/client/utils.gen.ts +316 -0
- package/src/generated/client.gen.ts +18 -0
- package/src/generated/core/auth.gen.ts +41 -0
- package/src/generated/core/bodySerializer.gen.ts +84 -0
- package/src/generated/core/params.gen.ts +169 -0
- package/src/generated/core/pathSerializer.gen.ts +171 -0
- package/src/generated/core/queryKeySerializer.gen.ts +117 -0
- package/src/generated/core/serverSentEvents.gen.ts +243 -0
- package/src/generated/core/types.gen.ts +104 -0
- package/src/generated/core/utils.gen.ts +140 -0
- package/src/generated/index.ts +672 -0
- package/src/generated/sdk.gen.ts +1984 -0
- package/src/generated/types.gen.ts +9049 -0
- package/src/hooks.ts +404 -0
- package/src/index.ts +18 -0
- package/src/logs-builder.ts +171 -0
- package/src/logs-hooks.ts +112 -0
- package/src/logs-transport.ts +112 -0
- package/src/logs.ts +15 -0
- package/src/models.ts +420 -0
- package/src/query-hooks.ts +1021 -0
- package/src/query.ts +175 -0
- package/src/sha256.ts +159 -0
- package/src/telemetry-events.ts +71 -0
- package/src/useAgentRunStream.ts +62 -0
- package/src/ws-socket.ts +202 -0
|
@@ -0,0 +1,1021 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TanStack Query integration for typed object queries.
|
|
3
|
+
*
|
|
4
|
+
* Layer 1: `objectQuery*Options` factories for prefetching, loaders, and SSR.
|
|
5
|
+
* Layer 2: `useObjects*` hooks for components. Both key on the normalized
|
|
6
|
+
* query IR, so identical queries share cache entries — inline builders are
|
|
7
|
+
* safe to construct on every render.
|
|
8
|
+
*/
|
|
9
|
+
import type { FileRef, InferPropertyValue, Property, TelemetryPropertyToken } from "@sixb/core"
|
|
10
|
+
import type {
|
|
11
|
+
ListResult,
|
|
12
|
+
ListResultWithoutTotal,
|
|
13
|
+
ObjectQuery,
|
|
14
|
+
ObjectQueryExecutor,
|
|
15
|
+
ObjectQueryExecutorFacetRequest,
|
|
16
|
+
ObjectQueryFacetResult,
|
|
17
|
+
ObjectQueryListOptions,
|
|
18
|
+
ObjectTypeWithPropertyTokens,
|
|
19
|
+
} from "@sixb/core/query"
|
|
20
|
+
import {
|
|
21
|
+
infiniteQueryOptions,
|
|
22
|
+
type QueryClient,
|
|
23
|
+
queryOptions,
|
|
24
|
+
type UseMutationOptions,
|
|
25
|
+
type UseMutationResult,
|
|
26
|
+
type UseQueryResult,
|
|
27
|
+
useInfiniteQuery,
|
|
28
|
+
useMutation,
|
|
29
|
+
useQuery,
|
|
30
|
+
useQueryClient,
|
|
31
|
+
} from "@tanstack/react-query"
|
|
32
|
+
import { createContext, createElement, type ReactNode, useContext, useMemo } from "react"
|
|
33
|
+
import {
|
|
34
|
+
type ActionRunDetail,
|
|
35
|
+
ActionRunFailedError,
|
|
36
|
+
type ActionWaitOptions,
|
|
37
|
+
type RequestActionAndWaitInput,
|
|
38
|
+
requestActionAndWait,
|
|
39
|
+
} from "./actions"
|
|
40
|
+
import { type SixbFileUploadError, type UploadFileInput, uploadFile } from "./file"
|
|
41
|
+
import {
|
|
42
|
+
getActionRunQueryKey,
|
|
43
|
+
listActionRunsInfiniteQueryKey,
|
|
44
|
+
listActionRunsQueryKey,
|
|
45
|
+
} from "./generated/@tanstack/react-query.gen"
|
|
46
|
+
import type { Client } from "./generated/client"
|
|
47
|
+
import { getBulkTelemetryHistory, getTelemetryHistory, type Options } from "./generated/sdk.gen"
|
|
48
|
+
import type {
|
|
49
|
+
GetBulkTelemetryHistoryData,
|
|
50
|
+
GetTelemetryHistoryData,
|
|
51
|
+
RequestActionData,
|
|
52
|
+
} from "./generated/types.gen"
|
|
53
|
+
import { createHttpQueryExecutor } from "./query"
|
|
54
|
+
|
|
55
|
+
const objectQueryBaseKey = ["sixb", "objects"] as const
|
|
56
|
+
|
|
57
|
+
export type ObjectQueryKeyScope = "query" | "count" | "exists" | "facets" | "infinite"
|
|
58
|
+
|
|
59
|
+
export type ObjectQueryBaseKey = typeof objectQueryBaseKey
|
|
60
|
+
|
|
61
|
+
export type ObjectQueryKey = readonly ["sixb", "objects", ObjectQueryKeyScope, ObjectQuery, unknown]
|
|
62
|
+
|
|
63
|
+
export type ObjectQueryLike = {
|
|
64
|
+
readonly ir: ObjectQuery
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type ObjectQueryFacetKeyInput =
|
|
68
|
+
| ObjectQueryExecutorFacetRequest
|
|
69
|
+
| {
|
|
70
|
+
readonly property: { readonly id: string }
|
|
71
|
+
readonly limit: number
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ObjectQueryInfiniteKeyOptions {
|
|
75
|
+
readonly pageSize: number
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type ObjectQueryInvalidationClient = Pick<QueryClient, "invalidateQueries">
|
|
79
|
+
|
|
80
|
+
function objectQueryKey(scope: ObjectQueryKeyScope, ir: ObjectQuery, extra?: unknown) {
|
|
81
|
+
return [...objectQueryBaseKey, scope, ir, extra ?? null] as const
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function objectQueryFacetKey(
|
|
85
|
+
facets: readonly ObjectQueryFacetKeyInput[]
|
|
86
|
+
): ObjectQueryExecutorFacetRequest[] {
|
|
87
|
+
return facets.map((facet) => ({
|
|
88
|
+
propertyId: "propertyId" in facet ? facet.propertyId : facet.property.id,
|
|
89
|
+
limit: facet.limit,
|
|
90
|
+
}))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const objectQueryKeys = {
|
|
94
|
+
all(): ObjectQueryBaseKey {
|
|
95
|
+
return objectQueryBaseKey
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
list(query: ObjectQueryLike, options?: ObjectQueryListOptions): ObjectQueryKey {
|
|
99
|
+
return objectQueryKey("query", query.ir, options)
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
count(query: ObjectQueryLike): ObjectQueryKey {
|
|
103
|
+
return objectQueryKey("count", query.ir)
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
exists(query: ObjectQueryLike): ObjectQueryKey {
|
|
107
|
+
return objectQueryKey("exists", query.ir)
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
facets(query: ObjectQueryLike, facets: readonly ObjectQueryFacetKeyInput[]): ObjectQueryKey {
|
|
111
|
+
return objectQueryKey("facets", query.ir, objectQueryFacetKey(facets))
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
infinite(query: ObjectQueryLike, options: ObjectQueryInfiniteKeyOptions): ObjectQueryKey {
|
|
115
|
+
return objectQueryKey("infinite", query.ir, options.pageSize)
|
|
116
|
+
},
|
|
117
|
+
} as const
|
|
118
|
+
|
|
119
|
+
export function invalidateObjectQueries(queryClient: ObjectQueryInvalidationClient): Promise<void> {
|
|
120
|
+
return queryClient.invalidateQueries({ queryKey: objectQueryKeys.all() })
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function invalidateObjectQuery(
|
|
124
|
+
queryClient: ObjectQueryInvalidationClient,
|
|
125
|
+
query: ObjectQueryLike,
|
|
126
|
+
options?: ObjectQueryListOptions
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
return queryClient.invalidateQueries({
|
|
129
|
+
queryKey: objectQueryKeys.list(query, options),
|
|
130
|
+
exact: true,
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function invalidateObjectCountQuery(
|
|
135
|
+
queryClient: ObjectQueryInvalidationClient,
|
|
136
|
+
query: ObjectQueryLike
|
|
137
|
+
): Promise<void> {
|
|
138
|
+
return queryClient.invalidateQueries({
|
|
139
|
+
queryKey: objectQueryKeys.count(query),
|
|
140
|
+
exact: true,
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function invalidateObjectExistsQuery(
|
|
145
|
+
queryClient: ObjectQueryInvalidationClient,
|
|
146
|
+
query: ObjectQueryLike
|
|
147
|
+
): Promise<void> {
|
|
148
|
+
return queryClient.invalidateQueries({
|
|
149
|
+
queryKey: objectQueryKeys.exists(query),
|
|
150
|
+
exact: true,
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function invalidateObjectFacetsQuery(
|
|
155
|
+
queryClient: ObjectQueryInvalidationClient,
|
|
156
|
+
query: ObjectQueryLike,
|
|
157
|
+
facets: readonly ObjectQueryFacetKeyInput[]
|
|
158
|
+
): Promise<void> {
|
|
159
|
+
return queryClient.invalidateQueries({
|
|
160
|
+
queryKey: objectQueryKeys.facets(query, facets),
|
|
161
|
+
exact: true,
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function invalidateObjectInfiniteQuery(
|
|
166
|
+
queryClient: ObjectQueryInvalidationClient,
|
|
167
|
+
query: ObjectQueryLike,
|
|
168
|
+
options: ObjectQueryInfiniteKeyOptions
|
|
169
|
+
): Promise<void> {
|
|
170
|
+
return queryClient.invalidateQueries({
|
|
171
|
+
queryKey: objectQueryKeys.infinite(query, options),
|
|
172
|
+
exact: true,
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const telemetryHistoryBaseKey = ["sixb", "telemetry", "history"] as const
|
|
177
|
+
|
|
178
|
+
function toTelemetryHistoryDate(value: Date | string | undefined): string | undefined {
|
|
179
|
+
return value instanceof Date ? value.toISOString() : value
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function toTelemetryHistoryLimit(value: number | string | undefined): string | undefined {
|
|
183
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
184
|
+
return Math.max(1, Math.trunc(value)).toString()
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (typeof value === "string") {
|
|
188
|
+
const parsed = Number.parseInt(value, 10)
|
|
189
|
+
if (Number.isFinite(parsed)) {
|
|
190
|
+
return Math.max(1, parsed).toString()
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return undefined
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function toBulkTelemetryHistoryLimit(value: number | undefined): number | undefined {
|
|
198
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
199
|
+
? Math.max(0, Math.trunc(value))
|
|
200
|
+
: undefined
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Options factories accept anything query-shaped, so they work with builders
|
|
204
|
+
// from `objects()` here and from the server runtime alike.
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* TanStack options for `query.list()`.
|
|
208
|
+
*
|
|
209
|
+
* Note on `includeTotal: false`: the request skips the count query and the
|
|
210
|
+
* response omits `total` at runtime, but `data` is still typed `ListResult`
|
|
211
|
+
* (with `total: number`). TanStack's `DataTag`-branded query keys are
|
|
212
|
+
* invariant in the data type, so narrowing this truthfully requires unsafe
|
|
213
|
+
* casts that aren't worth the niche path — infinite pagination, the main
|
|
214
|
+
* no-total consumer, has its own correctly typed factory.
|
|
215
|
+
*/
|
|
216
|
+
export function objectQueryOptions<TObject>(
|
|
217
|
+
query: {
|
|
218
|
+
readonly ir: ObjectQuery
|
|
219
|
+
list(options?: ObjectQueryListOptions): Promise<ListResult<TObject>>
|
|
220
|
+
},
|
|
221
|
+
options?: ObjectQueryListOptions
|
|
222
|
+
) {
|
|
223
|
+
return queryOptions({
|
|
224
|
+
queryKey: objectQueryKeys.list(query, options),
|
|
225
|
+
queryFn: () => query.list(options),
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function objectQueryCountOptions(query: {
|
|
230
|
+
readonly ir: ObjectQuery
|
|
231
|
+
count(): Promise<number>
|
|
232
|
+
}) {
|
|
233
|
+
return queryOptions({
|
|
234
|
+
queryKey: objectQueryKeys.count(query),
|
|
235
|
+
queryFn: () => query.count(),
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function objectQueryExistsOptions(query: {
|
|
240
|
+
readonly ir: ObjectQuery
|
|
241
|
+
exists(): Promise<boolean>
|
|
242
|
+
}) {
|
|
243
|
+
return queryOptions({
|
|
244
|
+
queryKey: objectQueryKeys.exists(query),
|
|
245
|
+
queryFn: () => query.exists(),
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function objectQueryFacetsOptions<TFacetInput>(
|
|
250
|
+
query: {
|
|
251
|
+
readonly ir: ObjectQuery
|
|
252
|
+
facets(input: readonly TFacetInput[]): Promise<ObjectQueryFacetResult[]>
|
|
253
|
+
},
|
|
254
|
+
facets: readonly TFacetInput[]
|
|
255
|
+
) {
|
|
256
|
+
return queryOptions({
|
|
257
|
+
queryKey: objectQueryKeys.facets(query, facets as readonly ObjectQueryFacetKeyInput[]),
|
|
258
|
+
queryFn: () => query.facets(facets),
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function objectQueryInfiniteOptions<TObject>(
|
|
263
|
+
query: {
|
|
264
|
+
readonly ir: ObjectQuery
|
|
265
|
+
page(input: { pageSize: number; pageToken?: string }): {
|
|
266
|
+
list(options: { includeTotal: false }): Promise<ListResultWithoutTotal<TObject>>
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
options: { pageSize: number }
|
|
270
|
+
) {
|
|
271
|
+
return infiniteQueryOptions({
|
|
272
|
+
queryKey: objectQueryKeys.infinite(query, options),
|
|
273
|
+
initialPageParam: undefined as string | undefined,
|
|
274
|
+
queryFn: ({ pageParam }) =>
|
|
275
|
+
query.page({ pageSize: options.pageSize, pageToken: pageParam }).list({
|
|
276
|
+
includeTotal: false,
|
|
277
|
+
}),
|
|
278
|
+
getNextPageParam: (lastPage) => lastPage.nextPageToken,
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Optional transport override. Without a provider, hooks use the global
|
|
283
|
+
// hey-api client, matching the rest of @sixb/client.
|
|
284
|
+
|
|
285
|
+
const SixbClientContext = createContext<Client | undefined>(undefined)
|
|
286
|
+
|
|
287
|
+
export function SixbProvider(props: { client: Client; children?: ReactNode }) {
|
|
288
|
+
return createElement(SixbClientContext.Provider, { value: props.client }, props.children)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export type UseUploadFileInput = UploadFileInput
|
|
292
|
+
|
|
293
|
+
export function useUploadFile(): UseMutationResult<
|
|
294
|
+
FileRef,
|
|
295
|
+
SixbFileUploadError,
|
|
296
|
+
UseUploadFileInput
|
|
297
|
+
> {
|
|
298
|
+
const client = useContext(SixbClientContext)
|
|
299
|
+
|
|
300
|
+
return useMutation({
|
|
301
|
+
mutationFn: ({
|
|
302
|
+
file,
|
|
303
|
+
fetch,
|
|
304
|
+
fileName,
|
|
305
|
+
logicalPath,
|
|
306
|
+
client: inputClient,
|
|
307
|
+
stagedUploadThresholdBytes,
|
|
308
|
+
signal,
|
|
309
|
+
}) =>
|
|
310
|
+
uploadFile(file, {
|
|
311
|
+
client: inputClient ?? client,
|
|
312
|
+
fetch,
|
|
313
|
+
fileName,
|
|
314
|
+
logicalPath,
|
|
315
|
+
stagedUploadThresholdBytes,
|
|
316
|
+
signal,
|
|
317
|
+
}),
|
|
318
|
+
})
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export type ActionRunMutationWireSubject = NonNullable<RequestActionData["body"]["subject"]>
|
|
322
|
+
|
|
323
|
+
export type ActionRunMutationObjectSubject<
|
|
324
|
+
TObjectType extends ObjectTypeWithPropertyTokens = ObjectTypeWithPropertyTokens,
|
|
325
|
+
> = {
|
|
326
|
+
readonly objectType: TObjectType
|
|
327
|
+
readonly primaryId: string
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export type ActionRunMutationSubject = ActionRunMutationWireSubject | ActionRunMutationObjectSubject
|
|
331
|
+
|
|
332
|
+
type ActionRunMutationRequestBody = Omit<RequestActionAndWaitInput["body"], "subject"> & {
|
|
333
|
+
readonly subject?: ActionRunMutationSubject
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export type ActionRunMutationRequest = Omit<RequestActionAndWaitInput, "body"> & {
|
|
337
|
+
readonly body: ActionRunMutationRequestBody
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export interface ActionRunMutationBaseOptions<TVariables, TContext>
|
|
341
|
+
extends ActionWaitOptions,
|
|
342
|
+
Omit<UseMutationOptions<ActionRunDetail, Error, TVariables, TContext>, "mutationFn"> {
|
|
343
|
+
/** hey-api client override. Defaults to the nearest SixbProvider client, then the global client. */
|
|
344
|
+
readonly client?: Client
|
|
345
|
+
/** Invalidate action-run caches, and object-query caches when the terminal run committed changes. */
|
|
346
|
+
readonly invalidateOnCommit?: boolean
|
|
347
|
+
/** Override used by tests and non-hook option factory consumers. */
|
|
348
|
+
readonly queryClient?: QueryClient
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export interface ConfiguredActionRunMutationOptions<
|
|
352
|
+
TParams extends Record<string, unknown> = Record<string, unknown>,
|
|
353
|
+
TContext = unknown,
|
|
354
|
+
> extends ActionRunMutationBaseOptions<TParams | undefined, TContext> {
|
|
355
|
+
readonly actionId: string
|
|
356
|
+
readonly subject?: ActionRunMutationSubject
|
|
357
|
+
readonly runId?: string
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export interface DynamicActionRunMutationOptions<TContext = unknown>
|
|
361
|
+
extends ActionRunMutationBaseOptions<ActionRunMutationRequest, TContext> {
|
|
362
|
+
readonly actionId?: never
|
|
363
|
+
readonly subject?: never
|
|
364
|
+
readonly runId?: never
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
type InternalActionRunMutationOptions<TVariables, TContext> = ActionRunMutationBaseOptions<
|
|
368
|
+
TVariables,
|
|
369
|
+
TContext
|
|
370
|
+
> & {
|
|
371
|
+
readonly actionId?: string
|
|
372
|
+
readonly subject?: ActionRunMutationSubject
|
|
373
|
+
readonly runId?: string
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export function actionRunMutationOptions<
|
|
377
|
+
TParams extends Record<string, unknown> = Record<string, unknown>,
|
|
378
|
+
TContext = unknown,
|
|
379
|
+
>(
|
|
380
|
+
options: ConfiguredActionRunMutationOptions<TParams, TContext>
|
|
381
|
+
): UseMutationOptions<ActionRunDetail, Error, TParams | undefined, TContext>
|
|
382
|
+
export function actionRunMutationOptions<TContext = unknown>(
|
|
383
|
+
options?: DynamicActionRunMutationOptions<TContext>
|
|
384
|
+
): UseMutationOptions<ActionRunDetail, Error, ActionRunMutationRequest, TContext>
|
|
385
|
+
export function actionRunMutationOptions<TVariables, TContext>(
|
|
386
|
+
options?: InternalActionRunMutationOptions<TVariables, TContext>
|
|
387
|
+
): UseMutationOptions<ActionRunDetail, Error, TVariables, TContext> {
|
|
388
|
+
return createActionRunMutationOptions(options)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function createActionRunMutationOptions<TVariables, TContext>(
|
|
392
|
+
options?: InternalActionRunMutationOptions<TVariables, TContext>
|
|
393
|
+
): UseMutationOptions<ActionRunDetail, Error, TVariables, TContext> {
|
|
394
|
+
const {
|
|
395
|
+
actionId,
|
|
396
|
+
subject,
|
|
397
|
+
runId,
|
|
398
|
+
timeoutMs,
|
|
399
|
+
fallbackPollIntervalMs,
|
|
400
|
+
disconnectedPollIntervalMs,
|
|
401
|
+
signal,
|
|
402
|
+
rejectOnTerminalFailure,
|
|
403
|
+
client,
|
|
404
|
+
invalidateOnCommit = false,
|
|
405
|
+
queryClient,
|
|
406
|
+
onSuccess,
|
|
407
|
+
onError,
|
|
408
|
+
...mutationOptions
|
|
409
|
+
} = options ?? {}
|
|
410
|
+
|
|
411
|
+
const waitOptions: ActionWaitOptions = {
|
|
412
|
+
timeoutMs,
|
|
413
|
+
fallbackPollIntervalMs,
|
|
414
|
+
disconnectedPollIntervalMs,
|
|
415
|
+
signal,
|
|
416
|
+
rejectOnTerminalFailure,
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
...mutationOptions,
|
|
421
|
+
mutationFn: (variables) =>
|
|
422
|
+
requestActionAndWait(
|
|
423
|
+
buildActionRunMutationRequest(variables, {
|
|
424
|
+
actionId,
|
|
425
|
+
subject,
|
|
426
|
+
runId,
|
|
427
|
+
client,
|
|
428
|
+
waitOptions,
|
|
429
|
+
})
|
|
430
|
+
),
|
|
431
|
+
onSuccess: async (run, variables, context, mutation) => {
|
|
432
|
+
if (invalidateOnCommit && queryClient) {
|
|
433
|
+
await invalidateActionRunMutationCaches(queryClient, run)
|
|
434
|
+
}
|
|
435
|
+
await onSuccess?.(run, variables, context, mutation)
|
|
436
|
+
},
|
|
437
|
+
onError: async (error, variables, context, mutation) => {
|
|
438
|
+
if (invalidateOnCommit && queryClient && error instanceof ActionRunFailedError) {
|
|
439
|
+
await invalidateActionRunMutationCaches(queryClient, error.run)
|
|
440
|
+
}
|
|
441
|
+
await onError?.(error, variables, context, mutation)
|
|
442
|
+
},
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
export function useActionRunMutation<
|
|
447
|
+
TParams extends Record<string, unknown> = Record<string, unknown>,
|
|
448
|
+
TContext = unknown,
|
|
449
|
+
>(
|
|
450
|
+
options: ConfiguredActionRunMutationOptions<TParams, TContext>
|
|
451
|
+
): UseMutationResult<ActionRunDetail, Error, TParams | undefined, TContext>
|
|
452
|
+
export function useActionRunMutation<TContext = unknown>(
|
|
453
|
+
options?: DynamicActionRunMutationOptions<TContext>
|
|
454
|
+
): UseMutationResult<ActionRunDetail, Error, ActionRunMutationRequest, TContext>
|
|
455
|
+
export function useActionRunMutation<TVariables, TContext>(
|
|
456
|
+
options?: InternalActionRunMutationOptions<TVariables, TContext>
|
|
457
|
+
): UseMutationResult<ActionRunDetail, Error, TVariables, TContext> {
|
|
458
|
+
const providerClient = useContext(SixbClientContext)
|
|
459
|
+
const queryClient = useQueryClient()
|
|
460
|
+
return useMutation(
|
|
461
|
+
createActionRunMutationOptions({
|
|
462
|
+
...options,
|
|
463
|
+
client: options?.client ?? providerClient,
|
|
464
|
+
queryClient: options?.queryClient ?? queryClient,
|
|
465
|
+
} as InternalActionRunMutationOptions<TVariables, TContext>)
|
|
466
|
+
) as UseMutationResult<ActionRunDetail, Error, TVariables, TContext>
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function buildActionRunMutationRequest<TVariables>(
|
|
470
|
+
variables: TVariables,
|
|
471
|
+
options: {
|
|
472
|
+
readonly actionId?: string
|
|
473
|
+
readonly subject?: ActionRunMutationSubject
|
|
474
|
+
readonly runId?: string
|
|
475
|
+
readonly client?: Client
|
|
476
|
+
readonly waitOptions: ActionWaitOptions
|
|
477
|
+
}
|
|
478
|
+
): RequestActionAndWaitInput {
|
|
479
|
+
if (options.actionId) {
|
|
480
|
+
const params = variables === undefined ? undefined : (variables as Record<string, unknown>)
|
|
481
|
+
const subject = normalizeActionRunMutationSubject(options.subject)
|
|
482
|
+
return {
|
|
483
|
+
...options.waitOptions,
|
|
484
|
+
client: options.client,
|
|
485
|
+
path: { actionId: options.actionId },
|
|
486
|
+
body: {
|
|
487
|
+
...(subject ? { subject } : {}),
|
|
488
|
+
...(params ? { params } : {}),
|
|
489
|
+
...(options.runId ? { runId: options.runId } : {}),
|
|
490
|
+
},
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (isActionRunMutationRequest(variables)) {
|
|
495
|
+
const subject = normalizeActionRunMutationSubject(variables.body.subject)
|
|
496
|
+
return {
|
|
497
|
+
...options.waitOptions,
|
|
498
|
+
client: options.client,
|
|
499
|
+
...variables,
|
|
500
|
+
body: {
|
|
501
|
+
...(subject ? { subject } : {}),
|
|
502
|
+
...(variables.body.params ? { params: variables.body.params } : {}),
|
|
503
|
+
...(variables.body.runId ? { runId: variables.body.runId } : {}),
|
|
504
|
+
},
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
throw new Error(
|
|
509
|
+
"[SixbClient] useActionRunMutation() without an actionId requires full request options."
|
|
510
|
+
)
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function normalizeActionRunMutationSubject(
|
|
514
|
+
subject: ActionRunMutationSubject | undefined
|
|
515
|
+
): ActionRunMutationWireSubject | undefined {
|
|
516
|
+
if (!subject) {
|
|
517
|
+
return undefined
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if ("objectType" in subject) {
|
|
521
|
+
return {
|
|
522
|
+
kind: "object",
|
|
523
|
+
objectTypeId: subject.objectType.id,
|
|
524
|
+
primaryId: subject.primaryId,
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
return subject
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function isActionRunMutationRequest(value: unknown): value is ActionRunMutationRequest {
|
|
532
|
+
return (
|
|
533
|
+
isRecord(value) &&
|
|
534
|
+
isRecord(value.path) &&
|
|
535
|
+
typeof value.path.actionId === "string" &&
|
|
536
|
+
isRecord(value.body)
|
|
537
|
+
)
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
async function invalidateActionRunMutationCaches(
|
|
541
|
+
queryClient: Pick<QueryClient, "invalidateQueries">,
|
|
542
|
+
run: ActionRunDetail
|
|
543
|
+
): Promise<void> {
|
|
544
|
+
const invalidations: Promise<unknown>[] = [
|
|
545
|
+
queryClient.invalidateQueries({
|
|
546
|
+
queryKey: getActionRunQueryKey({ path: { runId: run.id } }),
|
|
547
|
+
exact: true,
|
|
548
|
+
}),
|
|
549
|
+
queryClient.invalidateQueries({ queryKey: listActionRunsQueryKey() }),
|
|
550
|
+
queryClient.invalidateQueries({ queryKey: listActionRunsInfiniteQueryKey() }),
|
|
551
|
+
]
|
|
552
|
+
|
|
553
|
+
// A run that reached the commit phase may have changed any object the action touched. The run
|
|
554
|
+
// record no longer carries a per-object diff — object changes live in the authoritative ontology
|
|
555
|
+
// commit — so object reads are invalidated wholesale.
|
|
556
|
+
if (run.phase === "commit" || run.phase === "effects") {
|
|
557
|
+
invalidations.push(
|
|
558
|
+
invalidateObjectQueries(queryClient),
|
|
559
|
+
queryClient.invalidateQueries({ predicate: isGeneratedObjectRead })
|
|
560
|
+
)
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
await Promise.all(invalidations)
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
567
|
+
return typeof value === "object" && value !== null
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/** Matches every generated single-object read, regardless of object type or id. */
|
|
571
|
+
function isGeneratedObjectRead(query: { readonly queryKey: readonly unknown[] }): boolean {
|
|
572
|
+
const [descriptor] = query.queryKey
|
|
573
|
+
return isRecord(descriptor) && descriptor._id === "getObject"
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Hooks accept any built query — anything carrying a normalized `.ir` — and
|
|
577
|
+
// execute that IR through the SixbProvider client (or the global client), so
|
|
578
|
+
// the same query value works in components, event handlers, loaders, and on
|
|
579
|
+
// the server. Hooks constrain the query to `{ ir }` only and extract result
|
|
580
|
+
// types with conditional `infer`: putting the builder in a callback parameter
|
|
581
|
+
// position, constraining on its overloaded members, or extracting via
|
|
582
|
+
// `ReturnType` all overflow TypeScript's recursion depth on real ontologies.
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Result row type of a built query, taken from its `first()` terminal.
|
|
586
|
+
* Rebuilt as an anonymous structural type: relating the generic `TwinObject`
|
|
587
|
+
* reference itself in deep contexts (e.g. `rows.map(...)` callbacks) can
|
|
588
|
+
* overflow TypeScript's recursion limits.
|
|
589
|
+
*/
|
|
590
|
+
type BuiltRow<TBuilt> = TBuilt extends { first(): Promise<infer TRow> }
|
|
591
|
+
? NonNullable<TRow> extends { objectTypeId: infer TObjectTypeId; properties: infer TProperties }
|
|
592
|
+
? {
|
|
593
|
+
primaryId: string
|
|
594
|
+
objectTypeId: TObjectTypeId
|
|
595
|
+
properties: TProperties
|
|
596
|
+
createdAt: Date
|
|
597
|
+
updatedAt: Date
|
|
598
|
+
} & (NonNullable<TRow> extends { links: infer TLinks } ? { links: TLinks } : unknown)
|
|
599
|
+
: never
|
|
600
|
+
: never
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Common TanStack passthrough options. For anything beyond these (`select`,
|
|
604
|
+
* `placeholderData`, ...), compose the `objectQuery*Options` factories with
|
|
605
|
+
* `useQuery` directly.
|
|
606
|
+
*/
|
|
607
|
+
type QueryHookExtras = {
|
|
608
|
+
enabled?: boolean
|
|
609
|
+
staleTime?: number
|
|
610
|
+
gcTime?: number
|
|
611
|
+
refetchInterval?: number | false
|
|
612
|
+
refetchOnWindowFocus?: boolean
|
|
613
|
+
retry?: boolean | number
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export type TelemetryHistoryValue<
|
|
617
|
+
TProperty extends { readonly property: Pick<Property, "schema" | "nullable"> },
|
|
618
|
+
> = InferPropertyValue<TProperty["property"]>
|
|
619
|
+
|
|
620
|
+
export interface TelemetryHistoryPoint<TValue> {
|
|
621
|
+
readonly projectId: string
|
|
622
|
+
readonly objectTypeId: string
|
|
623
|
+
readonly objectId: string
|
|
624
|
+
readonly propertyId: string
|
|
625
|
+
readonly value: TValue
|
|
626
|
+
readonly unit?: string
|
|
627
|
+
readonly at: string
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
export type TelemetryHistoryPoints<
|
|
631
|
+
TProperty extends { readonly property: Pick<Property, "schema" | "nullable"> },
|
|
632
|
+
> = readonly TelemetryHistoryPoint<TelemetryHistoryValue<TProperty>>[]
|
|
633
|
+
|
|
634
|
+
export interface BulkTelemetryHistorySeries<TValue> {
|
|
635
|
+
readonly objectTypeId: string
|
|
636
|
+
readonly objectId: string
|
|
637
|
+
readonly propertyId: string
|
|
638
|
+
readonly points: readonly TelemetryHistoryPoint<TValue>[]
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
export type BulkTelemetryHistory<
|
|
642
|
+
TProperties extends readonly { readonly property: Pick<Property, "schema" | "nullable"> }[],
|
|
643
|
+
> = readonly BulkTelemetryHistorySeries<TelemetryHistoryValue<TProperties[number]>>[]
|
|
644
|
+
|
|
645
|
+
export interface TelemetryHistoryQueryOptions<
|
|
646
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
647
|
+
TProperty extends TelemetryPropertyToken<TObjectType> = TelemetryPropertyToken<TObjectType>,
|
|
648
|
+
> extends Omit<Options<GetTelemetryHistoryData>, "path" | "query"> {
|
|
649
|
+
readonly objectType: TObjectType
|
|
650
|
+
readonly objectId: string
|
|
651
|
+
readonly property: TProperty & TelemetryPropertyToken<NoInfer<TObjectType>>
|
|
652
|
+
readonly from?: Date | string
|
|
653
|
+
readonly to?: Date | string
|
|
654
|
+
readonly limit?: number | string
|
|
655
|
+
readonly order?: "asc" | "desc"
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
export type TelemetryHistoryHookOptions<
|
|
659
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
660
|
+
TProperty extends TelemetryPropertyToken<TObjectType> = TelemetryPropertyToken<TObjectType>,
|
|
661
|
+
> = TelemetryHistoryQueryOptions<TObjectType, TProperty> & QueryHookExtras
|
|
662
|
+
|
|
663
|
+
export interface BulkTelemetryHistoryQueryOptions<
|
|
664
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
665
|
+
TProperties extends
|
|
666
|
+
readonly TelemetryPropertyToken<TObjectType>[] = readonly TelemetryPropertyToken<TObjectType>[],
|
|
667
|
+
> extends Omit<Options<GetBulkTelemetryHistoryData>, "path" | "body"> {
|
|
668
|
+
readonly objectType: TObjectType
|
|
669
|
+
readonly objectIds: readonly string[]
|
|
670
|
+
readonly properties: TProperties & readonly TelemetryPropertyToken<NoInfer<TObjectType>>[]
|
|
671
|
+
readonly from?: Date | string
|
|
672
|
+
readonly to?: Date | string
|
|
673
|
+
readonly limit?: number
|
|
674
|
+
readonly order?: "asc" | "desc"
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
export type BulkTelemetryHistoryHookOptions<
|
|
678
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
679
|
+
TProperties extends
|
|
680
|
+
readonly TelemetryPropertyToken<TObjectType>[] = readonly TelemetryPropertyToken<TObjectType>[],
|
|
681
|
+
> = BulkTelemetryHistoryQueryOptions<TObjectType, TProperties> & QueryHookExtras
|
|
682
|
+
|
|
683
|
+
function telemetryHistoryPath<
|
|
684
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
685
|
+
TProperty extends TelemetryPropertyToken<TObjectType>,
|
|
686
|
+
>(options: TelemetryHistoryQueryOptions<TObjectType, TProperty>) {
|
|
687
|
+
return {
|
|
688
|
+
objectTypeId: options.objectType.id,
|
|
689
|
+
objectId: options.objectId,
|
|
690
|
+
propertyId: options.property.id,
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function telemetryHistoryQuery<
|
|
695
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
696
|
+
TProperty extends TelemetryPropertyToken<TObjectType>,
|
|
697
|
+
>(options: TelemetryHistoryQueryOptions<TObjectType, TProperty>) {
|
|
698
|
+
return {
|
|
699
|
+
from: toTelemetryHistoryDate(options.from),
|
|
700
|
+
to: toTelemetryHistoryDate(options.to),
|
|
701
|
+
limit: toTelemetryHistoryLimit(options.limit),
|
|
702
|
+
order: options.order ?? "asc",
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
export function telemetryHistoryQueryKey<
|
|
707
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
708
|
+
TProperty extends TelemetryPropertyToken<TObjectType>,
|
|
709
|
+
>(options: TelemetryHistoryQueryOptions<TObjectType, TProperty>) {
|
|
710
|
+
return [
|
|
711
|
+
...telemetryHistoryBaseKey,
|
|
712
|
+
telemetryHistoryPath(options),
|
|
713
|
+
telemetryHistoryQuery(options),
|
|
714
|
+
] as const
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
async function fetchTelemetryHistory<
|
|
718
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
719
|
+
TProperty extends TelemetryPropertyToken<TObjectType>,
|
|
720
|
+
>(
|
|
721
|
+
options: TelemetryHistoryQueryOptions<TObjectType, TProperty>,
|
|
722
|
+
signal?: AbortSignal
|
|
723
|
+
): Promise<TelemetryHistoryPoints<TProperty>> {
|
|
724
|
+
const path = telemetryHistoryPath(options)
|
|
725
|
+
const query = telemetryHistoryQuery(options)
|
|
726
|
+
const {
|
|
727
|
+
objectType: _objectType,
|
|
728
|
+
objectId: _objectId,
|
|
729
|
+
property: _property,
|
|
730
|
+
from: _from,
|
|
731
|
+
to: _to,
|
|
732
|
+
limit: _limit,
|
|
733
|
+
order: _order,
|
|
734
|
+
...rest
|
|
735
|
+
} = options
|
|
736
|
+
|
|
737
|
+
const { data } = await getTelemetryHistory({
|
|
738
|
+
...rest,
|
|
739
|
+
path,
|
|
740
|
+
query,
|
|
741
|
+
signal,
|
|
742
|
+
responseStyle: "fields",
|
|
743
|
+
throwOnError: true,
|
|
744
|
+
})
|
|
745
|
+
|
|
746
|
+
return data.map((point) => ({
|
|
747
|
+
projectId: point.projectId,
|
|
748
|
+
objectTypeId: point.objectTypeId,
|
|
749
|
+
objectId: point.objectId,
|
|
750
|
+
propertyId: point.propertyId,
|
|
751
|
+
value: point.value as TelemetryHistoryValue<TProperty>,
|
|
752
|
+
unit: point.unit,
|
|
753
|
+
at: point.at,
|
|
754
|
+
}))
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
function bulkTelemetryHistoryBody<
|
|
758
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
759
|
+
TProperties extends readonly TelemetryPropertyToken<TObjectType>[],
|
|
760
|
+
>(options: BulkTelemetryHistoryQueryOptions<TObjectType, TProperties>) {
|
|
761
|
+
const propertyIds = options.properties.map((property) => property.id)
|
|
762
|
+
return {
|
|
763
|
+
series: options.objectIds.flatMap((objectId) =>
|
|
764
|
+
propertyIds.map((propertyId) => ({
|
|
765
|
+
objectTypeId: options.objectType.id,
|
|
766
|
+
objectId,
|
|
767
|
+
propertyId,
|
|
768
|
+
}))
|
|
769
|
+
),
|
|
770
|
+
from: toTelemetryHistoryDate(options.from),
|
|
771
|
+
to: toTelemetryHistoryDate(options.to),
|
|
772
|
+
limitPerSeries: toBulkTelemetryHistoryLimit(options.limit),
|
|
773
|
+
order: options.order ?? "asc",
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
export function bulkTelemetryHistoryQueryKey<
|
|
778
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
779
|
+
TProperties extends readonly TelemetryPropertyToken<TObjectType>[],
|
|
780
|
+
>(options: BulkTelemetryHistoryQueryOptions<TObjectType, TProperties>) {
|
|
781
|
+
return [...telemetryHistoryBaseKey, "bulk", bulkTelemetryHistoryBody(options)] as const
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
async function fetchBulkTelemetryHistory<
|
|
785
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
786
|
+
TProperties extends readonly TelemetryPropertyToken<TObjectType>[],
|
|
787
|
+
>(
|
|
788
|
+
options: BulkTelemetryHistoryQueryOptions<TObjectType, TProperties>,
|
|
789
|
+
signal?: AbortSignal
|
|
790
|
+
): Promise<BulkTelemetryHistory<TProperties>> {
|
|
791
|
+
const body = bulkTelemetryHistoryBody(options)
|
|
792
|
+
const {
|
|
793
|
+
objectType: _objectType,
|
|
794
|
+
objectIds: _objectIds,
|
|
795
|
+
properties: _properties,
|
|
796
|
+
from: _from,
|
|
797
|
+
to: _to,
|
|
798
|
+
limit: _limit,
|
|
799
|
+
order: _order,
|
|
800
|
+
...rest
|
|
801
|
+
} = options
|
|
802
|
+
|
|
803
|
+
const { data } = await getBulkTelemetryHistory({
|
|
804
|
+
...rest,
|
|
805
|
+
body,
|
|
806
|
+
signal,
|
|
807
|
+
responseStyle: "fields",
|
|
808
|
+
throwOnError: true,
|
|
809
|
+
})
|
|
810
|
+
|
|
811
|
+
return data.series.map((series) => ({
|
|
812
|
+
objectTypeId: series.objectTypeId,
|
|
813
|
+
objectId: series.objectId,
|
|
814
|
+
propertyId: series.propertyId,
|
|
815
|
+
points: series.points.map((point) => ({
|
|
816
|
+
projectId: point.projectId,
|
|
817
|
+
objectTypeId: point.objectTypeId,
|
|
818
|
+
objectId: point.objectId,
|
|
819
|
+
propertyId: point.propertyId,
|
|
820
|
+
value: point.value as TelemetryHistoryValue<TProperties[number]>,
|
|
821
|
+
unit: point.unit,
|
|
822
|
+
at: point.at,
|
|
823
|
+
})),
|
|
824
|
+
}))
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
export function telemetryHistoryQueryOptions<
|
|
828
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
829
|
+
TProperty extends TelemetryPropertyToken<TObjectType>,
|
|
830
|
+
>(options: TelemetryHistoryQueryOptions<TObjectType, TProperty>) {
|
|
831
|
+
return queryOptions({
|
|
832
|
+
queryKey: telemetryHistoryQueryKey(options),
|
|
833
|
+
queryFn: ({ signal }) => fetchTelemetryHistory(options, signal),
|
|
834
|
+
})
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
export function bulkTelemetryHistoryQueryOptions<
|
|
838
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
839
|
+
TProperties extends readonly TelemetryPropertyToken<TObjectType>[],
|
|
840
|
+
>(options: BulkTelemetryHistoryQueryOptions<TObjectType, TProperties>) {
|
|
841
|
+
return queryOptions({
|
|
842
|
+
queryKey: bulkTelemetryHistoryQueryKey(options),
|
|
843
|
+
queryFn: ({ signal }) => fetchBulkTelemetryHistory(options, signal),
|
|
844
|
+
})
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* Executor bound to the nearest `SixbProvider` client, or the global client.
|
|
849
|
+
* Hooks run the query IR through this, so the binding a query was built with
|
|
850
|
+
* (`objects(Type, { client })`) does not apply inside hooks.
|
|
851
|
+
*/
|
|
852
|
+
function useClientQueryExecutor(): ObjectQueryExecutor {
|
|
853
|
+
const client = useContext(SixbClientContext)
|
|
854
|
+
return useMemo(() => createHttpQueryExecutor(client), [client])
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
export function useTelemetryHistoryQuery<
|
|
858
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
859
|
+
TProperty extends TelemetryPropertyToken<TObjectType>,
|
|
860
|
+
>(
|
|
861
|
+
options: TelemetryHistoryHookOptions<TObjectType, TProperty>
|
|
862
|
+
): UseQueryResult<TelemetryHistoryPoints<TProperty>, Error> {
|
|
863
|
+
const client = useContext(SixbClientContext)
|
|
864
|
+
const {
|
|
865
|
+
enabled,
|
|
866
|
+
staleTime,
|
|
867
|
+
gcTime,
|
|
868
|
+
refetchInterval,
|
|
869
|
+
refetchOnWindowFocus,
|
|
870
|
+
retry,
|
|
871
|
+
...historyOptions
|
|
872
|
+
} = options
|
|
873
|
+
|
|
874
|
+
const historyInput: TelemetryHistoryQueryOptions<TObjectType, TProperty> = {
|
|
875
|
+
...historyOptions,
|
|
876
|
+
client: historyOptions.client ?? client,
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
return useQuery({
|
|
880
|
+
...telemetryHistoryQueryOptions(historyInput),
|
|
881
|
+
enabled,
|
|
882
|
+
staleTime,
|
|
883
|
+
gcTime,
|
|
884
|
+
refetchInterval,
|
|
885
|
+
refetchOnWindowFocus,
|
|
886
|
+
retry,
|
|
887
|
+
})
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
export function useBulkTelemetryHistoryQuery<
|
|
891
|
+
TObjectType extends ObjectTypeWithPropertyTokens,
|
|
892
|
+
TProperties extends readonly TelemetryPropertyToken<TObjectType>[],
|
|
893
|
+
>(
|
|
894
|
+
options: BulkTelemetryHistoryHookOptions<TObjectType, TProperties>
|
|
895
|
+
): UseQueryResult<BulkTelemetryHistory<TProperties>, Error> {
|
|
896
|
+
const client = useContext(SixbClientContext)
|
|
897
|
+
const {
|
|
898
|
+
enabled,
|
|
899
|
+
staleTime,
|
|
900
|
+
gcTime,
|
|
901
|
+
refetchInterval,
|
|
902
|
+
refetchOnWindowFocus,
|
|
903
|
+
retry,
|
|
904
|
+
...historyOptions
|
|
905
|
+
} = options
|
|
906
|
+
|
|
907
|
+
const historyInput: BulkTelemetryHistoryQueryOptions<TObjectType, TProperties> = {
|
|
908
|
+
...historyOptions,
|
|
909
|
+
client: historyOptions.client ?? client,
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
return useQuery({
|
|
913
|
+
...bulkTelemetryHistoryQueryOptions(historyInput),
|
|
914
|
+
enabled,
|
|
915
|
+
staleTime,
|
|
916
|
+
gcTime,
|
|
917
|
+
refetchInterval,
|
|
918
|
+
refetchOnWindowFocus,
|
|
919
|
+
retry,
|
|
920
|
+
})
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
export function useObjectsQuery<TBuilt extends { readonly ir: ObjectQuery }>(
|
|
924
|
+
query: TBuilt,
|
|
925
|
+
options?: QueryHookExtras
|
|
926
|
+
): UseQueryResult<ListResult<BuiltRow<TBuilt>>, Error> {
|
|
927
|
+
const executor = useClientQueryExecutor()
|
|
928
|
+
const ir = query.ir
|
|
929
|
+
return useQuery({
|
|
930
|
+
queryKey: objectQueryKeys.list(query),
|
|
931
|
+
queryFn: async () => {
|
|
932
|
+
const result = await executor.list(ir)
|
|
933
|
+
return {
|
|
934
|
+
objects: [...result.objects],
|
|
935
|
+
hasMore: result.hasMore,
|
|
936
|
+
nextPageToken: result.nextPageToken,
|
|
937
|
+
total: result.total ?? result.objects.length,
|
|
938
|
+
} as ListResult<BuiltRow<TBuilt>>
|
|
939
|
+
},
|
|
940
|
+
...options,
|
|
941
|
+
})
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
export function useObjectsCount(
|
|
945
|
+
query: { readonly ir: ObjectQuery },
|
|
946
|
+
options?: QueryHookExtras
|
|
947
|
+
): UseQueryResult<number, Error> {
|
|
948
|
+
const executor = useClientQueryExecutor()
|
|
949
|
+
const ir = query.ir
|
|
950
|
+
return useQuery({
|
|
951
|
+
queryKey: objectQueryKeys.count(query),
|
|
952
|
+
queryFn: () => executor.count(ir),
|
|
953
|
+
...options,
|
|
954
|
+
})
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
export function useObjectsExists(
|
|
958
|
+
query: { readonly ir: ObjectQuery },
|
|
959
|
+
options?: QueryHookExtras
|
|
960
|
+
): UseQueryResult<boolean, Error> {
|
|
961
|
+
const executor = useClientQueryExecutor()
|
|
962
|
+
const ir = query.ir
|
|
963
|
+
return useQuery({
|
|
964
|
+
queryKey: objectQueryKeys.exists(query),
|
|
965
|
+
queryFn: () => executor.exists(ir),
|
|
966
|
+
...options,
|
|
967
|
+
})
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
export function useObjectsFacets(
|
|
971
|
+
query: { readonly ir: ObjectQuery },
|
|
972
|
+
// Loosely token-typed: tying this to the built query's facet input would
|
|
973
|
+
// require inference through the builder, which the ontology validates
|
|
974
|
+
// server-side anyway.
|
|
975
|
+
facets: readonly { property: { id: string }; limit: number }[],
|
|
976
|
+
options?: QueryHookExtras
|
|
977
|
+
): UseQueryResult<ObjectQueryFacetResult[], Error> {
|
|
978
|
+
const executor = useClientQueryExecutor()
|
|
979
|
+
const ir = query.ir
|
|
980
|
+
const facetRequests = facets.map((facet) => ({
|
|
981
|
+
propertyId: facet.property.id,
|
|
982
|
+
limit: facet.limit,
|
|
983
|
+
}))
|
|
984
|
+
return useQuery({
|
|
985
|
+
queryKey: objectQueryKeys.facets(query, facets),
|
|
986
|
+
queryFn: () => executor.facets(ir, facetRequests),
|
|
987
|
+
...options,
|
|
988
|
+
})
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
export function useObjectsInfinite<TBuilt extends { readonly ir: ObjectQuery }>(
|
|
992
|
+
query: TBuilt,
|
|
993
|
+
options: {
|
|
994
|
+
pageSize: number
|
|
995
|
+
enabled?: boolean
|
|
996
|
+
staleTime?: number
|
|
997
|
+
gcTime?: number
|
|
998
|
+
refetchInterval?: number | false
|
|
999
|
+
}
|
|
1000
|
+
) {
|
|
1001
|
+
const { pageSize, ...extras } = options
|
|
1002
|
+
const executor = useClientQueryExecutor()
|
|
1003
|
+
const ir = query.ir
|
|
1004
|
+
return useInfiniteQuery({
|
|
1005
|
+
queryKey: objectQueryKeys.infinite(query, { pageSize }),
|
|
1006
|
+
initialPageParam: undefined as string | undefined,
|
|
1007
|
+
queryFn: async ({ pageParam }) => {
|
|
1008
|
+
const result = await executor.list(
|
|
1009
|
+
{ kind: "page", pageSize, pageToken: pageParam, input: ir },
|
|
1010
|
+
{ includeTotal: false }
|
|
1011
|
+
)
|
|
1012
|
+
return {
|
|
1013
|
+
objects: [...result.objects],
|
|
1014
|
+
hasMore: result.hasMore,
|
|
1015
|
+
nextPageToken: result.nextPageToken,
|
|
1016
|
+
} as ListResultWithoutTotal<BuiltRow<TBuilt>>
|
|
1017
|
+
},
|
|
1018
|
+
getNextPageParam: (lastPage) => lastPage.nextPageToken,
|
|
1019
|
+
...extras,
|
|
1020
|
+
})
|
|
1021
|
+
}
|