@integration-app/react 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. package/dist/index.d.ts +371 -165
  2. package/dist/index.js +510 -234
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.module.d.ts +371 -165
  5. package/dist/index.module.mjs +500 -235
  6. package/dist/index.module.mjs.map +1 -1
  7. package/dist/index.umd.d.ts +371 -165
  8. package/dist/index.umd.js +510 -238
  9. package/dist/index.umd.js.map +1 -1
  10. package/package.json +5 -2
  11. package/src/actions/useAction.ts +35 -0
  12. package/src/actions/useActionInstance.ts +56 -0
  13. package/src/actions/useActionInstances.ts +11 -0
  14. package/src/actions/useActions.ts +11 -0
  15. package/src/app-events/useAppEventSubscription.ts +6 -6
  16. package/src/app-events/useAppEventSubscriptions.ts +5 -4
  17. package/src/app-events/useAppEventType.ts +10 -7
  18. package/src/app-events/useAppEventTypes.ts +2 -4
  19. package/src/app-events/useAppEvents.ts +2 -4
  20. package/src/contexts/index.tsx +4 -0
  21. package/src/data-collections/useDataCollectionSpec.ts +26 -0
  22. package/src/data-links/useDataLinkTable.ts +18 -0
  23. package/src/data-links/useDataLinkTableInstance.ts +39 -0
  24. package/src/data-links/useDataLinkTableInstances.ts +19 -0
  25. package/src/data-links/useDataLinkTables.ts +11 -0
  26. package/src/data-sources/useDataSource.ts +29 -6
  27. package/src/data-sources/useDataSourceEvents.ts +2 -4
  28. package/src/data-sources/useDataSourceInstance.ts +120 -26
  29. package/src/data-sources/useDataSourceInstanceCollection.ts +14 -4
  30. package/src/data-sources/useDataSourceInstanceLocations.ts +17 -6
  31. package/src/data-sources/useDataSourceInstances.ts +5 -4
  32. package/src/data-sources/useDataSources.ts +2 -4
  33. package/src/field-mappings/useFieldMapping.ts +29 -8
  34. package/src/field-mappings/useFieldMappingInstance.ts +35 -12
  35. package/src/field-mappings/useFieldMappingInstances.ts +5 -4
  36. package/src/field-mappings/useFieldMappings.ts +2 -4
  37. package/src/flows/useFlow.ts +29 -8
  38. package/src/flows/useFlowInstance.ts +62 -5
  39. package/src/flows/useFlowInstances.ts +2 -4
  40. package/src/flows/useFlowRun.ts +18 -6
  41. package/src/flows/useFlowRuns.ts +5 -5
  42. package/src/flows/useFlows.ts +5 -5
  43. package/src/hooks/useElement.tsx +137 -149
  44. package/src/hooks/useElements.tsx +44 -73
  45. package/src/hooks/useIntegrationAppSWR.tsx +13 -0
  46. package/src/index.tsx +27 -14
  47. package/src/integrations/useConnection.ts +14 -5
  48. package/src/integrations/useConnections.ts +3 -4
  49. package/src/integrations/useConnectorSpec.ts +7 -18
  50. package/src/integrations/useIntegration.ts +11 -7
  51. package/src/integrations/useIntegrations.ts +3 -4
  52. package/src/screens/useScreen.ts +19 -0
  53. package/rollup.dts.config.mjs +0 -21
  54. package/src/flows/useFlowTemplate.ts +0 -0
  55. package/src/flows/useFlowTemplates.ts +0 -0
  56. package/src/hooks/useGetter.tsx +0 -38
@@ -1,7 +1,11 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
- import { IntegrationAppClient } from '@integration-app/sdk';
2
+ import { IntegrationAppClient, parseDataLocationPath } from '@integration-app/sdk';
3
3
  export { DataForm } from '@integration-app/sdk';
4
- import { createContext, useMemo, useContext, useState, useEffect, useRef } from 'react';
4
+ import { createContext, useMemo, useContext, useState } from 'react';
5
+ import useSWR from 'swr';
6
+ import AwesomeDebouncePromise from 'awesome-debounce-promise';
7
+ import useSWRInfinite from 'swr/infinite';
8
+ import qs from 'query-string';
5
9
 
6
10
  const IntegrationAppContext = createContext(null);
7
11
  IntegrationAppContext.displayName = 'IntegrationAppClientContext';
@@ -20,374 +24,635 @@ function useIntegrationApp() {
20
24
  return useContext(IntegrationAppContext);
21
25
  }
22
26
 
