@integration-app/react 0.2.0 → 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 (57) hide show
  1. package/README.md +49 -34
  2. package/dist/index.d.ts +377 -160
  3. package/dist/index.js +517 -226
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.module.d.ts +377 -160
  6. package/dist/index.module.mjs +506 -227
  7. package/dist/index.module.mjs.map +1 -1
  8. package/dist/index.umd.d.ts +377 -160
  9. package/dist/index.umd.js +517 -230
  10. package/dist/index.umd.js.map +1 -1
  11. package/package.json +16 -9
  12. package/src/actions/useAction.ts +35 -0
  13. package/src/actions/useActionInstance.ts +56 -0
  14. package/src/actions/useActionInstances.ts +11 -0
  15. package/src/actions/useActions.ts +11 -0
  16. package/src/app-events/useAppEventSubscription.ts +6 -6
  17. package/src/app-events/useAppEventSubscriptions.ts +5 -4
  18. package/src/app-events/useAppEventType.ts +10 -7
  19. package/src/app-events/useAppEventTypes.ts +2 -4
  20. package/src/app-events/useAppEvents.ts +2 -4
  21. package/src/contexts/index.tsx +4 -0
  22. package/src/contexts/integration-app-context.tsx +11 -2
  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 +17 -0
  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 +37 -14
  37. package/src/field-mappings/useFieldMappingInstances.ts +6 -5
  38. package/src/field-mappings/useFieldMappings.ts +2 -4
  39. package/src/flows/useFlow.ts +28 -6
  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 +142 -136
  46. package/src/hooks/useElements.tsx +44 -73
  47. package/src/hooks/useIntegrationAppSWR.tsx +13 -0
  48. package/src/index.tsx +29 -16
  49. package/src/integrations/useConnection.ts +14 -5
  50. package/src/integrations/useConnections.ts +3 -4
  51. package/src/integrations/useConnectorSpec.ts +14 -0
  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/rollup.config.mjs +0 -64
  56. package/src/hooks/useGetter.tsx +0 -38
  57. package/src/integrations/useConnectionSpec.ts +0 -25
package/dist/index.js CHANGED
@@ -3,377 +3,656 @@
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';
9
- const IntegrationAppProvider = ({ token, apiUri = null, uiUri = null, children, }) => {
13
+ const IntegrationAppProvider = ({ token, fetchToken, credentials, fetchCredentials, apiUri = null, uiUri = null, children, }) => {
10
14
  const client = react.useMemo(() => new sdk.IntegrationAppClient({
11
15
  token,
16
+ fetchToken,
17
+ credentials,
18
+ fetchCredentials,
12
19
  apiUri,
13
20
  uiUri,
14
- }), [token, apiUri, uiUri]);
21
+ }), [token, JSON.stringify(credentials), apiUri, uiUri]);
15
22
  return (jsxRuntime.jsx(IntegrationAppContext.Provider, { value: client, children: children }));
16
23
  };
17
24
  function useIntegrationApp() {
18
25
  return react.useContext(IntegrationAppContext);
19
26
  }
20
27
 
21
- function useConnectorSpec(integrationKey) {
22
- const integrationApp = useIntegrationApp();
23
- const [data, setData] = react.useState(null);
24
- const [loading, setLoading] = react.useState(true);
25
- const [error, setError] = react.useState(null);
26
- react.useEffect(() => {
27
- if (!integrationApp) {
28
- return;
29
- }
30
- integrationApp
31
- .integration(integrationKey)
32
- .getConnectorSpec()
33
- .then(setData)
34
- .catch(setError)
35
- .finally(() => setLoading(false));
36
- }, [integrationApp, integrationKey]);
37
- 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);
38
35
  }
39
36
 
