@plasmicapp/data-sources 1.0.9 → 1.0.11
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 +16 -2
- package/dist/index.esm.js +275 -275
- package/dist/index.esm.js.map +4 -4
- package/dist/index.js +269 -269
- package/dist/index.js.map +4 -4
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -76,6 +76,7 @@ __export(index_exports, {
|
|
|
76
76
|
executeServerQuery: () => executeServerQuery,
|
|
77
77
|
makeCacheKey: () => makeCacheKey,
|
|
78
78
|
makeQueryCacheKey: () => makeQueryCacheKey,
|
|
79
|
+
matchesQueryCacheKey: () => matchesQueryCacheKey,
|
|
79
80
|
normalizeData: () => normalizeData,
|
|
80
81
|
throwIfPlasmicUndefinedDataError: () => throwIfPlasmicUndefinedDataError,
|
|
81
82
|
unstable_executePlasmicQueries: () => executePlasmicQueries,
|
|
@@ -270,61 +271,91 @@ function usePlasmicFetch(key, resolvedParams, fetcherFn, resultMapper, undefined
|
|
|
270
271
|
]);
|
|
271
272
|
}
|
|
272
273
|
|
|
273
|
-
// src/
|
|
274
|
+
// src/hooks/usePlasmicDataOp.tsx
|
|
275
|
+
var import_data_sources_context = require("@plasmicapp/data-sources-context");
|
|
276
|
+
var import_query3 = require("@plasmicapp/query");
|
|
277
|
+
var React2 = __toESM(require("react"));
|
|
278
|
+
|
|
279
|
+
// src/executor.tsx
|
|
280
|
+
var import_isomorphic_unfetch = __toESM(require("@plasmicapp/isomorphic-unfetch"));
|
|
274
281
|
var import_query2 = require("@plasmicapp/query");
|
|
275
|
-
var
|
|
282
|
+
var import_fast_stringify = __toESM(require("fast-stringify"));
|
|
276
283
|
|
|
277
|
-
// src/
|
|
278
|
-
|
|
284
|
+
// src/placeholders.ts
|
|
285
|
+
var PLASMIC_UNDEFINED = "__PLASMIC_UNDEFINED";
|
|
286
|
+
function addPlaceholders(val) {
|
|
287
|
+
return val === void 0 ? PLASMIC_UNDEFINED : val;
|
|
279
288
|
}
|
|
280
|
-
function
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (key in obj) {
|
|
284
|
-
res[key] = obj[key];
|
|
285
|
-
}
|
|
289
|
+
function addPlaceholdersToUserArgs(userArgs) {
|
|
290
|
+
if (!userArgs) {
|
|
291
|
+
return userArgs;
|
|
286
292
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return new Map(xs.map((x) => tuple(x.id, x)));
|
|
292
|
-
}
|
|
293
|
-
function notNil(x) {
|
|
294
|
-
return x !== null && x !== void 0;
|
|
295
|
-
}
|
|
296
|
-
function withoutNils(xs) {
|
|
297
|
-
return xs.filter(notNil);
|
|
293
|
+
Object.entries(userArgs).forEach(([key, val]) => {
|
|
294
|
+
userArgs[key] = Array.isArray(val) ? val.map((v) => addPlaceholders(v)) : addPlaceholders(val);
|
|
295
|
+
});
|
|
296
|
+
return userArgs;
|
|
298
297
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
298
|
+
|
|
299
|
+
// src/executor.tsx
|
|
300
|
+
var DEFAULT_HOST = "https://data.plasmic.app";
|
|
301
|
+
var UNAUTHORIZED_MESSAGE = "You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";
|
|
302
|
+
function executePlasmicDataOp(op, opts) {
|
|
303
|
+
return __async(this, null, function* () {
|
|
304
|
+
const func = getConfig(
|
|
305
|
+
"__PLASMIC_EXECUTE_DATA_OP",
|
|
306
|
+
_executePlasmicDataOp
|
|
308
307
|
);
|
|
308
|
+
op.userArgs = addPlaceholdersToUserArgs(op.userArgs);
|
|
309
|
+
const res = yield (0, import_query2.wrapLoadingFetcher)(func)(op, opts);
|
|
310
|
+
return res;
|
|
309
311
|
});
|
|
310
312
|
}
|
|
311
|
-
function
|
|
312
|
-
return
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
313
|
+
function _executePlasmicDataOp(op, opts) {
|
|
314
|
+
return __async(this, null, function* () {
|
|
315
|
+
var _a;
|
|
316
|
+
if (op.roleId) {
|
|
317
|
+
if (!(opts == null ? void 0 : opts.user) || !opts.user.roleIds.includes(op.roleId)) {
|
|
318
|
+
console.error(UNAUTHORIZED_MESSAGE);
|
|
319
|
+
throw new Error(UNAUTHORIZED_MESSAGE);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const host = getConfig("__PLASMIC_DATA_HOST", DEFAULT_HOST);
|
|
323
|
+
const url = `${host}/api/v1/server-data/sources/${op.sourceId}/execute`;
|
|
324
|
+
const resp = yield (0, import_isomorphic_unfetch.default)(url, {
|
|
325
|
+
method: "POST",
|
|
326
|
+
headers: __spreadValues({
|
|
327
|
+
"Content-Type": "application/json"
|
|
328
|
+
}, (opts == null ? void 0 : opts.userAuthToken) && {
|
|
329
|
+
"x-plasmic-data-user-auth-token": opts.userAuthToken
|
|
330
|
+
}),
|
|
331
|
+
body: (0, import_fast_stringify.default)({
|
|
332
|
+
opId: op.opId,
|
|
333
|
+
userArgs: (_a = op.userArgs) != null ? _a : {},
|
|
334
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
335
|
+
})
|
|
336
|
+
});
|
|
337
|
+
if (resp.status !== 200) {
|
|
338
|
+
const text = yield resp.text();
|
|
339
|
+
throw new Error(text);
|
|
340
|
+
}
|
|
341
|
+
return yield resp.json();
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
function getConfig(key, defaultValue) {
|
|
345
|
+
var _a;
|
|
346
|
+
if (typeof globalThis === "undefined") {
|
|
347
|
+
return defaultValue;
|
|
348
|
+
} else {
|
|
349
|
+
return (_a = globalThis[key]) != null ? _a : defaultValue;
|
|
350
|
+
}
|
|
320
351
|
}
|
|
321
|
-
|
|
322
|
-
// src/serverQueries/common.ts
|
|
323
|
-
var import_react2 = __toESM(require("react"));
|
|
324
352
|
|
|
325
353
|
// src/serverQueries/makeQueryCacheKey.ts
|
|
326
354
|
function makeQueryCacheKey(id, params) {
|
|
327
|
-
return `${id}
|
|
355
|
+
return `$q.$.${id}.$.${safeStableStringify(params)}`;
|
|
356
|
+
}
|
|
357
|
+
function matchesQueryCacheKey(cacheKey, invalidationKey) {
|
|
358
|
+
return cacheKey.includes(`.$.${invalidationKey}.$.`);
|
|
328
359
|
}
|
|
329
360
|
var shortPlasmicPrefix = "\u03C1";
|
|
330
361
|
function safeStableStringify(unstableValue) {
|
|
@@ -384,7 +415,194 @@ function sortObjectsDeep(value, visitedObjects) {
|
|
|
384
415
|
}
|
|
385
416
|
}
|
|
386
417
|
|
|
418
|
+
// src/utils.ts
|
|
419
|
+
function noopFn() {
|
|
420
|
+
}
|
|
421
|
+
function pick(obj, ...keys) {
|
|
422
|
+
const res = {};
|
|
423
|
+
for (const key of keys) {
|
|
424
|
+
if (key in obj) {
|
|
425
|
+
res[key] = obj[key];
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return res;
|
|
429
|
+
}
|
|
430
|
+
var tuple = (...args) => args;
|
|
431
|
+
function mkIdMap(xs) {
|
|
432
|
+
return new Map(xs.map((x) => tuple(x.id, x)));
|
|
433
|
+
}
|
|
434
|
+
function notNil(x) {
|
|
435
|
+
return x !== null && x !== void 0;
|
|
436
|
+
}
|
|
437
|
+
function withoutNils(xs) {
|
|
438
|
+
return xs.filter(notNil);
|
|
439
|
+
}
|
|
440
|
+
function mapRecordEntries(callback, record1, record2, record3) {
|
|
441
|
+
return Object.entries(record1).map(([k, v1]) => {
|
|
442
|
+
const v2 = record2 == null ? void 0 : record2[k];
|
|
443
|
+
const v3 = record3 == null ? void 0 : record3[k];
|
|
444
|
+
return callback(
|
|
445
|
+
k,
|
|
446
|
+
v1,
|
|
447
|
+
v2,
|
|
448
|
+
v3
|
|
449
|
+
);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
function mapRecords(callback, record1, record2, record3) {
|
|
453
|
+
return Object.fromEntries(
|
|
454
|
+
mapRecordEntries(
|
|
455
|
+
(k, v1, v2, v3) => [k, callback(k, v1, v2, v3)],
|
|
456
|
+
record1,
|
|
457
|
+
record2,
|
|
458
|
+
record3
|
|
459
|
+
)
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// src/hooks/usePlasmicDataOp.tsx
|
|
464
|
+
function makeCacheKey(dataOp, opts) {
|
|
465
|
+
const queryDependencies = JSON.stringify({
|
|
466
|
+
sourceId: dataOp.sourceId,
|
|
467
|
+
opId: dataOp.opId,
|
|
468
|
+
args: dataOp.userArgs,
|
|
469
|
+
userAuthToken: opts == null ? void 0 : opts.userAuthToken,
|
|
470
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
471
|
+
});
|
|
472
|
+
return dataOp.cacheKey ? `${dataOp.cacheKey}${queryDependencies}` : queryDependencies;
|
|
473
|
+
}
|
|
474
|
+
function usePlasmicInvalidate() {
|
|
475
|
+
const { cache, fallback, mutate } = (0, import_query3.usePlasmicDataConfig)();
|
|
476
|
+
return (invalidatedKeys) => __async(null, null, function* () {
|
|
477
|
+
const getKeysToInvalidate = () => {
|
|
478
|
+
var _a, _b;
|
|
479
|
+
if (!invalidatedKeys) {
|
|
480
|
+
return [];
|
|
481
|
+
}
|
|
482
|
+
const allKeys = Array.from(
|
|
483
|
+
/* @__PURE__ */ new Set([
|
|
484
|
+
...Array.from(cache.keys()),
|
|
485
|
+
...fallback ? Object.keys(fallback) : [],
|
|
486
|
+
// If this is running within the Studio, we also take the
|
|
487
|
+
// opportunity to invalidate the Studio cache. The keys that
|
|
488
|
+
// Studio may have can be a superset of `cache` here, because
|
|
489
|
+
// `cache` is updated as swr hooks are mounted and unmounted,
|
|
490
|
+
// but Studio's data cache keys don't get removed when hooks
|
|
491
|
+
// are unmounted. This makes it possible for Studio to hold
|
|
492
|
+
// onto a stale cache entry that doesn't get invalidated.
|
|
493
|
+
// For example, Studio may render page1, with key X, then goes
|
|
494
|
+
// to page 2, which performs a mutate. At this point, Studio
|
|
495
|
+
// has a cache entry for key X, but `cache` does not, because
|
|
496
|
+
// page2 does not use that query. But page 2 may perform a
|
|
497
|
+
// mutation that invalidates X. So we need to invalidate not
|
|
498
|
+
// only keys in `cache`, but also keys that Studio is still
|
|
499
|
+
// holding onto.
|
|
500
|
+
...(_b = (_a = globalThis.__PLASMIC_GET_ALL_CACHE_KEYS) == null ? void 0 : _a.call(globalThis)) != null ? _b : []
|
|
501
|
+
])
|
|
502
|
+
).filter((key) => typeof key === "string");
|
|
503
|
+
if (invalidatedKeys.includes("plasmic_refresh_all")) {
|
|
504
|
+
return allKeys;
|
|
505
|
+
}
|
|
506
|
+
return allKeys.filter(
|
|
507
|
+
(key) => invalidatedKeys.some((k) => matchesQueryCacheKey(key, k))
|
|
508
|
+
);
|
|
509
|
+
};
|
|
510
|
+
const keys = getKeysToInvalidate();
|
|
511
|
+
if (keys.length === 0) {
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
const invalidateKey = (key) => __async(null, null, function* () {
|
|
515
|
+
const studioInvalidate = globalThis.__PLASMIC_MUTATE_DATA_OP;
|
|
516
|
+
if (studioInvalidate) {
|
|
517
|
+
yield studioInvalidate(key);
|
|
518
|
+
}
|
|
519
|
+
return mutate(key);
|
|
520
|
+
});
|
|
521
|
+
return yield Promise.all(keys.map((key) => invalidateKey(key)));
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
function resolveDataOp(dataOp) {
|
|
525
|
+
if (typeof dataOp === "function") {
|
|
526
|
+
try {
|
|
527
|
+
return dataOp();
|
|
528
|
+
} catch (err) {
|
|
529
|
+
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
530
|
+
return err;
|
|
531
|
+
}
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
} else {
|
|
535
|
+
return dataOp;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
function usePlasmicDataOp(dataOp, opts) {
|
|
539
|
+
const resolvedDataOp = resolveDataOp(dataOp);
|
|
540
|
+
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
541
|
+
const key = !resolvedDataOp || isPlasmicUndefinedDataErrorPromise(resolvedDataOp) ? null : makeCacheKey(resolvedDataOp, {
|
|
542
|
+
paginate: opts == null ? void 0 : opts.paginate,
|
|
543
|
+
userAuthToken: ctx == null ? void 0 : ctx.userAuthToken
|
|
544
|
+
});
|
|
545
|
+
const fetcher = (op) => {
|
|
546
|
+
return executePlasmicDataOp(op, {
|
|
547
|
+
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
548
|
+
user: ctx == null ? void 0 : ctx.user,
|
|
549
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
550
|
+
});
|
|
551
|
+
};
|
|
552
|
+
const resultMapper = (result) => {
|
|
553
|
+
var _a;
|
|
554
|
+
return __spreadValues(__spreadValues({}, (_a = result.data) != null ? _a : {}), pick(result, "error", "isLoading"));
|
|
555
|
+
};
|
|
556
|
+
return usePlasmicFetch(
|
|
557
|
+
key,
|
|
558
|
+
resolvedDataOp,
|
|
559
|
+
fetcher,
|
|
560
|
+
resultMapper,
|
|
561
|
+
["data", "schema", "error"],
|
|
562
|
+
{
|
|
563
|
+
noUndefinedDataProxy: opts == null ? void 0 : opts.noUndefinedDataProxy
|
|
564
|
+
}
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
function usePlasmicDataMutationOp(dataOp) {
|
|
568
|
+
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
569
|
+
const userToken = ctx == null ? void 0 : ctx.userAuthToken;
|
|
570
|
+
const getRealDataOp = React2.useCallback(() => __async(null, null, function* () {
|
|
571
|
+
const tryGetRealDataOp = () => __async(null, null, function* () {
|
|
572
|
+
const resolved = resolveDataOp(dataOp);
|
|
573
|
+
if (!resolved) {
|
|
574
|
+
return null;
|
|
575
|
+
} else if (isPlasmicUndefinedDataErrorPromise(resolved)) {
|
|
576
|
+
yield resolved;
|
|
577
|
+
return tryGetRealDataOp();
|
|
578
|
+
} else {
|
|
579
|
+
return resolved;
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
return yield tryGetRealDataOp();
|
|
583
|
+
}), [dataOp]);
|
|
584
|
+
return React2.useCallback(() => __async(null, null, function* () {
|
|
585
|
+
var _a;
|
|
586
|
+
const { sourceId, opId, userArgs } = (_a = yield getRealDataOp()) != null ? _a : {};
|
|
587
|
+
if (!sourceId || !opId) {
|
|
588
|
+
return void 0;
|
|
589
|
+
}
|
|
590
|
+
return executePlasmicDataOp(
|
|
591
|
+
{ sourceId, opId, userArgs },
|
|
592
|
+
{
|
|
593
|
+
userAuthToken: userToken || void 0,
|
|
594
|
+
user: ctx == null ? void 0 : ctx.user
|
|
595
|
+
}
|
|
596
|
+
);
|
|
597
|
+
}), [getRealDataOp, userToken]);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// src/serverQueries/client.ts
|
|
601
|
+
var import_query4 = require("@plasmicapp/query");
|
|
602
|
+
var React4 = __toESM(require("react"));
|
|
603
|
+
|
|
387
604
|
// src/serverQueries/common.ts
|
|
605
|
+
var import_react2 = __toESM(require("react"));
|
|
388
606
|
function createDollarQueries(queryNames) {
|
|
389
607
|
return Object.fromEntries(
|
|
390
608
|
queryNames.map((queryName) => {
|
|
@@ -680,8 +898,8 @@ function createInitial$State($ctx, $props, $q, stateSpecs) {
|
|
|
680
898
|
var GLOBAL_CACHE = /* @__PURE__ */ new Map();
|
|
681
899
|
function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
682
900
|
var _a;
|
|
683
|
-
const wrappedQueries =
|
|
684
|
-
const $queries =
|
|
901
|
+
const wrappedQueries = React4.useMemo(() => wrapQueries(tree.queries), [tree]);
|
|
902
|
+
const $queries = React4.useMemo(
|
|
685
903
|
() => createDollarQueries(Object.keys(tree.queries)),
|
|
686
904
|
[tree]
|
|
687
905
|
);
|
|
@@ -689,7 +907,7 @@ function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
|
689
907
|
if (!$state) {
|
|
690
908
|
$state = createInitial$State($ctx, $props, $queryStates, tree.stateSpecs);
|
|
691
909
|
}
|
|
692
|
-
const { fallback: prefetchedCache, cache: swrCache } = (0,
|
|
910
|
+
const { fallback: prefetchedCache, cache: swrCache } = (0, import_query4.usePlasmicDataConfig)();
|
|
693
911
|
const paramsResults = {};
|
|
694
912
|
const executionCtx = {
|
|
695
913
|
$ctx,
|
|
@@ -728,7 +946,7 @@ function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
|
728
946
|
delete paramsResults[k];
|
|
729
947
|
}
|
|
730
948
|
}
|
|
731
|
-
const stopRef =
|
|
949
|
+
const stopRef = React4.useRef();
|
|
732
950
|
(_a = stopRef.current) == null ? void 0 : _a.call(stopRef);
|
|
733
951
|
let stopped = false;
|
|
734
952
|
const stop = new Promise((resolve) => {
|
|
@@ -737,7 +955,7 @@ function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
|
737
955
|
resolve();
|
|
738
956
|
};
|
|
739
957
|
});
|
|
740
|
-
|
|
958
|
+
React4.useEffect(() => () => {
|
|
741
959
|
var _a2;
|
|
742
960
|
return (_a2 = stopRef.current) == null ? void 0 : _a2.call(stopRef);
|
|
743
961
|
}, []);
|
|
@@ -785,7 +1003,7 @@ function wrapQueries(queries) {
|
|
|
785
1003
|
if (cached) {
|
|
786
1004
|
return cached.promise;
|
|
787
1005
|
}
|
|
788
|
-
const promise = (0,
|
|
1006
|
+
const promise = (0, import_query4.wrapLoadingFetcher)(query.fn)(...args);
|
|
789
1007
|
GLOBAL_CACHE.set(cacheKey, new SyncPromise(promise));
|
|
790
1008
|
return promise;
|
|
791
1009
|
};
|
|
@@ -854,7 +1072,7 @@ function initPlasmicQueriesSync($queries, queries, paramsResults, executionCtx,
|
|
|
854
1072
|
}
|
|
855
1073
|
function usePlasmicQuery($query, query, paramsResult) {
|
|
856
1074
|
const $queryState = $query;
|
|
857
|
-
const { key, fetcher } =
|
|
1075
|
+
const { key, fetcher } = React4.useMemo(() => {
|
|
858
1076
|
switch (paramsResult.status) {
|
|
859
1077
|
case "blocked":
|
|
860
1078
|
case "error": {
|
|
@@ -901,7 +1119,7 @@ function usePlasmicQuery($query, query, paramsResult) {
|
|
|
901
1119
|
}
|
|
902
1120
|
}
|
|
903
1121
|
}, [query, paramsResult]);
|
|
904
|
-
const result = (0,
|
|
1122
|
+
const result = (0, import_query4.useMutablePlasmicQueryData)(key, fetcher, {
|
|
905
1123
|
// If revalidateIfStale is true, then if there's a cache entry with a key,
|
|
906
1124
|
// but no mounted hook with that key yet, and when the hook mounts with the key,
|
|
907
1125
|
// swr will revalidate. This may be reasonable behavior, but for us, this
|
|
@@ -1209,224 +1427,6 @@ var import_query5 = require("@plasmicapp/query");
|
|
|
1209
1427
|
|
|
1210
1428
|
// src/components/Fetcher.tsx
|
|
1211
1429
|
var import_react3 = __toESM(require("react"));
|
|
1212
|
-
|
|
1213
|
-
// src/hooks/usePlasmicDataOp.tsx
|
|
1214
|
-
var import_data_sources_context = require("@plasmicapp/data-sources-context");
|
|
1215
|
-
var import_query4 = require("@plasmicapp/query");
|
|
1216
|
-
var React4 = __toESM(require("react"));
|
|
1217
|
-
|
|
1218
|
-
// src/executor.tsx
|
|
1219
|
-
var import_isomorphic_unfetch = __toESM(require("@plasmicapp/isomorphic-unfetch"));
|
|
1220
|
-
var import_query3 = require("@plasmicapp/query");
|
|
1221
|
-
var import_fast_stringify = __toESM(require("fast-stringify"));
|
|
1222
|
-
|
|
1223
|
-
// src/placeholders.ts
|
|
1224
|
-
var PLASMIC_UNDEFINED = "__PLASMIC_UNDEFINED";
|
|
1225
|
-
function addPlaceholders(val) {
|
|
1226
|
-
return val === void 0 ? PLASMIC_UNDEFINED : val;
|
|
1227
|
-
}
|
|
1228
|
-
function addPlaceholdersToUserArgs(userArgs) {
|
|
1229
|
-
if (!userArgs) {
|
|
1230
|
-
return userArgs;
|
|
1231
|
-
}
|
|
1232
|
-
Object.entries(userArgs).forEach(([key, val]) => {
|
|
1233
|
-
userArgs[key] = Array.isArray(val) ? val.map((v) => addPlaceholders(v)) : addPlaceholders(val);
|
|
1234
|
-
});
|
|
1235
|
-
return userArgs;
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
// src/executor.tsx
|
|
1239
|
-
var DEFAULT_HOST = "https://data.plasmic.app";
|
|
1240
|
-
var UNAUTHORIZED_MESSAGE = "You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";
|
|
1241
|
-
function executePlasmicDataOp(op, opts) {
|
|
1242
|
-
return __async(this, null, function* () {
|
|
1243
|
-
const func = getConfig(
|
|
1244
|
-
"__PLASMIC_EXECUTE_DATA_OP",
|
|
1245
|
-
_executePlasmicDataOp
|
|
1246
|
-
);
|
|
1247
|
-
op.userArgs = addPlaceholdersToUserArgs(op.userArgs);
|
|
1248
|
-
const res = yield (0, import_query3.wrapLoadingFetcher)(func)(op, opts);
|
|
1249
|
-
return res;
|
|
1250
|
-
});
|
|
1251
|
-
}
|
|
1252
|
-
function _executePlasmicDataOp(op, opts) {
|
|
1253
|
-
return __async(this, null, function* () {
|
|
1254
|
-
var _a;
|
|
1255
|
-
if (op.roleId) {
|
|
1256
|
-
if (!(opts == null ? void 0 : opts.user) || !opts.user.roleIds.includes(op.roleId)) {
|
|
1257
|
-
console.error(UNAUTHORIZED_MESSAGE);
|
|
1258
|
-
throw new Error(UNAUTHORIZED_MESSAGE);
|
|
1259
|
-
}
|
|
1260
|
-
}
|
|
1261
|
-
const host = getConfig("__PLASMIC_DATA_HOST", DEFAULT_HOST);
|
|
1262
|
-
const url = `${host}/api/v1/server-data/sources/${op.sourceId}/execute`;
|
|
1263
|
-
const resp = yield (0, import_isomorphic_unfetch.default)(url, {
|
|
1264
|
-
method: "POST",
|
|
1265
|
-
headers: __spreadValues({
|
|
1266
|
-
"Content-Type": "application/json"
|
|
1267
|
-
}, (opts == null ? void 0 : opts.userAuthToken) && {
|
|
1268
|
-
"x-plasmic-data-user-auth-token": opts.userAuthToken
|
|
1269
|
-
}),
|
|
1270
|
-
body: (0, import_fast_stringify.default)({
|
|
1271
|
-
opId: op.opId,
|
|
1272
|
-
userArgs: (_a = op.userArgs) != null ? _a : {},
|
|
1273
|
-
paginate: opts == null ? void 0 : opts.paginate
|
|
1274
|
-
})
|
|
1275
|
-
});
|
|
1276
|
-
if (resp.status !== 200) {
|
|
1277
|
-
const text = yield resp.text();
|
|
1278
|
-
throw new Error(text);
|
|
1279
|
-
}
|
|
1280
|
-
return yield resp.json();
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
function getConfig(key, defaultValue) {
|
|
1284
|
-
var _a;
|
|
1285
|
-
if (typeof globalThis === "undefined") {
|
|
1286
|
-
return defaultValue;
|
|
1287
|
-
} else {
|
|
1288
|
-
return (_a = globalThis[key]) != null ? _a : defaultValue;
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
|
-
// src/hooks/usePlasmicDataOp.tsx
|
|
1293
|
-
function makeCacheKey(dataOp, opts) {
|
|
1294
|
-
const queryDependencies = JSON.stringify({
|
|
1295
|
-
sourceId: dataOp.sourceId,
|
|
1296
|
-
opId: dataOp.opId,
|
|
1297
|
-
args: dataOp.userArgs,
|
|
1298
|
-
userAuthToken: opts == null ? void 0 : opts.userAuthToken,
|
|
1299
|
-
paginate: opts == null ? void 0 : opts.paginate
|
|
1300
|
-
});
|
|
1301
|
-
return dataOp.cacheKey ? `${dataOp.cacheKey}${queryDependencies}` : queryDependencies;
|
|
1302
|
-
}
|
|
1303
|
-
function usePlasmicInvalidate() {
|
|
1304
|
-
const { cache, fallback, mutate } = (0, import_query4.usePlasmicDataConfig)();
|
|
1305
|
-
return (invalidatedKeys) => __async(null, null, function* () {
|
|
1306
|
-
const getKeysToInvalidate = () => {
|
|
1307
|
-
var _a, _b;
|
|
1308
|
-
if (!invalidatedKeys) {
|
|
1309
|
-
return [];
|
|
1310
|
-
}
|
|
1311
|
-
const allKeys = Array.from(
|
|
1312
|
-
/* @__PURE__ */ new Set([
|
|
1313
|
-
...Array.from(cache.keys()),
|
|
1314
|
-
...fallback ? Object.keys(fallback) : [],
|
|
1315
|
-
// If this is running within the Studio, we also take the
|
|
1316
|
-
// opportunity to invalidate the Studio cache. The keys that
|
|
1317
|
-
// Studio may have can be a superset of `cache` here, because
|
|
1318
|
-
// `cache` is updated as swr hooks are mounted and unmounted,
|
|
1319
|
-
// but Studio's data cache keys don't get removed when hooks
|
|
1320
|
-
// are unmounted. This makes it possible for Studio to hold
|
|
1321
|
-
// onto a stale cache entry that doesn't get invalidated.
|
|
1322
|
-
// For example, Studio may render page1, with key X, then goes
|
|
1323
|
-
// to page 2, which performs a mutate. At this point, Studio
|
|
1324
|
-
// has a cache entry for key X, but `cache` does not, because
|
|
1325
|
-
// page2 does not use that query. But page 2 may perform a
|
|
1326
|
-
// mutation that invalidates X. So we need to invalidate not
|
|
1327
|
-
// only keys in `cache`, but also keys that Studio is still
|
|
1328
|
-
// holding onto.
|
|
1329
|
-
...(_b = (_a = globalThis.__PLASMIC_GET_ALL_CACHE_KEYS) == null ? void 0 : _a.call(globalThis)) != null ? _b : []
|
|
1330
|
-
])
|
|
1331
|
-
).filter((key) => typeof key === "string");
|
|
1332
|
-
if (invalidatedKeys.includes("plasmic_refresh_all")) {
|
|
1333
|
-
return allKeys;
|
|
1334
|
-
}
|
|
1335
|
-
return allKeys.filter(
|
|
1336
|
-
(key) => invalidatedKeys.some((k) => key.includes(`.$.${k}.$.`))
|
|
1337
|
-
);
|
|
1338
|
-
};
|
|
1339
|
-
const keys = getKeysToInvalidate();
|
|
1340
|
-
if (keys.length === 0) {
|
|
1341
|
-
return;
|
|
1342
|
-
}
|
|
1343
|
-
const invalidateKey = (key) => __async(null, null, function* () {
|
|
1344
|
-
const studioInvalidate = globalThis.__PLASMIC_MUTATE_DATA_OP;
|
|
1345
|
-
if (studioInvalidate) {
|
|
1346
|
-
yield studioInvalidate(key);
|
|
1347
|
-
}
|
|
1348
|
-
return mutate(key);
|
|
1349
|
-
});
|
|
1350
|
-
return yield Promise.all(keys.map((key) => invalidateKey(key)));
|
|
1351
|
-
});
|
|
1352
|
-
}
|
|
1353
|
-
function resolveDataOp(dataOp) {
|
|
1354
|
-
if (typeof dataOp === "function") {
|
|
1355
|
-
try {
|
|
1356
|
-
return dataOp();
|
|
1357
|
-
} catch (err) {
|
|
1358
|
-
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
1359
|
-
return err;
|
|
1360
|
-
}
|
|
1361
|
-
return null;
|
|
1362
|
-
}
|
|
1363
|
-
} else {
|
|
1364
|
-
return dataOp;
|
|
1365
|
-
}
|
|
1366
|
-
}
|
|
1367
|
-
function usePlasmicDataOp(dataOp, opts) {
|
|
1368
|
-
const resolvedDataOp = resolveDataOp(dataOp);
|
|
1369
|
-
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
1370
|
-
const key = !resolvedDataOp || isPlasmicUndefinedDataErrorPromise(resolvedDataOp) ? null : makeCacheKey(resolvedDataOp, {
|
|
1371
|
-
paginate: opts == null ? void 0 : opts.paginate,
|
|
1372
|
-
userAuthToken: ctx == null ? void 0 : ctx.userAuthToken
|
|
1373
|
-
});
|
|
1374
|
-
const fetcher = (op) => {
|
|
1375
|
-
return executePlasmicDataOp(op, {
|
|
1376
|
-
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
1377
|
-
user: ctx == null ? void 0 : ctx.user,
|
|
1378
|
-
paginate: opts == null ? void 0 : opts.paginate
|
|
1379
|
-
});
|
|
1380
|
-
};
|
|
1381
|
-
const resultMapper = (result) => {
|
|
1382
|
-
var _a;
|
|
1383
|
-
return __spreadValues(__spreadValues({}, (_a = result.data) != null ? _a : {}), pick(result, "error", "isLoading"));
|
|
1384
|
-
};
|
|
1385
|
-
return usePlasmicFetch(
|
|
1386
|
-
key,
|
|
1387
|
-
resolvedDataOp,
|
|
1388
|
-
fetcher,
|
|
1389
|
-
resultMapper,
|
|
1390
|
-
["data", "schema", "error"],
|
|
1391
|
-
{
|
|
1392
|
-
noUndefinedDataProxy: opts == null ? void 0 : opts.noUndefinedDataProxy
|
|
1393
|
-
}
|
|
1394
|
-
);
|
|
1395
|
-
}
|
|
1396
|
-
function usePlasmicDataMutationOp(dataOp) {
|
|
1397
|
-
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
1398
|
-
const userToken = ctx == null ? void 0 : ctx.userAuthToken;
|
|
1399
|
-
const getRealDataOp = React4.useCallback(() => __async(null, null, function* () {
|
|
1400
|
-
const tryGetRealDataOp = () => __async(null, null, function* () {
|
|
1401
|
-
const resolved = resolveDataOp(dataOp);
|
|
1402
|
-
if (!resolved) {
|
|
1403
|
-
return null;
|
|
1404
|
-
} else if (isPlasmicUndefinedDataErrorPromise(resolved)) {
|
|
1405
|
-
yield resolved;
|
|
1406
|
-
return tryGetRealDataOp();
|
|
1407
|
-
} else {
|
|
1408
|
-
return resolved;
|
|
1409
|
-
}
|
|
1410
|
-
});
|
|
1411
|
-
return yield tryGetRealDataOp();
|
|
1412
|
-
}), [dataOp]);
|
|
1413
|
-
return React4.useCallback(() => __async(null, null, function* () {
|
|
1414
|
-
var _a;
|
|
1415
|
-
const { sourceId, opId, userArgs } = (_a = yield getRealDataOp()) != null ? _a : {};
|
|
1416
|
-
if (!sourceId || !opId) {
|
|
1417
|
-
return void 0;
|
|
1418
|
-
}
|
|
1419
|
-
return executePlasmicDataOp(
|
|
1420
|
-
{ sourceId, opId, userArgs },
|
|
1421
|
-
{
|
|
1422
|
-
userAuthToken: userToken || void 0,
|
|
1423
|
-
user: ctx == null ? void 0 : ctx.user
|
|
1424
|
-
}
|
|
1425
|
-
);
|
|
1426
|
-
}), [getRealDataOp, userToken]);
|
|
1427
|
-
}
|
|
1428
|
-
|
|
1429
|
-
// src/components/Fetcher.tsx
|
|
1430
1430
|
function Fetcher(props) {
|
|
1431
1431
|
var _a;
|
|
1432
1432
|
const { dataOp, children, name, pageIndex, pageSize } = props;
|