@kivacore/app-utils 0.0.0 → 0.22.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/README.md +1515 -1
- package/dist/index.d.ts +82 -0
- package/dist/index.js +2304 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.js +4695 -0
- package/dist/index.native.js.map +1 -0
- package/dist/query.d.ts +54 -0
- package/dist/query.js +269 -0
- package/dist/query.js.map +1 -0
- package/dist/types-GNkXRpu4.d.ts +754 -0
- package/package.json +57 -3
- package/src/app-config-client.ts +48 -0
- package/src/async-each-helper.ts +98 -0
- package/src/brand-client.ts +80 -0
- package/src/data-form-client.ts +85 -0
- package/src/data-source-client.ts +298 -0
- package/src/data-source-queries.ts +149 -0
- package/src/data-view-client.ts +92 -0
- package/src/date-utils.test.ts +48 -0
- package/src/date-utils.ts +146 -0
- package/src/envelope-interceptor.ts +65 -0
- package/src/get-tenant-preferences.test.ts +42 -0
- package/src/get-tenant-preferences.ts +30 -0
- package/src/handlebars-async-helpers.d.ts +12 -0
- package/src/index.native.ts +9 -0
- package/src/index.ts +88 -0
- package/src/inventory-client.ts +725 -0
- package/src/jsonata-helpers.ts +545 -0
- package/src/number-utils.ts +305 -0
- package/src/php-date-formatter.d.ts +8 -0
- package/src/query.ts +5 -0
- package/src/shims/stream.ts +11 -0
- package/src/template-engine.test.ts +47 -0
- package/src/template-engine.ts +298 -0
- package/src/types.ts +871 -0
- package/src/unwrap-envelope.ts +51 -0
- package/src/user-app-config-client.ts +44 -0
- package/src/user-identity-client.ts +21 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +71 -0
- package/vitest.config.mts +13 -0
package/dist/query.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { DataSourceListParams, DataSourceApp, DataSource } from './index.js';
|
|
2
|
+
import { QueryKey, QueryClient } from '@tanstack/query-core';
|
|
3
|
+
import { RestApiClient } from '@kivacore/api-client';
|
|
4
|
+
|
|
5
|
+
interface QueryOptionsShape<TData, TKey extends QueryKey> {
|
|
6
|
+
queryKey: TKey;
|
|
7
|
+
queryFn: () => Promise<TData>;
|
|
8
|
+
}
|
|
9
|
+
declare const dataSourceKeys: {
|
|
10
|
+
all: () => readonly ["docyrus", "data-sources"];
|
|
11
|
+
apps: () => readonly ["docyrus", "data-sources", "apps"];
|
|
12
|
+
lists: () => readonly ["docyrus", "data-sources", "list"];
|
|
13
|
+
list: (params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "list", "all", DataSourceListParams];
|
|
14
|
+
listsByAppSlug: (appSlug: string) => readonly ["docyrus", "data-sources", "list", "by-app-slug", string];
|
|
15
|
+
listByAppSlug: (appSlug: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "list", "by-app-slug", string, DataSourceListParams];
|
|
16
|
+
listsByAppId: (appId: string) => readonly ["docyrus", "data-sources", "list", "by-app-id", string];
|
|
17
|
+
listByAppId: (appId: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "list", "by-app-id", string, DataSourceListParams];
|
|
18
|
+
details: () => readonly ["docyrus", "data-sources", "detail"];
|
|
19
|
+
byId: (dataSourceId: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "detail", "by-id", string, DataSourceListParams];
|
|
20
|
+
bySlug: (appSlug: string, dataSourceSlug: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "detail", "by-slug", string, string, DataSourceListParams];
|
|
21
|
+
};
|
|
22
|
+
declare function createDataSourceQueries(client: RestApiClient): {
|
|
23
|
+
keys: {
|
|
24
|
+
all: () => readonly ["docyrus", "data-sources"];
|
|
25
|
+
apps: () => readonly ["docyrus", "data-sources", "apps"];
|
|
26
|
+
lists: () => readonly ["docyrus", "data-sources", "list"];
|
|
27
|
+
list: (params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "list", "all", DataSourceListParams];
|
|
28
|
+
listsByAppSlug: (appSlug: string) => readonly ["docyrus", "data-sources", "list", "by-app-slug", string];
|
|
29
|
+
listByAppSlug: (appSlug: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "list", "by-app-slug", string, DataSourceListParams];
|
|
30
|
+
listsByAppId: (appId: string) => readonly ["docyrus", "data-sources", "list", "by-app-id", string];
|
|
31
|
+
listByAppId: (appId: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "list", "by-app-id", string, DataSourceListParams];
|
|
32
|
+
details: () => readonly ["docyrus", "data-sources", "detail"];
|
|
33
|
+
byId: (dataSourceId: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "detail", "by-id", string, DataSourceListParams];
|
|
34
|
+
bySlug: (appSlug: string, dataSourceSlug: string, params?: DataSourceListParams) => readonly ["docyrus", "data-sources", "detail", "by-slug", string, string, DataSourceListParams];
|
|
35
|
+
};
|
|
36
|
+
listAppsOptions: () => QueryOptionsShape<DataSourceApp[], readonly ["docyrus", "data-sources", "apps"]>;
|
|
37
|
+
listOptions: (params?: DataSourceListParams) => QueryOptionsShape<DataSource[], readonly ["docyrus", "data-sources", "list", "all", DataSourceListParams]>;
|
|
38
|
+
listByAppSlugOptions: (appSlug: string, params?: DataSourceListParams) => QueryOptionsShape<DataSource[], readonly ["docyrus", "data-sources", "list", "by-app-slug", string, DataSourceListParams]>;
|
|
39
|
+
listByAppIdOptions: (appId: string, params?: DataSourceListParams) => QueryOptionsShape<DataSource[], readonly ["docyrus", "data-sources", "list", "by-app-id", string, DataSourceListParams]>;
|
|
40
|
+
getByIdOptions: (dataSourceId: string, params?: DataSourceListParams) => QueryOptionsShape<DataSource, readonly ["docyrus", "data-sources", "detail", "by-id", string, DataSourceListParams]>;
|
|
41
|
+
getBySlugOptions: (appSlug: string, dataSourceSlug: string, params?: DataSourceListParams) => QueryOptionsShape<DataSource, readonly ["docyrus", "data-sources", "detail", "by-slug", string, string, DataSourceListParams]>;
|
|
42
|
+
invalidateAll: (queryClient: QueryClient) => Promise<void>;
|
|
43
|
+
invalidateApps: (queryClient: QueryClient) => Promise<void>;
|
|
44
|
+
invalidateLists: (queryClient: QueryClient) => Promise<void>;
|
|
45
|
+
invalidateDetails: (queryClient: QueryClient) => Promise<void>;
|
|
46
|
+
invalidateById: (queryClient: QueryClient, dataSourceId: string) => Promise<void>;
|
|
47
|
+
invalidateBySlug: (queryClient: QueryClient, appSlug: string, dataSourceSlug: string) => Promise<void>;
|
|
48
|
+
invalidateForAppSlug: (queryClient: QueryClient, appSlug: string) => Promise<void>;
|
|
49
|
+
invalidateForAppId: (queryClient: QueryClient, appId: string) => Promise<void>;
|
|
50
|
+
};
|
|
51
|
+
type DataSourceQueries = ReturnType<typeof createDataSourceQueries>;
|
|
52
|
+
|
|
53
|
+
export { createDataSourceQueries, dataSourceKeys };
|
|
54
|
+
export type { DataSourceQueries };
|
package/dist/query.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// src/unwrap-envelope.ts
|
|
2
|
+
var ENVELOPE_KEYS = /* @__PURE__ */ new Set([
|
|
3
|
+
"data",
|
|
4
|
+
"success",
|
|
5
|
+
"meta",
|
|
6
|
+
"error",
|
|
7
|
+
"message"
|
|
8
|
+
]);
|
|
9
|
+
function unwrapEnvelope(response) {
|
|
10
|
+
if (response !== null && typeof response === "object" && !Array.isArray(response)) {
|
|
11
|
+
const keys = Object.keys(response);
|
|
12
|
+
const looksLikeEnvelope = "data" in response && keys.every((key) => ENVELOPE_KEYS.has(key));
|
|
13
|
+
if (looksLikeEnvelope) {
|
|
14
|
+
return response.data;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return response;
|
|
18
|
+
}
|
|
19
|
+
function normalizeFieldType(type) {
|
|
20
|
+
return type.startsWith("field-") ? type.slice("field-".length) : type;
|
|
21
|
+
}
|
|
22
|
+
function getRelationTargetId(field) {
|
|
23
|
+
if (normalizeFieldType(field.type) !== "relation") return void 0;
|
|
24
|
+
const target = field.relation_data_source_id ?? field.relationDataSourceId;
|
|
25
|
+
return typeof target === "string" && target.length > 0 ? target : void 0;
|
|
26
|
+
}
|
|
27
|
+
function addRelationEdge(map, key, value) {
|
|
28
|
+
const existing = map.get(key);
|
|
29
|
+
if (existing) existing.add(value);
|
|
30
|
+
else map.set(key, /* @__PURE__ */ new Set([value]));
|
|
31
|
+
}
|
|
32
|
+
function buildRelationIndex(sources) {
|
|
33
|
+
const byId = /* @__PURE__ */ new Map();
|
|
34
|
+
const parentsByChild = /* @__PURE__ */ new Map();
|
|
35
|
+
const childrenByParent = /* @__PURE__ */ new Map();
|
|
36
|
+
for (const dataSource of sources) byId.set(dataSource.id, dataSource);
|
|
37
|
+
for (const dataSource of sources) {
|
|
38
|
+
for (const field of dataSource.fields ?? []) {
|
|
39
|
+
const parentId = getRelationTargetId(field);
|
|
40
|
+
if (!parentId || parentId === dataSource.id) continue;
|
|
41
|
+
addRelationEdge(parentsByChild, dataSource.id, parentId);
|
|
42
|
+
addRelationEdge(childrenByParent, parentId, dataSource.id);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { byId, parentsByChild, childrenByParent };
|
|
46
|
+
}
|
|
47
|
+
function createDataSourceClient(client, options) {
|
|
48
|
+
const basePath = "/v1/apps/data-sources";
|
|
49
|
+
function buildQueryParams(params) {
|
|
50
|
+
if (!params?.expand) return void 0;
|
|
51
|
+
return { expand: params.expand };
|
|
52
|
+
}
|
|
53
|
+
async function listApps() {
|
|
54
|
+
return unwrapEnvelope(await client.get("/v1/apps"));
|
|
55
|
+
}
|
|
56
|
+
async function list(params) {
|
|
57
|
+
return unwrapEnvelope(
|
|
58
|
+
await client.get(basePath, buildQueryParams(params))
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
async function listByAppSlug(appSlug, params) {
|
|
62
|
+
return unwrapEnvelope(
|
|
63
|
+
await client.get(`/v1/apps/${appSlug}/data-sources`, buildQueryParams(params))
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
async function listByAppId(appId, params) {
|
|
67
|
+
const queryParams = { appId };
|
|
68
|
+
if (params?.expand) {
|
|
69
|
+
queryParams.expand = params.expand;
|
|
70
|
+
}
|
|
71
|
+
return unwrapEnvelope(await client.get(basePath, queryParams));
|
|
72
|
+
}
|
|
73
|
+
async function getById(dataSourceId, params) {
|
|
74
|
+
return unwrapEnvelope(
|
|
75
|
+
await client.get(`${basePath}/${dataSourceId}`, buildQueryParams(params))
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
async function getBySlug(appSlug, dataSourceSlug, params) {
|
|
79
|
+
return unwrapEnvelope(
|
|
80
|
+
await client.get(
|
|
81
|
+
`/v1/apps/${appSlug}/data-sources/${dataSourceSlug}`,
|
|
82
|
+
buildQueryParams(params)
|
|
83
|
+
)
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
async function getFields(dataSourceId) {
|
|
87
|
+
const dataSource = await getById(dataSourceId, { expand: "fields" });
|
|
88
|
+
return dataSource.fields ?? [];
|
|
89
|
+
}
|
|
90
|
+
async function getFieldById(dataSourceId, fieldId) {
|
|
91
|
+
return (await getFields(dataSourceId)).find((field) => field.id === fieldId);
|
|
92
|
+
}
|
|
93
|
+
async function getFieldBySlug(dataSourceId, fieldSlug) {
|
|
94
|
+
return (await getFields(dataSourceId)).find((field) => field.slug === fieldSlug);
|
|
95
|
+
}
|
|
96
|
+
async function getEnumsByFieldId(dataSourceId, fieldId) {
|
|
97
|
+
const field = await getFieldById(dataSourceId, fieldId);
|
|
98
|
+
return field?.enums ?? [];
|
|
99
|
+
}
|
|
100
|
+
async function getEnumById(dataSourceId, fieldId, enumId) {
|
|
101
|
+
return (await getEnumsByFieldId(dataSourceId, fieldId)).find((option) => option.id === enumId);
|
|
102
|
+
}
|
|
103
|
+
let relationIndexSource = null;
|
|
104
|
+
let relationIndex = null;
|
|
105
|
+
async function loadAllDataSources() {
|
|
106
|
+
return unwrapEnvelope(await client.get(basePath, { expand: "fields" }));
|
|
107
|
+
}
|
|
108
|
+
async function getRelationIndex() {
|
|
109
|
+
const sources = await loadAllDataSources();
|
|
110
|
+
if (relationIndex && relationIndexSource === sources) return relationIndex;
|
|
111
|
+
relationIndex = buildRelationIndex(sources);
|
|
112
|
+
relationIndexSource = sources;
|
|
113
|
+
return relationIndex;
|
|
114
|
+
}
|
|
115
|
+
async function resolveRelated(dataSourceId, edge) {
|
|
116
|
+
const index = await getRelationIndex();
|
|
117
|
+
const ids = index[edge].get(dataSourceId);
|
|
118
|
+
if (!ids) return [];
|
|
119
|
+
const out = [];
|
|
120
|
+
for (const id of ids) {
|
|
121
|
+
const match = index.byId.get(id);
|
|
122
|
+
if (match) out.push(match);
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
async function listParentDataSources(dataSourceId) {
|
|
127
|
+
return resolveRelated(dataSourceId, "parentsByChild");
|
|
128
|
+
}
|
|
129
|
+
async function listChildDataSources(dataSourceId) {
|
|
130
|
+
return resolveRelated(dataSourceId, "childrenByParent");
|
|
131
|
+
}
|
|
132
|
+
async function listParentDataSourceFields(dataSourceId) {
|
|
133
|
+
return (await listParentDataSources(dataSourceId)).flatMap((ds) => ds.fields ?? []);
|
|
134
|
+
}
|
|
135
|
+
async function listChildDataSourceFields(dataSourceId) {
|
|
136
|
+
return (await listChildDataSources(dataSourceId)).flatMap((ds) => ds.fields ?? []);
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
listApps,
|
|
140
|
+
list,
|
|
141
|
+
listByAppSlug,
|
|
142
|
+
listByAppId,
|
|
143
|
+
getById,
|
|
144
|
+
getBySlug,
|
|
145
|
+
getFields,
|
|
146
|
+
getFieldById,
|
|
147
|
+
getFieldBySlug,
|
|
148
|
+
getEnumsByFieldId,
|
|
149
|
+
getEnumById,
|
|
150
|
+
listParentDataSources,
|
|
151
|
+
listChildDataSources,
|
|
152
|
+
listParentDataSourceFields,
|
|
153
|
+
listChildDataSourceFields
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/data-source-queries.ts
|
|
158
|
+
function defineQuery(opts) {
|
|
159
|
+
return opts;
|
|
160
|
+
}
|
|
161
|
+
var ROOT = ["docyrus", "data-sources"];
|
|
162
|
+
var dataSourceKeys = {
|
|
163
|
+
all: () => ROOT,
|
|
164
|
+
apps: () => [...ROOT, "apps"],
|
|
165
|
+
lists: () => [...ROOT, "list"],
|
|
166
|
+
list: (params) => [
|
|
167
|
+
...ROOT,
|
|
168
|
+
"list",
|
|
169
|
+
"all",
|
|
170
|
+
params ?? {}
|
|
171
|
+
],
|
|
172
|
+
listsByAppSlug: (appSlug) => [
|
|
173
|
+
...ROOT,
|
|
174
|
+
"list",
|
|
175
|
+
"by-app-slug",
|
|
176
|
+
appSlug
|
|
177
|
+
],
|
|
178
|
+
listByAppSlug: (appSlug, params) => [
|
|
179
|
+
...ROOT,
|
|
180
|
+
"list",
|
|
181
|
+
"by-app-slug",
|
|
182
|
+
appSlug,
|
|
183
|
+
params ?? {}
|
|
184
|
+
],
|
|
185
|
+
listsByAppId: (appId) => [
|
|
186
|
+
...ROOT,
|
|
187
|
+
"list",
|
|
188
|
+
"by-app-id",
|
|
189
|
+
appId
|
|
190
|
+
],
|
|
191
|
+
listByAppId: (appId, params) => [
|
|
192
|
+
...ROOT,
|
|
193
|
+
"list",
|
|
194
|
+
"by-app-id",
|
|
195
|
+
appId,
|
|
196
|
+
params ?? {}
|
|
197
|
+
],
|
|
198
|
+
details: () => [...ROOT, "detail"],
|
|
199
|
+
byId: (dataSourceId, params) => [
|
|
200
|
+
...ROOT,
|
|
201
|
+
"detail",
|
|
202
|
+
"by-id",
|
|
203
|
+
dataSourceId,
|
|
204
|
+
params ?? {}
|
|
205
|
+
],
|
|
206
|
+
bySlug: (appSlug, dataSourceSlug, params) => [
|
|
207
|
+
...ROOT,
|
|
208
|
+
"detail",
|
|
209
|
+
"by-slug",
|
|
210
|
+
appSlug,
|
|
211
|
+
dataSourceSlug,
|
|
212
|
+
params ?? {}
|
|
213
|
+
]
|
|
214
|
+
};
|
|
215
|
+
function createDataSourceQueries(client) {
|
|
216
|
+
const ds = createDataSourceClient(client);
|
|
217
|
+
return {
|
|
218
|
+
keys: dataSourceKeys,
|
|
219
|
+
listAppsOptions: () => defineQuery({
|
|
220
|
+
queryKey: dataSourceKeys.apps(),
|
|
221
|
+
queryFn: () => ds.listApps()
|
|
222
|
+
}),
|
|
223
|
+
listOptions: (params) => defineQuery({
|
|
224
|
+
queryKey: dataSourceKeys.list(params),
|
|
225
|
+
queryFn: () => ds.list(params)
|
|
226
|
+
}),
|
|
227
|
+
listByAppSlugOptions: (appSlug, params) => defineQuery({
|
|
228
|
+
queryKey: dataSourceKeys.listByAppSlug(appSlug, params),
|
|
229
|
+
queryFn: () => ds.listByAppSlug(appSlug, params)
|
|
230
|
+
}),
|
|
231
|
+
listByAppIdOptions: (appId, params) => defineQuery({
|
|
232
|
+
queryKey: dataSourceKeys.listByAppId(appId, params),
|
|
233
|
+
queryFn: () => ds.listByAppId(appId, params)
|
|
234
|
+
}),
|
|
235
|
+
getByIdOptions: (dataSourceId, params) => defineQuery({
|
|
236
|
+
queryKey: dataSourceKeys.byId(dataSourceId, params),
|
|
237
|
+
queryFn: () => ds.getById(dataSourceId, params)
|
|
238
|
+
}),
|
|
239
|
+
getBySlugOptions: (appSlug, dataSourceSlug, params) => defineQuery({
|
|
240
|
+
queryKey: dataSourceKeys.bySlug(appSlug, dataSourceSlug, params),
|
|
241
|
+
queryFn: () => ds.getBySlug(appSlug, dataSourceSlug, params)
|
|
242
|
+
}),
|
|
243
|
+
invalidateAll: (queryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.all() }),
|
|
244
|
+
invalidateApps: (queryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.apps() }),
|
|
245
|
+
invalidateLists: (queryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.lists() }),
|
|
246
|
+
invalidateDetails: (queryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.details() }),
|
|
247
|
+
invalidateById: (queryClient, dataSourceId) => queryClient.invalidateQueries({
|
|
248
|
+
queryKey: [
|
|
249
|
+
...dataSourceKeys.details(),
|
|
250
|
+
"by-id",
|
|
251
|
+
dataSourceId
|
|
252
|
+
]
|
|
253
|
+
}),
|
|
254
|
+
invalidateBySlug: (queryClient, appSlug, dataSourceSlug) => queryClient.invalidateQueries({
|
|
255
|
+
queryKey: [
|
|
256
|
+
...dataSourceKeys.details(),
|
|
257
|
+
"by-slug",
|
|
258
|
+
appSlug,
|
|
259
|
+
dataSourceSlug
|
|
260
|
+
]
|
|
261
|
+
}),
|
|
262
|
+
invalidateForAppSlug: (queryClient, appSlug) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.listsByAppSlug(appSlug) }),
|
|
263
|
+
invalidateForAppId: (queryClient, appId) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.listsByAppId(appId) })
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export { createDataSourceQueries, dataSourceKeys };
|
|
268
|
+
//# sourceMappingURL=query.js.map
|
|
269
|
+
//# sourceMappingURL=query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/unwrap-envelope.ts","../src/data-source-client.ts","../src/data-source-queries.ts"],"names":[],"mappings":";AAOA,IAAM,aAAA,uBAAoB,GAAA,CAAI;AAAA,EAC5B,MAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA;AACF,CAAC,CAAA;AAoBM,SAAS,eAAkB,QAAA,EAAsB;AACtD,EAAA,IACE,QAAA,KAAa,QACV,OAAO,QAAA,KAAa,YACpB,CAAC,KAAA,CAAM,OAAA,CAAQ,QAAQ,CAAA,EAC1B;AACA,IAAA,MAAM,IAAA,GAAO,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA;AACjC,IAAA,MAAM,iBAAA,GACF,UAAU,QAAA,IACP,IAAA,CAAK,MAAM,CAAA,GAAA,KAAO,aAAA,CAAc,GAAA,CAAI,GAAG,CAAC,C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;AAAA,GACjJ;AACF","file":"query.js","sourcesContent":["/**\n * Top-level keys allowed to coexist with `data` in a Docyrus API envelope.\n * Mirrors what the NestJS global ResponseInterceptor produces — every\n * success response is shaped as `{ success, data, ...rest }`, optionally\n * with `meta` (pagination) or `error` / `message` (deliberate\n * controller-level error envelope).\n */\nconst ENVELOPE_KEYS = new Set([\n 'data',\n 'success',\n 'meta',\n 'error',\n 'message'\n]);\n\n/**\n * Aggressively pull `.data` out of a Docyrus API envelope when the shape\n * matches. Used inside app-utils' typed clients (e.g. `dataSources.list`,\n * `dataViews.get`) which always return the inner payload — meta is\n * intentionally discarded here because callers asked for a typed array\n * or record, not the envelope.\n *\n * This is a wider rule than `@kivacore/api-client`'s envelope interceptor,\n * which preserves envelopes carrying `meta` / `error` / `message` so\n * callers (e.g. data grids) can read pagination/error info. App-utils\n * clients don't need that — they have explicit return types and the\n * sidecar info isn't part of the contract — so they extract regardless.\n *\n * The closed-set check still protects domain payloads like AppConfig /\n * UserAppConfig (records that carry a `data` column) from being falsely\n * unwrapped, because they also carry domain fields (`id`, `status`,\n * `tenant_app_id`, …) that aren't in the envelope set.\n */\nexport function unwrapEnvelope<T>(response: unknown): T {\n if (\n response !== null\n && typeof response === 'object'\n && !Array.isArray(response)\n ) {\n const keys = Object.keys(response);\n const looksLikeEnvelope\n = 'data' in response\n && keys.every(key => ENVELOPE_KEYS.has(key));\n\n if (looksLikeEnvelope) {\n return (response as { data: T }).data;\n }\n }\n\n return response as T;\n}\n","import { type RestApiClient } from '@kivacore/api-client';\n\nimport {\n type CreateDataSourceClientOptions,\n type DataSource,\n type DataSourceApp,\n type DataSourceClient,\n type DataSourceEnumOption,\n type DataSourceField,\n type DataSourceListParams,\n type InventoryClient\n} from './types';\nimport { unwrapEnvelope } from './unwrap-envelope';\n\nfunction matchesSlug(dataSource: DataSource, appSlug: string, dataSourceSlug: string): boolean {\n const ownerAppSlug = dataSource.appSlug ?? dataSource.app_slug;\n\n return ownerAppSlug === appSlug && dataSource.slug === dataSourceSlug;\n}\n\n/**\n * Directed relation graph over all data sources: `relation` fields point from a\n * child to its parent (`relation_data_source_id`). `parentsByChild` maps a\n * child id → its parent ids; `childrenByParent` is the reverse.\n */\ninterface RelationIndex {\n byId: Map<string, DataSource>;\n parentsByChild: Map<string, Set<string>>;\n childrenByParent: Map<string, Set<string>>;\n}\n\n/** Normalizes a field type to its bare name (`field-relation` → `relation`). */\nfunction normalizeFieldType(type: string): string {\n return type.startsWith('field-') ? type.slice('field-'.length) : type;\n}\n\n/** The parent data source id a `relation` field targets, or `undefined` for non-relation fields. */\nfunction getRelationTargetId(field: DataSourceField): string | undefined {\n if (normalizeFieldType(field.type) !== 'relation') return undefined;\n\n const target = field.relation_data_source_id ?? (field as Record<string, unknown>).relationDataSourceId;\n\n return typeof target === 'string' && target.length > 0 ? target : undefined;\n}\n\nfunction addRelationEdge(map: Map<string, Set<string>>, key: string, value: string): void {\n const existing = map.get(key);\n\n if (existing) existing.add(value);\n else map.set(key, new Set([value]));\n}\n\nfunction buildRelationIndex(sources: DataSource[]): RelationIndex {\n const byId = new Map<string, DataSource>();\n const parentsByChild = new Map<string, Set<string>>();\n const childrenByParent = new Map<string, Set<string>>();\n\n for (const dataSource of sources) byId.set(dataSource.id, dataSource);\n\n for (const dataSource of sources) {\n for (const field of dataSource.fields ?? []) {\n const parentId = getRelationTargetId(field);\n\n // Skip non-relations and self-links (e.g. a hierarchical `parent` field).\n if (!parentId || parentId === dataSource.id) continue;\n\n addRelationEdge(parentsByChild, dataSource.id, parentId);\n addRelationEdge(childrenByParent, parentId, dataSource.id);\n }\n }\n\n return { byId, parentsByChild, childrenByParent };\n}\n\nexport function createDataSourceClient(\n client: RestApiClient,\n options?: CreateDataSourceClientOptions\n): DataSourceClient {\n const basePath = '/v1/apps/data-sources';\n const inventory: InventoryClient | undefined = options?.inventory;\n\n function buildQueryParams(params?: DataSourceListParams): Record<string, string> | undefined {\n if (!params?.expand) return undefined;\n\n return { expand: params.expand };\n }\n\n async function listApps(): Promise<DataSourceApp[]> {\n if (inventory) return inventory.listApps();\n\n return unwrapEnvelope<DataSourceApp[]>(await client.get('/v1/apps'));\n }\n\n async function list(params?: DataSourceListParams): Promise<DataSource[]> {\n if (inventory && !params?.expand) return inventory.listDataSources();\n\n return unwrapEnvelope<DataSource[]>(\n await client.get(basePath, buildQueryParams(params))\n );\n }\n\n async function listByAppSlug(\n appSlug: string,\n params?: DataSourceListParams\n ): Promise<DataSource[]> {\n if (inventory && !params?.expand) {\n const sources = await inventory.listDataSources();\n\n return sources.filter(ds => (ds.appSlug ?? ds.app_slug) === appSlug);\n }\n\n return unwrapEnvelope<DataSource[]>(\n await client.get(`/v1/apps/${appSlug}/data-sources`, buildQueryParams(params))\n );\n }\n\n async function listByAppId(\n appId: string,\n params?: DataSourceListParams\n ): Promise<DataSource[]> {\n if (inventory && !params?.expand) {\n const sources = await inventory.listDataSources();\n\n return sources.filter(ds => ds.tenant_app_id === appId);\n }\n\n const queryParams: Record<string, string> = { appId };\n\n if (params?.expand) {\n queryParams.expand = params.expand;\n }\n\n return unwrapEnvelope<DataSource[]>(await client.get(basePath, queryParams));\n }\n\n async function getById(\n dataSourceId: string,\n params?: DataSourceListParams\n ): Promise<DataSource> {\n if (inventory && !params?.expand) {\n const sources = await inventory.listDataSources();\n const match = sources.find(ds => ds.id === dataSourceId);\n\n if (match) return match;\n }\n\n return unwrapEnvelope<DataSource>(\n await client.get(`${basePath}/${dataSourceId}`, buildQueryParams(params))\n );\n }\n\n async function getBySlug(\n appSlug: string,\n dataSourceSlug: string,\n params?: DataSourceListParams\n ): Promise<DataSource> {\n if (inventory && !params?.expand) {\n const sources = await inventory.listDataSources();\n const match = sources.find(ds => matchesSlug(ds, appSlug, dataSourceSlug));\n\n if (match) return match;\n }\n\n return unwrapEnvelope<DataSource>(\n await client.get(\n `/v1/apps/${appSlug}/data-sources/${dataSourceSlug}`,\n buildQueryParams(params)\n )\n );\n }\n\n async function getFields(dataSourceId: string): Promise<DataSourceField[]> {\n /*\n * Prefer the inventory cache — cached data sources already carry their\n * fields (with enums) via `dataSourcesExpand`.\n */\n if (inventory) {\n const cached = await getById(dataSourceId);\n\n if (cached.fields) return cached.fields;\n }\n\n // No inventory (or cache miss): fetch the fields explicitly.\n const dataSource = await getById(dataSourceId, { expand: 'fields' });\n\n return dataSource.fields ?? [];\n }\n\n async function getFieldById(\n dataSourceId: string,\n fieldId: string\n ): Promise<DataSourceField | undefined> {\n return (await getFields(dataSourceId)).find(field => field.id === fieldId);\n }\n\n async function getFieldBySlug(\n dataSourceId: string,\n fieldSlug: string\n ): Promise<DataSourceField | undefined> {\n return (await getFields(dataSourceId)).find(field => field.slug === fieldSlug);\n }\n\n async function getEnumsByFieldId(\n dataSourceId: string,\n fieldId: string\n ): Promise<DataSourceEnumOption[]> {\n const field = await getFieldById(dataSourceId, fieldId);\n\n return field?.enums ?? [];\n }\n\n async function getEnumById(\n dataSourceId: string,\n fieldId: string,\n enumId: string\n ): Promise<DataSourceEnumOption | undefined> {\n return (await getEnumsByFieldId(dataSourceId, fieldId)).find(option => option.id === enumId);\n }\n\n /*\n * Relation index, memoized against the data-source array it was built from.\n * The inventory returns the same array reference until it's invalidated, so\n * this rebuilds only when the underlying cache changes.\n */\n let relationIndexSource: DataSource[] | null = null;\n let relationIndex: RelationIndex | null = null;\n\n async function loadAllDataSources(): Promise<DataSource[]> {\n if (inventory) return inventory.listDataSources();\n\n return unwrapEnvelope<DataSource[]>(await client.get(basePath, { expand: 'fields' }));\n }\n\n async function getRelationIndex(): Promise<RelationIndex> {\n const sources = await loadAllDataSources();\n\n if (relationIndex && relationIndexSource === sources) return relationIndex;\n\n relationIndex = buildRelationIndex(sources);\n relationIndexSource = sources;\n\n return relationIndex;\n }\n\n async function resolveRelated(\n dataSourceId: string,\n edge: 'parentsByChild' | 'childrenByParent'\n ): Promise<DataSource[]> {\n const index = await getRelationIndex();\n const ids = index[edge].get(dataSourceId);\n\n if (!ids) return [];\n\n const out: DataSource[] = [];\n\n for (const id of ids) {\n const match = index.byId.get(id);\n\n if (match) out.push(match);\n }\n\n return out;\n }\n\n async function listParentDataSources(dataSourceId: string): Promise<DataSource[]> {\n return resolveRelated(dataSourceId, 'parentsByChild');\n }\n\n async function listChildDataSources(dataSourceId: string): Promise<DataSource[]> {\n return resolveRelated(dataSourceId, 'childrenByParent');\n }\n\n async function listParentDataSourceFields(dataSourceId: string): Promise<DataSourceField[]> {\n return (await listParentDataSources(dataSourceId)).flatMap(ds => ds.fields ?? []);\n }\n\n async function listChildDataSourceFields(dataSourceId: string): Promise<DataSourceField[]> {\n return (await listChildDataSources(dataSourceId)).flatMap(ds => ds.fields ?? []);\n }\n\n return {\n listApps,\n list,\n listByAppSlug,\n listByAppId,\n getById,\n getBySlug,\n getFields,\n getFieldById,\n getFieldBySlug,\n getEnumsByFieldId,\n getEnumById,\n listParentDataSources,\n listChildDataSources,\n listParentDataSourceFields,\n listChildDataSourceFields\n };\n}\n","import { type QueryClient, type QueryKey } from '@tanstack/query-core';\nimport { type RestApiClient } from '@kivacore/api-client';\n\nimport { createDataSourceClient } from './data-source-client';\nimport { type DataSourceListParams } from './types';\n\ninterface QueryOptionsShape<TData, TKey extends QueryKey> {\n queryKey: TKey;\n queryFn: () => Promise<TData>;\n}\n\nfunction defineQuery<TData, TKey extends QueryKey>(\n opts: QueryOptionsShape<TData, TKey>\n): QueryOptionsShape<TData, TKey> {\n return opts;\n}\n\nconst ROOT = ['docyrus', 'data-sources'] as const;\n\nexport const dataSourceKeys = {\n all: () => ROOT,\n apps: () => [...ROOT, 'apps'] as const,\n lists: () => [...ROOT, 'list'] as const,\n list: (params?: DataSourceListParams) => [\n ...ROOT,\n 'list',\n 'all',\n params ?? {}\n ] as const,\n listsByAppSlug: (appSlug: string) => [\n ...ROOT,\n 'list',\n 'by-app-slug',\n appSlug\n ] as const,\n listByAppSlug: (appSlug: string, params?: DataSourceListParams) => [\n ...ROOT,\n 'list',\n 'by-app-slug',\n appSlug,\n params ?? {}\n ] as const,\n listsByAppId: (appId: string) => [\n ...ROOT,\n 'list',\n 'by-app-id',\n appId\n ] as const,\n listByAppId: (appId: string, params?: DataSourceListParams) => [\n ...ROOT,\n 'list',\n 'by-app-id',\n appId,\n params ?? {}\n ] as const,\n details: () => [...ROOT, 'detail'] as const,\n byId: (dataSourceId: string, params?: DataSourceListParams) => [\n ...ROOT,\n 'detail',\n 'by-id',\n dataSourceId,\n params ?? {}\n ] as const,\n bySlug: (appSlug: string, dataSourceSlug: string, params?: DataSourceListParams) => [\n ...ROOT,\n 'detail',\n 'by-slug',\n appSlug,\n dataSourceSlug,\n params ?? {}\n ] as const\n};\n\nexport function createDataSourceQueries(client: RestApiClient) {\n const ds = createDataSourceClient(client);\n\n return {\n keys: dataSourceKeys,\n\n listAppsOptions: () => defineQuery({\n queryKey: dataSourceKeys.apps(),\n queryFn: () => ds.listApps()\n }),\n\n listOptions: (params?: DataSourceListParams) => defineQuery({\n queryKey: dataSourceKeys.list(params),\n queryFn: () => ds.list(params)\n }),\n\n listByAppSlugOptions: (appSlug: string, params?: DataSourceListParams) => defineQuery({\n queryKey: dataSourceKeys.listByAppSlug(appSlug, params),\n queryFn: () => ds.listByAppSlug(appSlug, params)\n }),\n\n listByAppIdOptions: (appId: string, params?: DataSourceListParams) => defineQuery({\n queryKey: dataSourceKeys.listByAppId(appId, params),\n queryFn: () => ds.listByAppId(appId, params)\n }),\n\n getByIdOptions: (dataSourceId: string, params?: DataSourceListParams) => defineQuery({\n queryKey: dataSourceKeys.byId(dataSourceId, params),\n queryFn: () => ds.getById(dataSourceId, params)\n }),\n\n getBySlugOptions: (\n appSlug: string,\n dataSourceSlug: string,\n params?: DataSourceListParams\n ) => defineQuery({\n queryKey: dataSourceKeys.bySlug(appSlug, dataSourceSlug, params),\n queryFn: () => ds.getBySlug(appSlug, dataSourceSlug, params)\n }),\n\n invalidateAll: (queryClient: QueryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.all() }),\n\n invalidateApps: (queryClient: QueryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.apps() }),\n\n invalidateLists: (queryClient: QueryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.lists() }),\n\n invalidateDetails: (queryClient: QueryClient) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.details() }),\n\n invalidateById: (queryClient: QueryClient, dataSourceId: string) => queryClient.invalidateQueries({\n queryKey: [\n ...dataSourceKeys.details(),\n 'by-id',\n dataSourceId\n ]\n }),\n\n invalidateBySlug: (\n queryClient: QueryClient,\n appSlug: string,\n dataSourceSlug: string\n ) => queryClient.invalidateQueries({\n queryKey: [\n ...dataSourceKeys.details(),\n 'by-slug',\n appSlug,\n dataSourceSlug\n ]\n }),\n\n invalidateForAppSlug: (queryClient: QueryClient, appSlug: string) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.listsByAppSlug(appSlug) }),\n\n invalidateForAppId: (queryClient: QueryClient, appId: string) => queryClient.invalidateQueries({ queryKey: dataSourceKeys.listsByAppId(appId) })\n };\n}\n\nexport type DataSourceQueries = ReturnType<typeof createDataSourceQueries>;\n"]}
|