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