@plasmicapp/data-sources 0.1.204 → 0.1.206

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 = {
@@ -79,13 +80,6 @@ declare interface ExecuteOpts {
79
80
 
80
81
  export declare function executePlasmicDataOp<T extends SingleRowResult | ManyRowsResult>(op: DataOp, opts?: ExecuteOpts): Promise<T>;
81
82
 
82
- /**
83
- * Executes a server query, returning either the result of the query or a
84
- * PlasmicUndefinedServerProxy if the query depends on data that is not yet ready
85
- * @deprecated
86
- */
87
- export declare function executeServerQuery<F extends (...args: any[]) => any>(query: PlasmicQuery<F>): Promise<ServerQueryResult<ReturnType<F>>>;
88
-
89
83
  export declare function Fetcher(props: FetcherProps): React_2.ReactElement | null;
90
84
 
91
85
  export declare const FetcherMeta: CodeComponentMeta<FetcherProps>;
@@ -111,9 +105,6 @@ export declare interface ManyRowsResult<T = any> {
111
105
  paginate?: Pagination;
112
106
  }
113
107
 
114
- /** @deprecated */
115
- export declare function mkPlasmicUndefinedServerProxy<T>(): ServerQueryResult<T>;
116
-
117
108
  export declare function normalizeData(rawData: unknown): NormalizedData | undefined;
118
109
 
119
110
  export declare interface NormalizedData {
@@ -158,10 +149,17 @@ export declare type QueryResult = Partial<ManyRowsResult<any>> & {
158
149
 
159
150
  declare type ResolvableDataOp = DataOp | undefined | null | (() => DataOp | undefined | null);
160
151
 
161
- /** @deprecated */
162
- export declare interface ServerQueryResult<T = any> {
163
- data: T;
164
- isLoading: boolean;
152
+ /**
153
+ * Wraps a Promise so that it can be easily resolved/rejected
154
+ * outside the executor param of the Promise constructor.
155
+ */
156
+ declare class SettablePromise<T> {
157
+ readonly promise: Promise<T>;
158
+ private _resolve;
159
+ private _reject;
160
+ constructor();
161
+ resolve(value: T): void;
162
+ reject(error: unknown): void;
165
163
  }
166
164
 
167
165
  export declare interface SingleRowResult<T = any> {
@@ -169,6 +167,60 @@ export declare interface SingleRowResult<T = any> {
169
167
  schema: TableSchema;
170
168
  }
171
169
 
170
+ /** @internal */
171
+ export declare class _StatefulQueryResult<T = unknown> implements PlasmicQueryResult<T> {
172
+ current: _StatefulQueryState<T>;
173
+ settable: SettablePromise<T>;
174
+ private readonly listeners;
175
+ constructor();
176
+ private transitionState;
177
+ addListener(listener: _StateListener<T>): void;
178
+ removeListener(listener: _StateListener<T>): void;
179
+ private notifyListeners;
180
+ reset(): void;
181
+ loadingPromise(key: string, promise: Promise<T>): void;
182
+ /**
183
+ * Resolve is allowed if:
184
+ * 1) no key / state is initial, which means we are resolving from cache
185
+ * 2) key / state is loading, which means we need to check the keys match
186
+ */
187
+ resolvePromise(key: string, data: T): void;
188
+ /**
189
+ * Reject is allowed if:
190
+ * 1) no key / state is initial, which means params resolution failed
191
+ * 2) key / state is loading, which means we need to check the keys match
192
+ */
193
+ rejectPromise(key: string | null, error: unknown): void;
194
+ get key(): string | null;
195
+ get data(): T;
196
+ get isLoading(): boolean;
197
+ getDoneResult(): Promise<PlasmicQueryResult<T> & {
198
+ current: {
199
+ state: "done";
200
+ };
201
+ }>;
202
+ }
203
+
204
+ /** @internal */
205
+ export declare type _StatefulQueryState<T = unknown> = {
206
+ state: "initial";
207
+ key: null;
208
+ } | {
209
+ state: "loading";
210
+ key: string;
211
+ } | {
212
+ state: "done";
213
+ key: string;
214
+ data: T;
215
+ } | {
216
+ state: "done";
217
+ key: string | null;
218
+ error: unknown;
219
+ };
220
+
221
+ /** @internal */
222
+ export declare type _StateListener<T = unknown> = (state: _StatefulQueryState<T>, prevState: _StatefulQueryState<T>) => void;
223
+
172
224
  export declare interface TableFieldSchema {
173
225
  id: string;
174
226
  label?: string;
@@ -284,7 +336,7 @@ export declare function unstable_executePlasmicQueries<QueryName extends string>
284
336
  * return <div>{$queries.result.data}</div> // $queries.result.data may suspend
285
337
  * }
286
338
  */
287
- export declare function unstable_usePlasmicQueries<QueryName extends string>($queries: Record<QueryName, PlasmicQueryResult>, queries: Record<QueryName, PlasmicQuery>): void;
339
+ export declare function unstable_usePlasmicQueries<QueryName extends string>($queries: Record<QueryName, PlasmicQueryResult>, queries: Record<QueryName, PlasmicQuery>): Record<QueryName, ReturnType<typeof usePlasmicQuery>>;
288
340
 
289
341
  /**
290
342
  * Wraps each PlasmicQueryResult so that they return a hardcoded string for
@@ -314,18 +366,8 @@ export declare function usePlasmicDataOp<T extends SingleRowResult | ManyRowsRes
314
366
  export declare function usePlasmicInvalidate(): (invalidatedKeys: string[] | null | undefined) => Promise<any[] | undefined>;
315
367
 
316
368
  /**
317
- * @deprecated
318
- * This export will be deleted before RSC release.
319
- * TODO: Rename to usePlasmicQuery
320
- * TODO: Reference $query directly.
321
369
  * TODO: Use paramsResult from usePlasmicQueries
322
370
  */
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>>;
371
+ declare function usePlasmicQuery<T, F extends (...args: any[]) => Promise<T>>($query: PlasmicQueryResult<T>, query: PlasmicQuery<F>, settledCount?: number): SWRResponse<T, unknown>;
330
372
 
331
373
  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;
@@ -857,53 +829,6 @@ function executePlasmicQuery($query, query) {
857
829
  } while (true);
858
830
  });
859
831
  }
860
- var PlasmicUndefinedServerError = class extends Error {
861
- constructor(msg) {
862
- super(msg);
863
- this.plasmicType = "PlasmicUndefinedServerError";
864
- }
865
- };
866
- function isPlasmicUndefinedServerError(x) {
867
- return !!x && typeof x === "object" && x.plasmicType === "PlasmicUndefinedServerError";
868
- }
869
- function mkPlasmicUndefinedServerProxy() {
870
- return {
871
- data: new Proxy(
872
- {},
873
- {
874
- get: (_, prop) => {
875
- if (prop === "isUndefinedServerProxy") {
876
- return true;
877
- } else if (prop === "then") {
878
- return void 0;
879
- }
880
- throw new PlasmicUndefinedServerError("Data is not available yet");
881
- }
882
- }
883
- ),
884
- isLoading: true
885
- };
886
- }
887
- function executeServerQuery(query) {
888
- return __async(this, null, function* () {
889
- const resolvedParams = resolveServerParams(query.execParams);
890
- if (isPlasmicUndefinedServerError(resolvedParams)) {
891
- return mkPlasmicUndefinedServerProxy();
892
- }
893
- return { data: yield query.fn(...resolvedParams), isLoading: false };
894
- });
895
- }
896
- function resolveServerParams(params) {
897
- try {
898
- return params();
899
- } catch (err) {
900
- if (isPlasmicUndefinedServerError(err)) {
901
- return err;
902
- } else {
903
- throw err;
904
- }
905
- }
906
- }
907
832
 
908
833
  // src/index.tsx
909
834
  import { usePlasmicDataConfig as usePlasmicDataConfig4 } from "@plasmicapp/query";
@@ -943,7 +868,7 @@ var DEFAULT_HOST = "https://data.plasmic.app";
943
868
  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
869
  function executePlasmicDataOp(op, opts) {
945
870
  return __async(this, null, function* () {
946
- const func = getConfig2(
871
+ const func = getConfig(
947
872
  "__PLASMIC_EXECUTE_DATA_OP",
948
873
  _executePlasmicDataOp
949
874
  );
@@ -961,7 +886,7 @@ function _executePlasmicDataOp(op, opts) {
961
886
  throw new Error(UNAUTHORIZED_MESSAGE);
962
887
  }
963
888
  }
964
- const host = getConfig2("__PLASMIC_DATA_HOST", DEFAULT_HOST);
889
+ const host = getConfig("__PLASMIC_DATA_HOST", DEFAULT_HOST);
965
890
  const url = `${host}/api/v1/server-data/sources/${op.sourceId}/execute`;
966
891
  const resp = yield fetch(url, {
967
892
  method: "POST",
@@ -983,7 +908,7 @@ function _executePlasmicDataOp(op, opts) {
983
908
  return yield resp.json();
984
909
  });
985
910
  }
986
- function getConfig2(key, defaultValue) {
911
+ function getConfig(key, defaultValue) {
987
912
  var _a;
988
913
  if (typeof globalThis === "undefined") {
989
914
  return defaultValue;
@@ -1328,12 +1253,11 @@ function useDependencyAwareQuery({
1328
1253
  export {
1329
1254
  Fetcher,
1330
1255
  FetcherMeta,
1256
+ StatefulQueryResult as _StatefulQueryResult,
1331
1257
  deriveFieldConfigs,
1332
1258
  executePlasmicDataOp,
1333
- executeServerQuery,
1334
1259
  makeCacheKey,
1335
1260
  makeQueryCacheKey,
1336
- mkPlasmicUndefinedServerProxy,
1337
1261
  normalizeData,
1338
1262
  createDollarQueries as unstable_createDollarQueries,
1339
1263
  executePlasmicQueries as unstable_executePlasmicQueries,
@@ -1344,7 +1268,6 @@ export {
1344
1268
  usePlasmicDataConfig4 as usePlasmicDataConfig,
1345
1269
  usePlasmicDataMutationOp,
1346
1270
  usePlasmicDataOp,
1347
- usePlasmicInvalidate,
1348
- usePlasmicServerQuery
1271
+ usePlasmicInvalidate
1349
1272
  };
1350
1273
  //# sourceMappingURL=index.esm.js.map