@plasmicapp/data-sources 0.1.204 → 0.1.205

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.
package/dist/index.js CHANGED
@@ -69,6 +69,7 @@ var src_exports = {};
69
69
  __export(src_exports, {
70
70
  Fetcher: () => Fetcher,
71
71
  FetcherMeta: () => FetcherMeta,
72
+ _StatefulQueryResult: () => StatefulQueryResult,
72
73
  deriveFieldConfigs: () => deriveFieldConfigs,
73
74
  executePlasmicDataOp: () => executePlasmicDataOp,
74
75
  executeServerQuery: () => executeServerQuery,
@@ -85,8 +86,7 @@ __export(src_exports, {
85
86
  usePlasmicDataConfig: () => import_query5.usePlasmicDataConfig,
86
87
  usePlasmicDataMutationOp: () => usePlasmicDataMutationOp,
87
88
  usePlasmicDataOp: () => usePlasmicDataOp,
88
- usePlasmicInvalidate: () => usePlasmicInvalidate,
89
- usePlasmicServerQuery: () => usePlasmicServerQuery
89
+ usePlasmicInvalidate: () => usePlasmicInvalidate
90
90
  });
91
91
  module.exports = __toCommonJS(src_exports);
92
92
 
@@ -94,6 +94,61 @@ module.exports = __toCommonJS(src_exports);
94
94
  var import_query2 = require("@plasmicapp/query");
95
95
  var React3 = __toESM(require("react"));
96
96
 
97
+ // src/utils.ts
98
+ function noopFn() {
99
+ }
100
+ function swallow(f) {
101
+ try {
102
+ return f();
103
+ } catch (e) {
104
+ return void 0;
105
+ }
106
+ }
107
+ function pick(obj, ...keys) {
108
+ const res = {};
109
+ for (const key of keys) {
110
+ if (key in obj) {
111
+ res[key] = obj[key];
112
+ }
113
+ }
114
+ return res;
115
+ }
116
+ var tuple = (...args) => args;
117
+ function mkIdMap(xs) {
118
+ return new Map(xs.map((x) => tuple(x.id, x)));
119
+ }
120
+ function notNil(x) {
121
+ return x !== null && x !== void 0;
122
+ }
123
+ function withoutNils(xs) {
124
+ return xs.filter(notNil);
125
+ }
126
+ function mapRecordEntries(callback, record1, record2, record3) {
127
+ return Object.entries(record1).map(([k, v1]) => {
128
+ const v2 = record2 == null ? void 0 : record2[k];
129
+ const v3 = record3 == null ? void 0 : record3[k];
130
+ return callback(
131
+ k,
132
+ v1,
133
+ v2,
134
+ v3
135
+ );
136
+ });
137
+ }
138
+ function mapRecords(callback, record1, record2, record3) {
139
+ return Object.fromEntries(
140
+ mapRecordEntries(
141
+ (k, v1, v2, v3) => [k, callback(k, v1, v2, v3)],
142
+ record1,
143
+ record2,
144
+ record3
145
+ )
146
+ );
147
+ }
148
+
149
+ // src/serverQueries/common.ts
150
+ var import_react2 = __toESM(require("react"));
151
+
97
152
  // src/common.ts
98
153
  var import_query = require("@plasmicapp/query");
99
154
  var ph = __toESM(require("@plasmicapp/host"));
@@ -269,69 +324,8 @@ function usePlasmicFetch(key, resolvedParams, fetcherFn, resultMapper, undefined
269
324
  fetchAndUpdateCache
270
325
  ]);
271
326
  }
272
- function getConfig(key, defaultValue) {
273
- var _a, _b;
274
- if (typeof globalThis === "undefined") {
275
- return defaultValue;
276
- } else {
277
- return (_b = (_a = globalThis.__PLASMIC__) == null ? void 0 : _a[key]) != null ? _b : defaultValue;
278
- }
279
- }
280
-
281
- // src/utils.ts
282
- function noopFn() {
283
- }
284
- function swallow(f) {
285
- try {
286
- return f();
287
- } catch (e) {
288
- return void 0;
289
- }
290
- }
291
- function pick(obj, ...keys) {
292
- const res = {};
293
- for (const key of keys) {
294
- if (key in obj) {
295
- res[key] = obj[key];
296
- }
297
- }
298
- return res;
299
- }
300
- var tuple = (...args) => args;
301
- function mkIdMap(xs) {
302
- return new Map(xs.map((x) => tuple(x.id, x)));
303
- }
304
- function notNil(x) {
305
- return x !== null && x !== void 0;
306
- }
307
- function withoutNils(xs) {
308
- return xs.filter(notNil);
309
- }
310
- function mapRecordEntries(callback, record1, record2, record3) {
311
- return Object.entries(record1).map(([k, v1]) => {
312
- const v2 = record2 == null ? void 0 : record2[k];
313
- const v3 = record3 == null ? void 0 : record3[k];
314
- return callback(
315
- k,
316
- v1,
317
- v2,
318
- v3
319
- );
320
- });
321
- }
322
- function mapRecords(callback, record1, record2, record3) {
323
- return Object.fromEntries(
324
- mapRecordEntries(
325
- (k, v1, v2, v3) => [k, callback(k, v1, v2, v3)],
326
- record1,
327
- record2,
328
- record3
329
- )
330
- );
331
- }
332
327
 
333
328
  // src/serverQueries/common.ts