40
- function useElement(props, accessorGenerator) {
37
+ const elementStateCache = new Map();
38
+ function useElement(selector, accessorGenerator) {
41
39
  const integrationApp = useIntegrationApp();
42
- const [data, setData] = react.useState();
43
- const [loading, setLoading] = react.useState(true);
44
- const [error, setError] = react.useState(null);
45
- const [refreshCounter, setRefreshCounter] = react.useState(0);
46
- const selector = (props === null || props === void 0 ? void 0 : props.id)
47
- ? props.id
48
- : props;
49
- const accessor = integrationApp
50
- ? accessorGenerator(integrationApp)(selector)
51
- : null;
52
- react.useEffect(() => {
53
- setLoading(true);
54
- setError(null);
55
- if (integrationApp) {
56
- accessor
57
- .get()
58
- .then(setData)
59
- .catch(setError)
60
- .finally(() => setLoading(false));
61
- }
62
- else {
63
- setError(new Error('IntegrationApp not found. Was this component wrapped in <IntegrationAppProvider>?'));
64
- }
65
- }, [integrationApp, JSON.stringify(props), refreshCounter]);
66
- async function create(createData) {
67
- if (data !== undefined) {
68
- setData({
69
- ...data,
70
- ...createData,
71
- });
72
- }
73
- return accessorGenerator(integrationApp)(selector).create(createData);
74
- }
75
- function refresh() {
76
- setRefreshCounter(refreshCounter + 1);
77
- }
78
- async function patch(patch) {
79
- if (typeof patch === 'object') {
80
- setData({
81
- ...data,
82
- ...(patch !== null && patch !== void 0 ? patch : {}),
83
- });
84
- return accessorGenerator(integrationApp)(selector).patch(patch);
85
- }
86
- else {
87
- return data;
88
- }
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
+ });
89
68
  }
