@atscript/moost-ui-presets 0.1.58
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/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/as-preset-entry-BCmIVhgQ.mjs +56 -0
- package/dist/index.d.mts +27 -0
- package/dist/index.mjs +386 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Artem Maltsev <artem@maltsev.nl>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @atscript/moost-ui-presets
|
|
2
|
+
|
|
3
|
+
Moost controller + `.as` schema for persisting [`@atscript/vue-table`](../vue-table) presets on the server.
|
|
4
|
+
|
|
5
|
+
Part of the [atscript-ui](https://github.com/moostjs/atscript-ui) monorepo.
|
|
6
|
+
|
|
7
|
+
## What it provides
|
|
8
|
+
|
|
9
|
+
- A drop-in Moost controller exposing CRUD endpoints for table presets (per-user, per-table)
|
|
10
|
+
- The `.as` schema and DB record for preset storage, backed by [`@atscript/moost-db`](https://github.com/moostjs/atscript-db)
|
|
11
|
+
- Wire-compatible with the preset payload that [`@atscript/ui-table`](../ui-table) serializes
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm add @atscript/moost-ui-presets
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Peer requirements: `moost`, `@moostjs/event-http`, `@atscript/db`, `@atscript/moost-db`, `@atscript/ui-table`.
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
|
|
23
|
+
MIT © Artem Maltsev
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { defineAnnotatedType, throwFeatureDisabled } from "@atscript/typescript/utils";
|
|
2
|
+
//#region src/as-capabilities-query.as
|
|
3
|
+
var AsCapabilitiesQuery = class {
|
|
4
|
+
static __is_atscript_annotated_type = true;
|
|
5
|
+
static type = {};
|
|
6
|
+
static metadata = /* @__PURE__ */ new Map();
|
|
7
|
+
static id = "AsCapabilitiesQuery";
|
|
8
|
+
static toJsonSchema() {
|
|
9
|
+
throwFeatureDisabled("JSON Schema", "jsonSchema", "emit.jsonSchema");
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
defineAnnotatedType("object", AsCapabilitiesQuery).prop("app", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 128 }).annotate("expect.minLength", { length: 1 }).$type).prop("tableKey", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 64 }).annotate("expect.minLength", { length: 1 }).$type);
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/as-preset-entry.as
|
|
15
|
+
var PresetId = class {
|
|
16
|
+
static __is_atscript_annotated_type = true;
|
|
17
|
+
static type = {};
|
|
18
|
+
static metadata = /* @__PURE__ */ new Map();
|
|
19
|
+
static id = "PresetId";
|
|
20
|
+
static toJsonSchema() {
|
|
21
|
+
throwFeatureDisabled("JSON Schema", "jsonSchema", "emit.jsonSchema");
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var FieldPath = class {
|
|
25
|
+
static __is_atscript_annotated_type = true;
|
|
26
|
+
static type = {};
|
|
27
|
+
static metadata = /* @__PURE__ */ new Map();
|
|
28
|
+
static id = "FieldPath";
|
|
29
|
+
static toJsonSchema() {
|
|
30
|
+
throwFeatureDisabled("JSON Schema", "jsonSchema", "emit.jsonSchema");
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var PresetAspect = class {
|
|
34
|
+
static __is_atscript_annotated_type = true;
|
|
35
|
+
static type = {};
|
|
36
|
+
static metadata = /* @__PURE__ */ new Map();
|
|
37
|
+
static id = "PresetAspect";
|
|
38
|
+
static toJsonSchema() {
|
|
39
|
+
throwFeatureDisabled("JSON Schema", "jsonSchema", "emit.jsonSchema");
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var AsPresetEntry = class {
|
|
43
|
+
static __is_atscript_annotated_type = true;
|
|
44
|
+
static type = {};
|
|
45
|
+
static metadata = /* @__PURE__ */ new Map();
|
|
46
|
+
static id = "AsPresetEntry";
|
|
47
|
+
static toJsonSchema() {
|
|
48
|
+
throwFeatureDisabled("JSON Schema", "jsonSchema", "emit.jsonSchema");
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
defineAnnotatedType("", PresetId).designType("string").tags("string").annotate("expect.maxLength", { length: 64 }).annotate("expect.minLength", { length: 3 });
|
|
52
|
+
defineAnnotatedType("", FieldPath).designType("string").tags("string").annotate("expect.maxLength", { length: 256 }).annotate("expect.minLength", { length: 1 });
|
|
53
|
+
defineAnnotatedType("union", PresetAspect).item(defineAnnotatedType().designType("string").value("columns").$type).item(defineAnnotatedType().designType("string").value("filters").$type).item(defineAnnotatedType().designType("string").value("filterOps").$type).item(defineAnnotatedType().designType("string").value("sorters").$type).item(defineAnnotatedType().designType("string").value("itemsPerPage").$type);
|
|
54
|
+
defineAnnotatedType("object", AsPresetEntry).prop("id", defineAnnotatedType().designType("string").tags("string").annotate("meta.id", true).annotate("db.default.uuid", true).annotate("expect.maxLength", { length: 256 }).annotate("expect.minLength", { length: 3 }).$type).prop("type", defineAnnotatedType("union").item(defineAnnotatedType().designType("string").value("preset").$type).item(defineAnnotatedType().designType("string").value("userConf").$type).item(defineAnnotatedType().designType("string").value("appConf").$type).annotate("db.index.plain", { name: "preset_scope_idx" }, true).$type).prop("app", defineAnnotatedType().designType("string").tags("string").annotate("db.index.plain", { name: "preset_scope_idx" }, true).annotate("db.index.unique", "preset_public_label_idx", true).annotate("expect.maxLength", { length: 128 }).annotate("expect.minLength", { length: 1 }).$type).prop("tableKey", defineAnnotatedType().designType("string").tags("string").annotate("db.index.plain", { name: "preset_scope_idx" }, true).annotate("db.index.unique", "preset_public_label_idx", true).annotate("expect.maxLength", { length: 64 }).annotate("expect.minLength", { length: 1 }).optional().$type).prop("user", defineAnnotatedType().designType("string").tags("string").annotate("db.index.plain", { name: "preset_scope_idx" }, true).annotate("db.index.plain", { name: "preset_user_idx" }, true).annotate("expect.maxLength", { length: 128 }).annotate("expect.minLength", { length: 1 }).annotate("db.default", "").$type).prop("userLabel", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 128 }).annotate("expect.minLength", { length: 1 }).optional().$type).prop("public", defineAnnotatedType().designType("boolean").tags("boolean").annotate("db.index.plain", { name: "preset_public_idx" }, true).optional().$type).prop("label", defineAnnotatedType().designType("string").tags("string").annotate("db.index.plain", { name: "preset_label_idx" }, true).annotate("expect.maxLength", { length: 128 }).annotate("expect.minLength", { length: 1 }).optional().$type).prop("publicLabel", defineAnnotatedType().designType("string").tags("string").annotate("db.index.unique", "preset_public_label_idx", true).annotate("expect.maxLength", { length: 128 }).annotate("expect.minLength", { length: 1 }).optional().$type).prop("aspects", defineAnnotatedType("array").of(defineAnnotatedType().refTo(PresetAspect).$type).annotate("expect.array.uniqueItems", {}).optional().$type).prop("data", defineAnnotatedType("union").item(defineAnnotatedType("object").prop("label", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 128 }).annotate("expect.minLength", { length: 1 }).$type).prop("content", defineAnnotatedType("object").prop("columns", defineAnnotatedType("object").prop("columnNames", defineAnnotatedType("array").of(defineAnnotatedType().refTo(FieldPath).annotate("expect.maxLength", { length: 256 }).annotate("expect.minLength", { length: 1 }).$type).$type).prop("columnWidths", defineAnnotatedType("array").of(defineAnnotatedType("object").prop("field", defineAnnotatedType().refTo(FieldPath).annotate("expect.maxLength", { length: 256 }).annotate("expect.minLength", { length: 1 }).$type).prop("width", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 32 }).annotate("expect.minLength", { length: 1 }).$type).$type).optional().$type).optional().$type).prop("filters", defineAnnotatedType("array").of(defineAnnotatedType().refTo(FieldPath).annotate("expect.maxLength", { length: 256 }).annotate("expect.minLength", { length: 1 }).$type).optional().$type).prop("filterOps", defineAnnotatedType("array").of(defineAnnotatedType("object").prop("field", defineAnnotatedType().refTo(FieldPath).annotate("expect.maxLength", { length: 256 }).annotate("expect.minLength", { length: 1 }).$type).prop("conditions", defineAnnotatedType("array").of(defineAnnotatedType("object").prop("type", defineAnnotatedType("union").item(defineAnnotatedType().designType("string").value("eq").$type).item(defineAnnotatedType().designType("string").value("ne").$type).item(defineAnnotatedType().designType("string").value("gt").$type).item(defineAnnotatedType().designType("string").value("gte").$type).item(defineAnnotatedType().designType("string").value("lt").$type).item(defineAnnotatedType().designType("string").value("lte").$type).item(defineAnnotatedType().designType("string").value("contains").$type).item(defineAnnotatedType().designType("string").value("starts").$type).item(defineAnnotatedType().designType("string").value("ends").$type).item(defineAnnotatedType().designType("string").value("bw").$type).item(defineAnnotatedType().designType("string").value("null").$type).item(defineAnnotatedType().designType("string").value("notNull").$type).item(defineAnnotatedType().designType("string").value("regex").$type).$type).prop("value", defineAnnotatedType("array").of(defineAnnotatedType("union").item(defineAnnotatedType().designType("string").tags("string").$type).item(defineAnnotatedType().designType("number").tags("number").$type).item(defineAnnotatedType().designType("boolean").tags("boolean").$type).$type).$type).$type).$type).$type).optional().$type).prop("sorters", defineAnnotatedType("array").of(defineAnnotatedType("object").prop("field", defineAnnotatedType().refTo(FieldPath).annotate("expect.maxLength", { length: 256 }).annotate("expect.minLength", { length: 1 }).$type).prop("direction", defineAnnotatedType("union").item(defineAnnotatedType().designType("string").value("asc").$type).item(defineAnnotatedType().designType("string").value("desc").$type).$type).$type).optional().$type).prop("itemsPerPage", defineAnnotatedType().designType("number").tags("number").optional().$type).optional().$type).$type).item(defineAnnotatedType("object").prop("defaultPresetId", defineAnnotatedType().refTo(PresetId).annotate("expect.maxLength", { length: 64 }).annotate("expect.minLength", { length: 3 }).optional().$type).prop("favPresetIds", defineAnnotatedType("array").of(defineAnnotatedType().refTo(PresetId).annotate("expect.maxLength", { length: 64 }).annotate("expect.minLength", { length: 3 }).$type).optional().$type).$type).item(defineAnnotatedType("object").prop("appearance", defineAnnotatedType("union").item(defineAnnotatedType().designType("string").value("system").$type).item(defineAnnotatedType().designType("string").value("light").$type).item(defineAnnotatedType().designType("string").value("dark").$type).optional().$type).prop("language", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 5 }).annotate("expect.minLength", { length: 2 }).optional().$type).prop("timezone", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 64 }).annotate("expect.minLength", { length: 3 }).optional().$type).prop("density", defineAnnotatedType("union").item(defineAnnotatedType().designType("string").value("compact").$type).item(defineAnnotatedType().designType("string").value("cozy").$type).item(defineAnnotatedType().designType("string").value("comfortable").$type).optional().$type).prop("dateFormat", defineAnnotatedType("union").item(defineAnnotatedType().designType("string").value("iso").$type).item(defineAnnotatedType().designType("string").value("us").$type).item(defineAnnotatedType().designType("string").value("eu").$type).optional().$type).prop("firstDayOfWeek", defineAnnotatedType("union").item(defineAnnotatedType().designType("number").value(0).$type).item(defineAnnotatedType().designType("number").value(1).$type).item(defineAnnotatedType().designType("number").value(6).$type).optional().$type).prop("customJson", defineAnnotatedType().designType("string").tags("string").annotate("expect.maxLength", { length: 1024 }).optional().$type).$type).annotate("db.json", true).$type).prop("createdAt", defineAnnotatedType().designType("number").tags("number").annotate("db.default.now", true).$type).prop("updatedAt", defineAnnotatedType().designType("number").tags("number").annotate("db.default.now", true).$type).annotate("db.table", "as_presets");
|
|
55
|
+
//#endregion
|
|
56
|
+
export { AsCapabilitiesQuery as n, AsPresetEntry as t };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { n as AsCapabilitiesQuery, t as AsPresetEntry } from "./as-preset-entry-BCmIVhgQ.mjs";
|
|
2
|
+
import { AsDbController } from "@atscript/moost-db";
|
|
3
|
+
import { TAtscriptAnnotatedType } from "@atscript/typescript/utils";
|
|
4
|
+
import { APP_CONF_PREFIX, AppConfData, AsPresetEntryData, AsPresetsErrorCode, FilterCondition, FilterConditionType, PRESET_ASPECTS, PresetAspect, PresetCapabilities, PresetCapabilities as PresetCapabilities$1, PresetData, PresetLimitReachedBody, PresetSnapshotWire, RESERVED_ID_PREFIXES, SYSTEM_PRESET_PREFIX, USER_CONF_PREFIX, UserConfData, appConfId, userConfId } from "@atscript/ui-table";
|
|
5
|
+
import { FilterExpr } from "@uniqu/core";
|
|
6
|
+
|
|
7
|
+
//#region src/preset-rules.d.ts
|
|
8
|
+
type WriteAction = "insert" | "insertMany" | "replace" | "replaceMany" | "update" | "updateMany";
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/as-presets.controller.d.ts
|
|
11
|
+
declare abstract class AsPresetsController<T extends TAtscriptAnnotatedType = typeof AsPresetEntry> extends AsDbController<T> {
|
|
12
|
+
protected maxPresetsPerUser: number;
|
|
13
|
+
protected abstract getCurrentUser(): Promise<string>;
|
|
14
|
+
/** Override for tiered logic (admin / paid / free). */
|
|
15
|
+
protected getMaxPresetsPerUser(_app: string, _tableKey: string, _user: string): Promise<number>;
|
|
16
|
+
/** Override to scope public-preset creation per app/table/user. */
|
|
17
|
+
protected canPublishPresets(_app: string, _tableKey: string, _user: string): Promise<boolean>;
|
|
18
|
+
protected getUserLabel(_user: string): Promise<string | undefined>;
|
|
19
|
+
private get hooks();
|
|
20
|
+
private get presetTable();
|
|
21
|
+
capabilities(query: AsCapabilitiesQuery): Promise<PresetCapabilities$1>;
|
|
22
|
+
protected transformFilter(filter: FilterExpr): Promise<FilterExpr>;
|
|
23
|
+
protected onWrite(action: WriteAction, data: unknown): Promise<unknown>;
|
|
24
|
+
protected onRemove(id: unknown): Promise<unknown>;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { APP_CONF_PREFIX, type AppConfData, AsPresetEntry, type AsPresetEntryData, AsPresetsController, type AsPresetsErrorCode, type FilterCondition, type FilterConditionType, PRESET_ASPECTS, type PresetAspect, type PresetCapabilities, type PresetData, type PresetLimitReachedBody, type PresetSnapshotWire, RESERVED_ID_PREFIXES, SYSTEM_PRESET_PREFIX, USER_CONF_PREFIX, type UserConfData, appConfId, userConfId };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { n as AsCapabilitiesQuery, t as AsPresetEntry } from "./as-preset-entry-BCmIVhgQ.mjs";
|
|
2
|
+
import { AsDbController } from "@atscript/moost-db";
|
|
3
|
+
import { Get, HttpError, Query } from "@moostjs/event-http";
|
|
4
|
+
import { Inherit } from "moost";
|
|
5
|
+
import { APP_CONF_PREFIX, PRESET_ASPECTS, RESERVED_ID_PREFIXES, RESERVED_ID_PREFIXES as RESERVED_ID_PREFIXES$1, SYSTEM_PRESET_PREFIX, USER_CONF_PREFIX, appConfId, appConfId as appConfId$1, derivePresetAspects, isSystemPresetId, userConfId, userConfId as userConfId$1 } from "@atscript/ui-table";
|
|
6
|
+
import { walkFilter } from "@uniqu/core";
|
|
7
|
+
//#region src/preset-rules.ts
|
|
8
|
+
const IDENTITY_FIELDS = [
|
|
9
|
+
"user",
|
|
10
|
+
"app",
|
|
11
|
+
"tableKey"
|
|
12
|
+
];
|
|
13
|
+
const SUPPORTED_ACTIONS = new Set(["insert", "update"]);
|
|
14
|
+
const ROW_TYPES = [
|
|
15
|
+
"preset",
|
|
16
|
+
"userConf",
|
|
17
|
+
"appConf"
|
|
18
|
+
];
|
|
19
|
+
function isRowType(t) {
|
|
20
|
+
return ROW_TYPES.includes(t);
|
|
21
|
+
}
|
|
22
|
+
function scrubPresetOnlyFields(row) {
|
|
23
|
+
row.public = void 0;
|
|
24
|
+
row.label = void 0;
|
|
25
|
+
row.publicLabel = void 0;
|
|
26
|
+
row.aspects = void 0;
|
|
27
|
+
}
|
|
28
|
+
function presetError(status, body) {
|
|
29
|
+
return new HttpError(status, body);
|
|
30
|
+
}
|
|
31
|
+
function requireString(value, message, code) {
|
|
32
|
+
if (typeof value !== "string" || value.length === 0) throw presetError(400, {
|
|
33
|
+
message,
|
|
34
|
+
code
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function assertNotReservedId(id) {
|
|
38
|
+
if (typeof id !== "string") return;
|
|
39
|
+
for (const prefix of RESERVED_ID_PREFIXES$1) if (id.startsWith(prefix)) throw presetError(400, {
|
|
40
|
+
message: `Reserved id namespace '${prefix}'; server owns these ids`,
|
|
41
|
+
code: "reserved_id"
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function assertNotSystemId(id) {
|
|
45
|
+
if (typeof id === "string" && isSystemPresetId(id)) throw presetError(400, {
|
|
46
|
+
message: "Reserved id namespace; system presets are synthetic and not persisted",
|
|
47
|
+
code: "reserved_id"
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const readGateVisitor = {
|
|
51
|
+
comparison(field, op, value) {
|
|
52
|
+
if (op !== "$eq") return { isAppConf: false };
|
|
53
|
+
if (field === "type") return { isAppConf: value === "appConf" };
|
|
54
|
+
if (typeof value !== "string") return { isAppConf: false };
|
|
55
|
+
if (field === "app") return {
|
|
56
|
+
app: value,
|
|
57
|
+
isAppConf: false
|
|
58
|
+
};
|
|
59
|
+
if (field === "tableKey") return {
|
|
60
|
+
tableKey: value,
|
|
61
|
+
isAppConf: false
|
|
62
|
+
};
|
|
63
|
+
return { isAppConf: false };
|
|
64
|
+
},
|
|
65
|
+
and(children) {
|
|
66
|
+
const out = { isAppConf: false };
|
|
67
|
+
for (const c of children) {
|
|
68
|
+
if (out.app === void 0 && c.app !== void 0) out.app = c.app;
|
|
69
|
+
if (out.tableKey === void 0 && c.tableKey !== void 0) out.tableKey = c.tableKey;
|
|
70
|
+
if (c.isAppConf) out.isAppConf = true;
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
},
|
|
74
|
+
or: () => ({ isAppConf: false }),
|
|
75
|
+
not: () => ({ isAppConf: false })
|
|
76
|
+
};
|
|
77
|
+
function buildReadGate(user, filter) {
|
|
78
|
+
const probe = walkFilter(filter ?? void 0, readGateVisitor) ?? { isAppConf: false };
|
|
79
|
+
requireString(probe.app, "Preset reads must include 'app'", "missing_scope");
|
|
80
|
+
if (!probe.isAppConf) requireString(probe.tableKey, "Preset / userConf reads must include 'tableKey'", "missing_scope");
|
|
81
|
+
const gate = { $or: [{ user }, { $and: [{ type: "preset" }, { public: true }] }] };
|
|
82
|
+
return filter ? { $and: [gate, filter] } : gate;
|
|
83
|
+
}
|
|
84
|
+
const EXISTING_SELECT = {
|
|
85
|
+
id: 1,
|
|
86
|
+
type: 1,
|
|
87
|
+
user: 1,
|
|
88
|
+
app: 1,
|
|
89
|
+
tableKey: 1,
|
|
90
|
+
public: 1,
|
|
91
|
+
label: 1,
|
|
92
|
+
data: 1
|
|
93
|
+
};
|
|
94
|
+
function fetchExisting(ctx, id) {
|
|
95
|
+
return ctx.table.findOne({
|
|
96
|
+
filter: { id },
|
|
97
|
+
controls: { $select: EXISTING_SELECT }
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function processWrite(table, user, action, data, hooks) {
|
|
101
|
+
if (!SUPPORTED_ACTIONS.has(action)) throw presetError(405, {
|
|
102
|
+
message: `Action '${action}' is not supported; use insert or update one row at a time`,
|
|
103
|
+
code: "action_unsupported"
|
|
104
|
+
});
|
|
105
|
+
const ctx = {
|
|
106
|
+
table,
|
|
107
|
+
user,
|
|
108
|
+
hooks
|
|
109
|
+
};
|
|
110
|
+
const row = data;
|
|
111
|
+
if (action === "update") return processUpdateRow(ctx, row);
|
|
112
|
+
return processCreateRow(ctx, row);
|
|
113
|
+
}
|
|
114
|
+
async function processCreateRow(ctx, row) {
|
|
115
|
+
const { user, hooks } = ctx;
|
|
116
|
+
const next = { ...row };
|
|
117
|
+
next.user = user;
|
|
118
|
+
const userLabelPromise = hooks.getUserLabel?.(user);
|
|
119
|
+
const now = Date.now();
|
|
120
|
+
next.updatedAt = now;
|
|
121
|
+
if (typeof next.createdAt !== "number") next.createdAt = now;
|
|
122
|
+
if (!isRowType(next.type)) throw presetError(400, {
|
|
123
|
+
message: "type must be 'preset', 'userConf', or 'appConf'",
|
|
124
|
+
code: "invalid_type"
|
|
125
|
+
});
|
|
126
|
+
requireString(next.app, "'app' is required", "missing_scope");
|
|
127
|
+
if (next.type === "appConf") {
|
|
128
|
+
next.tableKey = void 0;
|
|
129
|
+
next.id = appConfId$1(user, next.app);
|
|
130
|
+
scrubPresetOnlyFields(next);
|
|
131
|
+
next.userLabel = await userLabelPromise;
|
|
132
|
+
return next;
|
|
133
|
+
}
|
|
134
|
+
requireString(next.tableKey, "'tableKey' is required for preset / userConf rows", "missing_scope");
|
|
135
|
+
if (next.type === "userConf") {
|
|
136
|
+
next.id = userConfId$1(user, next.app, next.tableKey);
|
|
137
|
+
scrubPresetOnlyFields(next);
|
|
138
|
+
await sanitiseUserConfData(ctx, next, next.app, next.tableKey);
|
|
139
|
+
next.userLabel = await userLabelPromise;
|
|
140
|
+
return next;
|
|
141
|
+
}
|
|
142
|
+
assertNotReservedId(next.id);
|
|
143
|
+
if (typeof next.id !== "string" || next.id.length === 0) next.id = globalThis.crypto.randomUUID();
|
|
144
|
+
next.label = readPresetLabel(next.data);
|
|
145
|
+
next.publicLabel = next.public === true ? next.label : void 0;
|
|
146
|
+
const capCheck = assertWithinCap(ctx, next.app, next.tableKey);
|
|
147
|
+
const publicCheck = next.public === true ? gatePublicPreset(ctx, next.app, next.tableKey, null, {
|
|
148
|
+
public: true,
|
|
149
|
+
label: next.label,
|
|
150
|
+
id: next.id
|
|
151
|
+
}) : void 0;
|
|
152
|
+
const [, , userLabel] = await Promise.all([
|
|
153
|
+
capCheck,
|
|
154
|
+
publicCheck,
|
|
155
|
+
userLabelPromise
|
|
156
|
+
]);
|
|
157
|
+
next.userLabel = userLabel;
|
|
158
|
+
next.aspects = derivePresetAspects(next.data?.content);
|
|
159
|
+
return next;
|
|
160
|
+
}
|
|
161
|
+
async function gatePublicPreset(ctx, app, tableKey, prev, next) {
|
|
162
|
+
if (!next.public) return;
|
|
163
|
+
const wasPublic = prev?.public === true;
|
|
164
|
+
const labelChanged = !wasPublic || prev?.label !== next.label;
|
|
165
|
+
const publishCheck = wasPublic ? void 0 : assertCanPublish(ctx, app, tableKey);
|
|
166
|
+
const labelCheck = labelChanged ? assertPublicLabelFree(ctx, app, tableKey, next.label, next.id) : void 0;
|
|
167
|
+
await Promise.all([publishCheck, labelCheck]);
|
|
168
|
+
}
|
|
169
|
+
const ownerError = () => presetError(404, {
|
|
170
|
+
message: "Preset not found",
|
|
171
|
+
code: "preset_not_found"
|
|
172
|
+
});
|
|
173
|
+
function requireOwner(existing, user) {
|
|
174
|
+
if (!existing || existing.user !== user) throw ownerError();
|
|
175
|
+
}
|
|
176
|
+
async function processUpdateRow(ctx, row) {
|
|
177
|
+
const { user, hooks } = ctx;
|
|
178
|
+
const next = { ...row };
|
|
179
|
+
requireString(next.id, "Update payload must include 'id'", "missing_id");
|
|
180
|
+
assertNotSystemId(next.id);
|
|
181
|
+
const existing = await fetchExisting(ctx, next.id);
|
|
182
|
+
requireOwner(existing, user);
|
|
183
|
+
const userLabelPromise = hooks.getUserLabel?.(user);
|
|
184
|
+
if (next.type !== void 0 && next.type !== existing.type) throw presetError(400, {
|
|
185
|
+
message: "Field 'type' is immutable after create",
|
|
186
|
+
code: "type_immutable"
|
|
187
|
+
});
|
|
188
|
+
for (const key of IDENTITY_FIELDS) if (next[key] !== void 0 && next[key] !== existing[key]) throw presetError(400, {
|
|
189
|
+
message: `Field '${key}' is immutable after create`,
|
|
190
|
+
code: "identity_immutable"
|
|
191
|
+
});
|
|
192
|
+
next.updatedAt = Date.now();
|
|
193
|
+
const mergedData = next.data === void 0 ? existing.data ?? null : {
|
|
194
|
+
...existing.data,
|
|
195
|
+
...next.data
|
|
196
|
+
};
|
|
197
|
+
if (next.data !== void 0) next.data = mergedData;
|
|
198
|
+
if (existing.type === "appConf") {
|
|
199
|
+
next.tableKey = void 0;
|
|
200
|
+
scrubPresetOnlyFields(next);
|
|
201
|
+
next.userLabel = await userLabelPromise;
|
|
202
|
+
return next;
|
|
203
|
+
}
|
|
204
|
+
const app = existing.app;
|
|
205
|
+
const tableKey = existing.tableKey;
|
|
206
|
+
if (existing.type === "userConf") {
|
|
207
|
+
scrubPresetOnlyFields(next);
|
|
208
|
+
if (mergedData.defaultPresetId !== existing.data?.defaultPresetId) await sanitiseUserConfData(ctx, next, app, tableKey);
|
|
209
|
+
next.userLabel = await userLabelPromise;
|
|
210
|
+
return next;
|
|
211
|
+
}
|
|
212
|
+
const wasPublic = existing.public === true;
|
|
213
|
+
const willBePublic = next.public ?? wasPublic;
|
|
214
|
+
const prevLabel = existing.label ?? readPresetLabel(existing.data);
|
|
215
|
+
const nextLabel = readPresetLabel(mergedData);
|
|
216
|
+
next.label = nextLabel;
|
|
217
|
+
next.publicLabel = willBePublic ? nextLabel : void 0;
|
|
218
|
+
const [, userLabel] = await Promise.all([gatePublicPreset(ctx, app, tableKey, {
|
|
219
|
+
public: wasPublic,
|
|
220
|
+
label: prevLabel
|
|
221
|
+
}, {
|
|
222
|
+
public: willBePublic,
|
|
223
|
+
label: nextLabel,
|
|
224
|
+
id: existing.id
|
|
225
|
+
}), userLabelPromise]);
|
|
226
|
+
next.userLabel = userLabel;
|
|
227
|
+
if (next.data !== void 0 && typeof next.data === "object" && next.data !== null && "content" in next.data) next.aspects = derivePresetAspects(mergedData?.content);
|
|
228
|
+
return next;
|
|
229
|
+
}
|
|
230
|
+
async function processRemove(table, id, user) {
|
|
231
|
+
requireString(id, "id must be a string", "missing_scope");
|
|
232
|
+
assertNotSystemId(id);
|
|
233
|
+
requireOwner(await table.findOne({
|
|
234
|
+
filter: { id },
|
|
235
|
+
controls: { $select: {
|
|
236
|
+
id: 1,
|
|
237
|
+
user: 1
|
|
238
|
+
} }
|
|
239
|
+
}), user);
|
|
240
|
+
return id;
|
|
241
|
+
}
|
|
242
|
+
async function assertPublicLabelFree(ctx, app, tableKey, label, excludingId) {
|
|
243
|
+
if (!label) return;
|
|
244
|
+
const filter = {
|
|
245
|
+
type: "preset",
|
|
246
|
+
public: true,
|
|
247
|
+
app,
|
|
248
|
+
tableKey,
|
|
249
|
+
label
|
|
250
|
+
};
|
|
251
|
+
if (excludingId) filter.id = { $ne: excludingId };
|
|
252
|
+
if (await ctx.table.findOne({
|
|
253
|
+
filter,
|
|
254
|
+
controls: { $select: { id: 1 } }
|
|
255
|
+
})) throw presetError(409, {
|
|
256
|
+
message: `Public preset "${label}" already exists`,
|
|
257
|
+
code: "public_name_conflict"
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
async function assertCanPublish(ctx, app, tableKey) {
|
|
261
|
+
if (!await ctx.hooks.canPublishPresets(app, tableKey, ctx.user)) throw presetError(403, {
|
|
262
|
+
message: "Forbidden — current user cannot publish presets in this scope",
|
|
263
|
+
code: "publish_forbidden"
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
async function buildCapabilities(app, tableKey, user, hooks) {
|
|
267
|
+
requireString(app, "'app' query param is required", "missing_scope");
|
|
268
|
+
requireString(tableKey, "'tableKey' query param is required", "missing_scope");
|
|
269
|
+
const [canPublish, presetLimit] = await Promise.all([hooks.canPublishPresets(app, tableKey, user), hooks.getMaxPresetsPerUser(app, tableKey, user)]);
|
|
270
|
+
return {
|
|
271
|
+
canPublish,
|
|
272
|
+
presetLimit,
|
|
273
|
+
userId: user
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
async function assertWithinCap(ctx, app, tableKey) {
|
|
277
|
+
const { table, user, hooks } = ctx;
|
|
278
|
+
const [limit, count] = await Promise.all([hooks.getMaxPresetsPerUser(app, tableKey, user), table.count({ filter: {
|
|
279
|
+
type: "preset",
|
|
280
|
+
user,
|
|
281
|
+
app,
|
|
282
|
+
tableKey
|
|
283
|
+
} })]);
|
|
284
|
+
if (count >= limit) throw presetError(409, {
|
|
285
|
+
message: `Preset limit reached (${count}/${limit})`,
|
|
286
|
+
code: "preset_limit_reached",
|
|
287
|
+
limit,
|
|
288
|
+
count
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
async function sanitiseUserConfData(ctx, row, app, tableKey) {
|
|
292
|
+
if (!row.data || typeof row.data !== "object") return;
|
|
293
|
+
const data = row.data;
|
|
294
|
+
const ref = data.defaultPresetId;
|
|
295
|
+
if (typeof ref !== "string" || ref.length === 0) return;
|
|
296
|
+
if (isSystemPresetId(ref)) return;
|
|
297
|
+
const { table, user } = ctx;
|
|
298
|
+
const target = await table.findOne({
|
|
299
|
+
filter: { id: ref },
|
|
300
|
+
controls: { $select: {
|
|
301
|
+
app: 1,
|
|
302
|
+
tableKey: 1,
|
|
303
|
+
type: 1,
|
|
304
|
+
user: 1,
|
|
305
|
+
public: 1
|
|
306
|
+
} }
|
|
307
|
+
});
|
|
308
|
+
if (!(target && target.app === app && target.tableKey === tableKey && target.type === "preset" && (target.user === user || target.public === true))) delete data.defaultPresetId;
|
|
309
|
+
}
|
|
310
|
+
function readPresetLabel(data) {
|
|
311
|
+
if (data && typeof data === "object" && "label" in data) {
|
|
312
|
+
const label = data.label;
|
|
313
|
+
if (typeof label === "string") return label;
|
|
314
|
+
}
|
|
315
|
+
return "";
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
//#region \0@oxc-project+runtime@0.115.0/helpers/decorateMetadata.js
|
|
319
|
+
function __decorateMetadata(k, v) {
|
|
320
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
321
|
+
}
|
|
322
|
+
//#endregion
|
|
323
|
+
//#region \0@oxc-project+runtime@0.115.0/helpers/decorateParam.js
|
|
324
|
+
function __decorateParam(paramIndex, decorator) {
|
|
325
|
+
return function(target, key) {
|
|
326
|
+
decorator(target, key, paramIndex);
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region \0@oxc-project+runtime@0.115.0/helpers/decorate.js
|
|
331
|
+
function __decorate(decorators, target, key, desc) {
|
|
332
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
333
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
334
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
335
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
336
|
+
}
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/as-presets.controller.ts
|
|
339
|
+
var _ref;
|
|
340
|
+
let AsPresetsController = class AsPresetsController extends AsDbController {
|
|
341
|
+
maxPresetsPerUser = 10;
|
|
342
|
+
/** Override for tiered logic (admin / paid / free). */
|
|
343
|
+
async getMaxPresetsPerUser(_app, _tableKey, _user) {
|
|
344
|
+
return this.maxPresetsPerUser;
|
|
345
|
+
}
|
|
346
|
+
/** Override to scope public-preset creation per app/table/user. */
|
|
347
|
+
async canPublishPresets(_app, _tableKey, _user) {
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
async getUserLabel(_user) {}
|
|
351
|
+
get hooks() {
|
|
352
|
+
return {
|
|
353
|
+
getMaxPresetsPerUser: this.getMaxPresetsPerUser.bind(this),
|
|
354
|
+
canPublishPresets: this.canPublishPresets.bind(this),
|
|
355
|
+
getUserLabel: this.getUserLabel.bind(this)
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
get presetTable() {
|
|
359
|
+
return this.table;
|
|
360
|
+
}
|
|
361
|
+
async capabilities(query) {
|
|
362
|
+
const user = await this.getCurrentUser();
|
|
363
|
+
return buildCapabilities(query.app, query.tableKey, user, this.hooks);
|
|
364
|
+
}
|
|
365
|
+
async transformFilter(filter) {
|
|
366
|
+
return buildReadGate(await this.getCurrentUser(), filter);
|
|
367
|
+
}
|
|
368
|
+
async onWrite(action, data) {
|
|
369
|
+
const user = await this.getCurrentUser();
|
|
370
|
+
return processWrite(this.presetTable, user, action, data, this.hooks);
|
|
371
|
+
}
|
|
372
|
+
async onRemove(id) {
|
|
373
|
+
const user = await this.getCurrentUser();
|
|
374
|
+
return processRemove(this.presetTable, id, user);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
__decorate([
|
|
378
|
+
Get("capabilities"),
|
|
379
|
+
__decorateParam(0, Query()),
|
|
380
|
+
__decorateMetadata("design:type", Function),
|
|
381
|
+
__decorateMetadata("design:paramtypes", [typeof (_ref = typeof AsCapabilitiesQuery !== "undefined" && AsCapabilitiesQuery) === "function" ? _ref : Object]),
|
|
382
|
+
__decorateMetadata("design:returntype", Promise)
|
|
383
|
+
], AsPresetsController.prototype, "capabilities", null);
|
|
384
|
+
AsPresetsController = __decorate([Inherit()], AsPresetsController);
|
|
385
|
+
//#endregion
|
|
386
|
+
export { APP_CONF_PREFIX, AsPresetEntry, AsPresetsController, PRESET_ASPECTS, RESERVED_ID_PREFIXES, SYSTEM_PRESET_PREFIX, USER_CONF_PREFIX, appConfId, userConfId };
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@atscript/moost-ui-presets",
|
|
3
|
+
"version": "0.1.58",
|
|
4
|
+
"description": "Moost controller and atscript schema for table-preset persistence — server-side persistence layer for atscript-ui table presets",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"atscript",
|
|
7
|
+
"controller",
|
|
8
|
+
"metadata",
|
|
9
|
+
"moost",
|
|
10
|
+
"persistence",
|
|
11
|
+
"preset",
|
|
12
|
+
"table",
|
|
13
|
+
"typescript"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/moostjs/atscript-ui/tree/main/packages/moost-ui-presets#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/moostjs/atscript-ui/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Artem Maltsev <artem@maltsev.nl>",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/moostjs/atscript-ui.git",
|
|
24
|
+
"directory": "packages/moost-ui-presets"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"main": "dist/index.mjs",
|
|
31
|
+
"types": "dist/index.d.mts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"import": "./dist/index.mjs"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@atscript/core": "^0.1.54",
|
|
44
|
+
"@atscript/db": "^0.1.75",
|
|
45
|
+
"@atscript/db-sqlite": "^0.1.75",
|
|
46
|
+
"@atscript/moost-db": "^0.1.75",
|
|
47
|
+
"@atscript/typescript": "^0.1.54",
|
|
48
|
+
"@moostjs/event-http": "^0.6.9",
|
|
49
|
+
"@swc/core": "^1.15.18",
|
|
50
|
+
"@uniqu/core": "^0.1.6",
|
|
51
|
+
"@wooksjs/event-http": "^0.7.11",
|
|
52
|
+
"moost": "^0.6.9",
|
|
53
|
+
"unplugin-atscript": "^0.1.54",
|
|
54
|
+
"unplugin-swc": "^1.5.9",
|
|
55
|
+
"vitest": "npm:@voidzero-dev/vite-plus-test@latest",
|
|
56
|
+
"@atscript/ui-table": "0.1.58"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@atscript/core": "^0.1.54",
|
|
60
|
+
"@atscript/db": "^0.1.75",
|
|
61
|
+
"@atscript/moost-db": "^0.1.75",
|
|
62
|
+
"@atscript/typescript": "^0.1.54",
|
|
63
|
+
"@moostjs/event-http": "^0.6.9",
|
|
64
|
+
"moost": "^0.6.9",
|
|
65
|
+
"@atscript/ui-table": "^0.1.58"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "vp pack",
|
|
69
|
+
"dev": "vp pack --watch",
|
|
70
|
+
"test": "vp test",
|
|
71
|
+
"check": "vp check"
|
|
72
|
+
}
|
|
73
|
+
}
|