@protobase/schema 0.40.6 → 0.40.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/access/rule.d.ts +2 -2
- package/dist/access/rule.js +20 -4
- package/package.json +1 -1
package/dist/access/rule.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { AccessFn } from "./types.js";
|
|
|
2
2
|
//#region src/access/rule.d.ts
|
|
3
3
|
/** An access function that can be combined with plain functions or other rules. */
|
|
4
4
|
export type Rule<R = any> = AccessFn<R> & {
|
|
5
|
-
/** Both must allow; filters are ANDed. */
|
|
5
|
+
/** Both must allow; filters are ANDed. The other side is not run once this one denies. */
|
|
6
6
|
and: (other: AccessFn<R>) => Rule<R>;
|
|
7
|
-
/** Either may allow; filters are ORed. */
|
|
7
|
+
/** Either may allow; filters are ORed. The other side is not run once this one allows. */
|
|
8
8
|
or: (other: AccessFn<R>) => Rule<R>;
|
|
9
9
|
};
|
|
10
10
|
/**
|
package/dist/access/rule.js
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
import { andResults, orResults } from "./combine.js";
|
|
1
|
+
import { andResults, normalize, orResults } from "./combine.js";
|
|
2
2
|
//#region src/access/rule.ts
|
|
3
|
-
const
|
|
3
|
+
const settle = async (fn, ...args) => {
|
|
4
|
+
try {
|
|
5
|
+
return { result: normalize(await fn(...args)) };
|
|
6
|
+
} catch (error) {
|
|
7
|
+
if (error instanceof TypeError) return { error };
|
|
8
|
+
throw error;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const combine = (join, decided, left, right) => async (...args) => {
|
|
12
|
+
const a = await settle(left, ...args);
|
|
13
|
+
if (a.result === decided) return decided;
|
|
14
|
+
const b = await settle(right, ...args);
|
|
15
|
+
if (b.result === decided) return decided;
|
|
16
|
+
if (a.error) throw a.error;
|
|
17
|
+
if (b.error) throw b.error;
|
|
18
|
+
return join(a.result, b.result);
|
|
19
|
+
};
|
|
4
20
|
/**
|
|
5
21
|
* Wraps an access function so it combines with others through `.and()` and `.or()`; `roles.can()` and `roles.is()`
|
|
6
22
|
* return such rules already.
|
|
@@ -12,8 +28,8 @@ const combine = (join, left, right) => async (ctx, record, input) => join(await
|
|
|
12
28
|
* ```
|
|
13
29
|
*/
|
|
14
30
|
const rule = (fn) => Object.assign((ctx, record, input) => fn(ctx, record, input), {
|
|
15
|
-
and: (other) => rule(combine(andResults, fn, other)),
|
|
16
|
-
or: (other) => rule(combine(orResults, fn, other))
|
|
31
|
+
and: (other) => rule(combine(andResults, false, fn, other)),
|
|
32
|
+
or: (other) => rule(combine(orResults, true, fn, other))
|
|
17
33
|
});
|
|
18
34
|
//#endregion
|
|
19
35
|
export { rule };
|