90
- async function put(putData) {
91
- if (data !== undefined) {
92
- setData({
93
- ...data,
94
- ...putData,
95
- });
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}`);
96
83
  }
97
- return accessorGenerator(integrationApp)(selector).put(putData);
84
+ elementState.updatedLocally = true;
85
+ const newLocalData = {
86
+ ...item,
87
+ ...data,
88
+ };
89
+ await mutate(newLocalData, false);
90
+ await elementState.debouncedPut(data);
91
+ }
92
+ async function patch(data) {
93
+ const newData = {
94
+ ...item,
95
+ ...data,
96
+ };
97
+ return put(newData);
98
98
  }
99
99
  async function archive() {
100
- setData(null);
101
- return accessorGenerator(integrationApp)(selector).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);
102
113
  }
103
114
  return {
104
- data,
115
+ accessor,
116
+ item,
117
+ loading,
118
+ saving: elementState.updatedLocally || elementState.savingToServer,
119
+ error,
120
+ refresh,
121
+ refreshing,
105
122
  create,
106
123
  patch,
107
124
  put,
108
125
  archive,
109
- refresh,
110
- loading,
111
- error,
112
- accessor,
113
126
  };
114
127
  }
115
128
 
116
- function useIntegration(idOrKey) {
117
- const { data: integration, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.integration.bind(integrationApp));
118
- 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
+ };
119
135
  }
120
136
 
121
- function useElements(initialQuery, accessorGenerator) {
137
+ const LIMIT = 25;
138
+ function useElements(route, query = {}) {
122
139
  const integrationApp = useIntegrationApp();
123
- const refreshId = react.useRef(0);
124
- const [items, setItems] = react.useState([]);
125
- const [nextCursor, setNextCursor] = react.useState(undefined);
126
- const [loading, setLoading] = react.useState(false);
127
- 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;
128
160
  async function loadMore() {
129
- const startingRefreshId = refreshId.current;
130
- const isFirstPage = !nextCursor;
131
- function setStateIfCurrentRefresh(stateSetter, valueGetter) {
132
- stateSetter((value) => startingRefreshId === refreshId.current ? valueGetter(value) : value);
133
- }
134
- setStateIfCurrentRefresh(setError, () => null);
135
- setStateIfCurrentRefresh(setLoading, () => true);
136
- const queryParams = {
137
- ...initialQuery,
138
- };
139
- if (nextCursor)
140
- queryParams.cursor = nextCursor;
141
- try {
142
- const data = await accessorGenerator(integrationApp).find(queryParams);
143
- setStateIfCurrentRefresh(setNextCursor, () => data.cursor);
144
- setStateIfCurrentRefresh(setItems, (items) => isFirstPage ? data.items : [...items, ...data.items]);
145
- }
146
- catch (e) {
147
- setStateIfCurrentRefresh(setError, () => e);
148
- }
149
- finally {
150
- 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);
151
167
  }
152
168
  }
153
- react.useEffect(() => {
154
- if (!integrationApp) {
155
- setError(new Error('IntegrationApp not found. Was this component wrapped in <IntegrationAppProvider>?'));
156
- return;
157
- }
158
- refresh();
159
- }, [integrationApp, JSON.stringify(initialQuery)]);
160
169
  async function refresh() {
161
- refreshId.current += 1;
162
- setNextCursor(undefined);
163
- await loadMore();
170
+ await mutate();
164
171
  }
165
172
  return {
166
173
  items,
167
174
  refresh,
175
+ refreshing,
168
176
  loadMore,
177
+ loadingMore,
169
178
  loading,
170
179
  error,
171
180
  };
172
181
  }
173
182
 
174
- function useIntegrations(query) {
175
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.integrations);
183
+ function useConnections(query) {
184
+ const { ...rest } = useElements('connections', query);
176
185
  return {
186
+ connections: rest.items,
177
187
  ...rest,
178
188
  };
179
189
  }
180
190
 
181
- function useConnection(id) {
182
- const { data: connection, ...rest } = useElement(id, (integrationApp) => integrationApp.connection.bind(integrationApp));
183
- 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 };
184
195
  }
185
196
 
186
- function useConnections(query) {
187
- 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);
188
204
  return {
205
+ integrations: rest.items,
189
206
  ...rest,
190
207
  };
191
208
  }
192
209
 
193
- function useFieldMapping(idOrKey) {
194
- const { data: fieldMapping, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.fieldMapping.bind(integrationApp));
195
- 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 };
196
222
  }
197
223
 
198
- function useFieldMappings(query) {
199
- 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
+ }
200
237
  return {
238
+ fieldMappingInstance,
239
+ accessor,
240
+ refresh,
241
+ setup,
242
+ reset,
243
+ openConfiguration,
201
244
  ...rest,
202
245
  };
203
246
  }
204
247
 
205
- function useFieldMappingInstance(selector) {
206
- const { data: fieldMappingInstance, ...rest } = useElement(selector, (integrationApp) => integrationApp.fieldMappingInstance.bind(integrationApp));
207
- const accessor = rest.accessor;
248
+ function useFieldMappingInstances(query) {
249
+ const { ...rest } = useElements('field-mapping-instances', query);
208
250
  return {
209
- fieldMappingInstance,
210
- setup: () => accessor.setup(),
211
- reset: () => accessor.reset(),
212
- openConfiguration: (options) => accessor.openConfiguration(options),
251
+ fieldMappingInstances: rest.items,
213
252
  ...rest,
214
253
  };
215
254
  }
216
255
 
217
- function useFieldMappingInstances(query) {
218
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.fieldMappingInstances);
256
+ function useFieldMappings(query) {
257
+ const { ...rest } = useElements('field-mappings', query);
219
258
  return {
259
+ fieldMappings: rest.items,
220
260
  ...rest,
221
261
  };
222
262
  }
223
263
 
224
- function useDataSource(idOrKey) {
225
- const { data: dataSource, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.dataSource.bind(integrationApp));
226
- 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 };
227
276
  }
228
277
 
229
- function useDataSources(query) {
230
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.dataSources);
278
+ function useDataSourceEvents(query) {
279
+ const { ...rest } = useElements('data-source-events', query);
231
280
  return {
281
+ dataSourceEvents: rest,
232
282
  ...rest,
233
283
  };
234
284
  }
235
285
 
236
286
  function useDataSourceInstance(selector) {
237
- const { data: dataSourceInstance, ...rest } = useElement(selector, (integrationApp) => integrationApp.dataSourceInstance.bind(integrationApp));
238
- 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
+ }
239
349
  return {
240
350
  dataSourceInstance,
241
- setup: () => accessor.setup(),
242
- subscribe: (eventType) => accessor.subscribe(eventType),
243
- resubscribe: (eventType) => accessor.resubscribe(eventType),
244
- unsubscribe: (eventType) => accessor.unsubscribe(eventType),
245
- pullUpdates: () => accessor.pullUpdates(),
246
- fullSync: () => accessor.fullSync(),
247
- reset: () => accessor.reset(),
248
- openConfiguration: (options) => accessor.openConfiguration(options),
249
- findRecords: (request) => accessor.findRecords(request),
250
- findRecordById: (id) => accessor.findRecordById(id),
251
- createRecord: (request) => accessor.createRecord(request),
252
- updateRecord: (request) => accessor.updateRecord(request),
253
- deleteRecord: (id) => accessor.deleteRecord(id),
254
- unifiedFieldsToNative: (unifiedFields) => accessor.unifiedFieldsToNative(unifiedFields),
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,
255
371
  ...rest,
256
372
  };
257
373
  }
258
374
 
259
- function useGetter(key, getter) {
260
- const integrationApp = useIntegrationApp();
261
- const [data, setData] = react.useState();
262
- const [loading, setLoading] = react.useState(true);
263
- const [error, setError] = react.useState(null);
264
- const [refreshCounter, setRefreshCounter] = react.useState(0);
265
- function refresh() {
266
- setRefreshCounter(refreshCounter + 1);
267
- }
268
- react.useEffect(() => {
269
- if (key !== undefined) {
270
- setLoading(true);
271
- setError(null);
272
- if (integrationApp) {
273
- getter()
274
- .then(setData)
275
- .catch(setError)
276
- .finally(() => setLoading(false));
277
- }
278
- else {
279
- setError(new Error('IntegrationApp not found. Was this component wrapped in <IntegrationAppProvider>?'));
280
- }
281
- }
282
- }, [integrationApp, key, refreshCounter]);
283
- return { data, loading, error, refresh };
284
- }
285
-
286
375
  function useDataSourceInstanceCollection(dataSourceInstance) {
287
376
  const integrationApp = useIntegrationApp();
288
- 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;
289
382
  return {
290
383
  collection,
291
- ...rest,
384
+ refresh,
385
+ error,
386
+ loading: isLoading,
292
387
  };
293
388
  }
294
389
 
295
390
  function useDataSourceInstanceLocations(dataSourceInstance, args) {
296
391
  var _a;
297
392
  const integrationApp = useIntegrationApp();
298
- const { data, ...rest } = useGetter(dataSourceInstance
299
- ? `${dataSourceInstance.id}/${JSON.stringify(args)}`
300
- : undefined, () => integrationApp
393
+ const { data, error, isLoading, mutate } = useSWR(dataSourceInstance
394
+ ? `${dataSourceInstance.id}/locations?${qs.stringify(args)}`
395
+ : null, () => integrationApp
301
396
  .dataSourceInstance(dataSourceInstance.id)
302
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 : [];
402
+ return {
403
+ locations,
404
+ refresh,
405
+ error,
406
+ loading: isLoading,
407
+ };
408
+ }
409
+
410
+ function useDataSourceInstances(query) {
411
+ const { ...rest } = useElements('data-source-instances', query);
303
412
  return {
304
- locations: (_a = data === null || data === void 0 ? void 0 : data.locations) !== null && _a !== void 0 ? _a : [],
413
+ dataSourceInstances: rest.items,
305
414
  ...rest,
306
415
  };
307
416
  }
308
417
 
309
- function useDataSourceEvents(query) {
310
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.dataSourceEvents);
418
+ function useDataSources(query) {
419
+ const { ...rest } = useElements('data-sources', query);
311
420
  return {
421
+ dataSources: rest.items,
312
422
  ...rest,
313
423
  };
314
424
  }
315
425
 
316
- function useAppEvents(query) {
317
- 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);
318
433
  return {
434
+ appEventSubscriptions: rest.items,
319
435
  ...rest,
320
436
  };
321
437
  }
322
438
 
323
- function useAppEventType(idOrKey) {
324
- 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));
325
441
  return { appEventType, ...rest };
326
442
  }
327
443
 
328
444
  function useAppEventTypes(query) {
329
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.appEventTypes);
445
+ const { ...rest } = useElements('app-event-types', query);
330
446
  return {
447
+ appEventTypes: rest.items,
331
448
  ...rest,
332
449
  };
333
450
  }
334
451
 
335
- function useAppEventSubscription(selector) {
336
- const { data: appEventSubscription, ...rest } = useElement(selector, (integrationApp) => integrationApp.appEventSubscription.bind(integrationApp));
337
- return { appEventSubscription, ...rest };
338
- }
339
-
340
- function useAppEventSubscriptions(query) {
341
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.appEventSubscriptions);
452
+ function useAppEvents(query) {
453
+ const { ...rest } = useElements('app-events', query);
342
454
  return {
455
+ appEvents: rest.items,
343
456
  ...rest,
344
457
  };
345
458
  }
346
459
 
347
- function useFlow(idOrKey) {
348
- const { data: flow, ...rest } = useElement(idOrKey, (integrationApp) => integrationApp.flow.bind(integrationApp));
349
- 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 };
350
472
  }
351
473
 
352
474
  function useFlows(query) {
353
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flows);
354
- return { ...rest };
475
+ const { ...rest } = useElements('flows', query);
476
+ return {
477
+ flows: rest.items,
478
+ ...rest,
479
+ };
355
480
  }
356
481
 
357
- function useFlowInstance(props) {
358
- const { data: flowInstance, ...rest } = useElement(props, (integrationApp) => integrationApp.flowInstance.bind(integrationApp));
359
- 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
+ };
360
522
  }
361
523
 
362
524
  function useFlowInstances(query) {
363
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flowInstances);
525
+ const { ...rest } = useElements('flow-instances', query);
364
526
  return {
527
+ flowInstances: rest.items,
365
528
  ...rest,
366
529
  };
367
530
  }
368
531
 
369
532
  function useFlowRun(id) {
370
- const { data: flowRun, ...rest } = useElement(id, (integrationApp) => integrationApp.flowRun.bind(integrationApp));
371
- 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
+ };
372
541
  }
373
542
 
374
543
  function useFlowRuns(query) {
375
- const { ...rest } = useElements(query, (integrationApp) => integrationApp.flowRuns);
376
- 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;
377
656
  }
378
657
 
379
658
  Object.defineProperty(exports, 'DataForm', {
@@ -381,6 +660,10 @@ Object.defineProperty(exports, 'DataForm', {
381
660
  get: function () { return sdk.DataForm; }
382
661
  });
383
662
  exports.IntegrationAppProvider = IntegrationAppProvider;
663
+ exports.useAction = useAction;
664
+ exports.useActionInstance = useActionInstance;
665
+ exports.useActionInstances = useActionInstances;
666
+ exports.useActions = useActions;
384
667
  exports.useAppEventSubscription = useAppEventSubscription;
385
668
  exports.useAppEventSubscriptions = useAppEventSubscriptions;
386
669
  exports.useAppEventType = useAppEventType;
@@ -389,11 +672,17 @@ exports.useAppEvents = useAppEvents;
389
672
  exports.useConnection = useConnection;
390
673
  exports.useConnections = useConnections;
391
674
  exports.useConnectorSpec = useConnectorSpec;
675
+ exports.useDataCollectionSpec = useDataCollectionSpec;
676
+ exports.useDataLinkTable = useDataLinkTable;
677
+ exports.useDataLinkTableInstance = useDataLinkTableInstance;
678
+ exports.useDataLinkTableInstances = useDataLinkTableInstances;
679
+ exports.useDataLinkTables = useDataLinkTables;
392
680
  exports.useDataSource = useDataSource;
393
- exports.useDataSourceCollection = useDataSourceInstanceCollection;
394
681
  exports.useDataSourceEvents = useDataSourceEvents;
395
682
  exports.useDataSourceInstance = useDataSourceInstance;
396
- exports.useDataSourceLocations = useDataSourceInstanceLocations;
683
+ exports.useDataSourceInstanceCollection = useDataSourceInstanceCollection;
684
+ exports.useDataSourceInstanceLocations = useDataSourceInstanceLocations;
685
+ exports.useDataSourceInstances = useDataSourceInstances;
397
686
  exports.useDataSources = useDataSources;
398
687
  exports.useFieldMapping = useFieldMapping;
399
688
  exports.useFieldMappingInstance = useFieldMappingInstance;
@@ -407,5 +696,7 @@ exports.useFlowRuns = useFlowRuns;
407
696
  exports.useFlows = useFlows;
408
697
  exports.useIntegration = useIntegration;
409
698
  exports.useIntegrationApp = useIntegrationApp;
699
+ exports.useIntegrationAppSWR = useIntegrationAppSWR;
410
700
  exports.useIntegrations = useIntegrations;
701
+ exports.useScreen = useScreen;
411
702
  //# sourceMappingURL=index.js.map