@integration-app/react 0.2.1 → 0.3.2

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 (57) hide show
  1. package/dist/index.d.ts +396 -165
  2. package/dist/index.js +527 -234
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.module.d.ts +396 -165
  5. package/dist/index.module.mjs +515 -235
  6. package/dist/index.module.mjs.map +1 -1
  7. package/dist/index.umd.d.ts +396 -165
  8. package/dist/index.umd.js +527 -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/customers/useCustomer.ts +19 -0
  22. package/src/customers/useCustomers.ts +11 -0
  23. package/src/data-collections/useDataCollectionSpec.ts +26 -0
  24. package/src/data-links/useDataLinkTable.ts +18 -0
  25. package/src/data-links/useDataLinkTableInstance.ts +39 -0
  26. package/src/data-links/useDataLinkTableInstances.ts +19 -0
  27. package/src/data-links/useDataLinkTables.ts +11 -0
  28. package/src/data-sources/useDataSource.ts +29 -6
  29. package/src/data-sources/useDataSourceEvents.ts +2 -4
  30. package/src/data-sources/useDataSourceInstance.ts +120 -26
  31. package/src/data-sources/useDataSourceInstanceCollection.ts +14 -4
  32. package/src/data-sources/useDataSourceInstanceLocations.ts +17 -6
  33. package/src/data-sources/useDataSourceInstances.ts +5 -4
  34. package/src/data-sources/useDataSources.ts +2 -4
  35. package/src/field-mappings/useFieldMapping.ts +29 -8
  36. package/src/field-mappings/useFieldMappingInstance.ts +35 -12
  37. package/src/field-mappings/useFieldMappingInstances.ts +5 -4
  38. package/src/field-mappings/useFieldMappings.ts +2 -4
  39. package/src/flows/useFlow.ts +29 -8
  40. package/src/flows/useFlowInstance.ts +62 -5
  41. package/src/flows/useFlowInstances.ts +2 -4
  42. package/src/flows/useFlowRun.ts +18 -6
  43. package/src/flows/useFlowRuns.ts +5 -5
  44. package/src/flows/useFlows.ts +5 -5
  45. package/src/hooks/useElement.tsx +137 -149
  46. package/src/hooks/useElements.tsx +44 -73
  47. package/src/hooks/useIntegrationAppSWR.tsx +13 -0
  48. package/src/index.tsx +30 -14
  49. package/src/integrations/useConnection.ts +14 -5
  50. package/src/integrations/useConnections.ts +3 -4
  51. package/src/integrations/useConnectorSpec.ts +9 -18
  52. package/src/integrations/useIntegration.ts +11 -7
  53. package/src/integrations/useIntegrations.ts +3 -4
  54. package/src/screens/useScreen.ts +19 -0
  55. package/src/flows/useFlowTemplate.ts +0 -0
  56. package/src/flows/useFlowTemplates.ts +0 -0
  57. 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,649 @@ 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(integrationIdOrKey
194
+ ? `/integrations/${integrationIdOrKey}/connector-spec`
195
+ : undefined, () => integrationApp.integration(integrationIdOrKey).getConnectorSpec());
196
+ return { data, loading: isLoading, error };
191
197
  }
192
198
 
193
- function useConnections(query) {
194
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.connections);
199
+ function useIntegration(id) {
200
+ const { item: integration, ...rest } = useElement(id, (integrationApp) => integrationApp.integration(id));
201
+ return { integration, ...rest };
202
+ }
203
+
204
+ function useIntegrations(query) {
205
+ const { ...rest } = useElements('integrations', query);
195
206
  return {
207
+ integrations: rest.items,
196
208
  ...rest,
197
209
  };
198
210
  }
199
211
 
