@plasmicapp/data-sources 0.1.126 → 0.1.128
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/components/Fetcher.d.ts +15 -15
- package/dist/executor.d.ts +17 -17
- package/dist/helpers.d.ts +18 -18
- package/dist/hooks/useDependencyAwareQuery.d.ts +11 -11
- package/dist/hooks/usePlasmicDataOp.d.ts +20 -20
- package/dist/index.d.ts +10 -8
- package/dist/index.esm.js +600 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/index.js +622 -5
- package/dist/index.js.map +7 -0
- package/dist/types.d.ts +31 -31
- package/dist/utils.d.ts +31 -31
- package/package.json +22 -34
- package/dist/data-sources.cjs.development.js +0 -1171
- package/dist/data-sources.cjs.development.js.map +0 -1
- package/dist/data-sources.cjs.production.min.js +0 -2
- package/dist/data-sources.cjs.production.min.js.map +0 -1
- package/dist/data-sources.esm.js +0 -1151
- package/dist/data-sources.esm.js.map +0 -1
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/index.tsx
|
|
42
|
+
import { usePlasmicDataConfig as usePlasmicDataConfig2 } from "@plasmicapp/query";
|
|
43
|
+
|
|
44
|
+
// src/components/Fetcher.tsx
|
|
45
|
+
import React2 from "react";
|
|
46
|
+
|
|
47
|
+
// src/hooks/usePlasmicDataOp.tsx
|
|
48
|
+
import { usePlasmicDataSourceContext } from "@plasmicapp/data-sources-context";
|
|
49
|
+
import * as ph from "@plasmicapp/host";
|
|
50
|
+
import {
|
|
51
|
+
useMutablePlasmicQueryData,
|
|
52
|
+
usePlasmicDataConfig
|
|
53
|
+
} from "@plasmicapp/query";
|
|
54
|
+
import React from "react";
|
|
55
|
+
|
|
56
|
+
// src/executor.tsx
|
|
57
|
+
import fetch from "@plasmicapp/isomorphic-unfetch";
|
|
58
|
+
import { wrapLoadingFetcher } from "@plasmicapp/query";
|
|
59
|
+
import stringify from "fast-stringify";
|
|
60
|
+
var DEFAULT_HOST = "https://data.plasmic.app";
|
|
61
|
+
var UNAUTHORIZED_MESSAGE = "You do not have permission to perform this operation. Login to get access or contact the app owner to get access.";
|
|
62
|
+
function executePlasmicDataOp(op, opts) {
|
|
63
|
+
return __async(this, null, function* () {
|
|
64
|
+
const func = getConfig(
|
|
65
|
+
"__PLASMIC_EXECUTE_DATA_OP",
|
|
66
|
+
_executePlasmicDataOp
|
|
67
|
+
);
|
|
68
|
+
const res = yield wrapLoadingFetcher(func)(op, opts);
|
|
69
|
+
return res;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function _executePlasmicDataOp(op, opts) {
|
|
73
|
+
return __async(this, null, function* () {
|
|
74
|
+
var _a;
|
|
75
|
+
if (op.roleId) {
|
|
76
|
+
if (!(opts == null ? void 0 : opts.user) || !opts.user.roleIds.includes(op.roleId)) {
|
|
77
|
+
console.error(UNAUTHORIZED_MESSAGE);
|
|
78
|
+
throw new Error(UNAUTHORIZED_MESSAGE);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const host = getConfig("__PLASMIC_DATA_HOST", DEFAULT_HOST);
|
|
82
|
+
const url = `${host}/api/v1/server-data/sources/${op.sourceId}/execute`;
|
|
83
|
+
const resp = yield fetch(url, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: __spreadValues({
|
|
86
|
+
"Content-Type": "application/json"
|
|
87
|
+
}, (opts == null ? void 0 : opts.userAuthToken) && {
|
|
88
|
+
"x-plasmic-data-user-auth-token": opts.userAuthToken
|
|
89
|
+
}),
|
|
90
|
+
body: stringify({
|
|
91
|
+
opId: op.opId,
|
|
92
|
+
userArgs: (_a = op.userArgs) != null ? _a : {},
|
|
93
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
94
|
+
})
|
|
95
|
+
});
|
|
96
|
+
if (resp.status !== 200) {
|
|
97
|
+
const text = yield resp.text();
|
|
98
|
+
throw new Error(text);
|
|
99
|
+
}
|
|
100
|
+
return yield resp.json();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function getConfig(key, defaultValue) {
|
|
104
|
+
var _a;
|
|
105
|
+
if (typeof globalThis === "undefined") {
|
|
106
|
+
return defaultValue;
|
|
107
|
+
} else {
|
|
108
|
+
return (_a = globalThis[key]) != null ? _a : defaultValue;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/utils.ts
|
|
113
|
+
function swallow(f) {
|
|
114
|
+
try {
|
|
115
|
+
return f();
|
|
116
|
+
} catch (e) {
|
|
117
|
+
return void 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function pick(obj, ...keys) {
|
|
121
|
+
const res = {};
|
|
122
|
+
for (const key of keys) {
|
|
123
|
+
if (key in obj) {
|
|
124
|
+
res[key] = obj[key];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return res;
|
|
128
|
+
}
|
|
129
|
+
var tuple = (...args) => args;
|
|
130
|
+
function mkIdMap(xs) {
|
|
131
|
+
return new Map(xs.map((x) => tuple(x.id, x)));
|
|
132
|
+
}
|
|
133
|
+
function withoutNils(xs) {
|
|
134
|
+
return xs.filter((x) => x != null);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/hooks/usePlasmicDataOp.tsx
|
|
138
|
+
function makeCacheKey(dataOp, opts) {
|
|
139
|
+
const queryDependencies = JSON.stringify({
|
|
140
|
+
sourceId: dataOp.sourceId,
|
|
141
|
+
opId: dataOp.opId,
|
|
142
|
+
args: dataOp.userArgs,
|
|
143
|
+
userAuthToken: opts == null ? void 0 : opts.userAuthToken,
|
|
144
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
145
|
+
});
|
|
146
|
+
return dataOp.cacheKey ? `${dataOp.cacheKey}${queryDependencies}` : queryDependencies;
|
|
147
|
+
}
|
|
148
|
+
function usePlasmicInvalidate() {
|
|
149
|
+
const { cache, fallback, mutate } = usePlasmicDataConfig();
|
|
150
|
+
return (invalidatedKeys) => __async(this, null, function* () {
|
|
151
|
+
const getKeysToInvalidate = () => {
|
|
152
|
+
if (!invalidatedKeys) {
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
const allKeys = Array.from(
|
|
156
|
+
/* @__PURE__ */ new Set([
|
|
157
|
+
...Array.from(cache.keys()),
|
|
158
|
+
...fallback ? Object.keys(fallback) : []
|
|
159
|
+
])
|
|
160
|
+
).filter((key) => typeof key === "string");
|
|
161
|
+
if (invalidatedKeys.includes("plasmic_refresh_all")) {
|
|
162
|
+
return allKeys;
|
|
163
|
+
}
|
|
164
|
+
return allKeys.filter(
|
|
165
|
+
(key) => invalidatedKeys.some((k) => key.includes(`.$.${k}.$.`))
|
|
166
|
+
);
|
|
167
|
+
};
|
|
168
|
+
const keys = getKeysToInvalidate();
|
|
169
|
+
if (keys.length === 0) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const invalidateKey = (key) => __async(this, null, function* () {
|
|
173
|
+
const studioInvalidate = globalThis.__PLASMIC_MUTATE_DATA_OP;
|
|
174
|
+
if (studioInvalidate) {
|
|
175
|
+
yield studioInvalidate(key);
|
|
176
|
+
}
|
|
177
|
+
return mutate(key);
|
|
178
|
+
});
|
|
179
|
+
return yield Promise.all(keys.map((key) => invalidateKey(key)));
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
var enableLoadingBoundaryKey = "plasmicInternalEnableLoadingBoundary";
|
|
183
|
+
function mkUndefinedDataProxy(promiseRef, fetchAndUpdateCache) {
|
|
184
|
+
let fetchAndUpdatePromise = void 0;
|
|
185
|
+
return new Proxy(
|
|
186
|
+
{},
|
|
187
|
+
{
|
|
188
|
+
get: (_target, prop) => {
|
|
189
|
+
if (prop === "isPlasmicUndefinedDataProxy") {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
if (!fetchAndUpdateCache) {
|
|
193
|
+
return void 0;
|
|
194
|
+
}
|
|
195
|
+
const doFetchAndUpdate = () => {
|
|
196
|
+
if (!fetchAndUpdatePromise) {
|
|
197
|
+
fetchAndUpdatePromise = fetchAndUpdateCache().finally(() => {
|
|
198
|
+
fetchAndUpdatePromise = void 0;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return fetchAndUpdatePromise;
|
|
202
|
+
};
|
|
203
|
+
const promise = (
|
|
204
|
+
// existing fetch
|
|
205
|
+
promiseRef.fetchingPromise || // No existing fetch, so kick off a fetch
|
|
206
|
+
doFetchAndUpdate()
|
|
207
|
+
);
|
|
208
|
+
promise.plasmicType = "PlasmicUndefinedDataError";
|
|
209
|
+
promise.message = `Cannot read property ${String(
|
|
210
|
+
prop
|
|
211
|
+
)} - data is still loading`;
|
|
212
|
+
throw promise;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
function isPlasmicUndefinedDataErrorPromise(x) {
|
|
218
|
+
return !!x && typeof x === "object" && x.plasmicType === "PlasmicUndefinedDataError";
|
|
219
|
+
}
|
|
220
|
+
var reactMajorVersion = +React.version.split(".")[0];
|
|
221
|
+
function resolveDataOp(dataOp) {
|
|
222
|
+
if (typeof dataOp === "function") {
|
|
223
|
+
try {
|
|
224
|
+
return dataOp();
|
|
225
|
+
} catch (err) {
|
|
226
|
+
if (isPlasmicUndefinedDataErrorPromise(err)) {
|
|
227
|
+
return err;
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
return dataOp;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
var PRE_FETCHES = /* @__PURE__ */ new Map();
|
|
236
|
+
function usePlasmicDataOp(dataOp, opts) {
|
|
237
|
+
var _a, _b;
|
|
238
|
+
const resolvedDataOp = resolveDataOp(dataOp);
|
|
239
|
+
const ctx = usePlasmicDataSourceContext();
|
|
240
|
+
const enableLoadingBoundary = !!((_b = (_a = ph.useDataEnv) == null ? void 0 : _a()) == null ? void 0 : _b[enableLoadingBoundaryKey]);
|
|
241
|
+
const { mutate, cache } = usePlasmicDataConfig();
|
|
242
|
+
const isNullDataOp = !resolvedDataOp;
|
|
243
|
+
const isWaitingOnDependentQuery = isPlasmicUndefinedDataErrorPromise(resolvedDataOp);
|
|
244
|
+
const key = !resolvedDataOp || isPlasmicUndefinedDataErrorPromise(resolvedDataOp) ? null : makeCacheKey(resolvedDataOp, {
|
|
245
|
+
paginate: opts == null ? void 0 : opts.paginate,
|
|
246
|
+
userAuthToken: ctx == null ? void 0 : ctx.userAuthToken
|
|
247
|
+
});
|
|
248
|
+
const fetchingData = React.useMemo(
|
|
249
|
+
() => ({
|
|
250
|
+
fetchingPromise: void 0
|
|
251
|
+
}),
|
|
252
|
+
[key]
|
|
253
|
+
);
|
|
254
|
+
const fetcher = React.useMemo(
|
|
255
|
+
() => () => {
|
|
256
|
+
if (!key) {
|
|
257
|
+
throw new Error(`Fetcher should never be called without a proper key`);
|
|
258
|
+
}
|
|
259
|
+
if (fetchingData.fetchingPromise) {
|
|
260
|
+
return fetchingData.fetchingPromise;
|
|
261
|
+
}
|
|
262
|
+
if (key && PRE_FETCHES.has(key)) {
|
|
263
|
+
const existing = PRE_FETCHES.get(key);
|
|
264
|
+
fetchingData.fetchingPromise = existing;
|
|
265
|
+
return existing;
|
|
266
|
+
}
|
|
267
|
+
const fetcherFn = () => executePlasmicDataOp(resolvedDataOp, {
|
|
268
|
+
userAuthToken: (ctx == null ? void 0 : ctx.userAuthToken) || void 0,
|
|
269
|
+
user: ctx == null ? void 0 : ctx.user,
|
|
270
|
+
paginate: opts == null ? void 0 : opts.paginate
|
|
271
|
+
});
|
|
272
|
+
const fetcherPromise = fetcherFn();
|
|
273
|
+
fetchingData.fetchingPromise = fetcherPromise;
|
|
274
|
+
if (key) {
|
|
275
|
+
PRE_FETCHES.set(key, fetcherPromise);
|
|
276
|
+
fetcherPromise.then(
|
|
277
|
+
() => {
|
|
278
|
+
PRE_FETCHES.delete(key);
|
|
279
|
+
},
|
|
280
|
+
() => {
|
|
281
|
+
PRE_FETCHES.delete(key);
|
|
282
|
+
}
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return fetcherPromise;
|
|
286
|
+
},
|
|
287
|
+
[key, fetchingData]
|
|
288
|
+
);
|
|
289
|
+
const dependentKeyDataErrorPromise = isPlasmicUndefinedDataErrorPromise(
|
|
290
|
+
resolvedDataOp
|
|
291
|
+
) ? resolvedDataOp : void 0;
|
|
292
|
+
const fetchAndUpdateCache = React.useMemo(() => {
|
|
293
|
+
if (!key && !dependentKeyDataErrorPromise) {
|
|
294
|
+
return void 0;
|
|
295
|
+
}
|
|
296
|
+
return () => {
|
|
297
|
+
if (fetchingData.fetchingPromise) {
|
|
298
|
+
return fetchingData.fetchingPromise;
|
|
299
|
+
}
|
|
300
|
+
if (dependentKeyDataErrorPromise) {
|
|
301
|
+
return dependentKeyDataErrorPromise;
|
|
302
|
+
}
|
|
303
|
+
if (!key) {
|
|
304
|
+
throw new Error(`Expected key to be non-null`);
|
|
305
|
+
}
|
|
306
|
+
const cached = cache.get(key);
|
|
307
|
+
if (cached) {
|
|
308
|
+
return Promise.resolve(cached);
|
|
309
|
+
}
|
|
310
|
+
const cachedError = cache.get(`$swr$${key}`);
|
|
311
|
+
if (cachedError) {
|
|
312
|
+
return Promise.reject(cachedError.error);
|
|
313
|
+
}
|
|
314
|
+
const fetcherPromise = new Promise((resolve, reject) => {
|
|
315
|
+
setTimeout(() => {
|
|
316
|
+
fetcher().then(resolve, reject);
|
|
317
|
+
}, 1);
|
|
318
|
+
});
|
|
319
|
+
fetcherPromise.then((data2) => {
|
|
320
|
+
mutate(key, data2);
|
|
321
|
+
}).catch((err) => {
|
|
322
|
+
var _a2;
|
|
323
|
+
const keyInfo = key ? "$swr$" + key : "";
|
|
324
|
+
cache.set(keyInfo, __spreadProps(__spreadValues({}, (_a2 = cache.get(keyInfo)) != null ? _a2 : {}), { error: err }));
|
|
325
|
+
});
|
|
326
|
+
return fetcherPromise;
|
|
327
|
+
};
|
|
328
|
+
}, [fetcher, fetchingData, cache, key, dependentKeyDataErrorPromise]);
|
|
329
|
+
const res = useMutablePlasmicQueryData(key, fetcher, {
|
|
330
|
+
shouldRetryOnError: false,
|
|
331
|
+
// If revalidateIfStale is true, then if there's a cache entry with a key,
|
|
332
|
+
// but no mounted hook with that key yet, and when the hook mounts with the key,
|
|
333
|
+
// swr will revalidate. This may be reasonable behavior, but for us, this
|
|
334
|
+
// happens all the time -- we prepopulate the cache with proxy-invoked fetch,
|
|
335
|
+
// sometimes before swr had a chance to run the effect. So we turn off
|
|
336
|
+
// revalidateIfStale here, and just let the user manage invalidation.
|
|
337
|
+
revalidateIfStale: false
|
|
338
|
+
});
|
|
339
|
+
const { data, error, isLoading } = res;
|
|
340
|
+
if (fetchingData.fetchingPromise != null && data !== void 0) {
|
|
341
|
+
fetchingData.fetchingPromise = void 0;
|
|
342
|
+
}
|
|
343
|
+
return React.useMemo(() => {
|
|
344
|
+
const result = __spreadValues(__spreadValues({}, data != null ? data : {}), pick(res, "isLoading", "error"));
|
|
345
|
+
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) {
|
|
346
|
+
result.data = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
347
|
+
result.schema = mkUndefinedDataProxy(fetchingData, fetchAndUpdateCache);
|
|
348
|
+
}
|
|
349
|
+
return result;
|
|
350
|
+
}, [
|
|
351
|
+
isNullDataOp,
|
|
352
|
+
isWaitingOnDependentQuery,
|
|
353
|
+
data,
|
|
354
|
+
error,
|
|
355
|
+
isLoading,
|
|
356
|
+
opts == null ? void 0 : opts.noUndefinedDataProxy,
|
|
357
|
+
enableLoadingBoundary,
|
|
358
|
+
fetchingData,
|
|
359
|
+
fetchAndUpdateCache
|
|
360
|
+
]);
|
|
361
|
+
}
|
|
362
|
+
function usePlasmicDataMutationOp(dataOp) {
|
|
363
|
+
const ctx = usePlasmicDataSourceContext();
|
|
364
|
+
const userToken = ctx == null ? void 0 : ctx.userAuthToken;
|
|
365
|
+
const getRealDataOp = React.useCallback(() => __async(this, null, function* () {
|
|
366
|
+
const tryGetRealDataOp = () => __async(this, null, function* () {
|
|
367
|
+
const resolved = resolveDataOp(dataOp);
|
|
368
|
+
if (!resolved) {
|
|
369
|
+
return null;
|
|
370
|
+
} else if (isPlasmicUndefinedDataErrorPromise(resolved)) {
|
|
371
|
+
yield resolved;
|
|
372
|
+
return tryGetRealDataOp();
|
|
373
|
+
} else {
|
|
374
|
+
return resolved;
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
return yield tryGetRealDataOp();
|
|
378
|
+
}), [dataOp]);
|
|
379
|
+
return React.useCallback(() => __async(this, null, function* () {
|
|
380
|
+
var _a;
|
|
381
|
+
const { sourceId, opId, userArgs } = (_a = yield getRealDataOp()) != null ? _a : {};
|
|
382
|
+
if (!sourceId || !opId) {
|
|
383
|
+
return void 0;
|
|
384
|
+
}
|
|
385
|
+
return executePlasmicDataOp(
|
|
386
|
+
{ sourceId, opId, userArgs },
|
|
387
|
+
{
|
|
388
|
+
userAuthToken: userToken || void 0,
|
|
389
|
+
user: ctx == null ? void 0 : ctx.user
|
|
390
|
+
}
|
|
391
|
+
);
|
|
392
|
+
}), [getRealDataOp, userToken]);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// src/components/Fetcher.tsx
|
|
396
|
+
function Fetcher(props) {
|
|
397
|
+
var _a;
|
|
398
|
+
const { dataOp, children, name, pageIndex, pageSize } = props;
|
|
399
|
+
const data = usePlasmicDataOp(dataOp, __spreadValues({}, !!pageIndex && !!pageSize && {
|
|
400
|
+
paginate: { pageIndex, pageSize }
|
|
401
|
+
}));
|
|
402
|
+
const $queries = React2.useMemo(
|
|
403
|
+
() => __spreadProps(__spreadValues({}, props.queries), { [name != null ? name : "data"]: data }),
|
|
404
|
+
[props.queries, name, data]
|
|
405
|
+
);
|
|
406
|
+
return (_a = children == null ? void 0 : children($queries)) != null ? _a : null;
|
|
407
|
+
}
|
|
408
|
+
var FetcherMeta = {
|
|
409
|
+
name: "plasmic-data-source-fetcher",
|
|
410
|
+
displayName: "Data Fetcher",
|
|
411
|
+
props: {
|
|
412
|
+
dataOp: {
|
|
413
|
+
type: "dataSourceOp",
|
|
414
|
+
displayName: "Data"
|
|
415
|
+
},
|
|
416
|
+
name: {
|
|
417
|
+
type: "string",
|
|
418
|
+
displayName: "Variable name"
|
|
419
|
+
},
|
|
420
|
+
children: {
|
|
421
|
+
type: "slot",
|
|
422
|
+
renderPropParams: ["$queries"]
|
|
423
|
+
},
|
|
424
|
+
pageSize: {
|
|
425
|
+
type: "number",
|
|
426
|
+
advanced: true,
|
|
427
|
+
displayName: "Page size",
|
|
428
|
+
description: "Only fetch in batches of this size; for pagination"
|
|
429
|
+
},
|
|
430
|
+
pageIndex: {
|
|
431
|
+
type: "number",
|
|
432
|
+
advanced: true,
|
|
433
|
+
displayName: "Page index",
|
|
434
|
+
description: "0-based index of the paginated page to fetch"
|
|
435
|
+
}
|
|
436
|
+
},
|
|
437
|
+
importPath: "@plasmicapp/react-web/lib/data-sources",
|
|
438
|
+
importName: "Fetcher",
|
|
439
|
+
alwaysAutoName: true,
|
|
440
|
+
styleSections: false
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
// src/helpers.ts
|
|
444
|
+
function normalizeData(rawData) {
|
|
445
|
+
var _a;
|
|
446
|
+
if (!rawData) {
|
|
447
|
+
return void 0;
|
|
448
|
+
}
|
|
449
|
+
const dataArray = tryGetDataArray(rawData);
|
|
450
|
+
if (!dataArray) {
|
|
451
|
+
return void 0;
|
|
452
|
+
}
|
|
453
|
+
const schema = (_a = rawData.schema) != null ? _a : tryGetSchema(dataArray);
|
|
454
|
+
if (!schema) {
|
|
455
|
+
return void 0;
|
|
456
|
+
}
|
|
457
|
+
return { data: dataArray, schema };
|
|
458
|
+
}
|
|
459
|
+
function tryGetDataArray(rawData) {
|
|
460
|
+
if (rawData == null || typeof rawData !== "object") {
|
|
461
|
+
return void 0;
|
|
462
|
+
}
|
|
463
|
+
if (Array.isArray(rawData)) {
|
|
464
|
+
if (isArrayOfObjects(rawData)) {
|
|
465
|
+
return rawData;
|
|
466
|
+
} else {
|
|
467
|
+
return void 0;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (rawData == null) {
|
|
471
|
+
return void 0;
|
|
472
|
+
}
|
|
473
|
+
if ("data" in rawData && typeof rawData.data === "object") {
|
|
474
|
+
if (Array.isArray(rawData.data) && isArrayOfObjects(rawData.data)) {
|
|
475
|
+
return rawData.data;
|
|
476
|
+
} else if (rawData.data != null) {
|
|
477
|
+
return [rawData.data];
|
|
478
|
+
} else {
|
|
479
|
+
return void 0;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if ("isLoading" in rawData || "error" in rawData) {
|
|
483
|
+
return void 0;
|
|
484
|
+
}
|
|
485
|
+
return [rawData];
|
|
486
|
+
}
|
|
487
|
+
function isArrayOfObjects(arr) {
|
|
488
|
+
return arr.every((x) => typeof x === "object" && !Array.isArray(x));
|
|
489
|
+
}
|
|
490
|
+
function tryGetSchema(data) {
|
|
491
|
+
const fieldMap = {};
|
|
492
|
+
data.forEach((entry) => {
|
|
493
|
+
if (entry && typeof entry === "object") {
|
|
494
|
+
Array.from(Object.entries(entry)).forEach(([k, v]) => {
|
|
495
|
+
const inferredType = typeof v === "string" ? "string" : typeof v === "boolean" ? "boolean" : typeof v === "number" ? "number" : "unknown";
|
|
496
|
+
if (fieldMap[k] && fieldMap[k] !== inferredType) {
|
|
497
|
+
fieldMap[k] = "unknown";
|
|
498
|
+
} else {
|
|
499
|
+
fieldMap[k] = inferredType;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
return {
|
|
505
|
+
id: "inferred",
|
|
506
|
+
fields: Object.entries(fieldMap).map(([f, t]) => ({
|
|
507
|
+
id: f,
|
|
508
|
+
type: t,
|
|
509
|
+
readOnly: false
|
|
510
|
+
}))
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
var mkShortId = () => `${Math.random()}`;
|
|
514
|
+
function deriveFieldConfigs(specifiedFieldsPartial, schema, makeDefaultConfig) {
|
|
515
|
+
var _a;
|
|
516
|
+
const schemaFields = (_a = schema == null ? void 0 : schema.fields) != null ? _a : [];
|
|
517
|
+
const fieldById = mkIdMap(schemaFields);
|
|
518
|
+
const specifiedFieldIds = new Set(
|
|
519
|
+
withoutNils(specifiedFieldsPartial.map((f) => f.fieldId))
|
|
520
|
+
);
|
|
521
|
+
const keptSpecifiedFields = specifiedFieldsPartial.flatMap((f) => {
|
|
522
|
+
if (!f.fieldId) {
|
|
523
|
+
return [
|
|
524
|
+
__spreadValues(__spreadValues({ key: mkShortId() }, makeDefaultConfig(void 0)), f)
|
|
525
|
+
];
|
|
526
|
+
}
|
|
527
|
+
const field = fieldById.get(f.fieldId);
|
|
528
|
+
if (!field) {
|
|
529
|
+
return [];
|
|
530
|
+
}
|
|
531
|
+
return [
|
|
532
|
+
__spreadValues(__spreadValues({
|
|
533
|
+
key: mkShortId()
|
|
534
|
+
}, makeDefaultConfig(field)), f)
|
|
535
|
+
];
|
|
536
|
+
});
|
|
537
|
+
const newVirtualFields = schemaFields.filter((f) => !specifiedFieldIds.has(f.id)).map(
|
|
538
|
+
(f) => __spreadValues({
|
|
539
|
+
key: mkShortId()
|
|
540
|
+
}, makeDefaultConfig(f))
|
|
541
|
+
);
|
|
542
|
+
const mergedFields = [...keptSpecifiedFields, ...newVirtualFields];
|
|
543
|
+
const minimalFullLengthFields = [
|
|
544
|
+
...specifiedFieldsPartial,
|
|
545
|
+
...newVirtualFields.map((f) => ({ key: f.key, fieldId: f.fieldId }))
|
|
546
|
+
];
|
|
547
|
+
return { mergedFields, minimalFullLengthFields };
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// src/hooks/useDependencyAwareQuery.tsx
|
|
551
|
+
import React3 from "react";
|
|
552
|
+
function usePrevious(value) {
|
|
553
|
+
const prevValue = React3.useRef(void 0);
|
|
554
|
+
React3.useEffect(() => {
|
|
555
|
+
prevValue.current = value;
|
|
556
|
+
return () => {
|
|
557
|
+
prevValue.current = void 0;
|
|
558
|
+
};
|
|
559
|
+
});
|
|
560
|
+
return prevValue.current;
|
|
561
|
+
}
|
|
562
|
+
function useDependencyAwareQuery({
|
|
563
|
+
$queries,
|
|
564
|
+
getDataOp,
|
|
565
|
+
setDollarQueries,
|
|
566
|
+
name,
|
|
567
|
+
pageIndex,
|
|
568
|
+
pageSize
|
|
569
|
+
}) {
|
|
570
|
+
const data = usePlasmicDataOp(swallow(getDataOp), __spreadValues({}, !!pageIndex && !!pageSize && {
|
|
571
|
+
paginate: { pageIndex, pageSize }
|
|
572
|
+
}));
|
|
573
|
+
const finalName = name != null ? name : "data";
|
|
574
|
+
const prevName = usePrevious(finalName);
|
|
575
|
+
React3.useEffect(() => {
|
|
576
|
+
if (!(finalName in $queries) || $queries[finalName] !== data) {
|
|
577
|
+
const $queries2 = __spreadProps(__spreadValues({}, $queries), {
|
|
578
|
+
[finalName]: data
|
|
579
|
+
});
|
|
580
|
+
if (prevName && finalName !== prevName && prevName in $queries) {
|
|
581
|
+
delete $queries2[prevName];
|
|
582
|
+
}
|
|
583
|
+
setDollarQueries($queries2);
|
|
584
|
+
}
|
|
585
|
+
}, [finalName, prevName, data, $queries, setDollarQueries]);
|
|
586
|
+
}
|
|
587
|
+
export {
|
|
588
|
+
Fetcher,
|
|
589
|
+
FetcherMeta,
|
|
590
|
+
deriveFieldConfigs,
|
|
591
|
+
executePlasmicDataOp,
|
|
592
|
+
makeCacheKey,
|
|
593
|
+
normalizeData,
|
|
594
|
+
useDependencyAwareQuery,
|
|
595
|
+
usePlasmicDataConfig2 as usePlasmicDataConfig,
|
|
596
|
+
usePlasmicDataMutationOp,
|
|
597
|
+
usePlasmicDataOp,
|
|
598
|
+
usePlasmicInvalidate
|
|
599
|
+
};
|
|
600
|
+
//# sourceMappingURL=index.esm.js.map
|