@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.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { CodeComponentMeta } from '@plasmicapp/host/registerComponent';
2
2
  import { PlasmicDataSourceContextValue } from '@plasmicapp/data-sources-context';
3
3
  import { default as React_2 } from 'react';
4
+ import { SWRResponse } from '@plasmicapp/query';
4
5
  import { usePlasmicDataConfig } from '@plasmicapp/query';
5
6
 
6
7
  export declare type BaseFieldConfig = {
@@ -164,11 +165,78 @@ export declare interface ServerQueryResult<T = any> {
164
165
  isLoading: boolean;
165
166
  }
166
167
 
168
+ /**
169
+ * Wraps a Promise so that it can be easily resolved/rejected
170
+ * outside the executor param of the Promise constructor.
171
+ */
172
+ declare class SettablePromise<T> {
173
+ readonly promise: Promise<T>;
174
+ private _resolve;
175
+ private _reject;
176
+ constructor();
177
+ resolve(value: T): void;
178
+ reject(error: unknown): void;
179
+ }
180
+
167
181
  export declare interface SingleRowResult<T = any> {
168
182
  data: T;
169
183
  schema: TableSchema;
170
184
  }
171
185
 
186
+ /** @internal */
187
+ export declare class _StatefulQueryResult<T = unknown> implements PlasmicQueryResult<T> {
188
+ current: _StatefulQueryState<T>;
189
+ settable: SettablePromise<T>;
190
+ private readonly listeners;
191
+ constructor();
192
+ private transitionState;
193
+ addListener(listener: _StateListener<T>): void;
194
+ removeListener(listener: _StateListener<T>): void;
195
+ private notifyListeners;
196
+ reset(): void;
197
+ loadingPromise(key: string, promise: Promise<T>): void;
198
+ /**
199
+ * Resolve is allowed if:
200
+ * 1) no key / state is initial, which means we are resolving from cache
201
+ * 2) key / state is loading, which means we need to check the keys match
202
+ */
203
+ resolvePromise(key: string, data: T): void;
204
+ /**
205
+ * Reject is allowed if:
206
+ * 1) no key / state is initial, which means params resolution failed
207
+ * 2) key / state is loading, which means we need to check the keys match
208
+ */
209
+ rejectPromise(key: string | null, error: unknown): void;
210
+ get key(): string | null;
211
+ get data(): T;
212
+ get isLoading(): boolean;
213
+ getDoneResult(): Promise<PlasmicQueryResult<T> & {
214
+ current: {
215
+ state: "done";
216
+ };
217
+ }>;
218
+ }
219
+
220
+ /** @internal */
221
+ export declare type _StatefulQueryState<T = unknown> = {
222
+ state: "initial";
223
+ key: null;
224
+ } | {
225
+ state: "loading";
226
+ key: string;
227
+ } | {
228
+ state: "done";
229
+ key: string;
230
+ data: T;
231
+ } | {
232
+ state: "done";
233
+ key: string | null;
234
+ error: unknown;
235
+ };
236
+
237
+ /** @internal */
238
+ export declare type _StateListener<T = unknown> = (state: _StatefulQueryState<T>, prevState: _StatefulQueryState<T>) => void;
239
+
172
240
  export declare interface TableFieldSchema {
173
241
  id: string;
174
242
  label?: string;
@@ -284,7 +352,7 @@ export declare function unstable_executePlasmicQueries<QueryName extends string>
284
352
  * return <div>{$queries.result.data}</div> // $queries.result.data may suspend
285
353
  * }
286
354
  */
287
- export declare function unstable_usePlasmicQueries<QueryName extends string>($queries: Record<QueryName, PlasmicQueryResult>, queries: Record<QueryName, PlasmicQuery>): void;
355
+ export declare function unstable_usePlasmicQueries<QueryName extends string>($queries: Record<QueryName, PlasmicQueryResult>, queries: Record<QueryName, PlasmicQuery>): Record<QueryName, ReturnType<typeof usePlasmicQuery>>;
288
356
 
289
357
  /**
290
358
  * Wraps each PlasmicQueryResult so that they return a hardcoded string for
@@ -314,18 +382,8 @@ export declare function usePlasmicDataOp<T extends SingleRowResult | ManyRowsRes
314
382
  export declare function usePlasmicInvalidate(): (invalidatedKeys: string[] | null | undefined) => Promise<any[] | undefined>;
315
383
 
316
384
  /**
317
- * @deprecated
318
- * This export will be deleted before RSC release.
319
- * TODO: Rename to usePlasmicQuery
320
- * TODO: Reference $query directly.
321
385
  * TODO: Use paramsResult from usePlasmicQueries
322
386
  */
323
- export declare function usePlasmicServerQuery<T, F extends (...args: any[]) => Promise<T>>(query: PlasmicQuery<F>, fallbackData?: T, opts?: {
324
- noUndefinedDataProxy?: boolean;
325
- settledCount?: number;
326
- onStarted?: (key: string, promise: Promise<T>) => void;
327
- onResolved?: (key: string, data: T) => void;
328
- onRejected?: (key: string | null, error: unknown) => void;
329
- }): Partial<PlasmicQueryResult<T>>;
387
+ declare function usePlasmicQuery<T, F extends (...args: any[]) => Promise<T>>($query: PlasmicQueryResult<T>, query: PlasmicQuery<F>, settledCount?: number): SWRResponse<T, unknown>;
330
388
 
331
389
  export { }
package/dist/index.esm.js CHANGED
@@ -46,6 +46,61 @@ import {
46
46
  } from "@plasmicapp/query";
47
47
  import * as React3 from "react";
48
48
 
49
+ // src/utils.ts
50
+ function noopFn() {
51
+ }
52
+ function swallow(f) {
53
+ try {
54
+ return f();
55
+ } catch (e) {
56
+ return void 0;
57
+ }
58
+ }
59
+ function pick(obj, ...keys) {
60
+ const res = {};
61
+ for (const key of keys) {
62
+ if (key in obj) {
63
+ res[key] = obj[key];
64
+ }
65
+ }
66
+ return res;
67
+ }
68
+ var tuple = (...args) => args;
69
+ function mkIdMap(xs) {
70
+ return new Map(xs.map((x) => tuple(x.id, x)));
71
+ }
72
+ function notNil(x) {
73
+ return x !== null && x !== void 0;
74
+ }
75
+ function withoutNils(xs) {
76
+ return xs.filter(notNil);
77
+ }
78
+ function mapRecordEntries(callback, record1, record2, record3) {
79
+ return Object.entries(record1).map(([k, v1]) => {
80
+ const v2 = record2 == null ? void 0 : record2[k];
81
+ const v3 = record3 == null ? void 0 : record3[k];
82
+ return callback(
83
+ k,
84
+ v1,
85
+ v2,
86
+ v3
87
+ );
88
+ });
89
+ }
90
+ function mapRecords(callback, record1, record2, record3) {
91
+ return Object.fromEntries(
92
+ mapRecordEntries(
93
+ (k, v1, v2, v3) => [k, callback(k, v1, v2, v3)],
94
+ record1,
95
+ record2,
96
+ record3
97
+ )
98
+ );
99
+ }
100
+
101
+ // src/serverQueries/common.ts
102
+ import React2 from "react";
103
+
49
104
  // src/common.ts
50
105
  import {
51
106
  useMutablePlasmicQueryData,
@@ -224,69 +279,8 @@ function usePlasmicFetch(key, resolvedParams, fetcherFn, resultMapper, undefined
224
279
  fetchAndUpdateCache
225
280
  ]);
226
281
  }