200
- function useFieldMapping(idOrKey) {
201
- const { data: fieldMapping, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.fieldMapping.bind(integrationApp));
202
- return { fieldMapping, ...rest };
212
+ function useFieldMapping(selector) {
213
+ const { item: fieldMapping, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.fieldMapping(selector));
214
+ async function apply(integrationKeys) {
215
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
216
+ await refresh();
217
+ return result;
218
+ }
219
+ async function reset() {
220
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
221
+ await refresh();
222
+ }
223
+ return { fieldMapping, apply, reset, refresh, accessor, ...rest };
203
224
  }
204
225
 
205
- function useFieldMappings(query) {
206
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.fieldMappings);
226
+ function useFieldMappingInstance(selector) {
227
+ const { item: fieldMappingInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.fieldMappingInstance(selector));
228
+ async function setup() {
229
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
230
+ await refresh();
231
+ }
232
+ async function reset() {
233
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
234
+ await refresh();
235
+ }
236
+ async function openConfiguration(options) {
237
+ return accessor === null || accessor === void 0 ? void 0 : accessor.openConfiguration(options);
238
+ }
207
239
  return {
240
+ fieldMappingInstance,
241
+ accessor,
242
+ refresh,
243
+ setup,
244
+ reset,
245
+ openConfiguration,
208
246
  ...rest,
209
247
  };
210
248
  }
211
249
 
212
- function useFieldMappingInstance(selector) {
213
- const { data: fieldMappingInstance, ...rest } = useElement(selector, (integrationApp) => integrationApp.fieldMappingInstance.bind(integrationApp));
214
- const accessor = rest.accessor;
250
+ function useFieldMappingInstances(query) {
251
+ const { ...rest } = useElements('field-mapping-instances', query);
215
252
  return {
216
- fieldMappingInstance,
217
- setup: () => accessor.setup(),
218
- reset: () => accessor.reset(),
219
- openConfiguration: (options) => accessor.openConfiguration(options),
253
+ fieldMappingInstances: rest.items,
220
254
  ...rest,
221
255
  };
222
256
  }
223
257
 
224
- function useFieldMappingInstances(query) {
225
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.fieldMappingInstances);
258
+ function useFieldMappings(query) {
259
+ const { ...rest } = useElements('field-mappings', query);
226
260
  return {
261
+ fieldMappings: rest.items,
227
262
  ...rest,
228
263
  };
229
264
  }
230
265
 
231
- function useDataSource(idOrKey) {
232
- const { data: dataSource, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.dataSource.bind(integrationApp));
233
- return { dataSource, ...rest };
266
+ function useDataSource(selector) {
267
+ const { item: dataSource, refresh, accessor, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataSource(selector));
268
+ async function apply(integrationKeys) {
269
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
270
+ await refresh();
271
+ return result;
272
+ }
273
+ async function reset() {
274
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
275
+ await refresh();
276
+ }
277
+ return { dataSource, apply, reset, refresh, accessor, ...rest };
234
278
  }
235
279
 
236
- function useDataSources(query) {
237
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.dataSources);
280
+ function useDataSourceEvents(query) {
281
+ const { ...rest } = useElements('data-source-events', query);
238
282
  return {
283
+ dataSourceEvents: rest,
239
284
  ...rest,
240
285
  };
241
286
  }
242
287
 
243
288
  function useDataSourceInstance(selector) {
244
- const { data: dataSourceInstance, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataSourceInstance.bind(integrationApp));
245
- const accessor = rest.accessor;
289
+ const { item: dataSourceInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataSourceInstance(selector));
290
+ async function setup() {
291
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
292
+ await refresh();
293
+ }
294
+ async function reset() {
295
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
296
+ await refresh();
297
+ }
298
+ async function subscribe(eventType) {
299
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.subscribe(eventType));
300
+ await refresh();
301
+ }
302
+ async function resubscribe(eventType) {
303
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.resubscribe(eventType));
304
+ await refresh();
305
+ }
306
+ async function unsubscribe(eventType) {
307
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.unsubscribe(eventType));
308
+ await refresh();
309
+ }
310
+ async function pullUpdates() {
311
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.pullUpdates());
312
+ await refresh();
313
+ }
314
+ async function fullSync() {
315
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.fullSync());
316
+ await refresh();
317
+ }
318
+ async function getSyncsList() {
319
+ return await (accessor === null || accessor === void 0 ? void 0 : accessor.getSyncsList());
320
+ }
321
+ async function openConfiguration(options) {
322
+ return accessor === null || accessor === void 0 ? void 0 : accessor.openConfiguration(options);
323
+ }
324
+ async function listRecords(request) {
325
+ return accessor === null || accessor === void 0 ? void 0 : accessor.listRecords(request);
326
+ }
327
+ async function findRecords(request) {
328
+ return accessor === null || accessor === void 0 ? void 0 : accessor.findRecords(request);
329
+ }
330
+ async function findRecordById(id) {
331
+ return accessor === null || accessor === void 0 ? void 0 : accessor.findRecordById(id);
332
+ }
333
+ async function createRecord(request) {
334
+ return accessor === null || accessor === void 0 ? void 0 : accessor.createRecord(request);
335
+ }
336
+ async function updateRecord(request) {
337
+ return accessor === null || accessor === void 0 ? void 0 : accessor.updateRecord(request);
338
+ }
339
+ async function deleteRecord(id) {
340
+ return accessor === null || accessor === void 0 ? void 0 : accessor.deleteRecord(id);
341
+ }
342
+ async function unifiedFieldsToNative(unifiedFields) {
343
+ return accessor === null || accessor === void 0 ? void 0 : accessor.unifiedFieldsToNative(unifiedFields);
344
+ }
345
+ async function getCollection() {
346
+ return accessor === null || accessor === void 0 ? void 0 : accessor.getCollection();
347
+ }
348
+ async function getLocations(request) {
349
+ return accessor === null || accessor === void 0 ? void 0 : accessor.getLocations(request);
350
+ }
246
351
  return {
247
352
  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 {
353
+ accessor,
354
+ refresh,
355
+ setup,
356
+ reset,
357
+ subscribe,
358
+ resubscribe,
359
+ unsubscribe,
360
+ pullUpdates,
361
+ fullSync,
362
+ getSyncsList,
363
+ openConfiguration,
364
+ listRecords,
365
+ findRecords,
366
+ findRecordById,
367
+ createRecord,
368
+ updateRecord,
369
+ deleteRecord,
370
+ unifiedFieldsToNative,
371
+ getLocations,
372
+ getCollection,
269
373
  ...rest,
270
374
  };
271
375
  }
