@integration-app/react 0.2.1 → 0.3.2

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