227
- function getConfig(key, defaultValue) {
228
- var _a, _b;
229
- if (typeof globalThis === "undefined") {
230
- return defaultValue;
231
- } else {
232
- return (_b = (_a = globalThis.__PLASMIC__) == null ? void 0 : _a[key]) != null ? _b : defaultValue;
233
- }
234
- }
235
-
236
- // src/utils.ts
237
- function noopFn() {
238
- }
239
- function swallow(f) {
240
- try {
241
- return f();
242
- } catch (e) {
243
- return void 0;
244
- }
245
- }
246
- function pick(obj, ...keys) {
247
- const res = {};
248
- for (const key of keys) {
249
- if (key in obj) {
250
- res[key] = obj[key];
251
- }
252
- }
253
- return res;
254
- }
255
- var tuple = (...args) => args;
256
- function mkIdMap(xs) {
257
- return new Map(xs.map((x) => tuple(x.id, x)));
258
- }
259
- function notNil(x) {
260
- return x !== null && x !== void 0;
261
- }
262
- function withoutNils(xs) {
263
- return xs.filter(notNil);
264
- }
265
- function mapRecordEntries(callback, record1, record2, record3) {
266
- return Object.entries(record1).map(([k, v1]) => {
267
- const v2 = record2 == null ? void 0 : record2[k];
268
- const v3 = record3 == null ? void 0 : record3[k];
269
- return callback(
270
- k,
271
- v1,
272
- v2,
273
- v3
274
- );
275
- });
276
- }
277
- function mapRecords(callback, record1, record2, record3) {
278
- return Object.fromEntries(
279
- mapRecordEntries(
280
- (k, v1, v2, v3) => [k, callback(k, v1, v2, v3)],
281
- record1,
282
- record2,
283
- record3
284
- )
285
- );
286
- }
287
282
 
