semola 0.5.4 → 0.6.1
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/README.md +35 -50
- package/dist/lib/api/index.cjs +1 -523
- package/dist/lib/api/index.d.cts +1 -271
- package/dist/lib/api/index.d.mts +80 -84
- package/dist/lib/api/index.mjs +1 -521
- package/dist/lib/cache/index.cjs +1 -74
- package/dist/lib/cache/index.d.cts +1 -48
- package/dist/lib/cache/index.d.mts +3 -24
- package/dist/lib/cache/index.mjs +1 -73
- package/dist/lib/cron/index.cjs +1 -735
- package/dist/lib/cron/index.d.cts +1 -146
- package/dist/lib/cron/index.d.mts +99 -36
- package/dist/lib/cron/index.mjs +1 -726
- package/dist/lib/errors/index.cjs +1 -30
- package/dist/lib/errors/index.d.cts +1 -2
- package/dist/lib/errors/index.d.mts +12 -1
- package/dist/lib/errors/index.mjs +1 -26
- package/dist/lib/i18n/index.cjs +1 -42
- package/dist/lib/i18n/index.d.cts +1 -28
- package/dist/lib/i18n/index.mjs +1 -41
- package/dist/lib/logging/index.cjs +1 -387
- package/dist/lib/logging/index.d.cts +1 -108
- package/dist/lib/logging/index.mjs +1 -374
- package/dist/lib/orm/index.cjs +1 -0
- package/dist/lib/orm/index.d.cts +1 -0
- package/dist/lib/orm/index.d.mts +404 -0
- package/dist/lib/orm/index.mjs +1 -0
- package/dist/lib/policy/index.cjs +1 -285
- package/dist/lib/policy/index.d.cts +1 -77
- package/dist/lib/policy/index.mjs +1 -265
- package/dist/lib/prompts/index.cjs +6 -769
- package/dist/lib/prompts/index.d.cts +1 -96
- package/dist/lib/prompts/index.d.mts +12 -33
- package/dist/lib/prompts/index.mjs +6 -763
- package/dist/lib/pubsub/index.cjs +1 -117
- package/dist/lib/pubsub/index.d.cts +1 -41
- package/dist/lib/pubsub/index.d.mts +3 -18
- package/dist/lib/pubsub/index.mjs +1 -116
- package/dist/lib/queue/index.cjs +1 -182
- package/dist/lib/queue/index.d.cts +1 -74
- package/dist/lib/queue/index.d.mts +11 -4
- package/dist/lib/queue/index.mjs +1 -181
- package/dist/lib/workflow/index.cjs +1 -534
- package/dist/lib/workflow/index.d.cts +1 -85
- package/dist/lib/workflow/index.d.mts +76 -11
- package/dist/lib/workflow/index.mjs +1 -533
- package/dist/rolldown-runtime-CMqjfN_6.cjs +1 -0
- package/package.json +17 -5
- package/dist/index-BhGNDjPq.d.mts +0 -13
- package/dist/index-DxSbeGP-.d.cts +0 -13
|
@@ -1,285 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,
|
|
2
|
-
//#region src/lib/policy/helpers.ts
|
|
3
|
-
const _brand = Symbol("conditionHelper");
|
|
4
|
-
const eq = (value) => {
|
|
5
|
-
const fn = (actual) => actual === value;
|
|
6
|
-
return {
|
|
7
|
-
[_brand]: fn,
|
|
8
|
-
operator: "eq",
|
|
9
|
-
value,
|
|
10
|
-
fn
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
const neq = (value) => {
|
|
14
|
-
const fn = (actual) => actual !== value;
|
|
15
|
-
return {
|
|
16
|
-
[_brand]: fn,
|
|
17
|
-
operator: "neq",
|
|
18
|
-
value,
|
|
19
|
-
fn
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
const gt = (value) => {
|
|
23
|
-
const fn = (actual) => actual > value;
|
|
24
|
-
return {
|
|
25
|
-
[_brand]: fn,
|
|
26
|
-
operator: "gt",
|
|
27
|
-
value,
|
|
28
|
-
fn
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
const gte = (value) => {
|
|
32
|
-
const fn = (actual) => actual >= value;
|
|
33
|
-
return {
|
|
34
|
-
[_brand]: fn,
|
|
35
|
-
operator: "gte",
|
|
36
|
-
value,
|
|
37
|
-
fn
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
const lt = (value) => {
|
|
41
|
-
const fn = (actual) => actual < value;
|
|
42
|
-
return {
|
|
43
|
-
[_brand]: fn,
|
|
44
|
-
operator: "lt",
|
|
45
|
-
value,
|
|
46
|
-
fn
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
const lte = (value) => {
|
|
50
|
-
const fn = (actual) => actual <= value;
|
|
51
|
-
return {
|
|
52
|
-
[_brand]: fn,
|
|
53
|
-
operator: "lte",
|
|
54
|
-
value,
|
|
55
|
-
fn
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
const not = (inner) => {
|
|
59
|
-
const fn = (actual) => !inner.fn(actual);
|
|
60
|
-
return {
|
|
61
|
-
[_brand]: fn,
|
|
62
|
-
operator: "not",
|
|
63
|
-
value: inner,
|
|
64
|
-
fn
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
const and = (...helpers) => {
|
|
68
|
-
const fn = (actual) => helpers.every((h) => h.fn(actual));
|
|
69
|
-
return {
|
|
70
|
-
[_brand]: fn,
|
|
71
|
-
operator: "and",
|
|
72
|
-
value: helpers,
|
|
73
|
-
fn
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
const or = (...helpers) => {
|
|
77
|
-
const fn = (actual) => helpers.some((h) => h.fn(actual));
|
|
78
|
-
return {
|
|
79
|
-
[_brand]: fn,
|
|
80
|
-
operator: "or",
|
|
81
|
-
value: helpers,
|
|
82
|
-
fn
|
|
83
|
-
};
|
|
84
|
-
};
|
|
85
|
-
const startsWith = (prefix) => {
|
|
86
|
-
const fn = (actual) => actual.startsWith(prefix);
|
|
87
|
-
return {
|
|
88
|
-
[_brand]: fn,
|
|
89
|
-
operator: "startsWith",
|
|
90
|
-
value: prefix,
|
|
91
|
-
fn
|
|
92
|
-
};
|
|
93
|
-
};
|
|
94
|
-
const endsWith = (suffix) => {
|
|
95
|
-
const fn = (actual) => actual.endsWith(suffix);
|
|
96
|
-
return {
|
|
97
|
-
[_brand]: fn,
|
|
98
|
-
operator: "endsWith",
|
|
99
|
-
value: suffix,
|
|
100
|
-
fn
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
|
-
const includes = (substring) => {
|
|
104
|
-
const fn = (actual) => actual.includes(substring);
|
|
105
|
-
return {
|
|
106
|
-
[_brand]: fn,
|
|
107
|
-
operator: "includes",
|
|
108
|
-
value: substring,
|
|
109
|
-
fn
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
const matches = (pattern) => {
|
|
113
|
-
const fn = (actual) => {
|
|
114
|
-
pattern.lastIndex = 0;
|
|
115
|
-
return pattern.test(actual);
|
|
116
|
-
};
|
|
117
|
-
return {
|
|
118
|
-
[_brand]: fn,
|
|
119
|
-
operator: "matches",
|
|
120
|
-
value: pattern,
|
|
121
|
-
fn
|
|
122
|
-
};
|
|
123
|
-
};
|
|
124
|
-
const has = (items) => {
|
|
125
|
-
const fn = (actual) => {
|
|
126
|
-
if (Array.isArray(items)) return items.every((item) => actual.includes(item));
|
|
127
|
-
return actual.includes(items);
|
|
128
|
-
};
|
|
129
|
-
return {
|
|
130
|
-
[_brand]: fn,
|
|
131
|
-
operator: "has",
|
|
132
|
-
value: items,
|
|
133
|
-
fn
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
const hasAny = (items) => {
|
|
137
|
-
const normalized = Array.isArray(items) ? items : [items];
|
|
138
|
-
const fn = (actual) => normalized.some((item) => actual.includes(item));
|
|
139
|
-
return {
|
|
140
|
-
[_brand]: fn,
|
|
141
|
-
operator: "hasAny",
|
|
142
|
-
value: items,
|
|
143
|
-
fn
|
|
144
|
-
};
|
|
145
|
-
};
|
|
146
|
-
const hasLength = (length) => {
|
|
147
|
-
const fn = (actual) => {
|
|
148
|
-
const len = actual.length;
|
|
149
|
-
if (typeof length === "number") return len === length;
|
|
150
|
-
if (length.min !== void 0 && len < length.min) return false;
|
|
151
|
-
if (length.max !== void 0 && len > length.max) return false;
|
|
152
|
-
return true;
|
|
153
|
-
};
|
|
154
|
-
return {
|
|
155
|
-
[_brand]: fn,
|
|
156
|
-
operator: "hasLength",
|
|
157
|
-
value: length,
|
|
158
|
-
fn
|
|
159
|
-
};
|
|
160
|
-
};
|
|
161
|
-
const isEmpty = () => {
|
|
162
|
-
const fn = (actual) => actual.length === 0;
|
|
163
|
-
return {
|
|
164
|
-
[_brand]: fn,
|
|
165
|
-
operator: "isEmpty",
|
|
166
|
-
value: void 0,
|
|
167
|
-
fn
|
|
168
|
-
};
|
|
169
|
-
};
|
|
170
|
-
const isDefined = () => {
|
|
171
|
-
const fn = (actual) => actual !== null && actual !== void 0;
|
|
172
|
-
return {
|
|
173
|
-
[_brand]: fn,
|
|
174
|
-
operator: "isDefined",
|
|
175
|
-
value: void 0,
|
|
176
|
-
fn
|
|
177
|
-
};
|
|
178
|
-
};
|
|
179
|
-
const isNullish = () => {
|
|
180
|
-
const fn = (actual) => actual === null || actual === void 0;
|
|
181
|
-
return {
|
|
182
|
-
[_brand]: fn,
|
|
183
|
-
operator: "isNullish",
|
|
184
|
-
value: void 0,
|
|
185
|
-
fn
|
|
186
|
-
};
|
|
187
|
-
};
|
|
188
|
-
//#endregion
|
|
189
|
-
//#region src/lib/policy/index.ts
|
|
190
|
-
var Policy = class {
|
|
191
|
-
rules = [];
|
|
192
|
-
allow(params) {
|
|
193
|
-
const actions = this.toActions(params.action);
|
|
194
|
-
for (const action of actions) this.rules.push({
|
|
195
|
-
action,
|
|
196
|
-
conditions: params.conditions,
|
|
197
|
-
inverted: false,
|
|
198
|
-
reason: params.reason
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
forbid(params) {
|
|
202
|
-
const actions = this.toActions(params.action);
|
|
203
|
-
for (const action of actions) this.rules.push({
|
|
204
|
-
action,
|
|
205
|
-
conditions: params.conditions,
|
|
206
|
-
inverted: true,
|
|
207
|
-
reason: params.reason
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
toActions(action) {
|
|
211
|
-
if (Array.isArray(action)) return action;
|
|
212
|
-
return [action];
|
|
213
|
-
}
|
|
214
|
-
can(action, object) {
|
|
215
|
-
const filteredRules = this.rules.filter((rule) => rule.action === action);
|
|
216
|
-
const forbidResult = this.checkForbids(filteredRules, object);
|
|
217
|
-
if (forbidResult) return forbidResult;
|
|
218
|
-
const allowResult = this.checkAllows(filteredRules, object);
|
|
219
|
-
if (allowResult) return allowResult;
|
|
220
|
-
return {
|
|
221
|
-
allowed: false,
|
|
222
|
-
reason: void 0
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
checkForbids(rules, object) {
|
|
226
|
-
for (const rule of rules) {
|
|
227
|
-
if (!rule.inverted) continue;
|
|
228
|
-
if (!rule.conditions || Object.entries(rule.conditions).length === 0) return {
|
|
229
|
-
allowed: false,
|
|
230
|
-
reason: rule.reason
|
|
231
|
-
};
|
|
232
|
-
if (object && this.deepMatch(object, rule.conditions)) return {
|
|
233
|
-
allowed: false,
|
|
234
|
-
reason: rule.reason
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
checkAllows(rules, object) {
|
|
239
|
-
for (const rule of rules) {
|
|
240
|
-
if (rule.inverted) continue;
|
|
241
|
-
if (!rule.conditions || Object.entries(rule.conditions).length === 0) return {
|
|
242
|
-
allowed: true,
|
|
243
|
-
reason: rule.reason
|
|
244
|
-
};
|
|
245
|
-
if (object && this.deepMatch(object, rule.conditions)) return {
|
|
246
|
-
allowed: true,
|
|
247
|
-
reason: rule.reason
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
deepMatch(objectValue, conditionValue) {
|
|
252
|
-
if (typeof objectValue !== "object" || objectValue === null) return false;
|
|
253
|
-
for (const [key, nestedCondition] of Object.entries(conditionValue)) {
|
|
254
|
-
const nestedValue = Reflect.get(objectValue, key);
|
|
255
|
-
if (!this.matchValue(nestedValue, nestedCondition)) return false;
|
|
256
|
-
}
|
|
257
|
-
return true;
|
|
258
|
-
}
|
|
259
|
-
matchValue(actual, condition) {
|
|
260
|
-
if (typeof condition !== "object" || condition === null) return false;
|
|
261
|
-
if ("fn" in condition && typeof condition.fn === "function") return condition.fn(actual);
|
|
262
|
-
return this.deepMatch(actual, condition);
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
//#endregion
|
|
266
|
-
exports.Policy = Policy;
|
|
267
|
-
exports.and = and;
|
|
268
|
-
exports.endsWith = endsWith;
|
|
269
|
-
exports.eq = eq;
|
|
270
|
-
exports.gt = gt;
|
|
271
|
-
exports.gte = gte;
|
|
272
|
-
exports.has = has;
|
|
273
|
-
exports.hasAny = hasAny;
|
|
274
|
-
exports.hasLength = hasLength;
|
|
275
|
-
exports.includes = includes;
|
|
276
|
-
exports.isDefined = isDefined;
|
|
277
|
-
exports.isEmpty = isEmpty;
|
|
278
|
-
exports.isNullish = isNullish;
|
|
279
|
-
exports.lt = lt;
|
|
280
|
-
exports.lte = lte;
|
|
281
|
-
exports.matches = matches;
|
|
282
|
-
exports.neq = neq;
|
|
283
|
-
exports.not = not;
|
|
284
|
-
exports.or = or;
|
|
285
|
-
exports.startsWith = startsWith;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=Symbol(`conditionHelper`),t=t=>{let n=e=>e===t;return{[e]:n,operator:`eq`,value:t,fn:n}},n=t=>{let n=e=>e!==t;return{[e]:n,operator:`neq`,value:t,fn:n}},r=t=>{let n=e=>e>t;return{[e]:n,operator:`gt`,value:t,fn:n}},i=t=>{let n=e=>e>=t;return{[e]:n,operator:`gte`,value:t,fn:n}},a=t=>{let n=e=>e<t;return{[e]:n,operator:`lt`,value:t,fn:n}},o=t=>{let n=e=>e<=t;return{[e]:n,operator:`lte`,value:t,fn:n}},s=t=>{let n=e=>!t.fn(e);return{[e]:n,operator:`not`,value:t,fn:n}},c=(...t)=>{let n=e=>t.every(t=>t.fn(e));return{[e]:n,operator:`and`,value:t,fn:n}},l=(...t)=>{let n=e=>t.some(t=>t.fn(e));return{[e]:n,operator:`or`,value:t,fn:n}},u=t=>{let n=e=>e.startsWith(t);return{[e]:n,operator:`startsWith`,value:t,fn:n}},d=t=>{let n=e=>e.endsWith(t);return{[e]:n,operator:`endsWith`,value:t,fn:n}},f=t=>{let n=e=>e.includes(t);return{[e]:n,operator:`includes`,value:t,fn:n}},p=t=>{let n=e=>(t.lastIndex=0,t.test(e));return{[e]:n,operator:`matches`,value:t,fn:n}},m=t=>{let n=e=>Array.isArray(t)?t.every(t=>e.includes(t)):e.includes(t);return{[e]:n,operator:`has`,value:t,fn:n}},h=t=>{let n=Array.isArray(t)?t:[t],r=e=>n.some(t=>e.includes(t));return{[e]:r,operator:`hasAny`,value:t,fn:r}},g=t=>{let n=e=>{let n=e.length;return typeof t==`number`?n===t:!(t.min!==void 0&&n<t.min||t.max!==void 0&&n>t.max)};return{[e]:n,operator:`hasLength`,value:t,fn:n}},_=()=>{let t=e=>e.length===0;return{[e]:t,operator:`isEmpty`,value:void 0,fn:t}},v=()=>{let t=e=>e!=null;return{[e]:t,operator:`isDefined`,value:void 0,fn:t}},y=()=>{let t=e=>e==null;return{[e]:t,operator:`isNullish`,value:void 0,fn:t}};var b=class{rules=[];allow(e){let t=this.toActions(e.action);for(let n of t)this.rules.push({action:n,conditions:e.conditions,inverted:!1,reason:e.reason})}forbid(e){let t=this.toActions(e.action);for(let n of t)this.rules.push({action:n,conditions:e.conditions,inverted:!0,reason:e.reason})}toActions(e){return Array.isArray(e)?e:[e]}can(e,t){let n=this.rules.filter(t=>t.action===e);return this.checkForbids(n,t)||this.checkAllows(n,t)||{allowed:!1,reason:void 0}}checkForbids(e,t){for(let n of e)if(n.inverted&&(!n.conditions||Object.entries(n.conditions).length===0||t&&this.deepMatch(t,n.conditions)))return{allowed:!1,reason:n.reason}}checkAllows(e,t){for(let n of e)if(!n.inverted&&(!n.conditions||Object.entries(n.conditions).length===0||t&&this.deepMatch(t,n.conditions)))return{allowed:!0,reason:n.reason}}deepMatch(e,t){if(typeof e!=`object`||!e)return!1;for(let[n,r]of Object.entries(t)){let t=Reflect.get(e,n);if(!this.matchValue(t,r))return!1}return!0}matchValue(e,t){return typeof t!=`object`||!t?!1:`fn`in t&&typeof t.fn==`function`?t.fn(e):this.deepMatch(e,t)}};exports.Policy=b,exports.and=c,exports.endsWith=d,exports.eq=t,exports.gt=r,exports.gte=i,exports.has=m,exports.hasAny=h,exports.hasLength=g,exports.includes=f,exports.isDefined=v,exports.isEmpty=_,exports.isNullish=y,exports.lt=a,exports.lte=o,exports.matches=p,exports.neq=n,exports.not=s,exports.or=l,exports.startsWith=u;
|
|
@@ -1,77 +1 @@
|
|
|
1
|
-
|
|
2
|
-
declare const _brand: unique symbol;
|
|
3
|
-
type ConditionHelper<V> = {
|
|
4
|
-
readonly [_brand]: (actual: V) => boolean;
|
|
5
|
-
operator: string;
|
|
6
|
-
value: unknown;
|
|
7
|
-
fn: (actual: V) => boolean;
|
|
8
|
-
};
|
|
9
|
-
declare const eq: <V>(value: V) => ConditionHelper<V>;
|
|
10
|
-
declare const neq: <V>(value: V) => ConditionHelper<V>;
|
|
11
|
-
declare const gt: {
|
|
12
|
-
(value: Date): ConditionHelper<Date>;
|
|
13
|
-
(value: number): ConditionHelper<number>;
|
|
14
|
-
(value: string): ConditionHelper<string>;
|
|
15
|
-
};
|
|
16
|
-
declare const gte: {
|
|
17
|
-
(value: Date): ConditionHelper<Date>;
|
|
18
|
-
(value: number): ConditionHelper<number>;
|
|
19
|
-
(value: string): ConditionHelper<string>;
|
|
20
|
-
};
|
|
21
|
-
declare const lt: {
|
|
22
|
-
(value: Date): ConditionHelper<Date>;
|
|
23
|
-
(value: number): ConditionHelper<number>;
|
|
24
|
-
(value: string): ConditionHelper<string>;
|
|
25
|
-
};
|
|
26
|
-
declare const lte: {
|
|
27
|
-
(value: Date): ConditionHelper<Date>;
|
|
28
|
-
(value: number): ConditionHelper<number>;
|
|
29
|
-
(value: string): ConditionHelper<string>;
|
|
30
|
-
};
|
|
31
|
-
declare const not: <V>(inner: ConditionHelper<V>) => ConditionHelper<V>;
|
|
32
|
-
declare const and: <V>(...helpers: ConditionHelper<V>[]) => ConditionHelper<V>;
|
|
33
|
-
declare const or: <V>(...helpers: ConditionHelper<V>[]) => ConditionHelper<V>;
|
|
34
|
-
declare const startsWith: (prefix: string) => ConditionHelper<string>;
|
|
35
|
-
declare const endsWith: (suffix: string) => ConditionHelper<string>;
|
|
36
|
-
declare const includes: (substring: string) => ConditionHelper<string>;
|
|
37
|
-
declare const matches: (pattern: RegExp) => ConditionHelper<string>;
|
|
38
|
-
declare const has: <V>(items: V | V[]) => ConditionHelper<V[]>;
|
|
39
|
-
declare const hasAny: <V>(items: V | V[]) => ConditionHelper<V[]>;
|
|
40
|
-
type HasLengthArg = number | {
|
|
41
|
-
min?: number;
|
|
42
|
-
max?: number;
|
|
43
|
-
};
|
|
44
|
-
declare const hasLength: (length: HasLengthArg) => ConditionHelper<string | unknown[]>;
|
|
45
|
-
declare const isEmpty: () => ConditionHelper<string | unknown[]>;
|
|
46
|
-
declare const isDefined: () => ConditionHelper<unknown>;
|
|
47
|
-
declare const isNullish: () => ConditionHelper<unknown>;
|
|
48
|
-
//#endregion
|
|
49
|
-
//#region src/lib/policy/types.d.ts
|
|
50
|
-
type Action = "read" | "create" | "update" | "delete" | (string & {});
|
|
51
|
-
type ConditionValue<V> = V extends Record<string, unknown> ? ConditionHelper<V> | Conditions<V> : ConditionHelper<V>;
|
|
52
|
-
type Conditions<T = Record<string, unknown>> = { [K in keyof T]?: ConditionValue<T[K]> };
|
|
53
|
-
type PolicyRuleParams<T = Record<string, unknown>> = {
|
|
54
|
-
action: Action | Action[];
|
|
55
|
-
conditions?: Conditions<T>;
|
|
56
|
-
reason?: string;
|
|
57
|
-
};
|
|
58
|
-
type AllowParams<T = Record<string, unknown>> = PolicyRuleParams<T>;
|
|
59
|
-
type ForbidParams<T = Record<string, unknown>> = PolicyRuleParams<T>;
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/lib/policy/index.d.ts
|
|
62
|
-
declare class Policy<TEntity extends Record<string, unknown> = Record<string, unknown>> {
|
|
63
|
-
private rules;
|
|
64
|
-
allow(params: AllowParams<TEntity>): void;
|
|
65
|
-
forbid(params: ForbidParams<TEntity>): void;
|
|
66
|
-
private toActions;
|
|
67
|
-
can(action: Action, object?: TEntity): {
|
|
68
|
-
allowed: boolean;
|
|
69
|
-
reason: string | undefined;
|
|
70
|
-
};
|
|
71
|
-
private checkForbids;
|
|
72
|
-
private checkAllows;
|
|
73
|
-
private deepMatch;
|
|
74
|
-
private matchValue;
|
|
75
|
-
}
|
|
76
|
-
//#endregion
|
|
77
|
-
export { type ConditionHelper, Policy, and, endsWith, eq, gt, gte, has, hasAny, hasLength, includes, isDefined, isEmpty, isNullish, lt, lte, matches, neq, not, or, startsWith };
|
|
1
|
+
export type * from './index.d.mts'
|
|
@@ -1,265 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const _brand = Symbol("conditionHelper");
|
|
3
|
-
const eq = (value) => {
|
|
4
|
-
const fn = (actual) => actual === value;
|
|
5
|
-
return {
|
|
6
|
-
[_brand]: fn,
|
|
7
|
-
operator: "eq",
|
|
8
|
-
value,
|
|
9
|
-
fn
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
const neq = (value) => {
|
|
13
|
-
const fn = (actual) => actual !== value;
|
|
14
|
-
return {
|
|
15
|
-
[_brand]: fn,
|
|
16
|
-
operator: "neq",
|
|
17
|
-
value,
|
|
18
|
-
fn
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
const gt = (value) => {
|
|
22
|
-
const fn = (actual) => actual > value;
|
|
23
|
-
return {
|
|
24
|
-
[_brand]: fn,
|
|
25
|
-
operator: "gt",
|
|
26
|
-
value,
|
|
27
|
-
fn
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
const gte = (value) => {
|
|
31
|
-
const fn = (actual) => actual >= value;
|
|
32
|
-
return {
|
|
33
|
-
[_brand]: fn,
|
|
34
|
-
operator: "gte",
|
|
35
|
-
value,
|
|
36
|
-
fn
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
const lt = (value) => {
|
|
40
|
-
const fn = (actual) => actual < value;
|
|
41
|
-
return {
|
|
42
|
-
[_brand]: fn,
|
|
43
|
-
operator: "lt",
|
|
44
|
-
value,
|
|
45
|
-
fn
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
const lte = (value) => {
|
|
49
|
-
const fn = (actual) => actual <= value;
|
|
50
|
-
return {
|
|
51
|
-
[_brand]: fn,
|
|
52
|
-
operator: "lte",
|
|
53
|
-
value,
|
|
54
|
-
fn
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
const not = (inner) => {
|
|
58
|
-
const fn = (actual) => !inner.fn(actual);
|
|
59
|
-
return {
|
|
60
|
-
[_brand]: fn,
|
|
61
|
-
operator: "not",
|
|
62
|
-
value: inner,
|
|
63
|
-
fn
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
const and = (...helpers) => {
|
|
67
|
-
const fn = (actual) => helpers.every((h) => h.fn(actual));
|
|
68
|
-
return {
|
|
69
|
-
[_brand]: fn,
|
|
70
|
-
operator: "and",
|
|
71
|
-
value: helpers,
|
|
72
|
-
fn
|
|
73
|
-
};
|
|
74
|
-
};
|
|
75
|
-
const or = (...helpers) => {
|
|
76
|
-
const fn = (actual) => helpers.some((h) => h.fn(actual));
|
|
77
|
-
return {
|
|
78
|
-
[_brand]: fn,
|
|
79
|
-
operator: "or",
|
|
80
|
-
value: helpers,
|
|
81
|
-
fn
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
const startsWith = (prefix) => {
|
|
85
|
-
const fn = (actual) => actual.startsWith(prefix);
|
|
86
|
-
return {
|
|
87
|
-
[_brand]: fn,
|
|
88
|
-
operator: "startsWith",
|
|
89
|
-
value: prefix,
|
|
90
|
-
fn
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
const endsWith = (suffix) => {
|
|
94
|
-
const fn = (actual) => actual.endsWith(suffix);
|
|
95
|
-
return {
|
|
96
|
-
[_brand]: fn,
|
|
97
|
-
operator: "endsWith",
|
|
98
|
-
value: suffix,
|
|
99
|
-
fn
|
|
100
|
-
};
|
|
101
|
-
};
|
|
102
|
-
const includes = (substring) => {
|
|
103
|
-
const fn = (actual) => actual.includes(substring);
|
|
104
|
-
return {
|
|
105
|
-
[_brand]: fn,
|
|
106
|
-
operator: "includes",
|
|
107
|
-
value: substring,
|
|
108
|
-
fn
|
|
109
|
-
};
|
|
110
|
-
};
|
|
111
|
-
const matches = (pattern) => {
|
|
112
|
-
const fn = (actual) => {
|
|
113
|
-
pattern.lastIndex = 0;
|
|
114
|
-
return pattern.test(actual);
|
|
115
|
-
};
|
|
116
|
-
return {
|
|
117
|
-
[_brand]: fn,
|
|
118
|
-
operator: "matches",
|
|
119
|
-
value: pattern,
|
|
120
|
-
fn
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
const has = (items) => {
|
|
124
|
-
const fn = (actual) => {
|
|
125
|
-
if (Array.isArray(items)) return items.every((item) => actual.includes(item));
|
|
126
|
-
return actual.includes(items);
|
|
127
|
-
};
|
|
128
|
-
return {
|
|
129
|
-
[_brand]: fn,
|
|
130
|
-
operator: "has",
|
|
131
|
-
value: items,
|
|
132
|
-
fn
|
|
133
|
-
};
|
|
134
|
-
};
|
|
135
|
-
const hasAny = (items) => {
|
|
136
|
-
const normalized = Array.isArray(items) ? items : [items];
|
|
137
|
-
const fn = (actual) => normalized.some((item) => actual.includes(item));
|
|
138
|
-
return {
|
|
139
|
-
[_brand]: fn,
|
|
140
|
-
operator: "hasAny",
|
|
141
|
-
value: items,
|
|
142
|
-
fn
|
|
143
|
-
};
|
|
144
|
-
};
|
|
145
|
-
const hasLength = (length) => {
|
|
146
|
-
const fn = (actual) => {
|
|
147
|
-
const len = actual.length;
|
|
148
|
-
if (typeof length === "number") return len === length;
|
|
149
|
-
if (length.min !== void 0 && len < length.min) return false;
|
|
150
|
-
if (length.max !== void 0 && len > length.max) return false;
|
|
151
|
-
return true;
|
|
152
|
-
};
|
|
153
|
-
return {
|
|
154
|
-
[_brand]: fn,
|
|
155
|
-
operator: "hasLength",
|
|
156
|
-
value: length,
|
|
157
|
-
fn
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
const isEmpty = () => {
|
|
161
|
-
const fn = (actual) => actual.length === 0;
|
|
162
|
-
return {
|
|
163
|
-
[_brand]: fn,
|
|
164
|
-
operator: "isEmpty",
|
|
165
|
-
value: void 0,
|
|
166
|
-
fn
|
|
167
|
-
};
|
|
168
|
-
};
|
|
169
|
-
const isDefined = () => {
|
|
170
|
-
const fn = (actual) => actual !== null && actual !== void 0;
|
|
171
|
-
return {
|
|
172
|
-
[_brand]: fn,
|
|
173
|
-
operator: "isDefined",
|
|
174
|
-
value: void 0,
|
|
175
|
-
fn
|
|
176
|
-
};
|
|
177
|
-
};
|
|
178
|
-
const isNullish = () => {
|
|
179
|
-
const fn = (actual) => actual === null || actual === void 0;
|
|
180
|
-
return {
|
|
181
|
-
[_brand]: fn,
|
|
182
|
-
operator: "isNullish",
|
|
183
|
-
value: void 0,
|
|
184
|
-
fn
|
|
185
|
-
};
|
|
186
|
-
};
|
|
187
|
-
//#endregion
|
|
188
|
-
//#region src/lib/policy/index.ts
|
|
189
|
-
var Policy = class {
|
|
190
|
-
rules = [];
|
|
191
|
-
allow(params) {
|
|
192
|
-
const actions = this.toActions(params.action);
|
|
193
|
-
for (const action of actions) this.rules.push({
|
|
194
|
-
action,
|
|
195
|
-
conditions: params.conditions,
|
|
196
|
-
inverted: false,
|
|
197
|
-
reason: params.reason
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
forbid(params) {
|
|
201
|
-
const actions = this.toActions(params.action);
|
|
202
|
-
for (const action of actions) this.rules.push({
|
|
203
|
-
action,
|
|
204
|
-
conditions: params.conditions,
|
|
205
|
-
inverted: true,
|
|
206
|
-
reason: params.reason
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
toActions(action) {
|
|
210
|
-
if (Array.isArray(action)) return action;
|
|
211
|
-
return [action];
|
|
212
|
-
}
|
|
213
|
-
can(action, object) {
|
|
214
|
-
const filteredRules = this.rules.filter((rule) => rule.action === action);
|
|
215
|
-
const forbidResult = this.checkForbids(filteredRules, object);
|
|
216
|
-
if (forbidResult) return forbidResult;
|
|
217
|
-
const allowResult = this.checkAllows(filteredRules, object);
|
|
218
|
-
if (allowResult) return allowResult;
|
|
219
|
-
return {
|
|
220
|
-
allowed: false,
|
|
221
|
-
reason: void 0
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
checkForbids(rules, object) {
|
|
225
|
-
for (const rule of rules) {
|
|
226
|
-
if (!rule.inverted) continue;
|
|
227
|
-
if (!rule.conditions || Object.entries(rule.conditions).length === 0) return {
|
|
228
|
-
allowed: false,
|
|
229
|
-
reason: rule.reason
|
|
230
|
-
};
|
|
231
|
-
if (object && this.deepMatch(object, rule.conditions)) return {
|
|
232
|
-
allowed: false,
|
|
233
|
-
reason: rule.reason
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
checkAllows(rules, object) {
|
|
238
|
-
for (const rule of rules) {
|
|
239
|
-
if (rule.inverted) continue;
|
|
240
|
-
if (!rule.conditions || Object.entries(rule.conditions).length === 0) return {
|
|
241
|
-
allowed: true,
|
|
242
|
-
reason: rule.reason
|
|
243
|
-
};
|
|
244
|
-
if (object && this.deepMatch(object, rule.conditions)) return {
|
|
245
|
-
allowed: true,
|
|
246
|
-
reason: rule.reason
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
deepMatch(objectValue, conditionValue) {
|
|
251
|
-
if (typeof objectValue !== "object" || objectValue === null) return false;
|
|
252
|
-
for (const [key, nestedCondition] of Object.entries(conditionValue)) {
|
|
253
|
-
const nestedValue = Reflect.get(objectValue, key);
|
|
254
|
-
if (!this.matchValue(nestedValue, nestedCondition)) return false;
|
|
255
|
-
}
|
|
256
|
-
return true;
|
|
257
|
-
}
|
|
258
|
-
matchValue(actual, condition) {
|
|
259
|
-
if (typeof condition !== "object" || condition === null) return false;
|
|
260
|
-
if ("fn" in condition && typeof condition.fn === "function") return condition.fn(actual);
|
|
261
|
-
return this.deepMatch(actual, condition);
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
//#endregion
|
|
265
|
-
export { Policy, and, endsWith, eq, gt, gte, has, hasAny, hasLength, includes, isDefined, isEmpty, isNullish, lt, lte, matches, neq, not, or, startsWith };
|
|
1
|
+
const e=Symbol(`conditionHelper`),t=t=>{let n=e=>e===t;return{[e]:n,operator:`eq`,value:t,fn:n}},n=t=>{let n=e=>e!==t;return{[e]:n,operator:`neq`,value:t,fn:n}},r=t=>{let n=e=>e>t;return{[e]:n,operator:`gt`,value:t,fn:n}},i=t=>{let n=e=>e>=t;return{[e]:n,operator:`gte`,value:t,fn:n}},a=t=>{let n=e=>e<t;return{[e]:n,operator:`lt`,value:t,fn:n}},o=t=>{let n=e=>e<=t;return{[e]:n,operator:`lte`,value:t,fn:n}},s=t=>{let n=e=>!t.fn(e);return{[e]:n,operator:`not`,value:t,fn:n}},c=(...t)=>{let n=e=>t.every(t=>t.fn(e));return{[e]:n,operator:`and`,value:t,fn:n}},l=(...t)=>{let n=e=>t.some(t=>t.fn(e));return{[e]:n,operator:`or`,value:t,fn:n}},u=t=>{let n=e=>e.startsWith(t);return{[e]:n,operator:`startsWith`,value:t,fn:n}},d=t=>{let n=e=>e.endsWith(t);return{[e]:n,operator:`endsWith`,value:t,fn:n}},f=t=>{let n=e=>e.includes(t);return{[e]:n,operator:`includes`,value:t,fn:n}},p=t=>{let n=e=>(t.lastIndex=0,t.test(e));return{[e]:n,operator:`matches`,value:t,fn:n}},m=t=>{let n=e=>Array.isArray(t)?t.every(t=>e.includes(t)):e.includes(t);return{[e]:n,operator:`has`,value:t,fn:n}},h=t=>{let n=Array.isArray(t)?t:[t],r=e=>n.some(t=>e.includes(t));return{[e]:r,operator:`hasAny`,value:t,fn:r}},g=t=>{let n=e=>{let n=e.length;return typeof t==`number`?n===t:!(t.min!==void 0&&n<t.min||t.max!==void 0&&n>t.max)};return{[e]:n,operator:`hasLength`,value:t,fn:n}},_=()=>{let t=e=>e.length===0;return{[e]:t,operator:`isEmpty`,value:void 0,fn:t}},v=()=>{let t=e=>e!=null;return{[e]:t,operator:`isDefined`,value:void 0,fn:t}},y=()=>{let t=e=>e==null;return{[e]:t,operator:`isNullish`,value:void 0,fn:t}};var b=class{rules=[];allow(e){let t=this.toActions(e.action);for(let n of t)this.rules.push({action:n,conditions:e.conditions,inverted:!1,reason:e.reason})}forbid(e){let t=this.toActions(e.action);for(let n of t)this.rules.push({action:n,conditions:e.conditions,inverted:!0,reason:e.reason})}toActions(e){return Array.isArray(e)?e:[e]}can(e,t){let n=this.rules.filter(t=>t.action===e);return this.checkForbids(n,t)||this.checkAllows(n,t)||{allowed:!1,reason:void 0}}checkForbids(e,t){for(let n of e)if(n.inverted&&(!n.conditions||Object.entries(n.conditions).length===0||t&&this.deepMatch(t,n.conditions)))return{allowed:!1,reason:n.reason}}checkAllows(e,t){for(let n of e)if(!n.inverted&&(!n.conditions||Object.entries(n.conditions).length===0||t&&this.deepMatch(t,n.conditions)))return{allowed:!0,reason:n.reason}}deepMatch(e,t){if(typeof e!=`object`||!e)return!1;for(let[n,r]of Object.entries(t)){let t=Reflect.get(e,n);if(!this.matchValue(t,r))return!1}return!0}matchValue(e,t){return typeof t!=`object`||!t?!1:`fn`in t&&typeof t.fn==`function`?t.fn(e):this.deepMatch(e,t)}};export{b as Policy,c as and,d as endsWith,t as eq,r as gt,i as gte,m as has,h as hasAny,g as hasLength,f as includes,v as isDefined,_ as isEmpty,y as isNullish,a as lt,o as lte,p as matches,n as neq,s as not,l as or,u as startsWith};
|