@plasmicapp/data-sources 0.1.171 → 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 +2 -2
package/dist/index.js
CHANGED
|
@@ -77,26 +77,194 @@ __export(src_exports, {
|
|
|
77
77
|
normalizeData: () => normalizeData,
|
|
78
78
|
useDependencyAwareQuery: () => useDependencyAwareQuery,
|
|
79
79
|
useNormalizedData: () => useNormalizedData,
|
|
80
|
-
usePlasmicDataConfig: () =>
|
|
80
|
+
usePlasmicDataConfig: () => import_query4.usePlasmicDataConfig,
|
|
81
81
|
usePlasmicDataMutationOp: () => usePlasmicDataMutationOp,
|
|
82
82
|
usePlasmicDataOp: () => usePlasmicDataOp,
|
|
83
|
-
usePlasmicInvalidate: () => usePlasmicInvalidate
|
|
83
|
+
usePlasmicInvalidate: () => usePlasmicInvalidate,
|
|
84
|
+
usePlasmicServerQuery: () => usePlasmicServerQuery
|
|
84
85
|
});
|
|
85
86
|
module.exports = __toCommonJS(src_exports);
|
|
86
|
-
var
|
|
87
|
+
var import_query4 = require("@plasmicapp/query");
|
|
87
88
|
|
|
88
89
|
// src/components/Fetcher.tsx
|
|
89
|
-
var
|
|
90
|
+
var import_react2 = __toESM(require("react"));
|
|
90
91
|
|
|
91
92
|
// src/hooks/usePlasmicDataOp.tsx
|
|
92
93
|
var import_data_sources_context = require("@plasmicapp/data-sources-context");
|
|
94
|
+
var import_query3 = require("@plasmicapp/query");
|
|
95
|
+
var React2 = __toESM(require("react"));
|
|
96
|
+
|
|
97
|
+
// src/common.ts
|
|
98
|
+
var import_query = require("@plasmicapp/query");
|
|
93
99
|
var ph = __toESM(require("@plasmicapp/host"));
|
|
94
|
-
var
|
|
95
|
-
|
|
100
|
+
var import_react = __toESM(require("react"));
|
|
101
|
+
function isPlasmicUndefinedDataErrorPromise(x) {
|
|
102
|
+
return !!x && typeof x === "object" && (x == null ? void 0 : x.plasmicType) === "PlasmicUndefinedDataError";
|
|
103
|
+
}
|
|
104
|
+
function mkUndefinedDataProxy(promiseRef, fetchAndUpdateCache) {
|
|
105
|
+
let fetchAndUpdatePromise = void 0;
|
|
106
|
+
return new Proxy(
|
|
107
|
+
{},
|
|
108
|
+
{
|
|
109
|
+
get: (_target, prop) => {
|
|
110
|
+
if (prop === "isPlasmicUndefinedDataProxy") {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
if (!fetchAndUpdateCache) {
|
|
114
|
+
return void 0;
|
|
115
|
+
}
|
|
116
|
+
const doFetchAndUpdate = () => {
|
|
117
|
+
if (!fetchAndUpdatePromise) {
|
|
118
|
+
fetchAndUpdatePromise = fetchAndUpdateCache().finally(() => {
|
|
119
|
+
fetchAndUpdatePromise = void 0;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return fetchAndUpdatePromise;
|
|
123
|
+
};
|
|
124
|
+
const promise = (
|
|
125
|
+
// existing fetch
|
|
126
|
+
promiseRef.fetchingPromise || // No existing fetch, so kick off a fetch
|
|
127
|
+
doFetchAndUpdate()
|
|
128
|
+
);
|
|
129
|
+
promise.plasmicType = "PlasmicUndefinedDataError";
|
|
130
|
+
promise.message = `Cannot read property ${String(
|
|
131
|
+
prop
|
|
132
|
+
)} - data is still loading`;
|
|
133
|
+
throw promise;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
var isRSC = import_react.default.isRSC;
|
|
139
|
+
var reactMajorVersion = +import_react.default.version.split(".")[0];
|
|
140
|
+
var enableLoadingBoundaryKey = "plasmicInternalEnableLoadingBoundary";
|
|
141
|
+
var PRE_FETCHES = /* @__PURE__ */ new Map();
|
|
142
|
+
function usePlasmicFetch(key, resolvedParams, fetcherFn, resultMapper, undefinedDataProxyFields, opts) {
|
|
143
|
+
var _a, _b;
|
|
144
|
+
const enableLoadingBoundary = !!((_b = (_a = ph.useDataEnv) == null ? void 0 : _a()) == null ? void 0 : _b[enableLoadingBoundaryKey]);
|
|
145
|
+
const { mutate, cache } = isRSC ? {} : (0, import_query.usePlasmicDataConfig)();
|
|
146
|
+
const isNullParams = !resolvedParams;
|
|
147
|
+
const isWaitingOnDependentQuery = isPlasmicUndefinedDataErrorPromise(resolvedParams);
|
|
148
|
+
const fetchingData = import_react.default.useMemo(
|
|
149
|
+
() => ({
|
|
150
|
+
fetchingPromise: void 0
|
|
151
|
+
}),
|
|
152
|
+
[key]
|
|
153
|
+
);
|
|
154
|
+
const fetcher = import_react.default.useMemo(
|
|
155
|
+
() => () => {
|
|
156
|
+
if (!key) {
|
|
157
|
+
throw new Error(`Fetcher should never be called without a proper key`);
|
|
158
|
+
}
|
|
159
|
+
if (fetchingData.fetchingPromise) {
|
|
160
|
+
return fetchingData.fetchingPromise;
|
|
161
|
+
}
|
|
162
|
+
if (key && PRE_FETCHES.has(key)) {
|
|
163
|
+
const existing = PRE_FETCHES.get(key);
|
|
164
|
+
fetchingData.fetchingPromise = existing;
|
|
165
|
+
return existing;
|
|
166
|
+
}
|
|
167
|
+
const fetcherPromise = fetcherFn(resolvedParams);
|
|
168
|
+
fetchingData.fetchingPromise = fetcherPromise;
|
|
169
|
+
if (key) {
|
|
170
|
+
PRE_FETCHES.set(key, fetcherPromise);
|
|
171
|
+
fetcherPromise.then(
|
|
172
|
+
() => {
|
|
173
|
+
PRE_FETCHES.delete(key);
|
|
174
|
+
},
|
|
175
|
+
() => {
|
|
176
|
+
PRE_FETCHES.delete(key);
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return fetcherPromise;
|
|
181
|
+
},
|
|
182
|
+
[key, fetchingData]
|
|
183
|
+
);
|
|
184
|
+
const dependentKeyDataErrorPromise = isPlasmicUndefinedDataErrorPromise(
|
|
185
|
+
resolvedParams
|
|
186
|
+
) ? resolvedParams : void 0;
|
|
187
|
+
const fetchAndUpdateCache = import_react.default.useMemo(() => {
|
|
188
|
+
if (!key && !dependentKeyDataErrorPromise) {
|
|
189
|
+
return void 0;
|
|
190
|
+
}
|
|
191
|
+
return () => {
|
|
192
|
+
if (fetchingData.fetchingPromise) {
|
|
193
|
+
return fetchingData.fetchingPromise;
|
|
194
|
+
}
|
|
195
|
+
if (dependentKeyDataErrorPromise) {
|
|
196
|
+
return dependentKeyDataErrorPromise;
|
|
197
|
+
}
|
|
198
|
+
if (!key) {
|
|
199
|
+
throw new Error(`Expected key to be non-null`);
|
|
200
|
+
}
|
|
201
|
+
const cached = cache == null ? void 0 : cache.get(key);
|
|
202
|
+
if (cached) {
|
|
203
|
+
return Promise.resolve(cached);
|
|
204
|
+
}
|
|
205
|
+
const cachedError = cache == null ? void 0 : cache.get(`$swr$${key}`);
|
|
206
|
+
if (cachedError) {
|
|
207
|
+
return Promise.reject(cachedError.error);
|
|
208
|
+
}
|
|
209
|
+
const fetcherPromise = new Promise((resolve, reject) => {
|
|
210
|
+
setTimeout(() => {
|
|
211
|
+
fetcher().then(resolve, reject);
|
|
212
|
+
}, 1);
|
|
213
|
+
});
|
|
214
|
+
if (!isRSC) {
|
|
215
|
+
fetcherPromise.then((data2) => {
|
|
216
|
+
mutate == null ? void 0 : mutate(key, data2);
|
|
217
|
+
}).catch((err) => {
|
|
218
|
+
var _a2;
|
|
219
|
+
const keyInfo = key ? "$swr$" + key : "";
|
|
220
|
+
cache == null ? void 0 : cache.set(keyInfo, __spreadProps(__spreadValues({}, (_a2 = cache == null ? void 0 : cache.get(keyInfo)) != null ? _a2 : {}), { error: err }));
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return fetcherPromise;
|
|
224
|
+
};
|
|
225
|
+
}, [fetcher, fetchingData, cache, key, dependentKeyDataErrorPromise]);
|
|
226
|
+
const res = (0, import_query.useMutablePlasmicQueryData)(key, fetcher, {
|
|
227
|
+
fallbackData: opts == null ? void 0 : opts.fallbackData,
|
|
228
|
+
shouldRetryOnError: false,
|
|
229
|
+
// If revalidateIfStale is true, then if there's a cache entry with a key,
|
|
230
|
+
// but no mounted hook with that key yet, and when the hook mounts with the key,
|
|
231
|
+
// swr will revalidate. This may be reasonable behavior, but for us, this
|
|
232
|
+
// happens all the time -- we prepopulate the cache with proxy-invoked fetch,
|
|
233
|
+
// sometimes before swr had a chance to run the effect. So we turn off
|
|
234
|
+
// revalidateIfStale here, and just let the user manage invalidation.
|
|
235
|
+
revalidateIfStale: false
|
|
236
|
+
});
|
|
237
|
+
const { data, error, isLoading } = res;
|
|
238
|
+
if (fetchingData.fetchingPromise != null && data !== void 0) {
|
|
239
|
+
fetchingData.fetchingPromise = void 0;
|
|
240
|
+
}
|
|
241
|
+
return import_react.default.useMemo(() => {
|
|
242
|
+
const result = resultMapper(res);
|
|
243
|
+
if (!(opts == null ? void 0 : opts.noUndefinedDataProxy) && reactMajorVersion >= 18 && enableLoadingBoundary && (isLoading || isNullParams || isWaitingOnDependentQuery) && undefinedDataProxyFields.every((field) => result[field] === void 0)) {
|
|
244
|
+
undefinedDataProxyFields.forEach((field) => {
|
|
245
|
+
if (field === "error") {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
result[field] = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}, [
|
|
253
|
+
isNullParams,
|
|
254
|
+
isWaitingOnDependentQuery,
|
|
255
|
+
data,
|
|
256
|
+
error,
|
|
257
|
+
isLoading,
|
|
258
|
+
opts == null ? void 0 : opts.noUndefinedDataProxy,
|
|
259
|
+
enableLoadingBoundary,
|
|
260
|
+
fetchingData,
|
|
261
|
+
fetchAndUpdateCache
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
96
264
|
|
|
97
265
|
// src/executor.tsx
|
|
98
266
|
var import_isomorphic_unfetch = __toESM(require("@plasmicapp/isomorphic-unfetch"));
|
|
99
|
-
var
|
|
267
|
+
var import_query2 = require("@plasmicapp/query");
|
|
100
268
|
var import_fast_stringify = __toESM(require("fast-stringify"));
|
|
101
269
|
|
|
102
270
|
// src/placeholders.ts
|
|
@@ -124,7 +292,7 @@ function executePlasmicDataOp(op, opts) {
|
|
|
124
292
|
_executePlasmicDataOp
|
|
125
293
|
);
|
|
126
294
|
op.userArgs = addPlaceholdersToUserArgs(op.userArgs);
|
|
127
|
-
const res = yield (0,
|
|
295
|
+
const res = yield (0, import_query2.wrapLoadingFetcher)(func)(op, opts);
|
|
128
296
|
return res;
|
|
129
297
|
});
|
|
130
298
|
}
|
|
@@ -194,7 +362,6 @@ function withoutNils(xs) {
|
|
|
194
362
|
}
|
|
195
363
|
|
|
196
364
|
// src/hooks/usePlasmicDataOp.tsx
|
|
197
|
-
var isRSC2 = React.isRSC;
|
|
198
365
|
function makeCacheKey(dataOp, opts) {
|
|
199
366
|
const queryDependencies = JSON.stringify({
|
|
200
367
|
sourceId: dataOp.sourceId,
|
|
@@ -206,7 +373,7 @@ function makeCacheKey(dataOp, opts) {
|
|
|
206
373
|
return dataOp.cacheKey ? `${dataOp.cacheKey}${queryDependencies}` : queryDependencies;
|
|
207
374
|
}
|
|
208
375
|
function usePlasmicInvalidate() {
|
|
209
|
-
const { cache, fallback, mutate } = (0,
|
|
376
|
+
const { cache, fallback, mutate } = (0, import_query3.usePlasmicDataConfig)();
|
|
210
377
|
return (invalidatedKeys) => __async(this, null, function* () {
|
|
211
378
|
const getKeysToInvalidate = () => {
|
|
212
379
|
var _a, _b;
|
|
@@ -255,45 +422,6 @@ function usePlasmicInvalidate() {
|
|
|
255
422
|
return yield Promise.all(keys.map((key) => invalidateKey(key)));
|
|
256
423
|
});
|
|
257
424
|
}
|
|
258
|
-
var enableLoadingBoundaryKey = "plasmicInternalEnableLoadingBoundary";
|
|
259
|
-
function mkUndefinedDataProxy(promiseRef, fetchAndUpdateCache) {
|
|
260
|
-
let fetchAndUpdatePromise = void 0;
|
|
261
|
-
return new Proxy(
|
|
262
|
-
{},
|
|
263
|
-
{
|
|
264
|
-
get: (_target, prop) => {
|
|
265
|
-
if (prop === "isPlasmicUndefinedDataProxy") {
|
|
266
|
-
return true;
|
|
267
|
-
}
|
|
268
|
-
if (!fetchAndUpdateCache) {
|
|
269
|
-
return void 0;
|
|
270
|
-
}
|
|
271
|
-
const doFetchAndUpdate = () => {
|
|
272
|
-
if (!fetchAndUpdatePromise) {
|
|
273
|
-
fetchAndUpdatePromise = fetchAndUpdateCache().finally(() => {
|
|
274
|
-
fetchAndUpdatePromise = void 0;
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
return fetchAndUpdatePromise;
|
|
278
|
-
};
|
|
279
|
-
const promise = (
|
|
280
|
-
// existing fetch
|
|
281
|
-
promiseRef.fetchingPromise || // No existing fetch, so kick off a fetch
|
|
282
|
-
doFetchAndUpdate()
|
|
283
|
-
);
|
|
284
|
-
promise.plasmicType = "PlasmicUndefinedDataError";
|
|
285
|
-
promise.message = `Cannot read property ${String(
|
|
286
|
-
prop
|
|
287
|
-
)} - data is still loading`;
|
|
288
|
-
throw promise;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
function isPlasmicUndefinedDataErrorPromise(x) {
|
|
294
|
-
return !!x && typeof x === "object" && x.plasmicType === "PlasmicUndefinedDataError";
|
|
295
|
-
}
|
|
296
|
-
var reactMajorVersion = +React.version.split(".")[0];
|
|
297
425
|
function resolveDataOp(dataOp) {
|
|
298
426
|
if (typeof dataOp === "function") {
|
|
299
427
|
try {
|
|
@@ -308,138 +436,39 @@ function resolveDataOp(dataOp) {
|
|
|
308
436
|
return dataOp;
|
|
309
437
|
}
|
|
310
438
|
}
|
|
311
|
-
var PRE_FETCHES = /* @__PURE__ */ new Map();
|
|
312
439
|
function usePlasmicDataOp(dataOp, opts) {
|
|
313
|
-
var _a, _b;
|
|
314
440
|
const resolvedDataOp = resolveDataOp(dataOp);
|
|
315
441
|
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
316
|
-
const enableLoadingBoundary = !!((_b = (_a = ph.useDataEnv) == null ? void 0 : _a()) == null ? void 0 : _b[enableLoadingBoundaryKey]);
|
|
317
|
-
const { mutate, cache } = isRSC2 ? {} : (0, import_query2.usePlasmicDataConfig)();
|
|
318
|
-
const isNullDataOp = !resolvedDataOp;
|
|
319
|
-
const isWaitingOnDependentQuery = isPlasmicUndefinedDataErrorPromise(resolvedDataOp);
|
|
320
442
|
const key = !resolvedDataOp || isPlasmicUndefinedDataErrorPromise(resolvedDataOp) ? null : makeCacheKey(resolvedDataOp, {
|
|
321
443
|
paginate: opts == null ? void 0 : opts.paginate,
|
|
322
444
|
userAuthToken: ctx == null ? void 0 : ctx.userAuthToken
|
|
323
445
|
});
|
|
324
|
-
const
|
|
325
|
-
(
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const fetcherFn = () => executePlasmicDataOp(resolvedDataOp, {
|
|
344
|
-
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
345
|
-
user: ctx == null ? void 0 : ctx.user,
|
|
346
|
-
paginate: opts == null ? void 0 : opts.paginate
|
|
347
|
-
});
|
|
348
|
-
const fetcherPromise = fetcherFn();
|
|
349
|
-
fetchingData.fetchingPromise = fetcherPromise;
|
|
350
|
-
if (key) {
|
|
351
|
-
PRE_FETCHES.set(key, fetcherPromise);
|
|
352
|
-
fetcherPromise.then(
|
|
353
|
-
() => {
|
|
354
|
-
PRE_FETCHES.delete(key);
|
|
355
|
-
},
|
|
356
|
-
() => {
|
|
357
|
-
PRE_FETCHES.delete(key);
|
|
358
|
-
}
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
return fetcherPromise;
|
|
362
|
-
},
|
|
363
|
-
[key, fetchingData]
|
|
364
|
-
);
|
|
365
|
-
const dependentKeyDataErrorPromise = isPlasmicUndefinedDataErrorPromise(
|
|
366
|
-
resolvedDataOp
|
|
367
|
-
) ? resolvedDataOp : void 0;
|
|
368
|
-
const fetchAndUpdateCache = React.useMemo(() => {
|
|
369
|
-
if (!key && !dependentKeyDataErrorPromise) {
|
|
370
|
-
return void 0;
|
|
371
|
-
}
|
|
372
|
-
return () => {
|
|
373
|
-
if (fetchingData.fetchingPromise) {
|
|
374
|
-
return fetchingData.fetchingPromise;
|
|
375
|
-
}
|
|
376
|
-
if (dependentKeyDataErrorPromise) {
|
|
377
|
-
return dependentKeyDataErrorPromise;
|
|
378
|
-
}
|
|
379
|
-
if (!key) {
|
|
380
|
-
throw new Error(`Expected key to be non-null`);
|
|
381
|
-
}
|
|
382
|
-
const cached = cache == null ? void 0 : cache.get(key);
|
|
383
|
-
if (cached) {
|
|
384
|
-
return Promise.resolve(cached);
|
|
385
|
-
}
|
|
386
|
-
const cachedError = cache == null ? void 0 : cache.get(`$swr$${key}`);
|
|
387
|
-
if (cachedError) {
|
|
388
|
-
return Promise.reject(cachedError.error);
|
|
389
|
-
}
|
|
390
|
-
const fetcherPromise = new Promise((resolve, reject) => {
|
|
391
|
-
setTimeout(() => {
|
|
392
|
-
fetcher().then(resolve, reject);
|
|
393
|
-
}, 1);
|
|
394
|
-
});
|
|
395
|
-
if (!isRSC2)
|
|
396
|
-
fetcherPromise.then((data2) => {
|
|
397
|
-
mutate == null ? void 0 : mutate(key, data2);
|
|
398
|
-
}).catch((err) => {
|
|
399
|
-
var _a2;
|
|
400
|
-
const keyInfo = key ? "$swr$" + key : "";
|
|
401
|
-
cache == null ? void 0 : cache.set(keyInfo, __spreadProps(__spreadValues({}, (_a2 = cache == null ? void 0 : cache.get(keyInfo)) != null ? _a2 : {}), { error: err }));
|
|
402
|
-
});
|
|
403
|
-
return fetcherPromise;
|
|
404
|
-
};
|
|
405
|
-
}, [fetcher, fetchingData, cache, key, dependentKeyDataErrorPromise]);
|
|
406
|
-
const res = (0, import_query2.useMutablePlasmicQueryData)(key, fetcher, {
|
|
407
|
-
shouldRetryOnError: false,
|
|
408
|
-
// If revalidateIfStale is true, then if there's a cache entry with a key,
|
|
409
|
-
// but no mounted hook with that key yet, and when the hook mounts with the key,
|
|
410
|
-
// swr will revalidate. This may be reasonable behavior, but for us, this
|
|
411
|
-
// happens all the time -- we prepopulate the cache with proxy-invoked fetch,
|
|
412
|
-
// sometimes before swr had a chance to run the effect. So we turn off
|
|
413
|
-
// revalidateIfStale here, and just let the user manage invalidation.
|
|
414
|
-
revalidateIfStale: false
|
|
415
|
-
});
|
|
416
|
-
const { data, error, isLoading } = res;
|
|
417
|
-
if (fetchingData.fetchingPromise != null && data !== void 0) {
|
|
418
|
-
fetchingData.fetchingPromise = void 0;
|
|
419
|
-
}
|
|
420
|
-
return React.useMemo(() => {
|
|
421
|
-
const result = __spreadValues(__spreadValues({}, data != null ? data : {}), pick(res, "isLoading", "error"));
|
|
422
|
-
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) {
|
|
423
|
-
result.data = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
424
|
-
result.schema = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
446
|
+
const fetcher = (op) => {
|
|
447
|
+
return executePlasmicDataOp(op, {
|
|
448
|
+
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
449
|
+
user: ctx == null ? void 0 : ctx.user,
|
|
450
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
451
|
+
});
|
|
452
|
+
};
|
|
453
|
+
const resultMapper = (result) => {
|
|
454
|
+
var _a;
|
|
455
|
+
return __spreadValues(__spreadValues({}, (_a = result.data) != null ? _a : {}), pick(result, "error", "isLoading"));
|
|
456
|
+
};
|
|
457
|
+
return usePlasmicFetch(
|
|
458
|
+
key,
|
|
459
|
+
resolvedDataOp,
|
|
460
|
+
fetcher,
|
|
461
|
+
resultMapper,
|
|
462
|
+
["data", "schema", "error"],
|
|
463
|
+
{
|
|
464
|
+
noUndefinedDataProxy: opts == null ? void 0 : opts.noUndefinedDataProxy
|
|
425
465
|
}
|
|
426
|
-
|
|
427
|
-
}, [
|
|
428
|
-
isNullDataOp,
|
|
429
|
-
isWaitingOnDependentQuery,
|
|
430
|
-
data,
|
|
431
|
-
error,
|
|
432
|
-
isLoading,
|
|
433
|
-
opts == null ? void 0 : opts.noUndefinedDataProxy,
|
|
434
|
-
enableLoadingBoundary,
|
|
435
|
-
fetchingData,
|
|
436
|
-
fetchAndUpdateCache
|
|
437
|
-
]);
|
|
466
|
+
);
|
|
438
467
|
}
|
|
439
468
|
function usePlasmicDataMutationOp(dataOp) {
|
|
440
469
|
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
441
470
|
const userToken = ctx == null ? void 0 : ctx.userAuthToken;
|
|
442
|
-
const getRealDataOp =
|
|
471
|
+
const getRealDataOp = React2.useCallback(() => __async(this, null, function* () {
|
|
443
472
|
const tryGetRealDataOp = () => __async(this, null, function* () {
|
|
444
473
|
const resolved = resolveDataOp(dataOp);
|
|
445
474
|
if (!resolved) {
|
|
@@ -453,7 +482,7 @@ function usePlasmicDataMutationOp(dataOp) {
|
|
|
453
482
|
});
|
|
454
483
|
return yield tryGetRealDataOp();
|
|
455
484
|
}), [dataOp]);
|
|
456
|
-
return
|
|
485
|
+
return React2.useCallback(() => __async(this, null, function* () {
|
|
457
486
|
var _a;
|
|
458
487
|
const { sourceId, opId, userArgs } = (_a = yield getRealDataOp()) != null ? _a : {};
|
|
459
488
|
if (!sourceId || !opId) {
|
|
@@ -476,7 +505,7 @@ function Fetcher(props) {
|
|
|
476
505
|
const data = usePlasmicDataOp(dataOp, __spreadValues({}, !!pageIndex && !!pageSize && {
|
|
477
506
|
paginate: { pageIndex, pageSize }
|
|
478
507
|
}));
|
|
479
|
-
const $queries =
|
|
508
|
+
const $queries = import_react2.default.useMemo(
|
|
480
509
|
() => __spreadProps(__spreadValues({}, props.queries), { [name != null ? name : "data"]: data }),
|
|
481
510
|
[props.queries, name, data]
|
|
482
511
|
);
|
|
@@ -518,7 +547,7 @@ var FetcherMeta = {
|
|
|
518
547
|
};
|
|
519
548
|
|
|
520
549
|
// src/helpers.ts
|
|
521
|
-
var
|
|
550
|
+
var import_react3 = require("react");
|
|
522
551
|
function normalizeData(rawData) {
|
|
523
552
|
var _a;
|
|
524
553
|
if (!rawData) {
|
|
@@ -535,7 +564,7 @@ function normalizeData(rawData) {
|
|
|
535
564
|
return { data: dataArray, schema };
|
|
536
565
|
}
|
|
537
566
|
function useNormalizedData(rawData) {
|
|
538
|
-
return (0,
|
|
567
|
+
return (0, import_react3.useMemo)(() => normalizeData(rawData), [rawData]);
|
|
539
568
|
}
|
|
540
569
|
function tryGetDataArray(rawData) {
|
|
541
570
|
if (rawData == null || typeof rawData !== "object") {
|
|
@@ -629,10 +658,10 @@ function deriveFieldConfigs(specifiedFieldsPartial, schema, makeDefaultConfig) {
|
|
|
629
658
|
}
|
|
630
659
|
|
|
631
660
|
// src/hooks/useDependencyAwareQuery.tsx
|
|
632
|
-
var
|
|
661
|
+
var import_react4 = __toESM(require("react"));
|
|
633
662
|
function usePrevious(value) {
|
|
634
|
-
const prevValue =
|
|
635
|
-
|
|
663
|
+
const prevValue = import_react4.default.useRef(void 0);
|
|
664
|
+
import_react4.default.useEffect(() => {
|
|
636
665
|
prevValue.current = value;
|
|
637
666
|
return () => {
|
|
638
667
|
prevValue.current = void 0;
|
|
@@ -653,7 +682,7 @@ function useDependencyAwareQuery({
|
|
|
653
682
|
}));
|
|
654
683
|
const finalName = name != null ? name : "data";
|
|
655
684
|
const prevName = usePrevious(finalName);
|
|
656
|
-
|
|
685
|
+
import_react4.default.useEffect(() => {
|
|
657
686
|
if (!(finalName in $queries) || $queries[finalName] !== data) {
|
|
658
687
|
const $queries2 = __spreadProps(__spreadValues({}, $queries), {
|
|
659
688
|
[finalName]: data
|
|
@@ -666,6 +695,42 @@ function useDependencyAwareQuery({
|
|
|
666
695
|
}, [finalName, prevName, data, $queries, setDollarQueries]);
|
|
667
696
|
}
|
|
668
697
|
|
|
698
|
+
// src/serverQueries/common.ts
|
|
699
|
+
function resolveParams(params, errorFn) {
|
|
700
|
+
try {
|
|
701
|
+
return params();
|
|
702
|
+
} catch (err) {
|
|
703
|
+
return errorFn(err);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// src/serverQueries/client.ts
|
|
708
|
+
function usePlasmicServerQuery(serverQuery, fallbackData, opts) {
|
|
709
|
+
const resolvedParams = resolveParams(serverQuery.execParams, (err) => {
|
|
710
|
+
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
711
|
+
return err;
|
|
712
|
+
}
|
|
713
|
+
throw err;
|
|
714
|
+
});
|
|
715
|
+
const key = !resolvedParams || isPlasmicUndefinedDataErrorPromise(resolvedParams) ? null : `${serverQuery.id}:${JSON.stringify(resolvedParams)}`;
|
|
716
|
+
const fetcher = (params) => {
|
|
717
|
+
return serverQuery.fn(...params);
|
|
718
|
+
};
|
|
719
|
+
const resultMapper = (result) => {
|
|
720
|
+
return __spreadValues({}, pick(result, "data", "error", "isLoading"));
|
|
721
|
+
};
|
|
722
|
+
return usePlasmicFetch(
|
|
723
|
+
key,
|
|
724
|
+
resolvedParams,
|
|
725
|
+
fetcher,
|
|
726
|
+
resultMapper,
|
|
727
|
+
["data", "error"],
|
|
728
|
+
__spreadValues({
|
|
729
|
+
fallbackData
|
|
730
|
+
}, opts)
|
|
731
|
+
);
|
|
732
|
+
}
|
|
733
|
+
|
|
669
734
|
// src/serverQueries/server.ts
|
|
670
735
|
var PlasmicUndefinedServerError = class extends Error {
|
|
671
736
|
constructor(msg) {
|
|
@@ -694,19 +759,14 @@ function mkPlasmicUndefinedServerProxy() {
|
|
|
694
759
|
isLoading: true
|
|
695
760
|
};
|
|
696
761
|
}
|
|
697
|
-
function resolveParams(params) {
|
|
698
|
-
try {
|
|
699
|
-
return params();
|
|
700
|
-
} catch (err) {
|
|
701
|
-
if (isPlasmicUndefinedServerError(err)) {
|
|
702
|
-
return err;
|
|
703
|
-
}
|
|
704
|
-
throw err;
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
762
|
function executeServerQuery(serverQuery) {
|
|
708
763
|
return __async(this, null, function* () {
|
|
709
|
-
const resolvedParams = resolveParams(serverQuery.execParams)
|
|
764
|
+
const resolvedParams = resolveParams(serverQuery.execParams, (err) => {
|
|
765
|
+
if (isPlasmicUndefinedServerError(err)) {
|
|
766
|
+
return err;
|
|
767
|
+
}
|
|
768
|
+
throw err;
|
|
769
|
+
});
|
|
710
770
|
if (isPlasmicUndefinedServerError(resolvedParams)) {
|
|
711
771
|
return mkPlasmicUndefinedServerProxy();
|
|
712
772
|
}
|