23
- function useConnectorSpec(integrationKey) {
24
- const integrationApp = useIntegrationApp();
25
- const [data, setData] = useState(null);
26
- const [loading, setLoading] = useState(true);
27
- const [error, setError] = useState(null);
28
- useEffect(() => {
29
- if (!integrationApp) {
30
- return;
31
- }
32
- integrationApp
33
- .integration(integrationKey)
34
- .getConnectorSpec()
35
- .then(setData)
36
- .catch(setError)
37
- .finally(() => setLoading(false));
38
- }, [integrationApp, integrationKey]);
39
- return { data, loading, error };
27
+ function useIntegrationAppSWR(path, options) {
28
+ const client = useIntegrationApp();
29
+ const fetcher = async () => {
30
+ const response = await client.get(path, options);
31
+ return response;
32
+ };
33
+ return useSWR(client ? path : undefined, fetcher, options);
40
34
  }
41
35
 
42
- function useElement(props, accessorGenerator) {
36
+ const elementStateCache = new Map();
37
+ function useElement(selector, accessorGenerator) {
43
38
  const integrationApp = useIntegrationApp();
44
- const [data, setData] = useState();
45
- const [loading, setLoading] = useState(true);
46
- const [error, setError] = useState(null);
47
- const [refreshCounter, setRefreshCounter] = useState(0);
48
- function updateDataWith(newData) {
49
- if (data !== undefined) {
50
- setData({
51
- ...data,
52
- ...newData,
53
- });
54
- }
39
+ const elementKeyData = {
40
+ token: integrationApp.token,
41
+ selector,
42
+ };
43
+ const elementKey = JSON.stringify(elementKeyData);
44
+ if (!elementStateCache.has(elementKey)) {
45
+ elementStateCache.set(elementKey, {
46
+ updatedLocally: false,
47
+ savingToServer: false,
48
+ debouncedPut: AwesomeDebouncePromise(async (data) => {
49
+ elementState.updatedLocally = false;
50
+ elementState.savingToServer = true;
51
+ try {
52
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.put(data));
53
+ if (!elementState.updatedLocally) {
54
+ elementState.savingToServer = false;
55
+ await mutate(result, false);
56
+ }
57
+ }
58
+ catch (e) {
59
+ elementState.updatedLocally = true;
60
+ throw e;
61
+ }
62
+ finally {
63
+ elementState.savingToServer = false;
64
+ }
65
+ }, 500),
66
+ });
55
67
  }
56
- function replaceDataWith(newData) {
57
- setData(newData);
58
- }
59
- const selector = (props === null || props === void 0 ? void 0 : props.id)
60
- ? props.id
61
- : props;
62
- const accessor = integrationApp
63
- ? accessorGenerator(integrationApp)(selector)
64
- : null;
65
- useEffect(() => {
66
- setLoading(true);
67
- setError(null);
68
- if (integrationApp && selector) {
69
- accessor
70
- .get()
71
- .then(setData)
72
- .catch(setError)
73
- .finally(() => setLoading(false));
74
- }
75
- else {
76
- setError(new Error('IntegrationApp not found. Was this component wrapped in <IntegrationAppProvider>?'));
77
- }
78
- }, [
79
- integrationApp,
80
- JSON.stringify(selector),
81
- JSON.stringify(props),
82
- refreshCounter,
83
- ]);
84
- async function create(createData) {
85
- const returnedData = await accessor.create(createData);
86
- replaceDataWith(returnedData);
87
- return returnedData;
88
- }
89
- function refresh() {
90
- setRefreshCounter(refreshCounter + 1);
91
- }
92
- async function patch(patch) {
93
- if (typeof patch === 'object') {
94
- updateDataWith(patch !== null && patch !== void 0 ? patch : {});
95
- return accessor.patch(patch);
96
- }
97
- else {
98
- return data;
68
+ const elementState = elementStateCache.get(elementKey);
69
+ const accessor = integrationApp ? accessorGenerator(integrationApp) : null;
70
+ const swrKey = accessor && selector ? `element:${elementKey}` : null;
71
+ const { data: item, mutate, error, isLoading, isValidating, } = useSWR(swrKey, () => accessor === null || accessor === void 0 ? void 0 : accessor.get(), {
72
+ isPaused: () => elementState.updatedLocally || elementState.savingToServer,
73
+ });
74
+ const loading = isLoading;
75
+ const refreshing = isValidating;
76
+ async function refresh() {
77
+ return await mutate();
78
+ }
79
+ async function put(data) {
80
+ if (!(accessor === null || accessor === void 0 ? void 0 : accessor.put)) {
81
+ throw new Error(`"put method is not supported for accessor ${accessor.constructor.name}`);
99
82
  }
83
+ elementState.updatedLocally = true;
84
+ const newLocalData = {
85
+ ...item,
86
+ ...data,
87
+ };
88
+ await mutate(newLocalData, false);
89
+ await elementState.debouncedPut(data);
100
90
  }
101
- async function put(putData) {
102
- updateDataWith(putData);
103
- return await accessor.put(putData);
91
+ async function patch(data) {
92
+ const newData = {
93
+ ...item,
94
+ ...data,
95
+ };
96
+ return put(newData);
104
97
  }
105
98
  async function archive() {
106
- setData(null);
107
- return accessor.archive();
99
+ if (!(accessor === null || accessor === void 0 ? void 0 : accessor.archive)) {
100
+ return;
101
+ }
102
+ await mutate({ ...item, archivedAt: new Date().toISOString() }, false);
103
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.archive());
104
+ await mutate();
105
+ }
106
+ async function create(data) {
107
+ if (!(accessor === null || accessor === void 0 ? void 0 : accessor.create)) {
108
+ throw new Error(`"create method is not supported for accessor ${accessor.constructor.name}`);
109
+ }
110
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.create(data));
111
+ return await mutate(result);
108
112
  }
109
113
  return {
110
- data,
114
+ accessor,
115
+ item,
116
+ loading,
117
+ saving: elementState.updatedLocally || elementState.savingToServer,
118
+ error,
119
+ refresh,
120
+ refreshing,
111
121
  create,
112
122
  patch,
113
123
  put,
114
124
  archive,
115
- refresh,
116
- loading,
117
- error,
118
- accessor,
119
125
  };
120
126
  }
121
127
 
122
- function useIntegration(idOrKey) {
123
- const { data: integration, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.integration.bind(integrationApp));
124
- return { integration, ...rest };
128
+ function useConnection(id) {
129
+ const { item: connection, ...rest } = useElement(id, (integrationApp) => integrationApp.connection(id));
130
+ return {
131
+ connection,
132
+ ...rest,
133
+ };
125
134
  }
126
135
 
127
- function useElements(initialQuery, accessorGenerator) {
136
+ const LIMIT = 25;
137
+ function useElements(route, query = {}) {
128
138
  const integrationApp = useIntegrationApp();
129
- const refreshId = useRef(0);
130
- const [items, setItems] = useState([]);
131
- const [nextCursor, setNextCursor] = useState(undefined);
132
- const [loading, setLoading] = useState(false);
133
- const [error, setError] = useState(null);
139
+ function getKey(page, previousPageData) {
140
+ var _a;
141
+ if (page === 0)
142
+ return `/${route}?${qs.stringify({
143
+ ...query,
144
+ limit: LIMIT,
145
+ })}`;
146
+ if (((_a = previousPageData.items) === null || _a === void 0 ? void 0 : _a.length) < LIMIT)
147
+ return null;
148
+ return `/${route}?${qs.stringify({
149
+ ...query,
150
+ limit: LIMIT,
151
+ cursor: previousPageData.cursor,
152
+ })}`;
153
+ }
154
+ const [loadingMore, setIsLoadingMore] = useState(false);
155
+ const { data, size, setSize, isLoading, error, mutate, isValidating } = useSWRInfinite(getKey, (url) => integrationApp.get(url));
156
+ const items = data ? data.map((page) => page.items).flat() : [];
157
+ const loading = isLoading;
158
+ const refreshing = isValidating;
134
159
  async function loadMore() {
135
- const startingRefreshId = refreshId.current;
136
- const isFirstPage = !nextCursor;
137
- function setStateIfCurrentRefresh(stateSetter, valueGetter) {
138
- stateSetter((value) => startingRefreshId === refreshId.current ? valueGetter(value) : value);
139
- }
140
- setStateIfCurrentRefresh(setError, () => null);
141
- setStateIfCurrentRefresh(setLoading, () => true);
142
- const queryParams = {
143
- ...initialQuery,
144
- };
145
- if (nextCursor)
146
- queryParams.cursor = nextCursor;
147
- try {
148
- const data = await accessorGenerator(integrationApp).find(queryParams);
149
- setStateIfCurrentRefresh(setNextCursor, () => data.cursor);
150
- setStateIfCurrentRefresh(setItems, (items) => isFirstPage ? data.items : [...items, ...data.items]);
151
- }
152
- catch (e) {
153
- setStateIfCurrentRefresh(setError, () => e);
154
- }
155
- finally {
156
- setStateIfCurrentRefresh(setLoading, () => false);
160
+ var _a, _b;
161
+ const hasMoreToLoad = ((_b = (_a = data[size - 1]) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b.length) === LIMIT;
162
+ if (hasMoreToLoad) {
163
+ setIsLoadingMore(true);
164
+ await setSize(size + 1);
165
+ setIsLoadingMore(false);
157
166
  }
158
167
  }
159
- useEffect(() => {
160
- if (!integrationApp) {
161
- setError(new Error('IntegrationApp not found. Was this component wrapped in <IntegrationAppProvider>?'));
162
- return;
163
- }
164
- refresh();
165
- }, [integrationApp, JSON.stringify(initialQuery)]);
166
168
  async function refresh() {
167
- refreshId.current += 1;
168
- setNextCursor(undefined);
169
- await loadMore();
169
+ await mutate();
170
170
  }
171
171
  return {
172
172
  items,
173
173
  refresh,
174
+ refreshing,
174
175
  loadMore,
176
+ loadingMore,
175
177
  loading,
176
178
  error,
177
179
  };
178
180
  }
179
181
 
180
- function useIntegrations(query) {
181
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.integrations);
182
+ function useConnections(query) {
183
+ const { ...rest } = useElements('connections', query);
182
184
  return {
185
+ connections: rest.items,
183
186
  ...rest,
184
187
  };
185
188
  }
186
189
 
187
- function useConnection(id) {
188
- const { data: connection, ...rest } = useElement(id, (integrationApp) => integrationApp.connection.bind(integrationApp));
189
- return { connection, ...rest };
190
+ function useConnectorSpec(integrationIdOrKey) {
191
+ const integrationApp = useIntegrationApp();
192
+ const { data, isLoading, error } = useSWR(`/integrations/${integrationIdOrKey}/connector-spec`, () => integrationApp.integration(integrationIdOrKey).getConnectorSpec());
193
+ return { data, loading: isLoading, error };
190
194
  }
191
195
 
192
- function useConnections(query) {
193
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.connections);
196
+ function useIntegration(id) {
197
+ const { item: integration, ...rest } = useElement(id, (integrationApp) => integrationApp.integration(id));
198
+ return { integration, ...rest };
199
+ }
200
+
201
+ function useIntegrations(query) {
202
+ const { ...rest } = useElements('integrations', query);
194
203
  return {
204
+ integrations: rest.items,
195
205
  ...rest,
196
206
  };
197
207
  }
198
208
 
199
- function useFieldMapping(idOrKey) {
200
- const { data: fieldMapping, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.fieldMapping.bind(integrationApp));
201
- return { fieldMapping, ...rest };
209
+ function useFieldMapping(selector) {
210
+ const { item: fieldMapping, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.fieldMapping(selector));
211
+ async function apply(integrationKeys) {
212
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
213
+ await refresh();
214
+ return result;
215
+ }
216
+ async function reset() {
217
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
218
+ await refresh();
219
+ }
220
+ return { fieldMapping, apply, reset, refresh, accessor, ...rest };
202
221
  }
203
222
 
204
- function useFieldMappings(query) {
205
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.fieldMappings);
223
+ function useFieldMappingInstance(selector) {
224
+ const { item: fieldMappingInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.fieldMappingInstance(selector));
225
+ async function setup() {
226
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
227
+ await refresh();
228
+ }
229
+ async function reset() {
230
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
231
+ await refresh();
232
+ }
233
+ async function openConfiguration(options) {
234
+ return accessor === null || accessor === void 0 ? void 0 : accessor.openConfiguration(options);
235
+ }
206
236
  return {
237
+ fieldMappingInstance,
238
+ accessor,
239
+ refresh,
240
+ setup,
241
+ reset,
242
+ openConfiguration,
207
243
  ...rest,
208
244
  };
209
245
  }
210
246
 
211
- function useFieldMappingInstance(selector) {
212
- const { data: fieldMappingInstance, ...rest } = useElement(selector, (integrationApp) => integrationApp.fieldMappingInstance.bind(integrationApp));
213
- const accessor = rest.accessor;
247
+ function useFieldMappingInstances(query) {
248
+ const { ...rest } = useElements('field-mapping-instances', query);
214
249
  return {
215
- fieldMappingInstance,
216
- setup: () => accessor.setup(),
217
- reset: () => accessor.reset(),
218
- openConfiguration: (options) => accessor.openConfiguration(options),
250
+ fieldMappingInstances: rest.items,
219
251
  ...rest,
220
252
  };
221
253
  }
222
254
 
223
- function useFieldMappingInstances(query) {
224
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.fieldMappingInstances);
255
+ function useFieldMappings(query) {
256
+ const { ...rest } = useElements('field-mappings', query);
225
257
  return {
258
+ fieldMappings: rest.items,
226
259
  ...rest,
227
260
  };
228
261
  }
229
262
 
230
- function useDataSource(idOrKey) {
231
- const { data: dataSource, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.dataSource.bind(integrationApp));
232
- return { dataSource, ...rest };
263
+ function useDataSource(selector) {
264
+ const { item: dataSource, refresh, accessor, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataSource(selector));
265
+ async function apply(integrationKeys) {
266
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
267
+ await refresh();
268
+ return result;
269
+ }
270
+ async function reset() {
271
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
272
+ await refresh();
273
+ }
274
+ return { dataSource, apply, reset, refresh, accessor, ...rest };
233
275
  }
234
276
 
235
- function useDataSources(query) {
236
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.dataSources);
277
+ function useDataSourceEvents(query) {
278
+ const { ...rest } = useElements('data-source-events', query);
237
279
  return {
280
+ dataSourceEvents: rest,
238
281
  ...rest,
239
282
  };
240
283
  }
241
284
 
242
285
  function useDataSourceInstance(selector) {
243
- const { data: dataSourceInstance, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataSourceInstance.bind(integrationApp));
244
- const accessor = rest.accessor;
286
+ const { item: dataSourceInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataSourceInstance(selector));
287
+ async function setup() {
288
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
289
+ await refresh();
290
+ }
291
+ async function reset() {
292
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
293
+ await refresh();
294
+ }
295
+ async function subscribe(eventType) {
296
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.subscribe(eventType));
297
+ await refresh();
298
+ }
299
+ async function resubscribe(eventType) {
300
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.resubscribe(eventType));
301
+ await refresh();
302
+ }
303
+ async function unsubscribe(eventType) {
304
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.unsubscribe(eventType));
305
+ await refresh();
306
+ }
307
+ async function pullUpdates() {
308
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.pullUpdates());
309
+ await refresh();
310
+ }
311
+ async function fullSync() {
312
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.fullSync());
313
+ await refresh();
314
+ }
315
+ async function getSyncsList() {
316
+ return await (accessor === null || accessor === void 0 ? void 0 : accessor.getSyncsList());
317
+ }
318
+ async function openConfiguration(options) {
319
+ return accessor === null || accessor === void 0 ? void 0 : accessor.openConfiguration(options);
320
+ }
321
+ async function listRecords(request) {
322
+ return accessor === null || accessor === void 0 ? void 0 : accessor.listRecords(request);
323
+ }
324
+ async function findRecords(request) {
325
+ return accessor === null || accessor === void 0 ? void 0 : accessor.findRecords(request);
326
+ }
327
+ async function findRecordById(id) {
328
+ return accessor === null || accessor === void 0 ? void 0 : accessor.findRecordById(id);
329
+ }
330
+ async function createRecord(request) {
331
+ return accessor === null || accessor === void 0 ? void 0 : accessor.createRecord(request);
332
+ }
333
+ async function updateRecord(request) {
334
+ return accessor === null || accessor === void 0 ? void 0 : accessor.updateRecord(request);
335
+ }
336
+ async function deleteRecord(id) {
337
+ return accessor === null || accessor === void 0 ? void 0 : accessor.deleteRecord(id);
338
+ }
339
+ async function unifiedFieldsToNative(unifiedFields) {
340
+ return accessor === null || accessor === void 0 ? void 0 : accessor.unifiedFieldsToNative(unifiedFields);
341
+ }
342
+ async function getCollection() {
343
+ return accessor === null || accessor === void 0 ? void 0 : accessor.getCollection();
344
+ }
345
+ async function getLocations(request) {
346
+ return accessor === null || accessor === void 0 ? void 0 : accessor.getLocations(request);
347
+ }
245
348
  return {
246
349
  dataSourceInstance,
247
- setup: () => accessor.setup(),
248
- subscribe: (eventType) => accessor.subscribe(eventType),
249
- resubscribe: (eventType) => accessor.resubscribe(eventType),
250
- unsubscribe: (eventType) => accessor.unsubscribe(eventType),
251
- pullUpdates: () => accessor.pullUpdates(),
252
- fullSync: () => accessor.fullSync(),
253
- reset: () => accessor.reset(),
254
- openConfiguration: (options) => accessor.openConfiguration(options),
255
- findRecords: (request) => accessor.findRecords(request),
256
- findRecordById: (id) => accessor.findRecordById(id),
257
- createRecord: (request) => accessor.createRecord(request),
258
- updateRecord: (request) => accessor.updateRecord(request),
259
- deleteRecord: (id) => accessor.deleteRecord(id),
260
- unifiedFieldsToNative: (unifiedFields) => accessor.unifiedFieldsToNative(unifiedFields),
261
- ...rest,
262
- };
263
- }
264
-
265
- function useDataSourceInstances(query) {
266
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.dataSourceInstances);
267
- return {
350
+ accessor,
351
+ refresh,
352
+ setup,
353
+ reset,
354
+ subscribe,
355
+ resubscribe,
356
+ unsubscribe,
357
+ pullUpdates,
358
+ fullSync,
359
+ getSyncsList,
360
+ openConfiguration,
361
+ listRecords,
362
+ findRecords,
363
+ findRecordById,
364
+ createRecord,
365
+ updateRecord,
366
+ deleteRecord,
367
+ unifiedFieldsToNative,
368
+ getLocations,
369
+ getCollection,
268
370
  ...rest,
269
371
  };
270
372
  }
271
373
 
272
- function useGetter(key, getter) {
273
- const integrationApp = useIntegrationApp();
274
- const [data, setData] = useState();
275
- const [loading, setLoading] = useState(true);
276
- const [error, setError] = useState(null);
277
- const [refreshCounter, setRefreshCounter] = useState(0);
278
- function refresh() {
279
- setRefreshCounter(refreshCounter + 1);
280
- }
281
- useEffect(() => {
282
- if (key !== undefined) {
283
- setLoading(true);
284
- setError(null);
285
- if (integrationApp) {
286
- getter()
287
- .then(setData)
288
- .catch(setError)
289
- .finally(() => setLoading(false));
290
- }
291
- else {
292
- setError(new Error('IntegrationApp not found. Was this component wrapped in <IntegrationAppProvider>?'));
293
- }
294
- }
295
- }, [integrationApp, key, refreshCounter]);
296
- return { data, loading, error, refresh };
297
- }
298
-
299
374
  function useDataSourceInstanceCollection(dataSourceInstance) {
300
375
  const integrationApp = useIntegrationApp();
301
- const { data: collection, ...rest } = useGetter(dataSourceInstance === null || dataSourceInstance === void 0 ? void 0 : dataSourceInstance.id, () => integrationApp.dataSourceInstance(dataSourceInstance.id).getCollection());
376
+ const { data, error, isLoading, mutate } = useSWR(dataSourceInstance ? `${dataSourceInstance.id}/collection` : null, () => integrationApp.dataSourceInstance(dataSourceInstance.id).getCollection());
377
+ async function refresh() {
378
+ return await mutate();
379
+ }
380
+ const collection = data;
302
381
  return {
303
382
  collection,
304
- ...rest,
383
+ refresh,
384
+ error,
385
+ loading: isLoading,
305
386
  };
306
387
  }
307
388
 
308
389
  function useDataSourceInstanceLocations(dataSourceInstance, args) {
309
390
  var _a;
310
391
  const integrationApp = useIntegrationApp();
311
- const { data, ...rest } = useGetter(dataSourceInstance
312
- ? `${dataSourceInstance.id}/${JSON.stringify(args)}`
313
- : undefined, () => integrationApp
392
+ const { data, error, isLoading, mutate } = useSWR(dataSourceInstance
393
+ ? `${dataSourceInstance.id}/locations?${qs.stringify(args)}`
394
+ : null, () => integrationApp
314
395
  .dataSourceInstance(dataSourceInstance.id)
315
396
  .getLocations(args));
397
+ async function refresh() {
398
+ return await mutate();
399
+ }
400
+ const locations = (_a = data === null || data === void 0 ? void 0 : data.locations) !== null && _a !== void 0 ? _a : [];
316
401
  return {
317
- locations: (_a = data === null || data === void 0 ? void 0 : data.locations) !== null && _a !== void 0 ? _a : [],
402
+ locations,
403
+ refresh,
404
+ error,
405
+ loading: isLoading,
406
+ };
407
+ }
408
+
409
+ function useDataSourceInstances(query) {
410
+ const { ...rest } = useElements('data-source-instances', query);
411
+ return {
412
+ dataSourceInstances: rest.items,
318
413
  ...rest,
319
414
  };
320
415
  }
321
416
 
322
- function useDataSourceEvents(query) {
323
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.dataSourceEvents);
417
+ function useDataSources(query) {
418
+ const { ...rest } = useElements('data-sources', query);
324
419
  return {
420
+ dataSources: rest.items,
325
421
  ...rest,
326
422
  };
327
423
  }
328
424
 
329
- function useAppEvents(query) {
330
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.appEvents);
425
+ function useAppEventSubscription(selector) {
426
+ const { item: appEventSubscription, ...rest } = useElement(selector, (integrationApp) => integrationApp.appEventSubscription(selector));
427
+ return { appEventSubscription, ...rest };
428
+ }
429
+
430
+ function useAppEventSubscriptions(query) {
431
+ const { ...rest } = useElements('app-event-subscriptions', query);
331
432
  return {
433
+ appEventSubscriptions: rest.items,
332
434
  ...rest,
333
435
  };
334
436
  }
335
437
 
336
- function useAppEventType(idOrKey) {
337
- const { data: appEventType, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.appEventType.bind(integrationApp));
438
+ function useAppEventType(id) {
439
+ const { item: appEventType, ...rest } = useElement(id, (integrationApp) => integrationApp.appEventType(id));
338
440
  return { appEventType, ...rest };
339
441
  }
340
442
 
341
443
  function useAppEventTypes(query) {
342
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.appEventTypes);
444
+ const { ...rest } = useElements('app-event-types', query);
343
445
  return {
446
+ appEventTypes: rest.items,
344
447
  ...rest,
345
448
  };
346
449
  }
347
450
 
348
- function useAppEventSubscription(selector) {
349
- const { data: appEventSubscription, ...rest } = useElement(selector, (integrationApp) => integrationApp.appEventSubscription.bind(integrationApp));
350
- return { appEventSubscription, ...rest };
351
- }
352
-
353
- function useAppEventSubscriptions(query) {
354
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.appEventSubscriptions);
451
+ function useAppEvents(query) {
452
+ const { ...rest } = useElements('app-events', query);
355
453
  return {
454
+ appEvents: rest.items,
356
455
  ...rest,
357
456
  };
358
457
  }
359
458
 
360
- function useFlow(idOrSelector) {
361
- const { data: flow, ...rest } = useElement(idOrSelector, (integrationApp) => integrationApp.flow.bind(integrationApp));
362
- return { flow, ...rest };
459
+ function useFlow(selector) {
460
+ const { item: flow, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.flow(selector));
461
+ async function apply(integrationKeys) {
462
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
463
+ await refresh();
464
+ return result;
465
+ }
466
+ async function reset() {
467
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
468
+ return await refresh();
469
+ }
470
+ return { flow, apply, reset, refresh, accessor, ...rest };
363
471
  }
364
472
 
365
473
  function useFlows(query) {
366
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flows);
367
- return { ...rest };
474
+ const { ...rest } = useElements('flows', query);
475
+ return {
476
+ flows: rest.items,
477
+ ...rest,
478
+ };
368
479
  }
369
480
 
370
- function useFlowInstance(props) {
371
- const { data: flowInstance, ...rest } = useElement(props, (integrationApp) => integrationApp.flowInstance.bind(integrationApp));
372
- return { flowInstance, ...rest };
481
+ function useFlowInstance(selector) {
482
+ const { item: flowInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.flowInstance(selector));
483
+ async function enable() {
484
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.enable());
485
+ await refresh();
486
+ }
487
+ async function disable() {
488
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.disable());
489
+ await refresh();
490
+ }
491
+ async function reset() {
492
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
493
+ await refresh();
494
+ }
495
+ async function setup() {
496
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
497
+ await refresh();
498
+ }
499
+ async function openConfiguration(options) {
500
+ return accessor === null || accessor === void 0 ? void 0 : accessor.openConfiguration(options);
501
+ }
502
+ async function run(options = {}) {
503
+ return accessor === null || accessor === void 0 ? void 0 : accessor.run(options);
504
+ }
505
+ async function startRun(options = {}) {
506
+ return accessor === null || accessor === void 0 ? void 0 : accessor.startRun(options);
507
+ }
508
+ return {
509
+ flowInstance,
510
+ accessor,
511
+ refresh,
512
+ enable,
513
+ disable,
514
+ reset,
515
+ setup,
516
+ openConfiguration,
517
+ run,
518
+ startRun,
519
+ ...rest,
520
+ };
373
521
  }
374
522
 
375
523
  function useFlowInstances(query) {
376
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flowInstances);
524
+ const { ...rest } = useElements('flow-instances', query);
377
525
  return {
526
+ flowInstances: rest.items,
378
527
  ...rest,
379
528
  };
380
529
  }
381
530
 
382
531
  function useFlowRun(id) {
383
- const { data: flowRun, ...rest } = useElement(id, (integrationApp) => integrationApp.flowRun.bind(integrationApp));
384
- return { flowRun, ...rest };
532
+ const { item: flowRun, archive, refresh, error, loading, } = useElement(id, (integrationApp) => integrationApp.flowRun(id));
533
+ return {
534
+ flowRun,
535
+ error,
536
+ loading,
537
+ refresh,
538
+ archive,
539
+ };
385
540
  }
386
541
 
387
542
  function useFlowRuns(query) {
388
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flowRuns);
389
- return { ...rest };
543
+ const { ...rest } = useElements('flow-runs', query);
544
+ return {
545
+ flowRuns: rest.items,
546
+ ...rest,
547
+ };
548
+ }
549
+
550
+ function useDataLinkTable(selector) {
551
+ const { item: dataLinkTable, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataLinkTable(selector));
552
+ return { dataLinkTable, ...rest };
553
+ }
554
+
555
+ function useDataLinkTableInstance(selector) {
556
+ const { item: dataLinkTableInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataLinkTableInstance(selector));
557
+ return {
558
+ dataLinkTableInstance,
559
+ accessor,
560
+ refresh,
561
+ findLinks: accessor === null || accessor === void 0 ? void 0 : accessor.findLinks,
562
+ createLink: accessor === null || accessor === void 0 ? void 0 : accessor.createLink,
563
+ deleteLink: accessor === null || accessor === void 0 ? void 0 : accessor.deleteLink,
564
+ ...rest,
565
+ };
566
+ }
567
+
568
+ function useDataLinkTableInstances(query) {
569
+ const { ...rest } = useElements('data-link-table-instances', query);
570
+ return {
571
+ dataLinkTableInstances: rest.items,
572
+ ...rest,
573
+ };
574
+ }
575
+
576
+ function useDataLinkTables(query) {
577
+ const { ...rest } = useElements('data-link-tables', query);
578
+ return {
579
+ dataLinkTables: rest.items,
580
+ ...rest,
581
+ };
582
+ }
583
+
584
+ function useAction(selector) {
585
+ const { item: action, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.action(selector));
586
+ async function apply(integrationKeys) {
587
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
588
+ await refresh();
589
+ return result;
590
+ }
591
+ async function reset() {
592
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
593
+ await refresh();
594
+ }
595
+ return { action, apply, reset, refresh, accessor, ...rest };
596
+ }
597
+
598
+ function useActionInstance(selector) {
599
+ const { item: actionInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.actionInstance(selector));
600
+ async function run(input) {
601
+ return accessor === null || accessor === void 0 ? void 0 : accessor.run(input);
602
+ }
603
+ async function setup() {
604
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
605
+ await refresh();
606
+ }
607
+ async function reset() {
608
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
609
+ await refresh();
610
+ }
611
+ async function openConfiguration(options) {
612
+ return accessor === null || accessor === void 0 ? void 0 : accessor.open(options);
613
+ }
614
+ return {
615
+ actionInstance,
616
+ accessor,
617
+ refresh,
618
+ setup,
619
+ reset,
620
+ openConfiguration,
621
+ run,
622
+ ...rest,
623
+ };
624
+ }
625
+
626
+ function useActionInstances(query) {
627
+ const { ...rest } = useElements('action-instances', query);
628
+ return {
629
+ actionInstances: rest.items,
630
+ ...rest,
631
+ };
632
+ }
633
+
634
+ function useActions(query) {
635
+ const { ...rest } = useElements('actions', query);
636
+ return {
637
+ actions: rest.items,
638
+ ...rest,
639
+ };
640
+ }
641
+
642
+ function useScreen(selector) {
643
+ const { item: screen, ...rest } = useElement(selector, (integrationApp) => integrationApp.screen(selector));
644
+ return { screen, ...rest };
645
+ }
646
+
647
+ function useDataCollectionSpec({ path, key, integrationId, }) {
648
+ var _a;
649
+ const client = useIntegrationApp();
650
+ const dataCollectionKey = key !== null && key !== void 0 ? key : (_a = parseDataLocationPath(path)) === null || _a === void 0 ? void 0 : _a.key;
651
+ const { data: dataCollectionSpec } = useSWR(dataCollectionKey && integrationId
652
+ ? `/integrations/${integrationId}/data/${dataCollectionKey}`
653
+ : null, () => client.integration(integrationId).getDataLocation(dataCollectionKey));
654
+ return dataCollectionSpec;
390
655
  }
391
656
 
392
- export { IntegrationAppProvider, useAppEventSubscription, useAppEventSubscriptions, useAppEventType, useAppEventTypes, useAppEvents, useConnection, useConnections, useConnectorSpec, useDataSource, useDataSourceInstanceCollection as useDataSourceCollection, useDataSourceEvents, useDataSourceInstance, useDataSourceInstances, useDataSourceInstanceLocations as useDataSourceLocations, useDataSources, useFieldMapping, useFieldMappingInstance, useFieldMappingInstances, useFieldMappings, useFlow, useFlowInstance, useFlowInstances, useFlowRun, useFlowRuns, useFlows, useIntegration, useIntegrationApp, useIntegrations };
657
+ export { IntegrationAppProvider, useAction, useActionInstance, useActionInstances, useActions, useAppEventSubscription, useAppEventSubscriptions, useAppEventType, useAppEventTypes, useAppEvents, useConnection, useConnections, useConnectorSpec, useDataCollectionSpec, useDataLinkTable, useDataLinkTableInstance, useDataLinkTableInstances, useDataLinkTables, useDataSource, useDataSourceEvents, useDataSourceInstance, useDataSourceInstanceCollection, useDataSourceInstanceLocations, useDataSourceInstances, useDataSources, useFieldMapping, useFieldMappingInstance, useFieldMappingInstances, useFieldMappings, useFlow, useFlowInstance, useFlowInstances, useFlowRun, useFlowRuns, useFlows, useIntegration, useIntegrationApp, useIntegrationAppSWR, useIntegrations, useScreen };
393
658
  //# sourceMappingURL=index.module.mjs.map