272
376
 
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
377
  function useDataSourceInstanceCollection(dataSourceInstance) {
301
378
  const integrationApp = useIntegrationApp();
302
- const { data: collection, ...rest } = useGetter(dataSourceInstance === null || dataSourceInstance === void 0 ? void 0 : dataSourceInstance.id, () => integrationApp.dataSourceInstance(dataSourceInstance.id).getCollection());
379
+ const { data, error, isLoading, mutate } = useSWR(dataSourceInstance ? `${dataSourceInstance.id}/collection` : null, () => integrationApp.dataSourceInstance(dataSourceInstance.id).getCollection());
380
+ async function refresh() {
381
+ return await mutate();
382
+ }
383
+ const collection = data;
303
384
  return {
304
385
  collection,
305
- ...rest,
386
+ refresh,
387
+ error,
388
+ loading: isLoading,
306
389
  };
307
390
  }
308
391
 
309
392
  function useDataSourceInstanceLocations(dataSourceInstance, args) {
310
393
  var _a;
311
394
  const integrationApp = useIntegrationApp();
312
- const { data, ...rest } = useGetter(dataSourceInstance
313
- ? `${dataSourceInstance.id}/${JSON.stringify(args)}`
314
- : undefined, () => integrationApp
395
+ const { data, error, isLoading, mutate } = useSWR(dataSourceInstance
396
+ ? `${dataSourceInstance.id}/locations?${qs.stringify(args)}`
397
+ : null, () => integrationApp
315
398
  .dataSourceInstance(dataSourceInstance.id)
316
399
  .getLocations(args));
400
+ async function refresh() {
401
+ return await mutate();
402
+ }
403
+ const locations = (_a = data === null || data === void 0 ? void 0 : data.locations) !== null && _a !== void 0 ? _a : [];
317
404
  return {
318
- locations: (_a = data === null || data === void 0 ? void 0 : data.locations) !== null && _a !== void 0 ? _a : [],
405
+ locations,
406
+ refresh,
407
+ error,
408
+ loading: isLoading,
409
+ };
410
+ }
411
+
412
+ function useDataSourceInstances(query) {
413
+ const { ...rest } = useElements('data-source-instances', query);
414
+ return {
415
+ dataSourceInstances: rest.items,
319
416
  ...rest,
320
417
  };
321
418
  }
322
419
 
323
- function useDataSourceEvents(query) {
324
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.dataSourceEvents);
420
+ function useDataSources(query) {
421
+ const { ...rest } = useElements('data-sources', query);
325
422
  return {
423
+ dataSources: rest.items,
326
424
  ...rest,
327
425
  };
328
426
  }
329
427
 
330
- function useAppEvents(query) {
331
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.appEvents);
428
+ function useAppEventSubscription(selector) {
429
+ const { item: appEventSubscription, ...rest } = useElement(selector, (integrationApp) => integrationApp.appEventSubscription(selector));
430
+ return { appEventSubscription, ...rest };
431
+ }
432
+
433
+ function useAppEventSubscriptions(query) {
434
+ const { ...rest } = useElements('app-event-subscriptions', query);
332
435
  return {
436
+ appEventSubscriptions: rest.items,
333
437
  ...rest,
334
438
  };
335
439
  }
336
440
 
337
- function useAppEventType(idOrKey) {
338
- const { data: appEventType, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.appEventType.bind(integrationApp));
441
+ function useAppEventType(id) {
442
+ const { item: appEventType, ...rest } = useElement(id, (integrationApp) => integrationApp.appEventType(id));
339
443
  return { appEventType, ...rest };
340
444
  }
341
445
 
342
446
  function useAppEventTypes(query) {
343
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.appEventTypes);
447
+ const { ...rest } = useElements('app-event-types', query);
344
448
  return {
449
+ appEventTypes: rest.items,
345
450
  ...rest,
346
451
  };
347
452
  }
348
453
 
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);
454
+ function useAppEvents(query) {
455
+ const { ...rest } = useElements('app-events', query);
356
456
  return {
457
+ appEvents: rest.items,
357
458
  ...rest,
358
459
  };
359
460
  }
