@stonecrop/stonecrop 0.52.0 → 0.53.0
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/stonecrop.d.ts +30 -3
- package/dist/stonecrop.js +100 -7
- package/dist/stonecrop.js.map +1 -1
- package/package.json +4 -4
package/dist/stonecrop.d.ts
CHANGED
|
@@ -152,7 +152,7 @@ export declare interface BatchOperation {
|
|
|
152
152
|
/**
|
|
153
153
|
* Named capabilities injected into a clientHandler body as function parameters.
|
|
154
154
|
* The caller (the assembly composable) owns what each name resolves to — typically
|
|
155
|
-
* `router`, `record`, `runAction`,
|
|
155
|
+
* `router`, `record`, `runAction`, a read-only `graphql`, and `prefillDraft`.
|
|
156
156
|
*
|
|
157
157
|
* @public
|
|
158
158
|
*/
|
|
@@ -160,8 +160,9 @@ export declare type ClientHandlerApi = Record<string, unknown>;
|
|
|
160
160
|
|
|
161
161
|
/**
|
|
162
162
|
* Build a new record, once, from every starting value: the doctype's `defaults`, then the source the app registered
|
|
163
|
-
* for it,
|
|
164
|
-
*
|
|
163
|
+
* for it, then any values the app filled the record in with (`prefill`), over the record's tables and embedded
|
|
164
|
+
* records. Layers apply in that order, so a later one wins however long an earlier one took. A field none sets has
|
|
165
|
+
* no value, so a save leaves it to the database's default.
|
|
165
166
|
*
|
|
166
167
|
* Nothing is returned until every value is in, so nothing is written to the record after a user can see it.
|
|
167
168
|
* A value that throws, rejects, or has not arrived within `timeoutMs` (default {@link DEFAULTS_TIMEOUT_MS}) is
|
|
@@ -179,6 +180,10 @@ export declare type ComposeNewRecordOptions = {
|
|
|
179
180
|
now?: Date;
|
|
180
181
|
/** How long to wait for starting values that have not arrived, in milliseconds. */
|
|
181
182
|
timeoutMs?: number;
|
|
183
|
+
/** Values the app filled the record in with, applied over every default; Desktop passes what `prefillDraft` held. */
|
|
184
|
+
prefill?: {
|
|
185
|
+
[field: string]: DefaultsData;
|
|
186
|
+
};
|
|
182
187
|
};
|
|
183
188
|
|
|
184
189
|
/**
|
|
@@ -1514,6 +1519,11 @@ export declare class Registry {
|
|
|
1514
1519
|
* @internal
|
|
1515
1520
|
*/
|
|
1516
1521
|
private registeredDefaults;
|
|
1522
|
+
/**
|
|
1523
|
+
* Values held for each doctype's next new record, by slug, until its form takes them.
|
|
1524
|
+
* @internal
|
|
1525
|
+
*/
|
|
1526
|
+
private prefilledDrafts;
|
|
1517
1527
|
/**
|
|
1518
1528
|
* The Vue router instance
|
|
1519
1529
|
* @see {@link https://router.vuejs.org/}
|
|
@@ -1656,6 +1666,23 @@ export declare class Registry {
|
|
|
1656
1666
|
* @internal
|
|
1657
1667
|
*/
|
|
1658
1668
|
getRegisteredDefaults(slug: string): DefaultsSource | undefined;
|
|
1669
|
+
/**
|
|
1670
|
+
* Hold values for a doctype's next new record, by any name it is registered under, so its form opens filled in;
|
|
1671
|
+
* the caller then opens that form. The doctype must be registered already, and so must every doctype its tables
|
|
1672
|
+
* and embedded records hold, or what they hold is copied unchecked. The values are checked against its fields and
|
|
1673
|
+
* copied; values its fields cannot hold, or anything that is not plain data, throw and change nothing. A field
|
|
1674
|
+
* given as `undefined` is skipped, and a later call for the same doctype replaces an earlier one.
|
|
1675
|
+
* @public
|
|
1676
|
+
*/
|
|
1677
|
+
prefillDraft(doctype: string, values: Record<string, unknown>): void;
|
|
1678
|
+
/**
|
|
1679
|
+
* Take the values held for a doctype's next new record, by any name it is registered under, so no later form
|
|
1680
|
+
* opens with them. Desktop passes them to {@link Registry.composeNewRecord} as `prefill`.
|
|
1681
|
+
* @public
|
|
1682
|
+
*/
|
|
1683
|
+
takePrefill(doctype: string): {
|
|
1684
|
+
[field: string]: DefaultsData;
|
|
1685
|
+
} | undefined;
|
|
1659
1686
|
/**
|
|
1660
1687
|
* Compose a new record, once, from every starting value. See {@link composeNewRecord}.
|
|
1661
1688
|
* @public
|
package/dist/stonecrop.js
CHANGED
|
@@ -2308,15 +2308,18 @@ function useClientAction(options = {}) {
|
|
|
2308
2308
|
}
|
|
2309
2309
|
const record = sc.getRecordById(doctypeSlug, recordId)?.get("") ?? data;
|
|
2310
2310
|
const runAction = (action, extra) => dispatchAndWriteback(doctypeSlug, recordId, data, action, extra);
|
|
2311
|
+
const graphql = { query(query, variables) {
|
|
2312
|
+
const client = sc.getClient();
|
|
2313
|
+
if (!client?.query) return Promise.reject(/* @__PURE__ */ new Error("The configured data client does not support graphql.query"));
|
|
2314
|
+
return client.query(query, variables);
|
|
2315
|
+
} };
|
|
2316
|
+
const prefillDraft = (target, values) => sc.registry.prefillDraft(target, values);
|
|
2311
2317
|
await executeClientHandler(clientHandler, {
|
|
2312
2318
|
router: resolveRouter(),
|
|
2313
2319
|
record,
|
|
2314
2320
|
runAction,
|
|
2315
|
-
graphql
|
|
2316
|
-
|
|
2317
|
-
if (!client?.query) return Promise.reject(/* @__PURE__ */ new Error("The configured data client does not support graphql.query"));
|
|
2318
|
-
return client.query(query, variables);
|
|
2319
|
-
} }
|
|
2321
|
+
graphql,
|
|
2322
|
+
prefillDraft
|
|
2320
2323
|
});
|
|
2321
2324
|
} catch (error) {
|
|
2322
2325
|
onError({
|
|
@@ -3193,8 +3196,9 @@ async function applyLayer(target, layer, fields, run, path, within) {
|
|
|
3193
3196
|
}
|
|
3194
3197
|
/**
|
|
3195
3198
|
* Build a new record, once, from every starting value: the doctype's `defaults`, then the source the app registered
|
|
3196
|
-
* for it,
|
|
3197
|
-
*
|
|
3199
|
+
* for it, then any values the app filled the record in with (`prefill`), over the record's tables and embedded
|
|
3200
|
+
* records. Layers apply in that order, so a later one wins however long an earlier one took. A field none sets has
|
|
3201
|
+
* no value, so a save leaves it to the database's default.
|
|
3198
3202
|
*
|
|
3199
3203
|
* Nothing is returned until every value is in, so nothing is written to the record after a user can see it.
|
|
3200
3204
|
* A value that throws, rejects, or has not arrived within `timeoutMs` (default {@link DEFAULTS_TIMEOUT_MS}) is
|
|
@@ -3221,6 +3225,7 @@ async function composeNewRecord(registry, doctype, options) {
|
|
|
3221
3225
|
const working = resolveTokensInRecord(registry.initializeRecord(registry.resolveSchema(doctype)), fields, registry, now);
|
|
3222
3226
|
await applyDoctypeDefaults(working, doctype, run, "doctype defaults", []);
|
|
3223
3227
|
await applyLayer(working, registry.getRegisteredDefaults(doctype.slug), fields, run, "registered defaults", [doctype]);
|
|
3228
|
+
await applyLayer(working, options?.prefill, fields, run, "filled values", [doctype]);
|
|
3224
3229
|
Object.assign(working, resolveTokensInRecord(working, fields, registry, now));
|
|
3225
3230
|
if (run.problems.length > 0) console.warn(`[stonecrop] A new ${doctype.doctype} opened without some starting values:\n- ${run.problems.join("\n- ")}`, ...run.errors);
|
|
3226
3231
|
return { record: working };
|
|
@@ -3228,6 +3233,65 @@ async function composeNewRecord(registry, doctype, options) {
|
|
|
3228
3233
|
clearTimeout(timer);
|
|
3229
3234
|
}
|
|
3230
3235
|
}
|
|
3236
|
+
/**
|
|
3237
|
+
* A record written as `{ ... }`, a reactive one included. A `Date`, a `Map` or a class instance is not one: copying
|
|
3238
|
+
* its own fields would hand the form an empty object in its place.
|
|
3239
|
+
*/
|
|
3240
|
+
function isPlainRecord(value) {
|
|
3241
|
+
if (typeof value !== "object" || value === null) return false;
|
|
3242
|
+
const prototype = Object.getPrototypeOf(value);
|
|
3243
|
+
return prototype === Object.prototype || prototype === null;
|
|
3244
|
+
}
|
|
3245
|
+
/** Copy a field's own value, refusing anything inside it but lists, records, strings, numbers, booleans and null. */
|
|
3246
|
+
function copyFilledValue(value, path, check) {
|
|
3247
|
+
if (typeof value === "function") throw check.refused(`"${path}" can't be a function`);
|
|
3248
|
+
if (Array.isArray(value)) return value.map((item, index) => copyFilledValue(item, `${path}[${index}]`, check));
|
|
3249
|
+
if (isPlainRecord(value)) return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== void 0).map(([key, item]) => [key, copyFilledValue(item, `${path}.${key}`, check)]));
|
|
3250
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
3251
|
+
throw check.refused(`"${path}" must be plain data`);
|
|
3252
|
+
}
|
|
3253
|
+
/** A field given as `undefined` is skipped, as JSON drops it, so its default stays. */
|
|
3254
|
+
function checkFilledDocument(values, fields, prefix, check) {
|
|
3255
|
+
const copy = {};
|
|
3256
|
+
for (const [key, value] of Object.entries(values)) {
|
|
3257
|
+
if (value === void 0) continue;
|
|
3258
|
+
const path = `${prefix}${key}`;
|
|
3259
|
+
const field = fields.get(key);
|
|
3260
|
+
if (!field) throw check.refused(`it has no field "${path}"`);
|
|
3261
|
+
copy[key] = checkFilledField(field, value, path, check);
|
|
3262
|
+
}
|
|
3263
|
+
return copy;
|
|
3264
|
+
}
|
|
3265
|
+
/** One field's filled value, held as the build will apply it: rows as a list of records, an embedded record as one. */
|
|
3266
|
+
function checkFilledField(field, value, path, check) {
|
|
3267
|
+
if (field.holds === "rows" || field.holds === "columns") {
|
|
3268
|
+
if (!Array.isArray(value)) throw check.refused(`"${path}" must be a list`);
|
|
3269
|
+
const rowFields = field.holds === "rows" ? recordFields(check.registry, field.target) : columnFields(field.columns);
|
|
3270
|
+
return value.map((row, index) => {
|
|
3271
|
+
if (!isPlainRecord(row)) throw check.refused(`"${path}[${index}]" must be a record`);
|
|
3272
|
+
return checkFilledDocument(row, rowFields, `${path}[${index}].`, check);
|
|
3273
|
+
});
|
|
3274
|
+
}
|
|
3275
|
+
if (field.holds === "record") {
|
|
3276
|
+
if (value === null) return null;
|
|
3277
|
+
if (!isPlainRecord(value)) throw check.refused(`"${path}" must be a record`);
|
|
3278
|
+
return checkFilledDocument(value, recordFields(check.registry, field.target), `${path}.`, check);
|
|
3279
|
+
}
|
|
3280
|
+
return copyFilledValue(value, path, check);
|
|
3281
|
+
}
|
|
3282
|
+
/**
|
|
3283
|
+
* Check values an app fills a new record in with against the fields the build applies them to, and copy them, so a
|
|
3284
|
+
* mistake fails where the app hands them over instead of opening a form with blanks.
|
|
3285
|
+
* @internal
|
|
3286
|
+
*/
|
|
3287
|
+
function checkPrefill(registry, doctype, values) {
|
|
3288
|
+
const check = {
|
|
3289
|
+
registry,
|
|
3290
|
+
refused: (reason) => /* @__PURE__ */ new Error(`Couldn't open a new ${doctype.doctype}: ${reason}.`)
|
|
3291
|
+
};
|
|
3292
|
+
if (!isPlainRecord(values)) throw check.refused("its values must be a record");
|
|
3293
|
+
return checkFilledDocument(values, recordFields(registry, doctype), "", check);
|
|
3294
|
+
}
|
|
3231
3295
|
//#endregion
|
|
3232
3296
|
//#region src/registry.ts
|
|
3233
3297
|
/**
|
|
@@ -3274,6 +3338,11 @@ var Registry = class Registry {
|
|
|
3274
3338
|
*/
|
|
3275
3339
|
registeredDefaults = /* @__PURE__ */ new Map();
|
|
3276
3340
|
/**
|
|
3341
|
+
* Values held for each doctype's next new record, by slug, until its form takes them.
|
|
3342
|
+
* @internal
|
|
3343
|
+
*/
|
|
3344
|
+
prefilledDrafts = /* @__PURE__ */ new Map();
|
|
3345
|
+
/**
|
|
3277
3346
|
* The Vue router instance
|
|
3278
3347
|
* @see {@link https://router.vuejs.org/}
|
|
3279
3348
|
*/
|
|
@@ -3560,6 +3629,30 @@ var Registry = class Registry {
|
|
|
3560
3629
|
return this.registeredDefaults.get(slug);
|
|
3561
3630
|
}
|
|
3562
3631
|
/**
|
|
3632
|
+
* Hold values for a doctype's next new record, by any name it is registered under, so its form opens filled in;
|
|
3633
|
+
* the caller then opens that form. The doctype must be registered already, and so must every doctype its tables
|
|
3634
|
+
* and embedded records hold, or what they hold is copied unchecked. The values are checked against its fields and
|
|
3635
|
+
* copied; values its fields cannot hold, or anything that is not plain data, throw and change nothing. A field
|
|
3636
|
+
* given as `undefined` is skipped, and a later call for the same doctype replaces an earlier one.
|
|
3637
|
+
* @public
|
|
3638
|
+
*/
|
|
3639
|
+
prefillDraft(doctype, values) {
|
|
3640
|
+
const target = this.getDoctype(toSlug(doctype));
|
|
3641
|
+
if (!target) throw new Error(`Couldn't open a new ${doctype}: no doctype by that name is registered.`);
|
|
3642
|
+
this.prefilledDrafts.set(target.slug, checkPrefill(this, target, values));
|
|
3643
|
+
}
|
|
3644
|
+
/**
|
|
3645
|
+
* Take the values held for a doctype's next new record, by any name it is registered under, so no later form
|
|
3646
|
+
* opens with them. Desktop passes them to {@link Registry.composeNewRecord} as `prefill`.
|
|
3647
|
+
* @public
|
|
3648
|
+
*/
|
|
3649
|
+
takePrefill(doctype) {
|
|
3650
|
+
const slug = this.getDoctype(toSlug(doctype))?.slug ?? toSlug(doctype);
|
|
3651
|
+
const values = this.prefilledDrafts.get(slug);
|
|
3652
|
+
this.prefilledDrafts.delete(slug);
|
|
3653
|
+
return values;
|
|
3654
|
+
}
|
|
3655
|
+
/**
|
|
3563
3656
|
* Compose a new record, once, from every starting value. See {@link composeNewRecord}.
|
|
3564
3657
|
* @public
|
|
3565
3658
|
*/
|