288
283
  // src/serverQueries/common.ts
289
- import React2 from "react";
290
284
  function createDollarQueries(queryNames) {
291
285
  return Object.fromEntries(
292
286
  queryNames.map((queryName) => {
@@ -652,18 +646,9 @@ function usePlasmicQueries($queries, queries) {
652
646
  },
653
647
  [wrappedQueries, $queryStates, settledCount]
654
648
  );
655
- mapRecordEntries(
649
+ return mapRecords(
656
650
  (_queryName, $query, query) => {
657
- usePlasmicServerQuery(
658
- query,
659
- void 0,
660
- {
661
- settledCount,
662
- onStarted: $query.loadingPromise.bind($query),
663
- onResolved: $query.resolvePromise.bind($query),
664
- onRejected: $query.rejectPromise.bind($query)
665
- }
666
- );
651
+ return usePlasmicQuery($query, query, settledCount);
667
652
  },
668
653
  $queryStates,
669
654
  wrappedQueries
@@ -677,15 +662,7 @@ function wrapQueries(queries) {
677
662
  if (cached) {
678
663
  return cached.promise;
679
664
  }
680
- const wrapStudioCache = getConfig(
681
- "EXECUTE_SERVER_QUERY",
682
- (_, fn, ...args2) => fn(...args2)
683
- );
684
- const promise = wrapLoadingFetcher(wrapStudioCache)(
685
- query.id,
686
- query.fn,
687
- ...args
688
- );
665
+ const promise = wrapLoadingFetcher(query.fn)(...args);
689
666
  GLOBAL_CACHE.set(cacheKey, new SyncPromise(promise));
690
667
  return promise;
691
668
  };
@@ -748,11 +725,11 @@ function initPlasmicQueriesSync($queries, queries, prefetchedCache, clientCache)
748
725
  );
749
726
  } while (anySettled);
750
727
  }
751
- function usePlasmicServerQuery(query, fallbackData, opts) {
752
- var _a, _b;
728
+ function usePlasmicQuery($query, query, settledCount) {
729
+ const $queryState = $query;
753
730
  const paramsResult = React3.useMemo(() => {
754
731
  return resolveParams(query.execParams);
755
- }, [query.execParams, opts == null ? void 0 : opts.settledCount]);
732
+ }, [query.execParams, settledCount]);
756
733
  const { key, fetcher } = React3.useMemo(() => {
757
734
  switch (paramsResult.status) {
758
735
  case "blocked":
@@ -772,9 +749,8 @@ function usePlasmicServerQuery(query, fallbackData, opts) {
772
749
  return {
773
750
  key: cacheKey,
774
751
  fetcher: () => {
775
- var _a2;
776
752
  const promise = query.fn(...paramsResult.resolvedParams);
777
- (_a2 = opts == null ? void 0 : opts.onStarted) == null ? void 0 : _a2.call(opts, cacheKey, promise);
753
+ $queryState.loadingPromise(cacheKey, promise);
778
754
  return promise.finally(() => {
779
755
  GLOBAL_CACHE.delete(cacheKey);
780
756
  });
@@ -790,17 +766,13 @@ function usePlasmicServerQuery(query, fallbackData, opts) {
790
766
  // happens all the time -- we prepopulate the cache with proxy-invoked fetch,
791
767
  // sometimes before swr had a chance to run the effect. So we turn off
792
768
  // revalidateIfStale here, and just let the user manage invalidation.
793
- revalidateIfStale: false,
794
- // TODO: Remove per-hook fallbackData
795
- // Only used in older server query implementation.
796
- // New implementation should use prefetchedCache instead.
797
- fallbackData
769
+ revalidateIfStale: false
798
770
  });
799
771
  if (!result.isLoading) {
800
772
  if (result.error) {
801
- (_a = opts == null ? void 0 : opts.onRejected) == null ? void 0 : _a.call(opts, key, result.error);
773
+ $queryState.rejectPromise(key, result.error);
802
774
  } else if (key && result.data !== void 0) {
803
- (_b = opts == null ? void 0 : opts.onResolved) == null ? void 0 : _b.call(opts, key, result.data);
775
+ $queryState.resolvePromise(key, result.data);
804
776
  }
805
777
  }
806
778
  return result;
@@ -943,7 +915,7 @@ var DEFAULT_HOST = "https://data.plasmic.app";
943
915
  var UNAUTHORIZED_MESSAGE = "You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";
944
916
  function executePlasmicDataOp(op, opts) {
945
917
  return __async(this, null, function* () {
946
- const func = getConfig2(
918
+ const func = getConfig(
947
919
  "__PLASMIC_EXECUTE_DATA_OP",
948
920
  _executePlasmicDataOp
949
921
  );
@@ -961,7 +933,7 @@ function _executePlasmicDataOp(op, opts) {
961
933
  throw new Error(UNAUTHORIZED_MESSAGE);
962
934
  }
963
935
  }
964
- const host = getConfig2("__PLASMIC_DATA_HOST", DEFAULT_HOST);
936
+ const host = getConfig("__PLASMIC_DATA_HOST", DEFAULT_HOST);
965
937
  const url = `${host}/api/v1/server-data/sources/${op.sourceId}/execute`;
966
938
  const resp = yield fetch(url, {
967
939
  method: "POST",
@@ -983,7 +955,7 @@ function _executePlasmicDataOp(op, opts) {
983
955
  return yield resp.json();
984
956
  });
985
957
  }
986
- function getConfig2(key, defaultValue) {
958
+ function getConfig(key, defaultValue) {
987
959
  var _a;
988
960
  if (typeof globalThis === "undefined") {
989
961
  return defaultValue;
@@ -1328,6 +1300,7 @@ function useDependencyAwareQuery({
1328
1300
  export {
1329
1301
  Fetcher,
1330
1302
  FetcherMeta,
1303
+ StatefulQueryResult as _StatefulQueryResult,
1331
1304
  deriveFieldConfigs,
1332
1305
  executePlasmicDataOp,
1333
1306
  executeServerQuery,
@@ -1344,7 +1317,6 @@ export {
1344
1317
  usePlasmicDataConfig4 as usePlasmicDataConfig,
1345
1318
  usePlasmicDataMutationOp,
1346
1319
  usePlasmicDataOp,
1347
- usePlasmicInvalidate,
1348
- usePlasmicServerQuery
1320
+ usePlasmicInvalidate
1349
1321
  };
1350
1322
  //# sourceMappingURL=index.esm.js.map