@plasmicapp/data-sources 0.1.170 → 0.1.172
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 +41 -6
- package/dist/index.esm.js +248 -186
- package/dist/index.esm.js.map +4 -4
- package/dist/index.js +248 -188
- package/dist/index.js.map +4 -4
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,39 @@ export declare type BaseFieldConfig = {
|
|
|
8
8
|
fieldId?: string;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Represents the result of a client-side query.
|
|
13
|
+
*/
|
|
14
|
+
export declare interface ClientQueryResult<T = any> {
|
|
15
|
+
/**
|
|
16
|
+
* The data returned by the query. May be undefined if the query has not yet completed.
|
|
17
|
+
*/
|
|
18
|
+
data?: T;
|
|
19
|
+
/**
|
|
20
|
+
* The schema of the table from which the data was queried. Only available for plasmic data
|
|
21
|
+
* integration. May be undefined if the query has not yet completed.
|
|
22
|
+
*/
|
|
23
|
+
schema?: TableSchema;
|
|
24
|
+
/**
|
|
25
|
+
* The total number of records available. Only available for plasmic data integration. May be
|
|
26
|
+
* undefined if the query has not yet completed.
|
|
27
|
+
*/
|
|
28
|
+
total?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Pagination information for the query result. Only available for plasmic data integration. May be
|
|
31
|
+
* undefined if the query has not yet completed.
|
|
32
|
+
*/
|
|
33
|
+
paginate?: Pagination;
|
|
34
|
+
/**
|
|
35
|
+
* Any error that occurred during the query. This is optional and may be undefined.
|
|
36
|
+
*/
|
|
37
|
+
error?: any;
|
|
38
|
+
/**
|
|
39
|
+
* Indicates whether the query is currently loading.
|
|
40
|
+
*/
|
|
41
|
+
isLoading?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
11
44
|
export declare interface DataOp {
|
|
12
45
|
sourceId: string;
|
|
13
46
|
opId: string;
|
|
@@ -50,7 +83,7 @@ export declare function executePlasmicDataOp<T extends SingleRowResult | ManyRow
|
|
|
50
83
|
* Executes a server query, returning either the result of the query or a
|
|
51
84
|
* PlasmicUndefinedServerProxy if the query depends on data that is not yet ready
|
|
52
85
|
*/
|
|
53
|
-
export declare function executeServerQuery<F extends (...args: any[]) => any>(serverQuery: ServerQuery<F>): Promise<ServerQueryResult<ReturnType<F
|
|
86
|
+
export declare function executeServerQuery<F extends (...args: any[]) => any>(serverQuery: ServerQuery<F>): Promise<ServerQueryResult<ReturnType<F> | {}>>;
|
|
54
87
|
|
|
55
88
|
export declare function Fetcher(props: FetcherProps): React_2.ReactElement | null;
|
|
56
89
|
|
|
@@ -98,9 +131,10 @@ export declare type QueryResult = Partial<ManyRowsResult<any>> & {
|
|
|
98
131
|
|
|
99
132
|
declare type ResolvableDataOp = DataOp | undefined | null | (() => DataOp | undefined | null);
|
|
100
133
|
|
|
101
|
-
export declare interface ServerQuery<F extends (...args: any[]) => any
|
|
134
|
+
export declare interface ServerQuery<F extends (...args: any[]) => Promise<any>> {
|
|
102
135
|
fn: F;
|
|
103
136
|
execParams: () => Parameters<F>;
|
|
137
|
+
id: string;
|
|
104
138
|
}
|
|
105
139
|
|
|
106
140
|
export declare interface ServerQueryResult<T = any> {
|
|
@@ -145,14 +179,15 @@ export declare function usePlasmicDataMutationOp<T extends SingleRowResult | Man
|
|
|
145
179
|
export declare function usePlasmicDataOp<T extends SingleRowResult | ManyRowsResult, E = any>(dataOp: ResolvableDataOp, opts?: {
|
|
146
180
|
paginate?: Pagination;
|
|
147
181
|
noUndefinedDataProxy?: boolean;
|
|
148
|
-
}):
|
|
149
|
-
error?: E;
|
|
150
|
-
isLoading?: boolean;
|
|
151
|
-
};
|
|
182
|
+
}): ClientQueryResult<T["data"]>;
|
|
152
183
|
|
|
153
184
|
/**
|
|
154
185
|
* Returns a function that can be used to invalidate Plasmic query groups.
|
|
155
186
|
*/
|
|
156
187
|
export declare function usePlasmicInvalidate(): (invalidatedKeys: string[] | null | undefined) => Promise<any[] | undefined>;
|
|
157
188
|
|
|
189
|
+
export declare function usePlasmicServerQuery<F extends (...args: any[]) => any>(serverQuery: ServerQuery<F>, fallbackData?: ReturnType<F>, opts?: {
|
|
190
|
+
noUndefinedDataProxy?: boolean;
|
|
191
|
+
}): Partial<ServerQueryResult<ReturnType<F>>>;
|
|
192
|
+
|
|
158
193
|
export { }
|
package/dist/index.esm.js
CHANGED
|
@@ -39,19 +39,188 @@ var __async = (__this, __arguments, generator) => {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
// src/index.tsx
|
|
42
|
-
import { usePlasmicDataConfig as
|
|
42
|
+
import { usePlasmicDataConfig as usePlasmicDataConfig3 } from "@plasmicapp/query";
|
|
43
43
|
|
|
44
44
|
// src/components/Fetcher.tsx
|
|
45
|
-
import
|
|
45
|
+
import React3 from "react";
|
|
46
46
|
|
|
47
47
|
// src/hooks/usePlasmicDataOp.tsx
|
|
48
48
|
import { usePlasmicDataSourceContext } from "@plasmicapp/data-sources-context";
|
|
49
|
-
import
|
|
49
|
+
import {
|
|
50
|
+
usePlasmicDataConfig as usePlasmicDataConfig2
|
|
51
|
+
} from "@plasmicapp/query";
|
|
52
|
+
import * as React2 from "react";
|
|
53
|
+
|
|
54
|
+
// src/common.ts
|
|
50
55
|
import {
|
|
51
56
|
useMutablePlasmicQueryData,
|
|
52
57
|
usePlasmicDataConfig
|
|
53
58
|
} from "@plasmicapp/query";
|
|
54
|
-
import * as
|
|
59
|
+
import * as ph from "@plasmicapp/host";
|
|
60
|
+
import React from "react";
|
|
61
|
+
function isPlasmicUndefinedDataErrorPromise(x) {
|
|
62
|
+
return !!x && typeof x === "object" && (x == null ? void 0 : x.plasmicType) === "PlasmicUndefinedDataError";
|
|
63
|
+
}
|
|
64
|
+
function mkUndefinedDataProxy(promiseRef, fetchAndUpdateCache) {
|
|
65
|
+
let fetchAndUpdatePromise = void 0;
|
|
66
|
+
return new Proxy(
|
|
67
|
+
{},
|
|
68
|
+
{
|
|
69
|
+
get: (_target, prop) => {
|
|
70
|
+
if (prop === "isPlasmicUndefinedDataProxy") {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
if (!fetchAndUpdateCache) {
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
const doFetchAndUpdate = () => {
|
|
77
|
+
if (!fetchAndUpdatePromise) {
|
|
78
|
+
fetchAndUpdatePromise = fetchAndUpdateCache().finally(() => {
|
|
79
|
+
fetchAndUpdatePromise = void 0;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return fetchAndUpdatePromise;
|
|
83
|
+
};
|
|
84
|
+
const promise = (
|
|
85
|
+
// existing fetch
|
|
86
|
+
promiseRef.fetchingPromise || // No existing fetch, so kick off a fetch
|
|
87
|
+
doFetchAndUpdate()
|
|
88
|
+
);
|
|
89
|
+
promise.plasmicType = "PlasmicUndefinedDataError";
|
|
90
|
+
promise.message = `Cannot read property ${String(
|
|
91
|
+
prop
|
|
92
|
+
)} - data is still loading`;
|
|
93
|
+
throw promise;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
var isRSC = React.isRSC;
|
|
99
|
+
var reactMajorVersion = +React.version.split(".")[0];
|
|
100
|
+
var enableLoadingBoundaryKey = "plasmicInternalEnableLoadingBoundary";
|
|
101
|
+
var PRE_FETCHES = /* @__PURE__ */ new Map();
|
|
102
|
+
function usePlasmicFetch(key, resolvedParams, fetcherFn, resultMapper, undefinedDataProxyFields, opts) {
|
|
103
|
+
var _a, _b;
|
|
104
|
+
const enableLoadingBoundary = !!((_b = (_a = ph.useDataEnv) == null ? void 0 : _a()) == null ? void 0 : _b[enableLoadingBoundaryKey]);
|
|
105
|
+
const { mutate, cache } = isRSC ? {} : usePlasmicDataConfig();
|
|
106
|
+
const isNullParams = !resolvedParams;
|
|
107
|
+
const isWaitingOnDependentQuery = isPlasmicUndefinedDataErrorPromise(resolvedParams);
|
|
108
|
+
const fetchingData = React.useMemo(
|
|
109
|
+
() => ({
|
|
110
|
+
fetchingPromise: void 0
|
|
111
|
+
}),
|
|
112
|
+
[key]
|
|
113
|
+
);
|
|
114
|
+
const fetcher = React.useMemo(
|
|
115
|
+
() => () => {
|
|
116
|
+
if (!key) {
|
|
117
|
+
throw new Error(`Fetcher should never be called without a proper key`);
|
|
118
|
+
}
|
|
119
|
+
if (fetchingData.fetchingPromise) {
|
|
120
|
+
return fetchingData.fetchingPromise;
|
|
121
|
+
}
|
|
122
|
+
if (key && PRE_FETCHES.has(key)) {
|
|
123
|
+
const existing = PRE_FETCHES.get(key);
|
|
124
|
+
fetchingData.fetchingPromise = existing;
|
|
125
|
+
return existing;
|
|
126
|
+
}
|
|
127
|
+
const fetcherPromise = fetcherFn(resolvedParams);
|
|
128
|
+
fetchingData.fetchingPromise = fetcherPromise;
|
|
129
|
+
if (key) {
|
|
130
|
+
PRE_FETCHES.set(key, fetcherPromise);
|
|
131
|
+
fetcherPromise.then(
|
|
132
|
+
() => {
|
|
133
|
+
PRE_FETCHES.delete(key);
|
|
134
|
+
},
|
|
135
|
+
() => {
|
|
136
|
+
PRE_FETCHES.delete(key);
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return fetcherPromise;
|
|
141
|
+
},
|
|
142
|
+
[key, fetchingData]
|
|
143
|
+
);
|
|
144
|
+
const dependentKeyDataErrorPromise = isPlasmicUndefinedDataErrorPromise(
|
|
145
|
+
resolvedParams
|
|
146
|
+
) ? resolvedParams : void 0;
|
|
147
|
+
const fetchAndUpdateCache = React.useMemo(() => {
|
|
148
|
+
if (!key && !dependentKeyDataErrorPromise) {
|
|
149
|
+
return void 0;
|
|
150
|
+
}
|
|
151
|
+
return () => {
|
|
152
|
+
if (fetchingData.fetchingPromise) {
|
|
153
|
+
return fetchingData.fetchingPromise;
|
|
154
|
+
}
|
|
155
|
+
if (dependentKeyDataErrorPromise) {
|
|
156
|
+
return dependentKeyDataErrorPromise;
|
|
157
|
+
}
|
|
158
|
+
if (!key) {
|
|
159
|
+
throw new Error(`Expected key to be non-null`);
|
|
160
|
+
}
|
|
161
|
+
const cached = cache == null ? void 0 : cache.get(key);
|
|
162
|
+
if (cached) {
|
|
163
|
+
return Promise.resolve(cached);
|
|
164
|
+
}
|
|
165
|
+
const cachedError = cache == null ? void 0 : cache.get(`$swr$${key}`);
|
|
166
|
+
if (cachedError) {
|
|
167
|
+
return Promise.reject(cachedError.error);
|
|
168
|
+
}
|
|
169
|
+
const fetcherPromise = new Promise((resolve, reject) => {
|
|
170
|
+
setTimeout(() => {
|
|
171
|
+
fetcher().then(resolve, reject);
|
|
172
|
+
}, 1);
|
|
173
|
+
});
|
|
174
|
+
if (!isRSC) {
|
|
175
|
+
fetcherPromise.then((data2) => {
|
|
176
|
+
mutate == null ? void 0 : mutate(key, data2);
|
|
177
|
+
}).catch((err) => {
|
|
178
|
+
var _a2;
|
|
179
|
+
const keyInfo = key ? "$swr$" + key : "";
|
|
180
|
+
cache == null ? void 0 : cache.set(keyInfo, __spreadProps(__spreadValues({}, (_a2 = cache == null ? void 0 : cache.get(keyInfo)) != null ? _a2 : {}), { error: err }));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return fetcherPromise;
|
|
184
|
+
};
|
|
185
|
+
}, [fetcher, fetchingData, cache, key, dependentKeyDataErrorPromise]);
|
|
186
|
+
const res = useMutablePlasmicQueryData(key, fetcher, {
|
|
187
|
+
fallbackData: opts == null ? void 0 : opts.fallbackData,
|
|
188
|
+
shouldRetryOnError: false,
|
|
189
|
+
// If revalidateIfStale is true, then if there's a cache entry with a key,
|
|
190
|
+
// but no mounted hook with that key yet, and when the hook mounts with the key,
|
|
191
|
+
// swr will revalidate. This may be reasonable behavior, but for us, this
|
|
192
|
+
// happens all the time -- we prepopulate the cache with proxy-invoked fetch,
|
|
193
|
+
// sometimes before swr had a chance to run the effect. So we turn off
|
|
194
|
+
// revalidateIfStale here, and just let the user manage invalidation.
|
|
195
|
+
revalidateIfStale: false
|
|
196
|
+
});
|
|
197
|
+
const { data, error, isLoading } = res;
|
|
198
|
+
if (fetchingData.fetchingPromise != null && data !== void 0) {
|
|
199
|
+
fetchingData.fetchingPromise = void 0;
|
|
200
|
+
}
|
|
201
|
+
return React.useMemo(() => {
|
|
202
|
+
const result = resultMapper(res);
|
|
203
|
+
if (!(opts == null ? void 0 : opts.noUndefinedDataProxy) && reactMajorVersion >= 18 && enableLoadingBoundary && (isLoading || isNullParams || isWaitingOnDependentQuery) && undefinedDataProxyFields.every((field) => result[field] === void 0)) {
|
|
204
|
+
undefinedDataProxyFields.forEach((field) => {
|
|
205
|
+
if (field === "error") {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
result[field] = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return result;
|
|
212
|
+
}, [
|
|
213
|
+
isNullParams,
|
|
214
|
+
isWaitingOnDependentQuery,
|
|
215
|
+
data,
|
|
216
|
+
error,
|
|
217
|
+
isLoading,
|
|
218
|
+
opts == null ? void 0 : opts.noUndefinedDataProxy,
|
|
219
|
+
enableLoadingBoundary,
|
|
220
|
+
fetchingData,
|
|
221
|
+
fetchAndUpdateCache
|
|
222
|
+
]);
|
|
223
|
+
}
|
|
55
224
|
|
|
56
225
|
// src/executor.tsx
|
|
57
226
|
import fetch from "@plasmicapp/isomorphic-unfetch";
|
|
@@ -153,7 +322,6 @@ function withoutNils(xs) {
|
|
|
153
322
|
}
|
|
154
323
|
|
|
155
324
|
// src/hooks/usePlasmicDataOp.tsx
|
|
156
|
-
var isRSC2 = React.isRSC;
|
|
157
325
|
function makeCacheKey(dataOp, opts) {
|
|
158
326
|
const queryDependencies = JSON.stringify({
|
|
159
327
|
sourceId: dataOp.sourceId,
|
|
@@ -165,7 +333,7 @@ function makeCacheKey(dataOp, opts) {
|
|
|
165
333
|
return dataOp.cacheKey ? `${dataOp.cacheKey}${queryDependencies}` : queryDependencies;
|
|
166
334
|
}
|
|
167
335
|
function usePlasmicInvalidate() {
|
|
168
|
-
const { cache, fallback, mutate } =
|
|
336
|
+
const { cache, fallback, mutate } = usePlasmicDataConfig2();
|
|
169
337
|
return (invalidatedKeys) => __async(this, null, function* () {
|
|
170
338
|
const getKeysToInvalidate = () => {
|
|
171
339
|
var _a, _b;
|
|
@@ -214,45 +382,6 @@ function usePlasmicInvalidate() {
|
|
|
214
382
|
return yield Promise.all(keys.map((key) => invalidateKey(key)));
|
|
215
383
|
});
|
|
216
384
|
}
|
|
217
|
-
var enableLoadingBoundaryKey = "plasmicInternalEnableLoadingBoundary";
|
|
218
|
-
function mkUndefinedDataProxy(promiseRef, fetchAndUpdateCache) {
|
|
219
|
-
let fetchAndUpdatePromise = void 0;
|
|
220
|
-
return new Proxy(
|
|
221
|
-
{},
|
|
222
|
-
{
|
|
223
|
-
get: (_target, prop) => {
|
|
224
|
-
if (prop === "isPlasmicUndefinedDataProxy") {
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
if (!fetchAndUpdateCache) {
|
|
228
|
-
return void 0;
|
|
229
|
-
}
|
|
230
|
-
const doFetchAndUpdate = () => {
|
|
231
|
-
if (!fetchAndUpdatePromise) {
|
|
232
|
-
fetchAndUpdatePromise = fetchAndUpdateCache().finally(() => {
|
|
233
|
-
fetchAndUpdatePromise = void 0;
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
return fetchAndUpdatePromise;
|
|
237
|
-
};
|
|
238
|
-
const promise = (
|
|
239
|
-
// existing fetch
|
|
240
|
-
promiseRef.fetchingPromise || // No existing fetch, so kick off a fetch
|
|
241
|
-
doFetchAndUpdate()
|
|
242
|
-
);
|
|
243
|
-
promise.plasmicType = "PlasmicUndefinedDataError";
|
|
244
|
-
promise.message = `Cannot read property ${String(
|
|
245
|
-
prop
|
|
246
|
-
)} - data is still loading`;
|
|
247
|
-
throw promise;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
function isPlasmicUndefinedDataErrorPromise(x) {
|
|
253
|
-
return !!x && typeof x === "object" && x.plasmicType === "PlasmicUndefinedDataError";
|
|
254
|
-
}
|
|
255
|
-
var reactMajorVersion = +React.version.split(".")[0];
|
|
256
385
|
function resolveDataOp(dataOp) {
|
|
257
386
|
if (typeof dataOp === "function") {
|
|
258
387
|
try {
|
|
@@ -267,138 +396,39 @@ function resolveDataOp(dataOp) {
|
|
|
267
396
|
return dataOp;
|
|
268
397
|
}
|
|
269
398
|
}
|
|
270
|
-
var PRE_FETCHES = /* @__PURE__ */ new Map();
|
|
271
399
|
function usePlasmicDataOp(dataOp, opts) {
|
|
272
|
-
var _a, _b;
|
|
273
400
|
const resolvedDataOp = resolveDataOp(dataOp);
|
|
274
401
|
const ctx = usePlasmicDataSourceContext();
|
|
275
|
-
const enableLoadingBoundary = !!((_b = (_a = ph.useDataEnv) == null ? void 0 : _a()) == null ? void 0 : _b[enableLoadingBoundaryKey]);
|
|
276
|
-
const { mutate, cache } = isRSC2 ? {} : usePlasmicDataConfig();
|
|
277
|
-
const isNullDataOp = !resolvedDataOp;
|
|
278
|
-
const isWaitingOnDependentQuery = isPlasmicUndefinedDataErrorPromise(resolvedDataOp);
|
|
279
402
|
const key = !resolvedDataOp || isPlasmicUndefinedDataErrorPromise(resolvedDataOp) ? null : makeCacheKey(resolvedDataOp, {
|
|
280
403
|
paginate: opts == null ? void 0 : opts.paginate,
|
|
281
404
|
userAuthToken: ctx == null ? void 0 : ctx.userAuthToken
|
|
282
405
|
});
|
|
283
|
-
const
|
|
284
|
-
(
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
const fetcherFn = () => executePlasmicDataOp(resolvedDataOp, {
|
|
303
|
-
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
304
|
-
user: ctx == null ? void 0 : ctx.user,
|
|
305
|
-
paginate: opts == null ? void 0 : opts.paginate
|
|
306
|
-
});
|
|
307
|
-
const fetcherPromise = fetcherFn();
|
|
308
|
-
fetchingData.fetchingPromise = fetcherPromise;
|
|
309
|
-
if (key) {
|
|
310
|
-
PRE_FETCHES.set(key, fetcherPromise);
|
|
311
|
-
fetcherPromise.then(
|
|
312
|
-
() => {
|
|
313
|
-
PRE_FETCHES.delete(key);
|
|
314
|
-
},
|
|
315
|
-
() => {
|
|
316
|
-
PRE_FETCHES.delete(key);
|
|
317
|
-
}
|
|
318
|
-
);
|
|
319
|
-
}
|
|
320
|
-
return fetcherPromise;
|
|
321
|
-
},
|
|
322
|
-
[key, fetchingData]
|
|
323
|
-
);
|
|
324
|
-
const dependentKeyDataErrorPromise = isPlasmicUndefinedDataErrorPromise(
|
|
325
|
-
resolvedDataOp
|
|
326
|
-
) ? resolvedDataOp : void 0;
|
|
327
|
-
const fetchAndUpdateCache = React.useMemo(() => {
|
|
328
|
-
if (!key && !dependentKeyDataErrorPromise) {
|
|
329
|
-
return void 0;
|
|
330
|
-
}
|
|
331
|
-
return () => {
|
|
332
|
-
if (fetchingData.fetchingPromise) {
|
|
333
|
-
return fetchingData.fetchingPromise;
|
|
334
|
-
}
|
|
335
|
-
if (dependentKeyDataErrorPromise) {
|
|
336
|
-
return dependentKeyDataErrorPromise;
|
|
337
|
-
}
|
|
338
|
-
if (!key) {
|
|
339
|
-
throw new Error(`Expected key to be non-null`);
|
|
340
|
-
}
|
|
341
|
-
const cached = cache == null ? void 0 : cache.get(key);
|
|
342
|
-
if (cached) {
|
|
343
|
-
return Promise.resolve(cached);
|
|
344
|
-
}
|
|
345
|
-
const cachedError = cache == null ? void 0 : cache.get(`$swr$${key}`);
|
|
346
|
-
if (cachedError) {
|
|
347
|
-
return Promise.reject(cachedError.error);
|
|
348
|
-
}
|
|
349
|
-
const fetcherPromise = new Promise((resolve, reject) => {
|
|
350
|
-
setTimeout(() => {
|
|
351
|
-
fetcher().then(resolve, reject);
|
|
352
|
-
}, 1);
|
|
353
|
-
});
|
|
354
|
-
if (!isRSC2)
|
|
355
|
-
fetcherPromise.then((data2) => {
|
|
356
|
-
mutate == null ? void 0 : mutate(key, data2);
|
|
357
|
-
}).catch((err) => {
|
|
358
|
-
var _a2;
|
|
359
|
-
const keyInfo = key ? "$swr$" + key : "";
|
|
360
|
-
cache == null ? void 0 : cache.set(keyInfo, __spreadProps(__spreadValues({}, (_a2 = cache == null ? void 0 : cache.get(keyInfo)) != null ? _a2 : {}), { error: err }));
|
|
361
|
-
});
|
|
362
|
-
return fetcherPromise;
|
|
363
|
-
};
|
|
364
|
-
}, [fetcher, fetchingData, cache, key, dependentKeyDataErrorPromise]);
|
|
365
|
-
const res = useMutablePlasmicQueryData(key, fetcher, {
|
|
366
|
-
shouldRetryOnError: false,
|
|
367
|
-
// If revalidateIfStale is true, then if there's a cache entry with a key,
|
|
368
|
-
// but no mounted hook with that key yet, and when the hook mounts with the key,
|
|
369
|
-
// swr will revalidate. This may be reasonable behavior, but for us, this
|
|
370
|
-
// happens all the time -- we prepopulate the cache with proxy-invoked fetch,
|
|
371
|
-
// sometimes before swr had a chance to run the effect. So we turn off
|
|
372
|
-
// revalidateIfStale here, and just let the user manage invalidation.
|
|
373
|
-
revalidateIfStale: false
|
|
374
|
-
});
|
|
375
|
-
const { data, error, isLoading } = res;
|
|
376
|
-
if (fetchingData.fetchingPromise != null && data !== void 0) {
|
|
377
|
-
fetchingData.fetchingPromise = void 0;
|
|
378
|
-
}
|
|
379
|
-
return React.useMemo(() => {
|
|
380
|
-
const result = __spreadValues(__spreadValues({}, data != null ? data : {}), pick(res, "isLoading", "error"));
|
|
381
|
-
if (!(opts == null ? void 0 : opts.noUndefinedDataProxy) && reactMajorVersion >= 18 && enableLoadingBoundary && (isLoading || isNullDataOp || isWaitingOnDependentQuery) && result.data === void 0 && result.schema === void 0 && result.error === void 0) {
|
|
382
|
-
result.data = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
383
|
-
result.schema = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
406
|
+
const fetcher = (op) => {
|
|
407
|
+
return executePlasmicDataOp(op, {
|
|
408
|
+
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
409
|
+
user: ctx == null ? void 0 : ctx.user,
|
|
410
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
const resultMapper = (result) => {
|
|
414
|
+
var _a;
|
|
415
|
+
return __spreadValues(__spreadValues({}, (_a = result.data) != null ? _a : {}), pick(result, "error", "isLoading"));
|
|
416
|
+
};
|
|
417
|
+
return usePlasmicFetch(
|
|
418
|
+
key,
|
|
419
|
+
resolvedDataOp,
|
|
420
|
+
fetcher,
|
|
421
|
+
resultMapper,
|
|
422
|
+
["data", "schema", "error"],
|
|
423
|
+
{
|
|
424
|
+
noUndefinedDataProxy: opts == null ? void 0 : opts.noUndefinedDataProxy
|
|
384
425
|
}
|
|
385
|
-
|
|
386
|
-
}, [
|
|
387
|
-
isNullDataOp,
|
|
388
|
-
isWaitingOnDependentQuery,
|
|
389
|
-
data,
|
|
390
|
-
error,
|
|
391
|
-
isLoading,
|
|
392
|
-
opts == null ? void 0 : opts.noUndefinedDataProxy,
|
|
393
|
-
enableLoadingBoundary,
|
|
394
|
-
fetchingData,
|
|
395
|
-
fetchAndUpdateCache
|
|
396
|
-
]);
|
|
426
|
+
);
|
|
397
427
|
}
|
|
398
428
|
function usePlasmicDataMutationOp(dataOp) {
|
|
399
429
|
const ctx = usePlasmicDataSourceContext();
|
|
400
430
|
const userToken = ctx == null ? void 0 : ctx.userAuthToken;
|
|
401
|
-
const getRealDataOp =
|
|
431
|
+
const getRealDataOp = React2.useCallback(() => __async(this, null, function* () {
|
|
402
432
|
const tryGetRealDataOp = () => __async(this, null, function* () {
|
|
403
433
|
const resolved = resolveDataOp(dataOp);
|
|
404
434
|
if (!resolved) {
|
|
@@ -412,7 +442,7 @@ function usePlasmicDataMutationOp(dataOp) {
|
|
|
412
442
|
});
|
|
413
443
|
return yield tryGetRealDataOp();
|
|
414
444
|
}), [dataOp]);
|
|
415
|
-
return
|
|
445
|
+
return React2.useCallback(() => __async(this, null, function* () {
|
|
416
446
|
var _a;
|
|
417
447
|
const { sourceId, opId, userArgs } = (_a = yield getRealDataOp()) != null ? _a : {};
|
|
418
448
|
if (!sourceId || !opId) {
|
|
@@ -435,7 +465,7 @@ function Fetcher(props) {
|
|
|
435
465
|
const data = usePlasmicDataOp(dataOp, __spreadValues({}, !!pageIndex && !!pageSize && {
|
|
436
466
|
paginate: { pageIndex, pageSize }
|
|
437
467
|
}));
|
|
438
|
-
const $queries =
|
|
468
|
+
const $queries = React3.useMemo(
|
|
439
469
|
() => __spreadProps(__spreadValues({}, props.queries), { [name != null ? name : "data"]: data }),
|
|
440
470
|
[props.queries, name, data]
|
|
441
471
|
);
|
|
@@ -477,7 +507,7 @@ var FetcherMeta = {
|
|
|
477
507
|
};
|
|
478
508
|
|
|
479
509
|
// src/helpers.ts
|
|
480
|
-
import { useMemo
|
|
510
|
+
import { useMemo } from "react";
|
|
481
511
|
function normalizeData(rawData) {
|
|
482
512
|
var _a;
|
|
483
513
|
if (!rawData) {
|
|
@@ -494,7 +524,7 @@ function normalizeData(rawData) {
|
|
|
494
524
|
return { data: dataArray, schema };
|
|
495
525
|
}
|
|
496
526
|
function useNormalizedData(rawData) {
|
|
497
|
-
return
|
|
527
|
+
return useMemo(() => normalizeData(rawData), [rawData]);
|
|
498
528
|
}
|
|
499
529
|
function tryGetDataArray(rawData) {
|
|
500
530
|
if (rawData == null || typeof rawData !== "object") {
|
|
@@ -588,10 +618,10 @@ function deriveFieldConfigs(specifiedFieldsPartial, schema, makeDefaultConfig) {
|
|
|
588
618
|
}
|
|
589
619
|
|
|
590
620
|
// src/hooks/useDependencyAwareQuery.tsx
|
|
591
|
-
import
|
|
621
|
+
import React4 from "react";
|
|
592
622
|
function usePrevious(value) {
|
|
593
|
-
const prevValue =
|
|
594
|
-
|
|
623
|
+
const prevValue = React4.useRef(void 0);
|
|
624
|
+
React4.useEffect(() => {
|
|
595
625
|
prevValue.current = value;
|
|
596
626
|
return () => {
|
|
597
627
|
prevValue.current = void 0;
|
|
@@ -612,7 +642,7 @@ function useDependencyAwareQuery({
|
|
|
612
642
|
}));
|
|
613
643
|
const finalName = name != null ? name : "data";
|
|
614
644
|
const prevName = usePrevious(finalName);
|
|
615
|
-
|
|
645
|
+
React4.useEffect(() => {
|
|
616
646
|
if (!(finalName in $queries) || $queries[finalName] !== data) {
|
|
617
647
|
const $queries2 = __spreadProps(__spreadValues({}, $queries), {
|
|
618
648
|
[finalName]: data
|
|
@@ -625,6 +655,42 @@ function useDependencyAwareQuery({
|
|
|
625
655
|
}, [finalName, prevName, data, $queries, setDollarQueries]);
|
|
626
656
|
}
|
|
627
657
|
|
|
658
|
+
// src/serverQueries/common.ts
|
|
659
|
+
function resolveParams(params, errorFn) {
|
|
660
|
+
try {
|
|
661
|
+
return params();
|
|
662
|
+
} catch (err) {
|
|
663
|
+
return errorFn(err);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// src/serverQueries/client.ts
|
|
668
|
+
function usePlasmicServerQuery(serverQuery, fallbackData, opts) {
|
|
669
|
+
const resolvedParams = resolveParams(serverQuery.execParams, (err) => {
|
|
670
|
+
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
671
|
+
return err;
|
|
672
|
+
}
|
|
673
|
+
throw err;
|
|
674
|
+
});
|
|
675
|
+
const key = !resolvedParams || isPlasmicUndefinedDataErrorPromise(resolvedParams) ? null : `${serverQuery.id}:${JSON.stringify(resolvedParams)}`;
|
|
676
|
+
const fetcher = (params) => {
|
|
677
|
+
return serverQuery.fn(...params);
|
|
678
|
+
};
|
|
679
|
+
const resultMapper = (result) => {
|
|
680
|
+
return __spreadValues({}, pick(result, "data", "error", "isLoading"));
|
|
681
|
+
};
|
|
682
|
+
return usePlasmicFetch(
|
|
683
|
+
key,
|
|
684
|
+
resolvedParams,
|
|
685
|
+
fetcher,
|
|
686
|
+
resultMapper,
|
|
687
|
+
["data", "error"],
|
|
688
|
+
__spreadValues({
|
|
689
|
+
fallbackData
|
|
690
|
+
}, opts)
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
|
|
628
694
|
// src/serverQueries/server.ts
|
|
629
695
|
var PlasmicUndefinedServerError = class extends Error {
|
|
630
696
|
constructor(msg) {
|
|
@@ -653,19 +719,14 @@ function mkPlasmicUndefinedServerProxy() {
|
|
|
653
719
|
isLoading: true
|
|
654
720
|
};
|
|
655
721
|
}
|
|
656
|
-
function resolveParams(params) {
|
|
657
|
-
try {
|
|
658
|
-
return params();
|
|
659
|
-
} catch (err) {
|
|
660
|
-
if (isPlasmicUndefinedServerError(err)) {
|
|
661
|
-
return err;
|
|
662
|
-
}
|
|
663
|
-
throw err;
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
722
|
function executeServerQuery(serverQuery) {
|
|
667
723
|
return __async(this, null, function* () {
|
|
668
|
-
const resolvedParams = resolveParams(serverQuery.execParams)
|
|
724
|
+
const resolvedParams = resolveParams(serverQuery.execParams, (err) => {
|
|
725
|
+
if (isPlasmicUndefinedServerError(err)) {
|
|
726
|
+
return err;
|
|
727
|
+
}
|
|
728
|
+
throw err;
|
|
729
|
+
});
|
|
669
730
|
if (isPlasmicUndefinedServerError(resolvedParams)) {
|
|
670
731
|
return mkPlasmicUndefinedServerProxy();
|
|
671
732
|
}
|
|
@@ -683,9 +744,10 @@ export {
|
|
|
683
744
|
normalizeData,
|
|
684
745
|
useDependencyAwareQuery,
|
|
685
746
|
useNormalizedData,
|
|
686
|
-
|
|
747
|
+
usePlasmicDataConfig3 as usePlasmicDataConfig,
|
|
687
748
|
usePlasmicDataMutationOp,
|
|
688
749
|
usePlasmicDataOp,
|
|
689
|
-
usePlasmicInvalidate
|
|
750
|
+
usePlasmicInvalidate,
|
|
751
|
+
usePlasmicServerQuery
|
|
690
752
|
};
|
|
691
753
|
//# sourceMappingURL=index.esm.js.map
|