360
461
 
361
- function useFlow(idOrSelector) {
362
- const { data: flow, ...rest } = useElement(idOrSelector, (integrationApp) => integrationApp.flow.bind(integrationApp));
363
- return { flow, ...rest };
462
+ function useFlow(selector) {
463
+ const { item: flow, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.flow(selector));
464
+ async function apply(integrationKeys) {
465
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
466
+ await refresh();
467
+ return result;
468
+ }
469
+ async function reset() {
470
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
471
+ return await refresh();
472
+ }
473
+ return { flow, apply, reset, refresh, accessor, ...rest };
364
474
  }
365
475
 
366
476
  function useFlows(query) {
367
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flows);
368
- return { ...rest };
477
+ const { ...rest } = useElements('flows', query);
478
+ return {
479
+ flows: rest.items,
480
+ ...rest,
481
+ };
369
482
  }
370
483
 
371
- function useFlowInstance(props) {
372
- const { data: flowInstance, ...rest } = useElement(props, (integrationApp) => integrationApp.flowInstance.bind(integrationApp));
373
- return { flowInstance, ...rest };
484
+ function useFlowInstance(selector) {
485
+ const { item: flowInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.flowInstance(selector));
486
+ async function enable() {
487
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.enable());
488
+ await refresh();
489
+ }
490
+ async function disable() {
491
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.disable());
492
+ await refresh();
493
+ }
494
+ async function reset() {
495
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
496
+ await refresh();
497
+ }
498
+ async function setup() {
499
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
500
+ await refresh();
501
+ }
502
+ async function openConfiguration(options) {
503
+ return accessor === null || accessor === void 0 ? void 0 : accessor.openConfiguration(options);
504
+ }
505
+ async function run(options = {}) {
506
+ return accessor === null || accessor === void 0 ? void 0 : accessor.run(options);
507
+ }
508
+ async function startRun(options = {}) {
509
+ return accessor === null || accessor === void 0 ? void 0 : accessor.startRun(options);
510
+ }
511
+ return {
512
+ flowInstance,
513
+ accessor,
514
+ refresh,
515
+ enable,
516
+ disable,
517
+ reset,
518
+ setup,
519
+ openConfiguration,
520
+ run,
521
+ startRun,
522
+ ...rest,
523
+ };
374
524
  }