334
- var import_react2 = __toESM(require("react"));
335
329
  function createDollarQueries(queryNames) {
336
330
  return Object.fromEntries(
337
331
  queryNames.map((queryName) => {
@@ -697,18 +691,9 @@ function usePlasmicQueries($queries, queries) {
697
691
  },
698
692
  [wrappedQueries, $queryStates, settledCount]
699
693
  );
700
- mapRecordEntries(
694
+ return mapRecords(
701
695
  (_queryName, $query, query) => {
702
- usePlasmicServerQuery(
703
- query,
704
- void 0,
705
- {
706
- settledCount,
707
- onStarted: $query.loadingPromise.bind($query),
708
- onResolved: $query.resolvePromise.bind($query),
709
- onRejected: $query.rejectPromise.bind($query)
710
- }
711
- );
696
+ return usePlasmicQuery($query, query, settledCount);
712
697
  },
713
698
  $queryStates,
714
699
  wrappedQueries
@@ -722,15 +707,7 @@ function wrapQueries(queries) {
722
707
  if (cached) {
723
708
  return cached.promise;
724
709
  }
725
- const wrapStudioCache = getConfig(
726
- "EXECUTE_SERVER_QUERY",
727
- (_, fn, ...args2) => fn(...args2)
728
- );
729
- const promise = (0, import_query2.wrapLoadingFetcher)(wrapStudioCache)(
730
- query.id,
731
- query.fn,
732
- ...args
733
- );
710
+ const promise = (0, import_query2.wrapLoadingFetcher)(query.fn)(...args);
734
711
  GLOBAL_CACHE.set(cacheKey, new SyncPromise(promise));
735
712
  return promise;
736
713
  };
@@ -793,11 +770,11 @@ function initPlasmicQueriesSync($queries, queries, prefetchedCache, clientCache)
793
770
  );
794
771
  } while (anySettled);
795
772
  }
796
- function usePlasmicServerQuery(query, fallbackData, opts) {
797
- var _a, _b;
773
+ function usePlasmicQuery($query, query, settledCount) {
774
+ const $queryState = $query;
798
775
  const paramsResult = React3.useMemo(() => {
799
776
  return resolveParams(query.execParams);
800
- }, [query.execParams, opts == null ? void 0 : opts.settledCount]);
777
+ }, [query.execParams, settledCount]);
801
778
  const { key, fetcher } = React3.useMemo(() => {
802
779
  switch (paramsResult.status) {
803
780
  case "blocked":
@@ -817,9 +794,8 @@ function usePlasmicServerQuery(query, fallbackData, opts) {
817
794
  return {
818
795
  key: cacheKey,
819
796
  fetcher: () => {
820
- var _a2;
821
797
  const promise = query.fn(...paramsResult.resolvedParams);
822
- (_a2 = opts == null ? void 0 : opts.onStarted) == null ? void 0 : _a2.call(opts, cacheKey, promise);
798
+ $queryState.loadingPromise(cacheKey, promise);
823
799
  return promise.finally(() => {
824
800
  GLOBAL_CACHE.delete(cacheKey);
825
801
  });
@@ -835,17 +811,13 @@ function usePlasmicServerQuery(query, fallbackData, opts) {
835
811
  // happens all the time -- we prepopulate the cache with proxy-invoked fetch,
836
812
  // sometimes before swr had a chance to run the effect. So we turn off
837
813
  // revalidateIfStale here, and just let the user manage invalidation.
838
- revalidateIfStale: false,
839
- // TODO: Remove per-hook fallbackData
840
- // Only used in older server query implementation.
841
- // New implementation should use prefetchedCache instead.
842
- fallbackData
814
+ revalidateIfStale: false
843
815
  });
844
816
  if (!result.isLoading) {
845
817
  if (result.error) {
846
- (_a = opts == null ? void 0 : opts.onRejected) == null ? void 0 : _a.call(opts, key, result.error);
818
+ $queryState.rejectPromise(key, result.error);
847
819
  } else if (key && result.data !== void 0) {
848
- (_b = opts == null ? void 0 : opts.onResolved) == null ? void 0 : _b.call(opts, key, result.data);
820
+ $queryState.resolvePromise(key, result.data);
849
821
  }
850
822
  }
851
823
  return result;
@@ -986,7 +958,7 @@ var DEFAULT_HOST = "https://data.plasmic.app";
986
958
  var UNAUTHORIZED_MESSAGE = "You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";
987
959
  function executePlasmicDataOp(op, opts) {
988
960
  return __async(this, null, function* () {
989
- const func = getConfig2(
961
+ const func = getConfig(
990
962
  "__PLASMIC_EXECUTE_DATA_OP",
991
963
  _executePlasmicDataOp
992
964
  );
@@ -1004,7 +976,7 @@ function _executePlasmicDataOp(op, opts) {
1004
976
  throw new Error(UNAUTHORIZED_MESSAGE);
1005
977
  }
1006
978
  }
1007
- const host = getConfig2("__PLASMIC_DATA_HOST", DEFAULT_HOST);
979
+ const host = getConfig("__PLASMIC_DATA_HOST", DEFAULT_HOST);
1008
980
  const url = `${host}/api/v1/server-data/sources/${op.sourceId}/execute`;
1009
981
  const resp = yield (0, import_isomorphic_unfetch.default)(url, {
1010
982
  method: "POST",
@@ -1026,7 +998,7 @@ function _executePlasmicDataOp(op, opts) {
1026
998
  return yield resp.json();
1027
999
  });
1028
1000
  }
1029
- function getConfig2(key, defaultValue) {
1001
+ function getConfig(key, defaultValue) {
1030
1002
  var _a;
1031
1003
  if (typeof globalThis === "undefined") {
1032
1004
  return defaultValue;