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