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