375
525
 
376
526
  function useFlowInstances(query) {
377
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flowInstances);
527
+ const { ...rest } = useElements('flow-instances', query);
378
528
  return {
529
+ flowInstances: rest.items,
379
530
  ...rest,
380
531
  };
381
532
  }
382
533
 
383
534
  function useFlowRun(id) {
384
- const { data: flowRun, ...rest } = useElement(id, (integrationApp) => integrationApp.flowRun.bind(integrationApp));
385
- return { flowRun, ...rest };
535
+ const { item: flowRun, archive, refresh, error, loading, } = useElement(id, (integrationApp) => integrationApp.flowRun(id));
536
+ return {
537
+ flowRun,
538
+ error,
539
+ loading,
540
+ refresh,
541
+ archive,
542
+ };
386
543
  }
387
544
 
388
545
  function useFlowRuns(query) {
389
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flowRuns);
390
- return { ...rest };
546
+ const { ...rest } = useElements('flow-runs', query);
547
+ return {
548
+ flowRuns: rest.items,
549
+ ...rest,
550
+ };
551
+ }
552
+
553
+ function useDataLinkTable(selector) {
554
+ const { item: dataLinkTable, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataLinkTable(selector));
555
+ return { dataLinkTable, ...rest };
556
+ }
557
+
558
+ function useDataLinkTableInstance(selector) {
559
+ const { item: dataLinkTableInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataLinkTableInstance(selector));
560
+ return {
561
+ dataLinkTableInstance,
562
+ accessor,
563
+ refresh,
564
+ findLinks: accessor === null || accessor === void 0 ? void 0 : accessor.findLinks,
565
+ createLink: accessor === null || accessor === void 0 ? void 0 : accessor.createLink,
566
+ deleteLink: accessor === null || accessor === void 0 ? void 0 : accessor.deleteLink,
567
+ ...rest,
568
+ };
569
+ }
570
+
571
+ function useDataLinkTableInstances(query) {
572
+ const { ...rest } = useElements('data-link-table-instances', query);
573
+ return {
574
+ dataLinkTableInstances: rest.items,
575
+ ...rest,
576
+ };
577
+ }
578
+
579
+ function useDataLinkTables(query) {
580
+ const { ...rest } = useElements('data-link-tables', query);
581
+ return {
582
+ dataLinkTables: rest.items,
583
+ ...rest,
584
+ };
585
+ }
586
+
587
+ function useAction(selector) {
588
+ const { item: action, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.action(selector));
589
+ async function apply(integrationKeys) {
590
+ const result = await (accessor === null || accessor === void 0 ? void 0 : accessor.apply(integrationKeys));
591
+ await refresh();
592
+ return result;
593
+ }
594
+ async function reset() {
595
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
596
+ await refresh();
597
+ }
598
+ return { action, apply, reset, refresh, accessor, ...rest };
599
+ }
600
+
601
+ function useActionInstance(selector) {
602
+ const { item: actionInstance, accessor, refresh, ...rest } = useElement(selector, (integrationApp) => integrationApp.actionInstance(selector));
603
+ async function run(input) {
604
+ return accessor === null || accessor === void 0 ? void 0 : accessor.run(input);
605
+ }
606
+ async function setup() {
607
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.setup());
608
+ await refresh();
609
+ }
610
+ async function reset() {
611
+ await (accessor === null || accessor === void 0 ? void 0 : accessor.reset());
612
+ await refresh();
613
+ }
614
+ async function openConfiguration(options) {
615
+ return accessor === null || accessor === void 0 ? void 0 : accessor.open(options);
616
+ }
617
+ return {
618
+ actionInstance,
619
+ accessor,
620
+ refresh,
621
+ setup,
622
+ reset,
623
+ openConfiguration,
624
+ run,
625
+ ...rest,
626
+ };
627
+ }
628
+
629
+ function useActionInstances(query) {
630
+ const { ...rest } = useElements('action-instances', query);
631
+ return {
632
+ actionInstances: rest.items,
633
+ ...rest,
634
+ };
635
+ }
636
+
637
+ function useActions(query) {
638
+ const { ...rest } = useElements('actions', query);
639
+ return {
640
+ actions: rest.items,
641
+ ...rest,
642
+ };
643
+ }
644
+
645
+ function useScreen(selector) {
646
+ const { item: screen, ...rest } = useElement(selector, (integrationApp) => integrationApp.screen(selector));
647
+ return { screen, ...rest };
648
+ }
649
+
650
+ function useCustomer(selector) {
651
+ const { item: customer, ...rest } = useElement(selector, (c) => c.customer(selector));
652
+ return { customer, ...rest };
653
+ }
654
+
655
+ function useCustomers(query) {
656
+ const { ...rest } = useElements('users', query);
657
+ return {
658
+ customers: rest.items,
659
+ ...rest,
660
+ };
661
+ }
662
+
663
+ function useDataCollectionSpec({ path, key, integrationId, }) {
664
+ var _a;
665
+ const client = useIntegrationApp();
666
+ const dataCollectionKey = key !== null && key !== void 0 ? key : (_a = sdk.parseDataLocationPath(path)) === null || _a === void 0 ? void 0 : _a.key;
667
+ const { data: dataCollectionSpec } = useSWR(dataCollectionKey && integrationId
668
+ ? `/integrations/${integrationId}/data/${dataCollectionKey}`
669
+ : null, () => client.integration(integrationId).getDataLocation(dataCollectionKey));
670
+ return dataCollectionSpec;
391
671
  }
