@plasmicapp/data-sources 1.0.10 → 1.0.12
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 +19 -3
- package/dist/index.esm.js +276 -275
- package/dist/index.esm.js.map +4 -4
- package/dist/index.js +270 -269
- package/dist/index.js.map +4 -4
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -74,8 +74,10 @@ __export(index_exports, {
|
|
|
74
74
|
deriveFieldConfigs: () => deriveFieldConfigs,
|
|
75
75
|
executePlasmicDataOp: () => executePlasmicDataOp,
|
|
76
76
|
executeServerQuery: () => executeServerQuery,
|
|
77
|
+
isPlasmicUndefinedDataErrorPromise: () => isPlasmicUndefinedDataErrorPromise,
|
|
77
78
|
makeCacheKey: () => makeCacheKey,
|
|
78
79
|
makeQueryCacheKey: () => makeQueryCacheKey,
|
|
80
|
+
matchesQueryCacheKey: () => matchesQueryCacheKey,
|
|
79
81
|
normalizeData: () => normalizeData,
|
|
80
82
|
throwIfPlasmicUndefinedDataError: () => throwIfPlasmicUndefinedDataError,
|
|
81
83
|
unstable_executePlasmicQueries: () => executePlasmicQueries,
|
|
@@ -270,61 +272,91 @@ function usePlasmicFetch(key, resolvedParams, fetcherFn, resultMapper, undefined
|
|
|
270
272
|
]);
|
|
271
273
|
}
|
|
272
274
|
|
|
273
|
-
// src/
|
|
275
|
+
// src/hooks/usePlasmicDataOp.tsx
|
|
276
|
+
var import_data_sources_context = require("@plasmicapp/data-sources-context");
|
|
277
|
+
var import_query3 = require("@plasmicapp/query");
|
|
278
|
+
var React2 = __toESM(require("react"));
|
|
279
|
+
|
|
280
|
+
// src/executor.tsx
|
|
281
|
+
var import_isomorphic_unfetch = __toESM(require("@plasmicapp/isomorphic-unfetch"));
|
|
274
282
|
var import_query2 = require("@plasmicapp/query");
|
|
275
|
-
var
|
|
283
|
+
var import_fast_stringify = __toESM(require("fast-stringify"));
|
|
276
284
|
|
|
277
|
-
// src/
|
|
278
|
-
|
|
285
|
+
// src/placeholders.ts
|
|
286
|
+
var PLASMIC_UNDEFINED = "__PLASMIC_UNDEFINED";
|
|
287
|
+
function addPlaceholders(val) {
|
|
288
|
+
return val === void 0 ? PLASMIC_UNDEFINED : val;
|
|
279
289
|
}
|
|
280
|
-
function
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (key in obj) {
|
|
284
|
-
res[key] = obj[key];
|
|
285
|
-
}
|
|
290
|
+
function addPlaceholdersToUserArgs(userArgs) {
|
|
291
|
+
if (!userArgs) {
|
|
292
|
+
return userArgs;
|
|
286
293
|
}
|
|
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);
|
|
294
|
+
Object.entries(userArgs).forEach(([key, val]) => {
|
|
295
|
+
userArgs[key] = Array.isArray(val) ? val.map((v) => addPlaceholders(v)) : addPlaceholders(val);
|
|
296
|
+
});
|
|
297
|
+
return userArgs;
|
|
298
298
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
299
|
+
|
|
300
|
+
// src/executor.tsx
|
|
301
|
+
var DEFAULT_HOST = "https://data.plasmic.app";
|
|
302
|
+
var UNAUTHORIZED_MESSAGE = "You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";
|
|
303
|
+
function executePlasmicDataOp(op, opts) {
|
|
304
|
+
return __async(this, null, function* () {
|
|
305
|
+
const func = getConfig(
|
|
306
|
+
"__PLASMIC_EXECUTE_DATA_OP",
|
|
307
|
+
_executePlasmicDataOp
|
|
308
308
|
);
|
|
309
|
+
op.userArgs = addPlaceholdersToUserArgs(op.userArgs);
|
|
310
|
+
const res = yield (0, import_query2.wrapLoadingFetcher)(func)(op, opts);
|
|
311
|
+
return res;
|
|
309
312
|
});
|
|
310
313
|
}
|
|
311
|
-
function
|
|
312
|
-
return
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
314
|
+
function _executePlasmicDataOp(op, opts) {
|
|
315
|
+
return __async(this, null, function* () {
|
|
316
|
+
var _a;
|
|
317
|
+
if (op.roleId) {
|
|
318
|
+
if (!(opts == null ? void 0 : opts.user) || !opts.user.roleIds.includes(op.roleId)) {
|
|
319
|
+
console.error(UNAUTHORIZED_MESSAGE);
|
|
320
|
+
throw new Error(UNAUTHORIZED_MESSAGE);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const host = getConfig("__PLASMIC_DATA_HOST", DEFAULT_HOST);
|
|
324
|
+
const url = `${host}/api/v1/server-data/sources/${op.sourceId}/execute`;
|
|
325
|
+
const resp = yield (0, import_isomorphic_unfetch.default)(url, {
|
|
326
|
+
method: "POST",
|
|
327
|
+
headers: __spreadValues({
|
|
328
|
+
"Content-Type": "application/json"
|
|
329
|
+
}, (opts == null ? void 0 : opts.userAuthToken) && {
|
|
330
|
+
"x-plasmic-data-user-auth-token": opts.userAuthToken
|
|
331
|
+
}),
|
|
332
|
+
body: (0, import_fast_stringify.default)({
|
|
333
|
+
opId: op.opId,
|
|
334
|
+
userArgs: (_a = op.userArgs) != null ? _a : {},
|
|
335
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
336
|
+
})
|
|
337
|
+
});
|
|
338
|
+
if (resp.status !== 200) {
|
|
339
|
+
const text = yield resp.text();
|
|
340
|
+
throw new Error(text);
|
|
341
|
+
}
|
|
342
|
+
return yield resp.json();
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
function getConfig(key, defaultValue) {
|
|
346
|
+
var _a;
|
|
347
|
+
if (typeof globalThis === "undefined") {
|
|
348
|
+
return defaultValue;
|
|
349
|
+
} else {
|
|
350
|
+
return (_a = globalThis[key]) != null ? _a : defaultValue;
|
|
351
|
+
}
|
|
320
352
|
}
|
|
321
|
-
|
|
322
|
-
// src/serverQueries/common.ts
|
|
323
|
-
var import_react2 = __toESM(require("react"));
|
|
324
353
|
|
|
325
354
|
// src/serverQueries/makeQueryCacheKey.ts
|
|
326
355
|
function makeQueryCacheKey(id, params) {
|
|
327
|
-
return `${id}
|
|
356
|
+
return `$q.$.${id}.$.${safeStableStringify(params)}`;
|
|
357
|
+
}
|
|
358
|
+
function matchesQueryCacheKey(cacheKey, invalidationKey) {
|
|
359
|
+
return cacheKey.includes(`.$.${invalidationKey}.$.`);
|
|
328
360
|
}
|
|
329
361
|
var shortPlasmicPrefix = "\u03C1";
|
|
330
362
|
function safeStableStringify(unstableValue) {
|
|
@@ -384,7 +416,194 @@ function sortObjectsDeep(value, visitedObjects) {
|
|
|
384
416
|
}
|
|
385
417
|
}
|
|
386
418
|
|
|
419
|
+
// src/utils.ts
|
|
420
|
+
function noopFn() {
|
|
421
|
+
}
|
|
422
|
+
function pick(obj, ...keys) {
|
|
423
|
+
const res = {};
|
|
424
|
+
for (const key of keys) {
|
|
425
|
+
if (key in obj) {
|
|
426
|
+
res[key] = obj[key];
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return res;
|
|
430
|
+
}
|
|
431
|
+
var tuple = (...args) => args;
|
|
432
|
+
function mkIdMap(xs) {
|
|
433
|
+
return new Map(xs.map((x) => tuple(x.id, x)));
|
|
434
|
+
}
|
|
435
|
+
function notNil(x) {
|
|
436
|
+
return x !== null && x !== void 0;
|
|
437
|
+
}
|
|
438
|
+
function withoutNils(xs) {
|
|
439
|
+
return xs.filter(notNil);
|
|
440
|
+
}
|
|
441
|
+
function mapRecordEntries(callback, record1, record2, record3) {
|
|
442
|
+
return Object.entries(record1).map(([k, v1]) => {
|
|
443
|
+
const v2 = record2 == null ? void 0 : record2[k];
|
|
444
|
+
const v3 = record3 == null ? void 0 : record3[k];
|
|
445
|
+
return callback(
|
|
446
|
+
k,
|
|
447
|
+
v1,
|
|
448
|
+
v2,
|
|
449
|
+
v3
|
|
450
|
+
);
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
function mapRecords(callback, record1, record2, record3) {
|
|
454
|
+
return Object.fromEntries(
|
|
455
|
+
mapRecordEntries(
|
|
456
|
+
(k, v1, v2, v3) => [k, callback(k, v1, v2, v3)],
|
|
457
|
+
record1,
|
|
458
|
+
record2,
|
|
459
|
+
record3
|
|
460
|
+
)
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// src/hooks/usePlasmicDataOp.tsx
|
|
465
|
+
function makeCacheKey(dataOp, opts) {
|
|
466
|
+
const queryDependencies = JSON.stringify({
|
|
467
|
+
sourceId: dataOp.sourceId,
|
|
468
|
+
opId: dataOp.opId,
|
|
469
|
+
args: dataOp.userArgs,
|
|
470
|
+
userAuthToken: opts == null ? void 0 : opts.userAuthToken,
|
|
471
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
472
|
+
});
|
|
473
|
+
return dataOp.cacheKey ? `${dataOp.cacheKey}${queryDependencies}` : queryDependencies;
|
|
474
|
+
}
|
|
475
|
+
function usePlasmicInvalidate() {
|
|
476
|
+
const { cache, fallback, mutate } = (0, import_query3.usePlasmicDataConfig)();
|
|
477
|
+
return (invalidatedKeys) => __async(null, null, function* () {
|
|
478
|
+
const getKeysToInvalidate = () => {
|
|
479
|
+
var _a, _b;
|
|
480
|
+
if (!invalidatedKeys) {
|
|
481
|
+
return [];
|
|
482
|
+
}
|
|
483
|
+
const allKeys = Array.from(
|
|
484
|
+
/* @__PURE__ */ new Set([
|
|
485
|
+
...Array.from(cache.keys()),
|
|
486
|
+
...fallback ? Object.keys(fallback) : [],
|
|
487
|
+
// If this is running within the Studio, we also take the
|
|
488
|
+
// opportunity to invalidate the Studio cache. The keys that
|
|
489
|
+
// Studio may have can be a superset of `cache` here, because
|
|
490
|
+
// `cache` is updated as swr hooks are mounted and unmounted,
|
|
491
|
+
// but Studio's data cache keys don't get removed when hooks
|
|
492
|
+
// are unmounted. This makes it possible for Studio to hold
|
|
493
|
+
// onto a stale cache entry that doesn't get invalidated.
|
|
494
|
+
// For example, Studio may render page1, with key X, then goes
|
|
495
|
+
// to page 2, which performs a mutate. At this point, Studio
|
|
496
|
+
// has a cache entry for key X, but `cache` does not, because
|
|
497
|
+
// page2 does not use that query. But page 2 may perform a
|
|
498
|
+
// mutation that invalidates X. So we need to invalidate not
|
|
499
|
+
// only keys in `cache`, but also keys that Studio is still
|
|
500
|
+
// holding onto.
|
|
501
|
+
...(_b = (_a = globalThis.__PLASMIC_GET_ALL_CACHE_KEYS) == null ? void 0 : _a.call(globalThis)) != null ? _b : []
|
|
502
|
+
])
|
|
503
|
+
).filter((key) => typeof key === "string");
|
|
504
|
+
if (invalidatedKeys.includes("plasmic_refresh_all")) {
|
|
505
|
+
return allKeys;
|
|
506
|
+
}
|
|
507
|
+
return allKeys.filter(
|
|
508
|
+
(key) => invalidatedKeys.some((k) => matchesQueryCacheKey(key, k))
|
|
509
|
+
);
|
|
510
|
+
};
|
|
511
|
+
const keys = getKeysToInvalidate();
|
|
512
|
+
if (keys.length === 0) {
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const invalidateKey = (key) => __async(null, null, function* () {
|
|
516
|
+
const studioInvalidate = globalThis.__PLASMIC_MUTATE_DATA_OP;
|
|
517
|
+
if (studioInvalidate) {
|
|
518
|
+
yield studioInvalidate(key);
|
|
519
|
+
}
|
|
520
|
+
return mutate(key);
|
|
521
|
+
});
|
|
522
|
+
return yield Promise.all(keys.map((key) => invalidateKey(key)));
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
function resolveDataOp(dataOp) {
|
|
526
|
+
if (typeof dataOp === "function") {
|
|
527
|
+
try {
|
|
528
|
+
return dataOp();
|
|
529
|
+
} catch (err) {
|
|
530
|
+
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
531
|
+
return err;
|
|
532
|
+
}
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
535
|
+
} else {
|
|
536
|
+
return dataOp;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function usePlasmicDataOp(dataOp, opts) {
|
|
540
|
+
const resolvedDataOp = resolveDataOp(dataOp);
|
|
541
|
+
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
542
|
+
const key = !resolvedDataOp || isPlasmicUndefinedDataErrorPromise(resolvedDataOp) ? null : makeCacheKey(resolvedDataOp, {
|
|
543
|
+
paginate: opts == null ? void 0 : opts.paginate,
|
|
544
|
+
userAuthToken: ctx == null ? void 0 : ctx.userAuthToken
|
|
545
|
+
});
|
|
546
|
+
const fetcher = (op) => {
|
|
547
|
+
return executePlasmicDataOp(op, {
|
|
548
|
+
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
549
|
+
user: ctx == null ? void 0 : ctx.user,
|
|
550
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
551
|
+
});
|
|
552
|
+
};
|
|
553
|
+
const resultMapper = (result) => {
|
|
554
|
+
var _a;
|
|
555
|
+
return __spreadValues(__spreadValues({}, (_a = result.data) != null ? _a : {}), pick(result, "error", "isLoading"));
|
|
556
|
+
};
|
|
557
|
+
return usePlasmicFetch(
|
|
558
|
+
key,
|
|
559
|
+
resolvedDataOp,
|
|
560
|
+
fetcher,
|
|
561
|
+
resultMapper,
|
|
562
|
+
["data", "schema", "error"],
|
|
563
|
+
{
|
|
564
|
+
noUndefinedDataProxy: opts == null ? void 0 : opts.noUndefinedDataProxy
|
|
565
|
+
}
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
function usePlasmicDataMutationOp(dataOp) {
|
|
569
|
+
const ctx = (0, import_data_sources_context.usePlasmicDataSourceContext)();
|
|
570
|
+
const userToken = ctx == null ? void 0 : ctx.userAuthToken;
|
|
571
|
+
const getRealDataOp = React2.useCallback(() => __async(null, null, function* () {
|
|
572
|
+
const tryGetRealDataOp = () => __async(null, null, function* () {
|
|
573
|
+
const resolved = resolveDataOp(dataOp);
|
|
574
|
+
if (!resolved) {
|
|
575
|
+
return null;
|
|
576
|
+
} else if (isPlasmicUndefinedDataErrorPromise(resolved)) {
|
|
577
|
+
yield resolved;
|
|
578
|
+
return tryGetRealDataOp();
|
|
579
|
+
} else {
|
|
580
|
+
return resolved;
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
return yield tryGetRealDataOp();
|
|
584
|
+
}), [dataOp]);
|
|
585
|
+
return React2.useCallback(() => __async(null, null, function* () {
|
|
586
|
+
var _a;
|
|
587
|
+
const { sourceId, opId, userArgs } = (_a = yield getRealDataOp()) != null ? _a : {};
|
|
588
|
+
if (!sourceId || !opId) {
|
|
589
|
+
return void 0;
|
|
590
|
+
}
|
|
591
|
+
return executePlasmicDataOp(
|
|
592
|
+
{ sourceId, opId, userArgs },
|
|
593
|
+
{
|
|
594
|
+
userAuthToken: userToken || void 0,
|
|
595
|
+
user: ctx == null ? void 0 : ctx.user
|
|
596
|
+
}
|
|
597
|
+
);
|
|
598
|
+
}), [getRealDataOp, userToken]);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// src/serverQueries/client.ts
|
|
602
|
+
var import_query4 = require("@plasmicapp/query");
|
|
603
|
+
var React4 = __toESM(require("react"));
|
|
604
|
+
|
|
387
605
|
// src/serverQueries/common.ts
|
|
606
|
+
var import_react2 = __toESM(require("react"));
|
|
388
607
|
function createDollarQueries(queryNames) {
|
|
389
608
|
return Object.fromEntries(
|
|
390
609
|
queryNames.map((queryName) => {
|
|
@@ -680,8 +899,8 @@ function createInitial$State($ctx, $props, $q, stateSpecs) {
|
|
|
680
899
|
var GLOBAL_CACHE = /* @__PURE__ */ new Map();
|
|
681
900
|
function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
682
901
|
var _a;
|
|
683
|
-
const wrappedQueries =
|
|
684
|
-
const $queries =
|
|
902
|
+
const wrappedQueries = React4.useMemo(() => wrapQueries(tree.queries), [tree]);
|
|
903
|
+
const $queries = React4.useMemo(
|
|
685
904
|
() => createDollarQueries(Object.keys(tree.queries)),
|
|
686
905
|
[tree]
|
|
687
906
|
);
|
|
@@ -689,7 +908,7 @@ function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
|
689
908
|
if (!$state) {
|
|
690
909
|
$state = createInitial$State($ctx, $props, $queryStates, tree.stateSpecs);
|
|
691
910
|
}
|
|
692
|
-
const { fallback: prefetchedCache, cache: swrCache } = (0,
|
|
911
|
+
const { fallback: prefetchedCache, cache: swrCache } = (0, import_query4.usePlasmicDataConfig)();
|
|
693
912
|
const paramsResults = {};
|
|
694
913
|
const executionCtx = {
|
|
695
914
|
$ctx,
|
|
@@ -728,7 +947,7 @@ function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
|
728
947
|
delete paramsResults[k];
|
|
729
948
|
}
|
|
730
949
|
}
|
|
731
|
-
const stopRef =
|
|
950
|
+
const stopRef = React4.useRef();
|
|
732
951
|
(_a = stopRef.current) == null ? void 0 : _a.call(stopRef);
|
|
733
952
|
let stopped = false;
|
|
734
953
|
const stop = new Promise((resolve) => {
|
|
@@ -737,7 +956,7 @@ function usePlasmicQueries(tree, $ctx, $props, $state) {
|
|
|
737
956
|
resolve();
|
|
738
957
|
};
|
|
739
958
|
});
|
|
740
|
-
|
|
959
|
+
React4.useEffect(() => () => {
|
|
741
960
|
var _a2;
|
|
742
961
|
return (_a2 = stopRef.current) == null ? void 0 : _a2.call(stopRef);
|
|
743
962
|
}, []);
|
|
@@ -785,7 +1004,7 @@ function wrapQueries(queries) {
|
|
|
785
1004
|
if (cached) {
|
|
786
1005
|
return cached.promise;
|
|
787
1006
|
}
|
|
788
|
-
const promise = (0,
|
|
1007
|
+
const promise = (0, import_query4.wrapLoadingFetcher)(query.fn)(...args);
|
|
789
1008
|
GLOBAL_CACHE.set(cacheKey, new SyncPromise(promise));
|
|
790
1009
|
return promise;
|
|
791
1010
|
};
|
|
@@ -854,7 +1073,7 @@ function initPlasmicQueriesSync($queries, queries, paramsResults, executionCtx,
|
|
|
854
1073
|
}
|
|
855
1074
|
function usePlasmicQuery($query, query, paramsResult) {
|
|
856
1075
|
const $queryState = $query;
|
|
857
|
-
const { key, fetcher } =
|
|
1076
|
+
const { key, fetcher } = React4.useMemo(() => {
|
|
858
1077
|
switch (paramsResult.status) {
|
|
859
1078
|
case "blocked":
|
|
860
1079
|
case "error": {
|
|
@@ -901,7 +1120,7 @@ function usePlasmicQuery($query, query, paramsResult) {
|
|
|
901
1120
|
}
|
|
902
1121
|
}
|
|
903
1122
|
}, [query, paramsResult]);
|
|
904
|
-
const result = (0,
|
|
1123
|
+
const result = (0, import_query4.useMutablePlasmicQueryData)(key, fetcher, {
|
|
905
1124
|
// If revalidateIfStale is true, then if there's a cache entry with a key,
|
|
906
1125
|
// but no mounted hook with that key yet, and when the hook mounts with the key,
|
|
907
1126
|
// swr will revalidate. This may be reasonable behavior, but for us, this
|
|
@@ -1209,224 +1428,6 @@ var import_query5 = require("@plasmicapp/query");
|
|
|
1209
1428
|
|
|
1210
1429
|
// src/components/Fetcher.tsx
|
|
1211
1430
|
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
1431
|
function Fetcher(props) {
|
|
1431
1432
|
var _a;
|
|
1432
1433
|
const { dataOp, children, name, pageIndex, pageSize } = props;
|