@web-ts-toolkit/access-router 0.29.0 → 0.31.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/advanced.d.mts +1433 -4
- package/advanced.d.ts +1433 -4
- package/index.d.mts +1157 -14
- package/index.d.ts +1157 -14
- package/index.js +111 -108
- package/index.mjs +15 -9
- package/package.json +3 -3
- package/parsers-Cj4r_G4g.d.mts +0 -1436
- package/parsers-Cj4r_G4g.d.ts +0 -1436
package/index.js
CHANGED
|
@@ -67,14 +67,14 @@ __export(index_exports, {
|
|
|
67
67
|
setModelOptions: () => setModelOptions
|
|
68
68
|
});
|
|
69
69
|
module.exports = __toCommonJS(index_exports);
|
|
70
|
-
var
|
|
70
|
+
var import_utils28 = require("@web-ts-toolkit/utils");
|
|
71
71
|
|
|
72
72
|
// src/middleware.ts
|
|
73
73
|
var import_express_json_router2 = __toESM(require("@web-ts-toolkit/express-json-router"));
|
|
74
|
-
var
|
|
74
|
+
var import_utils21 = require("@web-ts-toolkit/utils");
|
|
75
75
|
|
|
76
76
|
// src/core.ts
|
|
77
|
-
var
|
|
77
|
+
var import_utils20 = require("@web-ts-toolkit/utils");
|
|
78
78
|
|
|
79
79
|
// src/runtime.ts
|
|
80
80
|
var import_mongoose3 = __toESM(require("mongoose"));
|
|
@@ -163,7 +163,8 @@ function setDocValue(doc, path, value) {
|
|
|
163
163
|
}
|
|
164
164
|
function getDocPermissions(modelName, doc) {
|
|
165
165
|
const docPermissionField = getModelOption(modelName, "documentPermissionField");
|
|
166
|
-
|
|
166
|
+
const permissions = getDocValue(doc, docPermissionField, {});
|
|
167
|
+
return (0, import_utils3.isPlainObject)(permissions) ? permissions : {};
|
|
167
168
|
}
|
|
168
169
|
function toObject(doc) {
|
|
169
170
|
return isDocument(doc) ? doc.toObject() : doc;
|
|
@@ -921,7 +922,8 @@ var AccessRuntime = class {
|
|
|
921
922
|
}
|
|
922
923
|
getModelRef(modelName, refPath) {
|
|
923
924
|
this.ensureModelMeta(modelName);
|
|
924
|
-
|
|
925
|
+
const value = (0, import_utils10.get)(this.modelRefs, `${modelName}.${refPath}`, null);
|
|
926
|
+
return (0, import_utils10.isString)(value) ? value : null;
|
|
925
927
|
}
|
|
926
928
|
getModelSub(modelName) {
|
|
927
929
|
this.ensureModelMeta(modelName);
|
|
@@ -2094,7 +2096,7 @@ var Service = class extends Base {
|
|
|
2094
2096
|
return resolveExistsOptions(this, options);
|
|
2095
2097
|
}
|
|
2096
2098
|
async getFieldPermissionAccess(ids) {
|
|
2097
|
-
const uniqueIds = (0, import_utils13.
|
|
2099
|
+
const uniqueIds = (0, import_utils13.compact)((0, import_utils13.uniqBy)(ids, (id) => String(id)).map((id) => String(id)));
|
|
2098
2100
|
if (uniqueIds.length === 0) {
|
|
2099
2101
|
return {
|
|
2100
2102
|
readIds: /* @__PURE__ */ new Set(),
|
|
@@ -2572,9 +2574,10 @@ var Cache = class {
|
|
|
2572
2574
|
};
|
|
2573
2575
|
|
|
2574
2576
|
// src/core-shared.ts
|
|
2575
|
-
var
|
|
2577
|
+
var import_utils18 = require("@web-ts-toolkit/utils");
|
|
2576
2578
|
|
|
2577
2579
|
// src/permission.ts
|
|
2580
|
+
var import_utils17 = require("@web-ts-toolkit/utils");
|
|
2578
2581
|
var Permission = class {
|
|
2579
2582
|
constructor(permissions) {
|
|
2580
2583
|
this.permissions = { ...permissions };
|
|
@@ -2590,7 +2593,7 @@ var Permission = class {
|
|
|
2590
2593
|
return [permissionOrPermissions].concat(permissions);
|
|
2591
2594
|
}
|
|
2592
2595
|
hasKey(permission) {
|
|
2593
|
-
return
|
|
2596
|
+
return (0, import_utils17.hasOwn)(this.permissions, permission);
|
|
2594
2597
|
}
|
|
2595
2598
|
has(permission) {
|
|
2596
2599
|
return this.permissions[permission] === true;
|
|
@@ -2610,10 +2613,10 @@ var permission_default = Permission;
|
|
|
2610
2613
|
|
|
2611
2614
|
// src/core-shared.ts
|
|
2612
2615
|
function isAndFilter(filter2) {
|
|
2613
|
-
return !!filter2 && (0,
|
|
2616
|
+
return !!filter2 && (0, import_utils18.isPlainObject)(filter2) && Object.keys(filter2).length === 1 && (0, import_utils18.isArray)(filter2.$and);
|
|
2614
2617
|
}
|
|
2615
2618
|
function isMergeableClause(filter2) {
|
|
2616
|
-
return !!filter2 && (0,
|
|
2619
|
+
return !!filter2 && (0, import_utils18.isPlainObject)(filter2) && Object.keys(filter2).every((key) => !key.startsWith("$"));
|
|
2617
2620
|
}
|
|
2618
2621
|
function optimizeAndFilter(clausesInput) {
|
|
2619
2622
|
const clauses = [];
|
|
@@ -2628,7 +2631,7 @@ function optimizeAndFilter(clausesInput) {
|
|
|
2628
2631
|
}
|
|
2629
2632
|
}
|
|
2630
2633
|
const dedupedClauses = clauses.filter(
|
|
2631
|
-
(clause, index) => clauses.findIndex((item) => (0,
|
|
2634
|
+
(clause, index) => clauses.findIndex((item) => (0, import_utils18.isEqual)(item, clause)) === index
|
|
2632
2635
|
);
|
|
2633
2636
|
const mergedClause = {};
|
|
2634
2637
|
const remainingClauses = [];
|
|
@@ -2640,7 +2643,7 @@ function optimizeAndFilter(clausesInput) {
|
|
|
2640
2643
|
}
|
|
2641
2644
|
let canMerge = true;
|
|
2642
2645
|
for (const [key, value] of Object.entries(clause)) {
|
|
2643
|
-
if (
|
|
2646
|
+
if ((0, import_utils18.hasOwn)(mergedClause, key) && !(0, import_utils18.isEqual)(mergedClause[key], value)) {
|
|
2644
2647
|
canMerge = false;
|
|
2645
2648
|
break;
|
|
2646
2649
|
}
|
|
@@ -2651,24 +2654,24 @@ function optimizeAndFilter(clausesInput) {
|
|
|
2651
2654
|
}
|
|
2652
2655
|
Object.assign(mergedClause, clause);
|
|
2653
2656
|
}
|
|
2654
|
-
const finalClauses = (0,
|
|
2657
|
+
const finalClauses = (0, import_utils18.isEmpty)(mergedClause) ? remainingClauses : [mergedClause, ...remainingClauses];
|
|
2655
2658
|
if (finalClauses.length === 0) return null;
|
|
2656
2659
|
if (finalClauses.length === 1) return finalClauses[0];
|
|
2657
2660
|
return { $and: finalClauses };
|
|
2658
2661
|
}
|
|
2659
2662
|
function normalizeFilter(filter2) {
|
|
2660
2663
|
if (filter2 === false) return false;
|
|
2661
|
-
if (!filter2 || !(0,
|
|
2664
|
+
if (!filter2 || !(0, import_utils18.isPlainObject)(filter2) || (0, import_utils18.isEmpty)(filter2)) return null;
|
|
2662
2665
|
if (!isAndFilter(filter2)) {
|
|
2663
2666
|
return filter2;
|
|
2664
2667
|
}
|
|
2665
2668
|
return optimizeAndFilter(filter2.$and);
|
|
2666
2669
|
}
|
|
2667
2670
|
async function resolveIdentifierFilter(req, idField, resolveIdFilter, id) {
|
|
2668
|
-
if ((0,
|
|
2671
|
+
if ((0, import_utils18.isFunction)(resolveIdFilter)) {
|
|
2669
2672
|
return await resolveIdFilter.call(req, id);
|
|
2670
2673
|
}
|
|
2671
|
-
if ((0,
|
|
2674
|
+
if ((0, import_utils18.isString)(idField)) {
|
|
2672
2675
|
return { [idField]: id };
|
|
2673
2676
|
}
|
|
2674
2677
|
return { _id: id };
|
|
@@ -2684,11 +2687,11 @@ async function resolveAccessFilter({
|
|
|
2684
2687
|
}) {
|
|
2685
2688
|
let nextFilter = normalizeFilter(filter2);
|
|
2686
2689
|
const overrideFilterFn = getOption(`overrideFilter.${access}`, null);
|
|
2687
|
-
if ((0,
|
|
2690
|
+
if ((0, import_utils18.isFunction)(overrideFilterFn)) {
|
|
2688
2691
|
nextFilter = normalizeFilter(await overrideFilterFn.call(req, nextFilter, permissions));
|
|
2689
2692
|
}
|
|
2690
2693
|
const baseFilterFn = getOption(`baseFilter.${access}`, null);
|
|
2691
|
-
if (!(0,
|
|
2694
|
+
if (!(0, import_utils18.isFunction)(baseFilterFn)) return nextFilter || {};
|
|
2692
2695
|
const baseFilter = normalizeFilter(
|
|
2693
2696
|
cache.has(cacheKey) ? cache.get(cacheKey) : await baseFilterFn.call(req, permissions)
|
|
2694
2697
|
);
|
|
@@ -2708,33 +2711,33 @@ async function setRequestPermissions(req) {
|
|
|
2708
2711
|
const requestPermissionField = getGlobalOption("requestPermissionField");
|
|
2709
2712
|
if (req[requestPermissionField]) return;
|
|
2710
2713
|
const globalPermissions = getGlobalOption("globalPermissions");
|
|
2711
|
-
if (!(0,
|
|
2714
|
+
if (!(0, import_utils18.isFunction)(globalPermissions)) return;
|
|
2712
2715
|
const permissions = await globalPermissions.call(req, req);
|
|
2713
|
-
if ((0,
|
|
2714
|
-
else if ((0,
|
|
2715
|
-
else if ((0,
|
|
2716
|
+
if ((0, import_utils18.isPlainObject)(permissions)) req[requestPermissionField] = permissions;
|
|
2717
|
+
else if ((0, import_utils18.isArray)(permissions)) req[requestPermissionField] = (0, import_utils18.arrayToRecord)(permissions);
|
|
2718
|
+
else if ((0, import_utils18.isString)(permissions)) req[requestPermissionField] = { [permissions]: true };
|
|
2716
2719
|
}
|
|
2717
2720
|
async function evaluateRouteGuard(req, permissions, routeGuard) {
|
|
2718
2721
|
const phas = (key) => permissions.has(key);
|
|
2719
2722
|
const { stringHandler, arrayHandler } = createValidator(phas);
|
|
2720
|
-
if ((0,
|
|
2723
|
+
if ((0, import_utils18.isBoolean)(routeGuard)) {
|
|
2721
2724
|
return routeGuard === true;
|
|
2722
2725
|
}
|
|
2723
|
-
if ((0,
|
|
2726
|
+
if ((0, import_utils18.isString)(routeGuard)) {
|
|
2724
2727
|
return stringHandler(routeGuard);
|
|
2725
2728
|
}
|
|
2726
|
-
if ((0,
|
|
2729
|
+
if ((0, import_utils18.isArray)(routeGuard)) {
|
|
2727
2730
|
return arrayHandler(routeGuard);
|
|
2728
2731
|
}
|
|
2729
|
-
if ((0,
|
|
2732
|
+
if ((0, import_utils18.isFunction)(routeGuard)) {
|
|
2730
2733
|
return routeGuard.call(req, permissions);
|
|
2731
2734
|
}
|
|
2732
2735
|
return false;
|
|
2733
2736
|
}
|
|
2734
2737
|
async function callHookChain(req, hook, doc, permissions, context) {
|
|
2735
|
-
const hooks = (0,
|
|
2738
|
+
const hooks = (0, import_utils18.castArray)(hook);
|
|
2736
2739
|
for (let x = 0; x < hooks.length; x++) {
|
|
2737
|
-
if ((0,
|
|
2740
|
+
if ((0, import_utils18.isFunction)(hooks[x])) {
|
|
2738
2741
|
doc = await hooks[x].call(req, doc, permissions, context);
|
|
2739
2742
|
}
|
|
2740
2743
|
}
|
|
@@ -2757,13 +2760,13 @@ async function collectSchemaFields({
|
|
|
2757
2760
|
if (baseFields.includes(key)) continue;
|
|
2758
2761
|
const val = permissionSchema[key];
|
|
2759
2762
|
const value = val && val[access] || val;
|
|
2760
|
-
if ((0,
|
|
2763
|
+
if ((0, import_utils18.isBoolean)(value)) {
|
|
2761
2764
|
if (value) fields.push(key);
|
|
2762
|
-
} else if ((0,
|
|
2765
|
+
} else if ((0, import_utils18.isString)(value)) {
|
|
2763
2766
|
if (stringHandler(value)) fields.push(key);
|
|
2764
|
-
} else if ((0,
|
|
2767
|
+
} else if ((0, import_utils18.isArray)(value)) {
|
|
2765
2768
|
if (arrayHandler(value)) fields.push(key);
|
|
2766
|
-
} else if ((0,
|
|
2769
|
+
} else if ((0, import_utils18.isFunction)(value)) {
|
|
2767
2770
|
if (await value.call(req, ...functionArgs)) fields.push(key);
|
|
2768
2771
|
}
|
|
2769
2772
|
}
|
|
@@ -2852,7 +2855,7 @@ async function runDecorateAllHook({
|
|
|
2852
2855
|
}
|
|
2853
2856
|
|
|
2854
2857
|
// src/acl/select-resolution.ts
|
|
2855
|
-
var
|
|
2858
|
+
var import_utils19 = require("@web-ts-toolkit/utils");
|
|
2856
2859
|
async function collectAllowedFieldsForRequest({
|
|
2857
2860
|
req,
|
|
2858
2861
|
permissionSchema,
|
|
@@ -2920,15 +2923,15 @@ async function resolveSelectForRequest({
|
|
|
2920
2923
|
const excludeall = normalizedSelect.every((v) => v.startsWith("-"));
|
|
2921
2924
|
if (excludeall) {
|
|
2922
2925
|
normalizedSelect = normalizedSelect.map((v) => v.substring(1));
|
|
2923
|
-
fields = (0,
|
|
2926
|
+
fields = (0, import_utils19.difference)(fields, normalizedSelect);
|
|
2924
2927
|
if (excludeid) fields.push("-_id");
|
|
2925
2928
|
} else {
|
|
2926
|
-
fields = (0,
|
|
2929
|
+
fields = (0, import_utils19.intersection)(normalizedSelect, fields.concat(excludeid ? "-_id" : "_id"));
|
|
2927
2930
|
}
|
|
2928
2931
|
}
|
|
2929
2932
|
return fields.concat(alwaysSelectFields);
|
|
2930
2933
|
}
|
|
2931
|
-
return (0,
|
|
2934
|
+
return (0, import_utils19.intersection)(normalizedSelect, fields);
|
|
2932
2935
|
}
|
|
2933
2936
|
|
|
2934
2937
|
// src/core.ts
|
|
@@ -2942,10 +2945,10 @@ var Core = class {
|
|
|
2942
2945
|
getIdentifier(modelName) {
|
|
2943
2946
|
const resolveIdFilter = getModelOption(modelName, "resolveIdFilter");
|
|
2944
2947
|
const idField = getModelOption(modelName, "idField");
|
|
2945
|
-
if ((0,
|
|
2948
|
+
if ((0, import_utils20.isFunction)(resolveIdFilter)) {
|
|
2946
2949
|
return null;
|
|
2947
2950
|
}
|
|
2948
|
-
if ((0,
|
|
2951
|
+
if ((0, import_utils20.isString)(idField)) {
|
|
2949
2952
|
return idField;
|
|
2950
2953
|
}
|
|
2951
2954
|
return "_id";
|
|
@@ -2985,7 +2988,7 @@ var Core = class {
|
|
|
2985
2988
|
permissionSchema,
|
|
2986
2989
|
access,
|
|
2987
2990
|
baseFields,
|
|
2988
|
-
hasPermission: (key) => permissions.has(key) || docPermissions[this.removePrefix(key, modelPermissionPrefix)],
|
|
2991
|
+
hasPermission: (key) => permissions.has(key) || Boolean(docPermissions[this.removePrefix(key, modelPermissionPrefix)]),
|
|
2989
2992
|
functionArgs: [permissions, docPermissions]
|
|
2990
2993
|
});
|
|
2991
2994
|
}
|
|
@@ -3000,7 +3003,7 @@ var Core = class {
|
|
|
3000
3003
|
permissionSchema,
|
|
3001
3004
|
access,
|
|
3002
3005
|
baseFields,
|
|
3003
|
-
hasPermission: (key) => permissions.has(key) || docPermissions[this.removePrefix(key, modelPermissionPrefix)],
|
|
3006
|
+
hasPermission: (key) => permissions.has(key) || Boolean(docPermissions[this.removePrefix(key, modelPermissionPrefix)]),
|
|
3004
3007
|
functionArgs: [permissions, docPermissions]
|
|
3005
3008
|
});
|
|
3006
3009
|
}
|
|
@@ -3023,11 +3026,11 @@ var Core = class {
|
|
|
3023
3026
|
async genPopulate(modelName, access = "read", _populate = null) {
|
|
3024
3027
|
if (!_populate) return [];
|
|
3025
3028
|
let populate = Array.isArray(_populate) ? _populate : [_populate];
|
|
3026
|
-
populate = (0,
|
|
3029
|
+
populate = (0, import_utils20.compact)(
|
|
3027
3030
|
await Promise.all(
|
|
3028
3031
|
populate.map(async (p) => {
|
|
3029
|
-
const populateAccess = !(0,
|
|
3030
|
-
const ret = (0,
|
|
3032
|
+
const populateAccess = !(0, import_utils20.isString)(p) && p.access ? p.access : access;
|
|
3033
|
+
const ret = (0, import_utils20.isString)(p) ? { path: p } : {
|
|
3031
3034
|
path: p.path,
|
|
3032
3035
|
select: normalizeSelect(p.select)
|
|
3033
3036
|
};
|
|
@@ -3047,10 +3050,10 @@ var Core = class {
|
|
|
3047
3050
|
}
|
|
3048
3051
|
async validate(modelName, allowedData, access, context) {
|
|
3049
3052
|
const validate = getModelOption(modelName, `validate.${access}`, null);
|
|
3050
|
-
if ((0,
|
|
3053
|
+
if ((0, import_utils20.isFunction)(validate)) {
|
|
3051
3054
|
const permissions = this.getGlobalPermissions();
|
|
3052
3055
|
return validate.call(this.req, allowedData, permissions, context);
|
|
3053
|
-
} else if ((0,
|
|
3056
|
+
} else if ((0, import_utils20.isBoolean)(validate) || (0, import_utils20.isArray)(validate)) {
|
|
3054
3057
|
return validate;
|
|
3055
3058
|
} else {
|
|
3056
3059
|
return true;
|
|
@@ -3075,7 +3078,7 @@ var Core = class {
|
|
|
3075
3078
|
const changeOptions = getModelOption(modelName, `onChange`, {});
|
|
3076
3079
|
for (let x = 0; x < context.modifiedPaths.length; x++) {
|
|
3077
3080
|
const mpath = context.modifiedPaths[x];
|
|
3078
|
-
if ((0,
|
|
3081
|
+
if ((0, import_utils20.isFunction)(changeOptions[mpath])) {
|
|
3079
3082
|
await changeOptions[mpath].call(
|
|
3080
3083
|
this.req,
|
|
3081
3084
|
context.originalDocumentSnapshot[mpath],
|
|
@@ -3099,7 +3102,7 @@ var Core = class {
|
|
|
3099
3102
|
async genDocPermissions(modelName, doc, access, context) {
|
|
3100
3103
|
const docPermissionsFn = getModelOption(modelName, `docPermissions.${access}`, null);
|
|
3101
3104
|
let docPermissions = {};
|
|
3102
|
-
if ((0,
|
|
3105
|
+
if ((0, import_utils20.isFunction)(docPermissionsFn)) {
|
|
3103
3106
|
const permissions = this.getGlobalPermissions();
|
|
3104
3107
|
try {
|
|
3105
3108
|
docPermissions = await docPermissionsFn.call(this.req, doc, permissions, context);
|
|
@@ -3147,7 +3150,7 @@ var Core = class {
|
|
|
3147
3150
|
readExists ? this.genAllowedFields(modelName, doc, "read") : [],
|
|
3148
3151
|
updateExists ? this.genAllowedFields(modelName, doc, "update") : []
|
|
3149
3152
|
]);
|
|
3150
|
-
const viewObj = (0,
|
|
3153
|
+
const viewObj = (0, import_utils20.reduce)(
|
|
3151
3154
|
views,
|
|
3152
3155
|
(ret, view) => {
|
|
3153
3156
|
ret[view] = true;
|
|
@@ -3155,7 +3158,7 @@ var Core = class {
|
|
|
3155
3158
|
},
|
|
3156
3159
|
{}
|
|
3157
3160
|
);
|
|
3158
|
-
const editObj = (0,
|
|
3161
|
+
const editObj = (0, import_utils20.reduce)(
|
|
3159
3162
|
edits,
|
|
3160
3163
|
(ret, view) => {
|
|
3161
3164
|
ret[view] = true;
|
|
@@ -3179,9 +3182,9 @@ var Core = class {
|
|
|
3179
3182
|
return runDecorateAllHook({ req: this.req, hook: decorateAll, docs, permissions, context });
|
|
3180
3183
|
}
|
|
3181
3184
|
runTasks(modelName, docObject, task) {
|
|
3182
|
-
const tasks = (0,
|
|
3185
|
+
const tasks = (0, import_utils20.compact)((0, import_utils20.castArray)(task));
|
|
3183
3186
|
if (tasks.length === 0) return docObject;
|
|
3184
|
-
(0,
|
|
3187
|
+
(0, import_utils20.forEach)(tasks, (task2) => {
|
|
3185
3188
|
const { type, args, options } = task2;
|
|
3186
3189
|
switch (type) {
|
|
3187
3190
|
case "COPY_AND_DEPOPULATE":
|
|
@@ -3212,9 +3215,9 @@ var Core = class {
|
|
|
3212
3215
|
}
|
|
3213
3216
|
const [, field, op] = keys2;
|
|
3214
3217
|
const subOption = getExactModelOption(modelName, `operationAccess.${access}`);
|
|
3215
|
-
if ((0,
|
|
3218
|
+
if ((0, import_utils20.isUndefined)(subOption)) {
|
|
3216
3219
|
const subFieldOption = getExactModelOption(modelName, `operationAccess.subs.${field}`);
|
|
3217
|
-
if ((0,
|
|
3220
|
+
if ((0, import_utils20.isUndefined)(subFieldOption)) {
|
|
3218
3221
|
const opOption = getModelOption(modelName, `operationAccess.${op}`);
|
|
3219
3222
|
return this.canActivate(opOption);
|
|
3220
3223
|
}
|
|
@@ -3264,25 +3267,25 @@ function guard(condition) {
|
|
|
3264
3267
|
const permissions = req[PERMISSIONS];
|
|
3265
3268
|
let cond = condition;
|
|
3266
3269
|
let phas = (key) => permissions.has(key);
|
|
3267
|
-
if ((0,
|
|
3270
|
+
if ((0, import_utils21.isPlainObject)(condition)) {
|
|
3268
3271
|
const { modelName, id, condition: _cond } = condition;
|
|
3269
3272
|
const svc = req.macl.getPublicService(modelName);
|
|
3270
3273
|
const select = getModelOption(modelName, `alwaysSelectFields.read`, void 0);
|
|
3271
|
-
let _id = (0,
|
|
3272
|
-
if ((0,
|
|
3274
|
+
let _id = (0, import_utils21.isString)(id) ? id : null;
|
|
3275
|
+
if ((0, import_utils21.isPlainObject)(id)) {
|
|
3273
3276
|
const { type = "param", key } = id;
|
|
3274
3277
|
if (type === "param") {
|
|
3275
3278
|
const paramValue = req.params[key];
|
|
3276
|
-
_id = (0,
|
|
3279
|
+
_id = (0, import_utils21.isArray)(paramValue) ? paramValue[0] ?? null : paramValue ?? null;
|
|
3277
3280
|
} else if (type === "query") {
|
|
3278
3281
|
const _qval = req.query[key];
|
|
3279
|
-
if ((0,
|
|
3282
|
+
if ((0, import_utils21.isArray)(_qval)) _id = _qval.length > 0 ? _qval[0] : null;
|
|
3280
3283
|
else _id = _qval;
|
|
3281
3284
|
} else {
|
|
3282
3285
|
_id = null;
|
|
3283
3286
|
}
|
|
3284
3287
|
}
|
|
3285
|
-
if (!(0,
|
|
3288
|
+
if (!(0, import_utils21.isString)(_id) || !_id) {
|
|
3286
3289
|
return next(new import_express_json_router2.default.clientErrors.BadRequestError());
|
|
3287
3290
|
}
|
|
3288
3291
|
const result = await svc._read(_id, { select });
|
|
@@ -3290,15 +3293,15 @@ function guard(condition) {
|
|
|
3290
3293
|
return next(new import_express_json_router2.default.clientErrors.UnauthorizedError());
|
|
3291
3294
|
}
|
|
3292
3295
|
const docPermissions = getDocPermissions(modelName, result.data);
|
|
3293
|
-
phas = (key) => permissions.has(key) || docPermissions[key];
|
|
3296
|
+
phas = (key) => permissions.has(key) || Boolean(docPermissions[key]);
|
|
3294
3297
|
cond = _cond;
|
|
3295
3298
|
}
|
|
3296
3299
|
const { stringHandler, arrayHandler } = createValidator(phas);
|
|
3297
|
-
if ((0,
|
|
3300
|
+
if ((0, import_utils21.isString)(cond)) {
|
|
3298
3301
|
if (stringHandler(cond)) return next();
|
|
3299
|
-
} else if ((0,
|
|
3302
|
+
} else if ((0, import_utils21.isArray)(cond)) {
|
|
3300
3303
|
if (arrayHandler(cond)) return next();
|
|
3301
|
-
} else if ((0,
|
|
3304
|
+
} else if ((0, import_utils21.isFunction)(cond)) {
|
|
3302
3305
|
const result = await cond.call(req, permissions);
|
|
3303
3306
|
if (!!result) return next();
|
|
3304
3307
|
}
|
|
@@ -3311,7 +3314,7 @@ var import_express2 = __toESM(require("express"));
|
|
|
3311
3314
|
|
|
3312
3315
|
// src/routers/model-router.ts
|
|
3313
3316
|
var import_express_json_router5 = __toESM(require("@web-ts-toolkit/express-json-router"));
|
|
3314
|
-
var
|
|
3317
|
+
var import_utils25 = require("@web-ts-toolkit/utils");
|
|
3315
3318
|
|
|
3316
3319
|
// src/routers/router-mutation.ts
|
|
3317
3320
|
var MODEL_BUILD_TIME_OPTION_KEYS = /* @__PURE__ */ new Set([
|
|
@@ -4251,7 +4254,7 @@ var rootDataQueryEntrySchema = import_zod5.z.union([
|
|
|
4251
4254
|
var rootQuerySchema = import_zod5.z.array(import_zod5.z.union([rootModelQueryEntrySchema, rootDataQueryEntrySchema]));
|
|
4252
4255
|
|
|
4253
4256
|
// src/routers/shared.ts
|
|
4254
|
-
var
|
|
4257
|
+
var import_utils22 = require("@web-ts-toolkit/utils");
|
|
4255
4258
|
|
|
4256
4259
|
// src/routers/model-router-collection-routes.ts
|
|
4257
4260
|
function setModelCollectionRoutes(context) {
|
|
@@ -4269,14 +4272,14 @@ function setModelCollectionRoutes(context) {
|
|
|
4269
4272
|
await context.assertAllowed(req, "list");
|
|
4270
4273
|
const { skip, limit, page, page_size, skim, include_permissions, include_count, include_extra_headers } = parseQuery(requestSchemas.listQuery, req.query);
|
|
4271
4274
|
const svc = context.getPublicService(req);
|
|
4272
|
-
const includeCount = (0,
|
|
4273
|
-
const includeExtraHeaders = (0,
|
|
4275
|
+
const includeCount = (0, import_utils22.parseBooleanString)(include_count);
|
|
4276
|
+
const includeExtraHeaders = (0, import_utils22.parseBooleanString)(include_extra_headers);
|
|
4274
4277
|
const result = await svc._list(
|
|
4275
4278
|
{},
|
|
4276
4279
|
{ skip, limit, page, pageSize: page_size },
|
|
4277
4280
|
{
|
|
4278
|
-
skim: (0,
|
|
4279
|
-
includePermissions: (0,
|
|
4281
|
+
skim: (0, import_utils22.parseBooleanString)(skim),
|
|
4282
|
+
includePermissions: (0, import_utils22.parseBooleanString)(include_permissions),
|
|
4280
4283
|
includeCount
|
|
4281
4284
|
}
|
|
4282
4285
|
);
|
|
@@ -4331,7 +4334,7 @@ function setModelCollectionRoutes(context) {
|
|
|
4331
4334
|
context.getRequestSchema("requestSchemas.create")
|
|
4332
4335
|
);
|
|
4333
4336
|
const svc = context.getPublicService(req);
|
|
4334
|
-
const result = await svc._create(data, {}, { includePermissions: (0,
|
|
4337
|
+
const result = await svc._create(data, {}, { includePermissions: (0, import_utils22.parseBooleanString)(include_permissions) });
|
|
4335
4338
|
handleResultError(result);
|
|
4336
4339
|
return formatModelCreatedResponse(result);
|
|
4337
4340
|
});
|
|
@@ -4365,7 +4368,7 @@ function setModelCollectionRoutes(context) {
|
|
|
4365
4368
|
data,
|
|
4366
4369
|
{ select, populate, tasks },
|
|
4367
4370
|
{
|
|
4368
|
-
includePermissions: includePermissions ?? (0,
|
|
4371
|
+
includePermissions: includePermissions ?? (0, import_utils22.parseBooleanString)(include_permissions),
|
|
4369
4372
|
populateAccess
|
|
4370
4373
|
}
|
|
4371
4374
|
);
|
|
@@ -4459,8 +4462,8 @@ function setModelDocumentRoutes(context) {
|
|
|
4459
4462
|
id,
|
|
4460
4463
|
{},
|
|
4461
4464
|
{
|
|
4462
|
-
includePermissions: (0,
|
|
4463
|
-
tryList: (0,
|
|
4465
|
+
includePermissions: (0, import_utils22.parseBooleanString)(include_permissions),
|
|
4466
|
+
tryList: (0, import_utils22.parseBooleanString)(try_list)
|
|
4464
4467
|
}
|
|
4465
4468
|
);
|
|
4466
4469
|
handleResultError(result);
|
|
@@ -4545,8 +4548,8 @@ function setModelDocumentRoutes(context) {
|
|
|
4545
4548
|
data,
|
|
4546
4549
|
{},
|
|
4547
4550
|
{
|
|
4548
|
-
returningAll: (0,
|
|
4549
|
-
includePermissions: (0,
|
|
4551
|
+
returningAll: (0, import_utils22.parseBooleanString)(returning_all),
|
|
4552
|
+
includePermissions: (0, import_utils22.parseBooleanString)(include_permissions)
|
|
4550
4553
|
}
|
|
4551
4554
|
);
|
|
4552
4555
|
handleResultError(result);
|
|
@@ -4584,8 +4587,8 @@ function setModelDocumentRoutes(context) {
|
|
|
4584
4587
|
data,
|
|
4585
4588
|
{ select, populate, tasks },
|
|
4586
4589
|
{
|
|
4587
|
-
returningAll: returningAll ?? (0,
|
|
4588
|
-
includePermissions: includePermissions ?? (0,
|
|
4590
|
+
returningAll: returningAll ?? (0, import_utils22.parseBooleanString)(returning_all),
|
|
4591
|
+
includePermissions: includePermissions ?? (0, import_utils22.parseBooleanString)(include_permissions),
|
|
4589
4592
|
populateAccess
|
|
4590
4593
|
}
|
|
4591
4594
|
);
|
|
@@ -4613,8 +4616,8 @@ function setModelDocumentRoutes(context) {
|
|
|
4613
4616
|
body,
|
|
4614
4617
|
{},
|
|
4615
4618
|
{
|
|
4616
|
-
returningAll: (0,
|
|
4617
|
-
includePermissions: (0,
|
|
4619
|
+
returningAll: (0, import_utils22.parseBooleanString)(returning_all),
|
|
4620
|
+
includePermissions: (0, import_utils22.parseBooleanString)(include_permissions)
|
|
4618
4621
|
}
|
|
4619
4622
|
);
|
|
4620
4623
|
handleResultError(result);
|
|
@@ -4650,8 +4653,8 @@ function setModelDocumentRoutes(context) {
|
|
|
4650
4653
|
data,
|
|
4651
4654
|
{ select, populate, tasks },
|
|
4652
4655
|
{
|
|
4653
|
-
returningAll: returningAll ?? (0,
|
|
4654
|
-
includePermissions: includePermissions ?? (0,
|
|
4656
|
+
returningAll: returningAll ?? (0, import_utils22.parseBooleanString)(returning_all),
|
|
4657
|
+
includePermissions: includePermissions ?? (0, import_utils22.parseBooleanString)(include_permissions),
|
|
4655
4658
|
populateAccess
|
|
4656
4659
|
}
|
|
4657
4660
|
);
|
|
@@ -4717,7 +4720,7 @@ function setModelDocumentRoutes(context) {
|
|
|
4717
4720
|
}
|
|
4718
4721
|
|
|
4719
4722
|
// src/routers/model-router-subdocument-routes.ts
|
|
4720
|
-
var
|
|
4723
|
+
var import_utils23 = require("@web-ts-toolkit/utils");
|
|
4721
4724
|
function setModelSubDocumentRoutes(context) {
|
|
4722
4725
|
const subs = getModelSub(context.modelName);
|
|
4723
4726
|
for (let x = 0; x < subs.length; x++) {
|
|
@@ -4808,7 +4811,7 @@ function setModelSubDocumentRoutes(context) {
|
|
|
4808
4811
|
context.getRequestSchema("requestSchemas.subRead")
|
|
4809
4812
|
);
|
|
4810
4813
|
const populate = body.populate;
|
|
4811
|
-
const normalizedPopulate = Array.isArray(populate) ? populate.map((item) => (0,
|
|
4814
|
+
const normalizedPopulate = Array.isArray(populate) ? populate.map((item) => (0, import_utils23.isString)(item) ? { path: item } : item) : (0, import_utils23.isString)(populate) ? { path: populate } : populate ?? [];
|
|
4812
4815
|
const svc = context.getPublicService(req);
|
|
4813
4816
|
const result = await svc.readSub(id, sub, subId, { select: body.select ?? [], populate: normalizedPopulate });
|
|
4814
4817
|
handleResultError(result);
|
|
@@ -4886,11 +4889,11 @@ function setModelSubDocumentRoutes(context) {
|
|
|
4886
4889
|
}
|
|
4887
4890
|
|
|
4888
4891
|
// src/openapi/route-registration.ts
|
|
4889
|
-
var
|
|
4892
|
+
var import_utils24 = require("@web-ts-toolkit/utils");
|
|
4890
4893
|
function registerOpenApiRoute(runtime, basePath, defaultTag, route) {
|
|
4891
4894
|
runtime.registerOpenApiRoute({
|
|
4892
4895
|
...route,
|
|
4893
|
-
path: (0,
|
|
4896
|
+
path: (0, import_utils24.normalizeUrlPath)(basePath + route.path),
|
|
4894
4897
|
tags: route.tags ?? [defaultTag]
|
|
4895
4898
|
});
|
|
4896
4899
|
}
|
|
@@ -4898,8 +4901,8 @@ function registerOpenApiRoute(runtime, basePath, defaultTag, route) {
|
|
|
4898
4901
|
// src/routers/model-router.ts
|
|
4899
4902
|
var clientErrors2 = import_express_json_router5.default.clientErrors;
|
|
4900
4903
|
function setOption(parentKey, optionKey, option) {
|
|
4901
|
-
const key = (0,
|
|
4902
|
-
const value = (0,
|
|
4904
|
+
const key = (0, import_utils25.isUndefined)(option) ? parentKey : `${parentKey}.${optionKey}`;
|
|
4905
|
+
const value = (0, import_utils25.isUndefined)(option) ? optionKey : option;
|
|
4903
4906
|
assertMutableRouterOption("model", key);
|
|
4904
4907
|
this.runtime.setModelOption(this.modelName, key, value);
|
|
4905
4908
|
return this;
|
|
@@ -5060,7 +5063,7 @@ var ModelRouter = class {
|
|
|
5060
5063
|
this.runtime.setModelOptions(modelName, initialOptions);
|
|
5061
5064
|
attachRuntimeToModel(modelName, this.runtime);
|
|
5062
5065
|
this.options = this.runtime.getModelOptions(modelName);
|
|
5063
|
-
this.fullBasePath = (0,
|
|
5066
|
+
this.fullBasePath = (0, import_utils25.normalizeUrlPath)(this.options.parentPath + this.options.basePath);
|
|
5064
5067
|
this.modelName = modelName;
|
|
5065
5068
|
this.router = new import_express_json_router5.default(this.options.basePath, createSetCore(this.runtime), accessRouterResponseHandler);
|
|
5066
5069
|
this.model = new model_default(modelName);
|
|
@@ -5132,13 +5135,13 @@ var ModelRouter = class {
|
|
|
5132
5135
|
}
|
|
5133
5136
|
logEndpoints() {
|
|
5134
5137
|
runWithRuntime(this.runtime, () => {
|
|
5135
|
-
(0,
|
|
5136
|
-
logger.info(`${(0,
|
|
5138
|
+
(0, import_utils25.forEach)(this.router.endpoints, ({ method, path }) => {
|
|
5139
|
+
logger.info(`${(0, import_utils25.padEnd)(method, 6)} ${(0, import_utils25.normalizeUrlPath)(this.options.parentPath + path)}`);
|
|
5137
5140
|
});
|
|
5138
5141
|
});
|
|
5139
5142
|
}
|
|
5140
5143
|
set(keyOrOptions, value) {
|
|
5141
|
-
if (arguments.length === 2 && (0,
|
|
5144
|
+
if (arguments.length === 2 && (0, import_utils25.isString)(keyOrOptions)) {
|
|
5142
5145
|
assertMutableRouterOption("model", keyOrOptions);
|
|
5143
5146
|
this.runtime.setModelOption(
|
|
5144
5147
|
this.modelName,
|
|
@@ -5146,7 +5149,7 @@ var ModelRouter = class {
|
|
|
5146
5149
|
value
|
|
5147
5150
|
);
|
|
5148
5151
|
}
|
|
5149
|
-
if (arguments.length === 1 && (0,
|
|
5152
|
+
if (arguments.length === 1 && (0, import_utils25.isPlainObject)(keyOrOptions)) {
|
|
5150
5153
|
assertMutableRouterOptions("model", keyOrOptions);
|
|
5151
5154
|
this.runtime.setModelOptions(this.modelName, keyOrOptions);
|
|
5152
5155
|
}
|
|
@@ -5169,7 +5172,7 @@ var ModelRouter = class {
|
|
|
5169
5172
|
|
|
5170
5173
|
// src/routers/root-router.ts
|
|
5171
5174
|
var import_express_json_router6 = __toESM(require("@web-ts-toolkit/express-json-router"));
|
|
5172
|
-
var
|
|
5175
|
+
var import_utils26 = require("@web-ts-toolkit/utils");
|
|
5173
5176
|
|
|
5174
5177
|
// src/core-data.ts
|
|
5175
5178
|
var DataCore = class {
|
|
@@ -5342,7 +5345,7 @@ var createErrorResult = (code, detail) => ({
|
|
|
5342
5345
|
code,
|
|
5343
5346
|
errors: [{ detail }]
|
|
5344
5347
|
});
|
|
5345
|
-
var normalizeSubPopulate = (populate) => Array.isArray(populate) ? populate.map((item) => (0,
|
|
5348
|
+
var normalizeSubPopulate = (populate) => Array.isArray(populate) ? populate.map((item) => (0, import_utils26.isString)(item) ? { path: item } : item) : (0, import_utils26.isString)(populate) ? { path: populate } : populate ?? [];
|
|
5346
5349
|
var RootRouter = class {
|
|
5347
5350
|
constructor(options = { basePath: "", operationAccess: true }, runtime = defaultRuntime) {
|
|
5348
5351
|
this.modelOperations = {
|
|
@@ -5416,7 +5419,7 @@ var RootRouter = class {
|
|
|
5416
5419
|
this.setRoutes();
|
|
5417
5420
|
}
|
|
5418
5421
|
registerOpenApiRoute(route) {
|
|
5419
|
-
registerOpenApiRoute(this.runtime, (0,
|
|
5422
|
+
registerOpenApiRoute(this.runtime, (0, import_utils26.normalizeUrlPath)(this.basename), "root", route);
|
|
5420
5423
|
}
|
|
5421
5424
|
wrapResult(index, item, result) {
|
|
5422
5425
|
return {
|
|
@@ -5477,7 +5480,7 @@ var RootRouter = class {
|
|
|
5477
5480
|
groupItemsByOrder(items) {
|
|
5478
5481
|
return items.reduce((acc, item, index) => {
|
|
5479
5482
|
let order = 0;
|
|
5480
|
-
if ((0,
|
|
5483
|
+
if ((0, import_utils26.isNumber)(item.order)) {
|
|
5481
5484
|
order = item.order < 0 ? 0 : item.order;
|
|
5482
5485
|
}
|
|
5483
5486
|
if (!acc[order]) {
|
|
@@ -5501,7 +5504,7 @@ var RootRouter = class {
|
|
|
5501
5504
|
const arrResult = await Promise.all(groupedItems[x].map(({ item, index }) => this.processOp(req, item, index)));
|
|
5502
5505
|
results.push(...arrResult);
|
|
5503
5506
|
}
|
|
5504
|
-
return (0,
|
|
5507
|
+
return (0, import_utils26.orderBy)(results, ["index"], ["asc"]);
|
|
5505
5508
|
});
|
|
5506
5509
|
this.registerOpenApiRoute({
|
|
5507
5510
|
method: "post",
|
|
@@ -5518,11 +5521,11 @@ var RootRouter = class {
|
|
|
5518
5521
|
|
|
5519
5522
|
// src/routers/data-router.ts
|
|
5520
5523
|
var import_express_json_router7 = __toESM(require("@web-ts-toolkit/express-json-router"));
|
|
5521
|
-
var
|
|
5524
|
+
var import_utils27 = require("@web-ts-toolkit/utils");
|
|
5522
5525
|
var clientErrors4 = import_express_json_router7.default.clientErrors;
|
|
5523
5526
|
function setOption2(parentKey, optionKey, option) {
|
|
5524
|
-
const key = (0,
|
|
5525
|
-
const value = (0,
|
|
5527
|
+
const key = (0, import_utils27.isUndefined)(option) ? parentKey : `${parentKey}.${optionKey}`;
|
|
5528
|
+
const value = (0, import_utils27.isUndefined)(option) ? optionKey : option;
|
|
5526
5529
|
assertMutableRouterOption("data", key);
|
|
5527
5530
|
this.runtime.setDataOption(this.dataName, key, value);
|
|
5528
5531
|
return this;
|
|
@@ -5558,7 +5561,7 @@ var DataRouter = class {
|
|
|
5558
5561
|
this.runtime = runtime;
|
|
5559
5562
|
this.runtime.setDataOptions(dataName, initialOptions);
|
|
5560
5563
|
this.options = this.runtime.getDataOptions(dataName);
|
|
5561
|
-
this.fullBasePath = (0,
|
|
5564
|
+
this.fullBasePath = (0, import_utils27.normalizeUrlPath)(this.options.parentPath + this.options.basePath);
|
|
5562
5565
|
this.dataName = dataName;
|
|
5563
5566
|
this.router = new import_express_json_router7.default(this.options.basePath, createSetDataCore(this.runtime), accessRouterResponseHandler);
|
|
5564
5567
|
this.setCollectionRoutes();
|
|
@@ -5591,8 +5594,8 @@ var DataRouter = class {
|
|
|
5591
5594
|
req.query
|
|
5592
5595
|
);
|
|
5593
5596
|
const svc = this.getService(req);
|
|
5594
|
-
const includeCount = (0,
|
|
5595
|
-
const includeExtraHeaders = (0,
|
|
5597
|
+
const includeCount = (0, import_utils22.parseBooleanString)(include_count);
|
|
5598
|
+
const includeExtraHeaders = (0, import_utils22.parseBooleanString)(include_extra_headers);
|
|
5596
5599
|
const result = await svc.find(
|
|
5597
5600
|
{},
|
|
5598
5601
|
{ skip, limit, page, pageSize: page_size },
|
|
@@ -5710,11 +5713,11 @@ var DataRouter = class {
|
|
|
5710
5713
|
});
|
|
5711
5714
|
}
|
|
5712
5715
|
set(keyOrOptions, value) {
|
|
5713
|
-
if (arguments.length === 2 && (0,
|
|
5716
|
+
if (arguments.length === 2 && (0, import_utils27.isString)(keyOrOptions)) {
|
|
5714
5717
|
assertMutableRouterOption("data", keyOrOptions);
|
|
5715
5718
|
this.runtime.setDataOption(this.dataName, keyOrOptions, value);
|
|
5716
5719
|
}
|
|
5717
|
-
if (arguments.length === 1 && (0,
|
|
5720
|
+
if (arguments.length === 1 && (0, import_utils27.isPlainObject)(keyOrOptions)) {
|
|
5718
5721
|
assertMutableRouterOptions("data", keyOrOptions);
|
|
5719
5722
|
this.runtime.setDataOptions(this.dataName, keyOrOptions);
|
|
5720
5723
|
}
|
|
@@ -5834,7 +5837,7 @@ function createRuntimeApi(runtime) {
|
|
|
5834
5837
|
wtt2.runtime = runtime;
|
|
5835
5838
|
wtt2.createRouter = function(modelName, options) {
|
|
5836
5839
|
const resolvedModelName = typeof modelName === "string" ? modelName : modelName && "modelName" in modelName ? modelName.modelName : modelName;
|
|
5837
|
-
return (0,
|
|
5840
|
+
return (0, import_utils28.isUndefined)(options) ? new RootRouter(modelName, runtime) : new ModelRouter(resolvedModelName, options, runtime);
|
|
5838
5841
|
};
|
|
5839
5842
|
wtt2.createDataRouter = function(dataName, options) {
|
|
5840
5843
|
return new DataRouter(dataName, options, runtime);
|
|
@@ -5844,10 +5847,10 @@ function createRuntimeApi(runtime) {
|
|
|
5844
5847
|
};
|
|
5845
5848
|
wtt2.combineRoutes = combineRoutes;
|
|
5846
5849
|
wtt2.set = function(keyOrOptions, value) {
|
|
5847
|
-
if (arguments.length === 2 && (0,
|
|
5850
|
+
if (arguments.length === 2 && (0, import_utils28.isString)(keyOrOptions)) {
|
|
5848
5851
|
return runtime.setGlobalOption(keyOrOptions, value);
|
|
5849
5852
|
}
|
|
5850
|
-
if (arguments.length === 1 && (0,
|
|
5853
|
+
if (arguments.length === 1 && (0, import_utils28.isPlainObject)(keyOrOptions)) {
|
|
5851
5854
|
return runtime.setGlobalOptions(keyOrOptions);
|
|
5852
5855
|
}
|
|
5853
5856
|
};
|