392
672
 
393
673
  Object.defineProperty(exports, 'DataForm', {
@@ -395,6 +675,10 @@ Object.defineProperty(exports, 'DataForm', {
395
675
  get: function () { return sdk.DataForm; }
396
676
  });
397
677
  exports.IntegrationAppProvider = IntegrationAppProvider;
678
+ exports.useAction = useAction;
679
+ exports.useActionInstance = useActionInstance;
680
+ exports.useActionInstances = useActionInstances;
681
+ exports.useActions = useActions;
398
682
  exports.useAppEventSubscription = useAppEventSubscription;
399
683
  exports.useAppEventSubscriptions = useAppEventSubscriptions;
400
684
  exports.useAppEventType = useAppEventType;
@@ -403,12 +687,19 @@ exports.useAppEvents = useAppEvents;
403
687
  exports.useConnection = useConnection;
404
688
  exports.useConnections = useConnections;
405
689
  exports.useConnectorSpec = useConnectorSpec;
690
+ exports.useCustomer = useCustomer;
691
+ exports.useCustomers = useCustomers;
692
+ exports.useDataCollectionSpec = useDataCollectionSpec;
693
+ exports.useDataLinkTable = useDataLinkTable;
694
+ exports.useDataLinkTableInstance = useDataLinkTableInstance;
695
+ exports.useDataLinkTableInstances = useDataLinkTableInstances;
696
+ exports.useDataLinkTables = useDataLinkTables;
406
697
  exports.useDataSource = useDataSource;
407
- exports.useDataSourceCollection = useDataSourceInstanceCollection;
408
698
  exports.useDataSourceEvents = useDataSourceEvents;
409
699
  exports.useDataSourceInstance = useDataSourceInstance;
700
+ exports.useDataSourceInstanceCollection = useDataSourceInstanceCollection;
701
+ exports.useDataSourceInstanceLocations = useDataSourceInstanceLocations;
410
702
  exports.useDataSourceInstances = useDataSourceInstances;
411
- exports.useDataSourceLocations = useDataSourceInstanceLocations;
412
703
  exports.useDataSources = useDataSources;
413
704
  exports.useFieldMapping = useFieldMapping;
414
705
  exports.useFieldMappingInstance = useFieldMappingInstance;
@@ -422,5 +713,7 @@ exports.useFlowRuns = useFlowRuns;
422
713
  exports.useFlows = useFlows;
423
714
  exports.useIntegration = useIntegration;
424
715
  exports.useIntegrationApp = useIntegrationApp;
716
+ exports.useIntegrationAppSWR = useIntegrationAppSWR;
425
717
  exports.useIntegrations = useIntegrations;
718
+ exports.useScreen = useScreen;
426
719
  //# sourceMappingURL=index.js.map