@integration-app/react 0.2.0 → 0.3.1

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