@nmnmcc/ability 0.2.0 → 1.0.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.
Files changed (58) hide show
  1. package/README.md +462 -454
  2. package/dist/Authorizer.d.ts +241 -0
  3. package/dist/Authorizer.d.ts.map +1 -0
  4. package/dist/Authorizer.js +340 -0
  5. package/dist/Authorizer.js.map +1 -0
  6. package/dist/Condition.d.ts +915 -0
  7. package/dist/Condition.d.ts.map +1 -0
  8. package/dist/Condition.js +1123 -0
  9. package/dist/Condition.js.map +1 -0
  10. package/dist/Decision.d.ts +131 -0
  11. package/dist/Decision.d.ts.map +1 -0
  12. package/dist/Decision.js +83 -0
  13. package/dist/Decision.js.map +1 -0
  14. package/dist/Domain.d.ts +158 -0
  15. package/dist/Domain.d.ts.map +1 -0
  16. package/dist/Domain.js +76 -0
  17. package/dist/Domain.js.map +1 -0
  18. package/dist/HttpApi.d.ts +173 -0
  19. package/dist/HttpApi.d.ts.map +1 -0
  20. package/dist/HttpApi.js +185 -0
  21. package/dist/HttpApi.js.map +1 -0
  22. package/dist/Policy.d.ts +687 -0
  23. package/dist/Policy.d.ts.map +1 -0
  24. package/dist/Policy.js +1339 -0
  25. package/dist/Policy.js.map +1 -0
  26. package/dist/Principal.d.ts +35 -0
  27. package/dist/Principal.d.ts.map +1 -0
  28. package/dist/Principal.js +37 -0
  29. package/dist/Principal.js.map +1 -0
  30. package/dist/Resource.d.ts +364 -0
  31. package/dist/Resource.d.ts.map +1 -0
  32. package/dist/Resource.js +844 -0
  33. package/dist/Resource.js.map +1 -0
  34. package/dist/Rule.d.ts +275 -0
  35. package/dist/Rule.d.ts.map +1 -0
  36. package/dist/Rule.js +257 -0
  37. package/dist/Rule.js.map +1 -0
  38. package/dist/_virtual/_rolldown/runtime.js +1 -2
  39. package/dist/index.d.ts +9 -3
  40. package/dist/index.js +9 -3
  41. package/package.json +61 -72
  42. package/src/Authorizer.ts +894 -0
  43. package/src/Condition.ts +2997 -0
  44. package/src/Decision.ts +117 -0
  45. package/src/Domain.ts +246 -0
  46. package/src/HttpApi.ts +348 -0
  47. package/src/Policy.ts +3315 -0
  48. package/src/Principal.ts +41 -0
  49. package/src/Resource.ts +1805 -0
  50. package/src/Rule.ts +683 -0
  51. package/src/index.ts +57 -0
  52. package/dist/Ability.d.ts +0 -491
  53. package/dist/Ability.js +0 -207
  54. package/dist/AbilityExtra.d.ts +0 -79
  55. package/dist/AbilityExtra.js +0 -187
  56. package/dist/internal/ability.d.ts +0 -90
  57. package/dist/internal/ability.js +0 -550
  58. package/dist/internal/serializable.js +0 -22
@@ -0,0 +1,915 @@
1
+ import { PathSegment } from "./Resource.js";
2
+ import * as Context from "effect/Context";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Option from "effect/Option";
5
+ import * as Schema from "effect/Schema";
6
+ import * as Data from "effect/Data";
7
+ import * as HashMap from "effect/HashMap";
8
+ import * as HashSet from "effect/HashSet";
9
+ import * as Chunk from "effect/Chunk";
10
+ import * as Duration from "effect/Duration";
11
+ import * as BigDecimal from "effect/BigDecimal";
12
+ import * as Cause from "effect/Cause";
13
+ import * as DateTime from "effect/DateTime";
14
+ import * as Redacted from "effect/Redacted";
15
+ import * as Result from "effect/Result";
16
+ declare namespace Condition_d_exports {
17
+ export { And, AndCondition, Any, Ast, Builder, Checked, Compare, CompareOperator, Condition, Encoded, EncodedAnd, EncodedNot, EncodedOr, Error, Example, Exists, False, FieldPath, FieldSelector, FieldValue, In, Input, Interpreter, Literal, Marks, Not, NotCondition, Opaque, OpaqueOf, Operand, OperandOf, Or, OrCondition, PathValue, PlainCondition, Predicate, PredicateCondition, PredicateRun, PrincipalRef, Reads, ReadsOf, Ref, Satisfiability, SatisfiableOptions, Scalar, Services, Test, True, Under, Untranslatable, Variance, always, and, andAll, builder, evaluate, evaluateSync, fields, fold, hasPredicate, isAst, never, not, or, orAll, partial, provideContext, satisfiable, schema, simplifier, simplify, toJsonValue };
18
+ }
19
+ /**
20
+ * A value that a condition can compare.
21
+ *
22
+ * @stability stable
23
+ * @category models
24
+ * @since 0.3.0
25
+ */
26
+ export type Scalar = string | number | boolean | null;
27
+ /**
28
+ * A fixed value in a condition.
29
+ *
30
+ * @stability stable
31
+ * @category models
32
+ * @since 0.3.0
33
+ */
34
+ export interface Literal {
35
+ readonly _tag: "Literal";
36
+ readonly value: Scalar;
37
+ }
38
+ /**
39
+ * A field of the principal. {@link partial} puts the value of the field in its
40
+ * place.
41
+ *
42
+ * A principal field that is `null` or missing has no value. A `Compare`, `In`
43
+ * or `Under` test with it is always false, also with `ne`. So a principal with
44
+ * no id never matches a resource with no owner. `not` still turns false into
45
+ * true.
46
+ *
47
+ * @stability stable
48
+ * @category models
49
+ * @since 0.3.0
50
+ */
51
+ export interface PrincipalRef {
52
+ readonly _tag: "Principal";
53
+ readonly path: string;
54
+ }
55
+ /**
56
+ * A field of the principal with a known value type `A`, from a principal of
57
+ * the type `P`. The type fields are only in the types. A reference with no
58
+ * type does not have them, so it can not stand for a field of any type. A
59
+ * test with the reference reads a principal of the type `P`, so it can not go
60
+ * into a rule of a domain with another principal. With no `P`, it is `never`,
61
+ * and a test with the reference fits no rule.
62
+ *
63
+ * @stability stable
64
+ * @category models
65
+ * @since 0.3.0
66
+ */
67
+ export type Ref<A, P = never> = PrincipalRef & {
68
+ readonly "~Ref": () => A;
69
+ readonly "~Principal": (principal: P) => void;
70
+ };
71
+ /**
72
+ * @stability stable
73
+ * @category models
74
+ * @since 0.3.0
75
+ */
76
+ export type Operand = Literal | PrincipalRef;
77
+ /**
78
+ * @stability stable
79
+ * @category models
80
+ * @since 0.3.0
81
+ */
82
+ export type CompareOperator = "eq" | "ne" | "lt" | "lte" | "gt" | "gte";
83
+ /**
84
+ * Always true.
85
+ *
86
+ * @stability stable
87
+ * @category models
88
+ * @since 0.3.0
89
+ */
90
+ export interface True {
91
+ readonly _tag: "True";
92
+ }
93
+ /**
94
+ * Always false.
95
+ *
96
+ * @stability stable
97
+ * @category models
98
+ * @since 0.3.0
99
+ */
100
+ export interface False {
101
+ readonly _tag: "False";
102
+ }
103
+ /**
104
+ * Compares a field with a value. A missing field has the value `null`. `lt`,
105
+ * `lte`, `gt` and `gte` are false when the two sides are not both numbers or
106
+ * both strings.
107
+ *
108
+ * @stability stable
109
+ * @category models
110
+ * @since 0.3.0
111
+ */
112
+ export interface Compare {
113
+ readonly _tag: "Compare";
114
+ readonly op: CompareOperator;
115
+ readonly field: string;
116
+ readonly value: Operand;
117
+ }
118
+ /**
119
+ * True when a field is equal to one of the values.
120
+ *
121
+ * @stability stable
122
+ * @category models
123
+ * @since 0.3.0
124
+ */
125
+ export interface In {
126
+ readonly _tag: "In";
127
+ readonly field: string;
128
+ readonly values: ReadonlyArray<Operand>;
129
+ }
130
+ /**
131
+ * True when a field is not `null` and not missing.
132
+ *
133
+ * @stability stable
134
+ * @category models
135
+ * @since 0.3.0
136
+ */
137
+ export interface Exists {
138
+ readonly _tag: "Exists";
139
+ readonly field: string;
140
+ }
141
+ /**
142
+ * True when the path of the resource starts with the prefix. A resource with
143
+ * no path is never under a prefix.
144
+ *
145
+ * @stability stable
146
+ * @category models
147
+ * @since 0.3.0
148
+ */
149
+ export interface Under {
150
+ readonly _tag: "Under";
151
+ readonly prefix: ReadonlyArray<Operand>;
152
+ }
153
+ /**
154
+ * @stability stable
155
+ * @category models
156
+ * @since 0.3.0
157
+ */
158
+ export interface And {
159
+ readonly _tag: "And";
160
+ readonly conditions: ReadonlyArray<Ast>;
161
+ }
162
+ /**
163
+ * @stability stable
164
+ * @category models
165
+ * @since 0.3.0
166
+ */
167
+ export interface Or {
168
+ readonly _tag: "Or";
169
+ readonly conditions: ReadonlyArray<Ast>;
170
+ }
171
+ /**
172
+ * @stability stable
173
+ * @category models
174
+ * @since 0.3.0
175
+ */
176
+ export interface Not {
177
+ readonly _tag: "Not";
178
+ readonly condition: Ast;
179
+ }
180
+ /**
181
+ * A test that runs an `Effect`. It can not become JSON or a query.
182
+ *
183
+ * @stability stable
184
+ * @category models
185
+ * @since 0.3.0
186
+ */
187
+ export interface Predicate {
188
+ readonly _tag: "Predicate";
189
+ readonly name: string;
190
+ run(value: any, principal: any): Effect.Effect<boolean, unknown, unknown>;
191
+ }
192
+ /**
193
+ * The nodes of a condition, without types.
194
+ *
195
+ * @stability stable
196
+ * @category models
197
+ * @since 0.3.0
198
+ */
199
+ export type Ast = True | False | Compare | In | Exists | Under | And | Or | Not | Predicate;
200
+ /**
201
+ * The type marker for a condition with a {@link Predicate}.
202
+ *
203
+ * @stability stable
204
+ * @category models
205
+ * @since 0.3.0
206
+ */
207
+ export type Opaque = "Opaque";
208
+ /**
209
+ * @stability stable
210
+ * @category models
211
+ * @since 0.3.0
212
+ */
213
+ export interface Variance<E, R, O> {
214
+ readonly E: () => E;
215
+ readonly R: () => R;
216
+ readonly O: () => O;
217
+ }
218
+ /**
219
+ * What the predicates of a condition read: a resource value of the type `V`
220
+ * and a principal of the type `P`. It is only in the types.
221
+ *
222
+ * @stability stable
223
+ * @category models
224
+ * @since 1.0.0
225
+ */
226
+ export interface Reads<V, P> {
227
+ readonly value: V;
228
+ readonly principal: P;
229
+ }
230
+ /**
231
+ * The type fields of a condition. They are only in the types.
232
+ *
233
+ * @stability stable
234
+ * @category models
235
+ * @since 1.0.0
236
+ */
237
+ export interface Marks<E, R, O, I> {
238
+ readonly "~Condition": Variance<E, R, O>;
239
+ /** `I` is what the predicates read. See {@link Reads}. */
240
+ readonly "~Reads": (reads: I) => void;
241
+ }
242
+ /**
243
+ * An `And` node with the types of its children.
244
+ *
245
+ * @stability stable
246
+ * @category models
247
+ * @since 1.0.0
248
+ */
249
+ export interface AndCondition<E, R, O, I> extends Marks<E, R, O, I> {
250
+ readonly _tag: "And";
251
+ readonly conditions: ReadonlyArray<Condition<E, R, O, I>>;
252
+ }
253
+ /**
254
+ * An `Or` node with the types of its children.
255
+ *
256
+ * @stability stable
257
+ * @category models
258
+ * @since 1.0.0
259
+ */
260
+ export interface OrCondition<E, R, O, I> extends Marks<E, R, O, I> {
261
+ readonly _tag: "Or";
262
+ readonly conditions: ReadonlyArray<Condition<E, R, O, I>>;
263
+ }
264
+ /**
265
+ * A `Not` node with the types of its child.
266
+ *
267
+ * @stability stable
268
+ * @category models
269
+ * @since 1.0.0
270
+ */
271
+ export interface NotCondition<E, R, O, I> extends Marks<E, R, O, I> {
272
+ readonly _tag: "Not";
273
+ readonly condition: Condition<E, R, O, I>;
274
+ }
275
+ /**
276
+ * A predicate node with its types. `"~Predicate"` is only in the types. It has
277
+ * the type `O`, so a plain condition, with `O` as `never`, can not be a
278
+ * predicate. The builder makes predicates with `O` as {@link Opaque}.
279
+ *
280
+ * @stability stable
281
+ * @category models
282
+ * @since 1.0.0
283
+ */
284
+ export interface PredicateCondition<E, R, O, I> extends Marks<E, R, O, I> {
285
+ readonly _tag: "Predicate";
286
+ readonly name: string;
287
+ /**
288
+ * The test. Its inputs have the type `never`, so it can not be called with
289
+ * a value that it does not read. Use {@link evaluate}, which checks the
290
+ * input against `I`.
291
+ */
292
+ readonly run: PredicateRun<E, R, I>;
293
+ readonly "~Predicate": O;
294
+ }
295
+ /**
296
+ * The test of a predicate node. The type field `"~Reads"` is only in the
297
+ * types. It ties the test to what it reads, so the test of one predicate can
298
+ * not go into a node that says it reads other values.
299
+ *
300
+ * @stability stable
301
+ * @category models
302
+ * @since 1.0.0
303
+ */
304
+ export type PredicateRun<E, R, I> = ((value: never, principal: never) => Effect.Effect<boolean, E, R>) & {
305
+ readonly "~Reads": (reads: I) => void;
306
+ };
307
+ /**
308
+ * A condition. `E` and `R` are the error type and the service type of its
309
+ * predicates. `O` is {@link Opaque} when the condition has a predicate, and
310
+ * `never` when it is plain data. `I` is what its predicates read. See
311
+ * {@link Reads}.
312
+ *
313
+ * The type fields are only in the types. A plain node made by hand does not
314
+ * have them, so it is not a condition. Use the builder. Each child of a node
315
+ * is a condition with the same types, so a node can not hide the types of a
316
+ * predicate inside it. A predicate is a condition only when `O` is
317
+ * {@link Opaque}.
318
+ *
319
+ * @stability stable
320
+ * @category models
321
+ * @since 0.3.0
322
+ */
323
+ export type Condition<E = never, R = never, O = never, I = unknown> = ((True | False | Compare | In | Exists | Under) & Marks<E, R, O, I>) | AndCondition<E, R, O, I> | OrCondition<E, R, O, I> | NotCondition<E, R, O, I> | PredicateCondition<E, R, O, I>;
324
+ /**
325
+ * Any condition.
326
+ *
327
+ * @stability stable
328
+ * @category models
329
+ * @since 0.3.0
330
+ */
331
+ export type Any = Condition<any, any, any, any>;
332
+ /**
333
+ * The error type of a condition.
334
+ *
335
+ * @stability stable
336
+ * @category utility types
337
+ * @since 0.3.0
338
+ */
339
+ export type Error<C> = C extends {
340
+ readonly "~Condition"?: Variance<infer E, infer _R, infer _O>;
341
+ } ? E : never;
342
+ /**
343
+ * The service type of a condition.
344
+ *
345
+ * @stability stable
346
+ * @category utility types
347
+ * @since 0.3.0
348
+ */
349
+ export type Services<C> = C extends {
350
+ readonly "~Condition"?: Variance<infer _E, infer R, infer _O>;
351
+ } ? R : never;
352
+ /**
353
+ * What the predicates of a condition read. For a union of conditions, it is
354
+ * what all of them read.
355
+ *
356
+ * @stability stable
357
+ * @category utility types
358
+ * @since 1.0.0
359
+ */
360
+ export type ReadsOf<C> = [C] extends [{
361
+ readonly "~Reads": (reads: infer I) => void;
362
+ }] ? I : unknown;
363
+ /**
364
+ * `unknown` when a condition is a condition of its own type fields: each of
365
+ * its children has them too. A node made with a spread may keep the type
366
+ * fields of another node, and then it is `never`, so the call stops.
367
+ *
368
+ * @stability stable
369
+ * @category utility types
370
+ * @since 1.0.0
371
+ */
372
+ export type Checked<C> = [NotChecked<C>] extends [never] ? unknown : never;
373
+ /**
374
+ * The members of a union of conditions that do not fit their own type fields.
375
+ * Each member is checked alone, so a good member does not hide a bad one.
376
+ */
377
+ type NotChecked<C> = C extends Condition<Error<C>, Services<C>, OpaqueOf<C>, ReadsOf<C>> ? never : C;
378
+ /** Each condition, checked with {@link Checked}. */
379
+ type Consistent<Cs extends ReadonlyArray<Any>> = { readonly [K in keyof Cs]: Cs[K] & Checked<Cs[K]>; };
380
+ /**
381
+ * {@link Opaque} when a condition has a predicate.
382
+ *
383
+ * @stability stable
384
+ * @category utility types
385
+ * @since 0.3.0
386
+ */
387
+ export type OpaqueOf<C> = C extends {
388
+ readonly "~Condition"?: Variance<infer _E, infer _R, infer O>;
389
+ } ? O : never;
390
+ type Leaf = string | number | boolean | bigint | symbol | null | undefined;
391
+ /**
392
+ * Values of a class that keeps its data inside. A path does not go into them,
393
+ * as in the field paths of a resource, so a field with such a value is one
394
+ * field. A condition can test only if it exists.
395
+ */
396
+ type Whole = ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | HashMap.HashMap<unknown, unknown> | HashSet.HashSet<unknown> | Date | URL | Uint8Array | Option.Option<unknown> | BigDecimal.BigDecimal | Duration.Duration | DateTime.DateTime | Redacted.Redacted<unknown> | Chunk.Chunk<unknown> | Cause.Cause<unknown> | Result.Result<unknown, unknown> | File | FormData | RegExp;
397
+ type PreviousDepth = [never, 0, 1, 2, 3];
398
+ type FieldPathInternal<T, Depth extends number, Arrays extends boolean> = [Depth] extends [never] ? never : { [K in keyof T & string]-?: NonNullable<T[K]> extends Leaf | Whole ? K : NonNullable<T[K]> extends Function ? never : NonNullable<T[K]> extends ReadonlyArray<unknown> ? Arrays extends true ? K : never : NonNullable<T[K]> extends object ? `${K}.${FieldPathInternal<NonNullable<T[K]>, PreviousDepth[Depth], Arrays>}` : never; }[keyof T & string];
399
+ /**
400
+ * The paths of the fields of `T` that hold one value, with dots for nested
401
+ * fields. Arrays are left out. A map, a set, a date, a URL, a byte array, an
402
+ * `Option` and the other value types of Effect, such as `BigDecimal` and
403
+ * `Duration`, are one field, and a path does not go into them. The type goes down four levels, so type
404
+ * checking stays fast. For a union of types, only the common fields are
405
+ * paths.
406
+ *
407
+ * @stability stable
408
+ * @category utility types
409
+ * @since 0.3.0
410
+ */
411
+ export type FieldPath<T> = [T] extends [object] ? FieldPathInternal<T, 4, false> : never;
412
+ /**
413
+ * The paths of the fields of `T` that a rule or a request may name, as in
414
+ * `Rule.Options.fields`. They are the paths of {@link FieldPath}, and the
415
+ * paths of arrays too: a rule may allow or deny a whole array field, and
416
+ * `permittedFields` lists it.
417
+ *
418
+ * @stability stable
419
+ * @category utility types
420
+ * @since 1.0.0
421
+ */
422
+ export type FieldSelector<T> = [T] extends [object] ? FieldPathInternal<T, 4, true> : never;
423
+ /**
424
+ * The value type that fits every field in `F`. For one field, it is the type
425
+ * of the field. For a union of fields, it is the values that each of them can
426
+ * hold, so a value of a union type can not test a field of another type. For
427
+ * a union of data types, as for a rule on two kinds, it is the values that
428
+ * the field can hold in each of them.
429
+ *
430
+ * @stability stable
431
+ * @category utility types
432
+ * @since 1.0.0
433
+ */
434
+ export type FieldValue<Data, F extends string> = (Data extends unknown ? F extends unknown ? (value: PathValue<Data, F>) => void : never : never) extends ((value: infer V) => void) ? V : never;
435
+ type Normalize<A> = Exclude<A, undefined> | (undefined extends A ? null : never);
436
+ /**
437
+ * The type of the field of `T` at `Path`. A field that may be missing may also
438
+ * be `null`.
439
+ *
440
+ * @stability stable
441
+ * @category utility types
442
+ * @since 0.3.0
443
+ */
444
+ export type PathValue<T, Path extends string> = Path extends `${infer Head}.${infer Rest}` ? Head extends keyof T ? undefined extends T[Head] ? PathValue<NonNullable<T[Head]>, Rest> | null : null extends T[Head] ? PathValue<NonNullable<T[Head]>, Rest> | null : PathValue<T[Head], Rest> : never : Path extends keyof T ? Normalize<T[Path]> : never;
445
+ /**
446
+ * A value for a field of type `A`: a fixed value, or a field of the principal
447
+ * with the same type.
448
+ *
449
+ * @stability stable
450
+ * @category models
451
+ * @since 0.3.0
452
+ */
453
+ export type OperandOf<A, P = never> = Extract<A, Scalar> | Ref<A, P>;
454
+ type ScalarPath<T> = { [F in FieldPath<T>]: PathValue<T, F> extends Scalar ? F : never; }[FieldPath<T>];
455
+ type OrderedPath<T> = { [F in FieldPath<T>]: Exclude<PathValue<T, F>, null> extends number | string ? F : never; }[FieldPath<T>];
456
+ /**
457
+ * A test made by a {@link Builder}. It runs no code, but it keeps the value
458
+ * type of its resource, so a test on the fields of one resource can not go
459
+ * into a rule for another resource. A test with a field of the principal
460
+ * keeps the principal type `P` too.
461
+ *
462
+ * @stability stable
463
+ * @category models
464
+ * @since 1.0.0
465
+ */
466
+ export type Test<Value, P = unknown> = Condition<never, never, never, Reads<Value, P>>;
467
+ /**
468
+ * The principal type that a test reads: the principal of each field of the
469
+ * principal in the values `V`, and `unknown` when there is none. A reference
470
+ * from another builder keeps the principal of that builder.
471
+ */
472
+ type PrincipalOf<V, _P> = [Extract<V, PrincipalRef>] extends [never] ? unknown : [Extract<V, PrincipalRef>] extends [{
473
+ readonly "~Principal": (principal: infer Q) => void;
474
+ }] ? Q : unknown;
475
+ /**
476
+ * Makes conditions for a resource with the data type `Data` and the value type
477
+ * `Value`, and a principal with the type `P`.
478
+ *
479
+ * @stability stable
480
+ * @category models
481
+ * @since 0.3.0
482
+ */
483
+ export interface Builder<Data, Value, P> {
484
+ /** The field is equal to the value. */
485
+ readonly eq: <const F extends FieldPath<Data>, const V extends OperandOf<FieldValue<Data, F>, any>>(field: F, value: V) => Test<Value, PrincipalOf<V, P>>;
486
+ /** The field is not equal to the value. */
487
+ readonly ne: <const F extends FieldPath<Data>, const V extends OperandOf<FieldValue<Data, F>, any>>(field: F, value: V) => Test<Value, PrincipalOf<V, P>>;
488
+ readonly lt: <const F extends OrderedPath<Data>, const V extends OperandOf<Exclude<FieldValue<Data, F>, null>, any>>(field: F, value: V) => Test<Value, PrincipalOf<V, P>>;
489
+ readonly lte: <const F extends OrderedPath<Data>, const V extends OperandOf<Exclude<FieldValue<Data, F>, null>, any>>(field: F, value: V) => Test<Value, PrincipalOf<V, P>>;
490
+ readonly gt: <const F extends OrderedPath<Data>, const V extends OperandOf<Exclude<FieldValue<Data, F>, null>, any>>(field: F, value: V) => Test<Value, PrincipalOf<V, P>>;
491
+ readonly gte: <const F extends OrderedPath<Data>, const V extends OperandOf<Exclude<FieldValue<Data, F>, null>, any>>(field: F, value: V) => Test<Value, PrincipalOf<V, P>>;
492
+ /** The field is equal to one of the values. */
493
+ readonly in: <const F extends FieldPath<Data>, const Vs extends ReadonlyArray<OperandOf<FieldValue<Data, F>, any>>>(field: F, values: Vs) => Test<Value, PrincipalOf<Vs[number], P>>;
494
+ /** The field has a value that is not `null`. */
495
+ readonly exists: <const F extends FieldPath<Data>>(field: F) => Test<Value>;
496
+ /** The path of the resource starts with these parts. */
497
+ readonly under: <const Ps extends ReadonlyArray<string | null | Ref<string | null, any>>>(...prefix: Ps) => Test<Value, PrincipalOf<Ps[number], P>>;
498
+ /**
499
+ * A field of the principal, to use as a value. The field must have a
500
+ * {@link Scalar} type.
501
+ */
502
+ readonly principal: <const F extends ScalarPath<P>>(field: F) => Ref<PathValue<P, F>, P>;
503
+ readonly and: <const Cs extends ReadonlyArray<Any>>(...conditions: Consistent<Cs>) => Condition<Error<Cs[number]>, Services<Cs[number]>, OpaqueOf<Cs[number]>, ReadsOf<Cs[number]>>;
504
+ readonly or: <const Cs extends ReadonlyArray<Any>>(...conditions: Consistent<Cs>) => Condition<Error<Cs[number]>, Services<Cs[number]>, OpaqueOf<Cs[number]>, ReadsOf<Cs[number]>>;
505
+ readonly not: <C extends Any>(condition: C & Checked<C>) => Condition<Error<C>, Services<C>, OpaqueOf<C>, ReadsOf<C>>;
506
+ /**
507
+ * A test that runs any code. It may give a boolean or an `Effect`. A
508
+ * condition with a predicate can not become JSON or a query.
509
+ */
510
+ readonly predicate: <E = never, R = never>(name: string, test: (value: Value, principal: P) => boolean | Effect.Effect<boolean, E, R>) => Condition<E, R, Opaque, Reads<Value, P>>;
511
+ }
512
+ /**
513
+ * Makes a builder. Rules give you a builder with the right types, so you do not
514
+ * often need this.
515
+ *
516
+ * @stability stable
517
+ * @category constructors
518
+ * @since 0.3.0
519
+ */
520
+ export declare const builder: <Data, Value, P>() => Builder<Data, Value, P>;
521
+ /**
522
+ * Always true.
523
+ *
524
+ * @stability stable
525
+ * @category constructors
526
+ * @since 0.3.0
527
+ */
528
+ export declare const always: Condition;
529
+ /**
530
+ * Always false.
531
+ *
532
+ * @stability stable
533
+ * @category constructors
534
+ * @since 0.3.0
535
+ */
536
+ export declare const never: Condition;
537
+ /**
538
+ * True when all the conditions are true. With no conditions, it is true.
539
+ *
540
+ * @stability stable
541
+ * @category combinators
542
+ * @since 0.3.0
543
+ */
544
+ export declare const and: <const Cs extends ReadonlyArray<Any>>(...conditions: Consistent<Cs>) => Condition<Error<Cs[number]>, Services<Cs[number]>, OpaqueOf<Cs[number]>, ReadsOf<Cs[number]>>;
545
+ /**
546
+ * The same as {@link and}, for a list of conditions of one type. Use it for a
547
+ * long list: a function call may take only so many arguments.
548
+ *
549
+ * @stability stable
550
+ * @category combinators
551
+ * @since 1.0.0
552
+ */
553
+ export declare const andAll: <C extends Any>(conditions: ReadonlyArray<C & Checked<C>>) => Condition<Error<C>, Services<C>, OpaqueOf<C>, ReadsOf<C>>;
554
+ /**
555
+ * The same as {@link or}, for a list of conditions of one type. Use it for a
556
+ * long list: a function call may take only so many arguments.
557
+ *
558
+ * @stability stable
559
+ * @category combinators
560
+ * @since 1.0.0
561
+ */
562
+ export declare const orAll: <C extends Any>(conditions: ReadonlyArray<C & Checked<C>>) => Condition<Error<C>, Services<C>, OpaqueOf<C>, ReadsOf<C>>;
563
+ /**
564
+ * True when one of the conditions is true. With no conditions, it is false.
565
+ *
566
+ * @stability stable
567
+ * @category combinators
568
+ * @since 0.3.0
569
+ */
570
+ export declare const or: <const Cs extends ReadonlyArray<Any>>(...conditions: Consistent<Cs>) => Condition<Error<Cs[number]>, Services<Cs[number]>, OpaqueOf<Cs[number]>, ReadsOf<Cs[number]>>;
571
+ /**
572
+ * @stability stable
573
+ * @category combinators
574
+ * @since 0.3.0
575
+ */
576
+ export declare const not: <C extends Any>(condition: C & Checked<C>) => Condition<Error<C>, Services<C>, OpaqueOf<C>, ReadsOf<C>>;
577
+ /**
578
+ * The data that a condition needs for a test.
579
+ *
580
+ * @stability stable
581
+ * @category models
582
+ * @since 0.3.0
583
+ */
584
+ export interface Input {
585
+ /** The resource value. */
586
+ readonly value: object;
587
+ /**
588
+ * The principal. Without it, a principal field has no value. See
589
+ * {@link PrincipalRef}.
590
+ */
591
+ readonly principal?: object | undefined;
592
+ /** The path of the resource. Without it, `Under` is false. */
593
+ readonly path?: ReadonlyArray<PathSegment> | undefined;
594
+ }
595
+ /**
596
+ * True when a condition has a {@link Predicate}.
597
+ *
598
+ * @stability stable
599
+ * @category predicates
600
+ * @since 1.0.0
601
+ */
602
+ export declare const hasPredicate: (condition: Ast) => boolean;
603
+ /**
604
+ * Gives the services in `context` to every predicate of a condition, so the
605
+ * condition needs no services when it runs.
606
+ *
607
+ * @stability stable
608
+ * @category services
609
+ * @since 1.0.0
610
+ */
611
+ export declare const provideContext: {
612
+ <R>(context: Context.Context<R>): <E, O, I>(condition: Condition<E, R, O, I>) => Condition<E, never, O, I>;
613
+ <E, R, O, I>(condition: Condition<E, R, O, I>, context: Context.Context<R>): Condition<E, never, O, I>;
614
+ };
615
+ /**
616
+ * The input of {@link evaluate} that the predicates of a condition need: a
617
+ * value and a principal of the types in `I`. See {@link Reads}.
618
+ */
619
+ type ReadsInput<I> = I extends Reads<infer V, infer P> ? {
620
+ readonly value: V;
621
+ } & (unknown extends P ? unknown : {
622
+ readonly principal: P;
623
+ }) : unknown;
624
+ /**
625
+ * `unknown` when `In` gives what `I` reads, and `never` when it does not. For
626
+ * a union, each member must give it, so the test is not split over the union.
627
+ */
628
+ type ReadsFrom<I, In> = [In] extends [ReadsInput<I>] ? unknown : never;
629
+ /**
630
+ * Tests a condition on a value. A predicate may fail with its error. When the
631
+ * condition has a predicate, the input must have the value and the principal
632
+ * that the predicate reads.
633
+ *
634
+ * @stability stable
635
+ * @category evaluation
636
+ * @since 0.3.0
637
+ */
638
+ export declare const evaluate: {
639
+ <In extends Input>(input: In): <E, R, O, I>(condition: Condition<E, R, O, I> & ReadsFrom<I, In>) => Effect.Effect<boolean, E, R>;
640
+ <E, R, O, I, In extends Input>(condition: Condition<E, R, O, I>, input: In & ReadsInput<I>): Effect.Effect<boolean, E, R>;
641
+ };
642
+ /**
643
+ * Tests a condition with no predicate on a value, at once.
644
+ *
645
+ * @stability stable
646
+ * @category evaluation
647
+ * @since 0.3.0
648
+ */
649
+ export declare const evaluateSync: {
650
+ <In extends Input>(input: In): <I>(condition: Condition<never, never, never, I> & ReadsFrom<I, In>) => boolean;
651
+ <I, In extends Input>(condition: Condition<never, never, never, I>, input: In & ReadsInput<I>): boolean;
652
+ };
653
+ /**
654
+ * Makes a condition smaller without a change to its meaning. It removes `True`
655
+ * and `False` from `And` and `Or`, joins nested `And` and `Or` nodes, and
656
+ * removes double `Not` nodes.
657
+ *
658
+ * @stability stable
659
+ * @category evaluation
660
+ * @since 0.3.0
661
+ */
662
+ export declare const simplify: <E, R, O, I>(condition: Condition<E, R, O, I>) => Condition<E, R, O, I>;
663
+ /**
664
+ * Makes a {@link simplify} function that keeps its work for the parts it has
665
+ * seen. Use one for many calls on conditions that share parts, such as the
666
+ * steps of one residual condition. Do not keep it for long: it keeps every
667
+ * part that it has seen.
668
+ *
669
+ * @stability stable
670
+ * @category evaluation
671
+ * @since 1.0.0
672
+ */
673
+ export declare const simplifier: () => (<E, R, O, I>(condition: Condition<E, R, O, I>) => Condition<E, R, O, I>);
674
+ interface PartialInput {
675
+ readonly principal?: object | undefined;
676
+ readonly path?: ReadonlyArray<PathSegment> | undefined;
677
+ }
678
+ /**
679
+ * The principal that {@link partial} must get for the predicates of a
680
+ * condition. A predicate keeps the principal that it gets, so the principal
681
+ * must have the type that the predicate reads.
682
+ */
683
+ type PrincipalInput<I> = I extends Reads<infer _V, infer P> ? unknown extends P ? unknown : {
684
+ readonly principal: P;
685
+ } : unknown;
686
+ /** `unknown` when `In` gives what `I` reads, and `never` when it does not. */
687
+ type PrincipalFrom<I, In> = [In] extends [PrincipalInput<I>] ? unknown : never;
688
+ /** What a condition reads after {@link partial}: the value, and no principal. */
689
+ type Bound<I> = I extends Reads<infer V, infer _P> ? Reads<V, unknown> : I;
690
+ /**
691
+ * Puts the principal into a condition and expands `Under` with the path of the
692
+ * resource. The result is simplified. It has no principal fields and no
693
+ * `Under` nodes. It is `True` or `False` when the answer does not depend on the
694
+ * resource value.
695
+ *
696
+ * @stability stable
697
+ * @category evaluation
698
+ * @since 0.3.0
699
+ */
700
+ export declare const partial: {
701
+ <In extends PartialInput>(input: In): <E, R, O, I>(condition: Condition<E, R, O, I> & PrincipalFrom<I, In>) => Condition<E, R, O, Bound<I>>;
702
+ <E, R, O, I>(condition: Condition<E, R, O, I>, input: PartialInput & PrincipalInput<I>): Condition<E, R, O, Bound<I>>;
703
+ };
704
+ /**
705
+ * The functions that change each node of a condition into another form, such
706
+ * as a database query.
707
+ *
708
+ * @stability stable
709
+ * @category models
710
+ * @since 0.3.0
711
+ */
712
+ export interface Interpreter<A> {
713
+ readonly true: () => A;
714
+ readonly false: () => A;
715
+ readonly compare: (op: CompareOperator, field: string, value: Scalar) => A;
716
+ readonly in: (field: string, values: ReadonlyArray<Scalar>) => A;
717
+ readonly exists: (field: string) => A;
718
+ readonly and: (conditions: ReadonlyArray<A>) => A;
719
+ readonly or: (conditions: ReadonlyArray<A>) => A;
720
+ readonly not: (condition: A) => A;
721
+ /** Optional. Without it, a predicate makes `fold` fail. */
722
+ readonly predicate?: (name: string) => A;
723
+ }
724
+ declare const Untranslatable_base: Schema.Class<Untranslatable, Schema.TaggedStruct<"Untranslatable", {
725
+ readonly reason: Schema.String;
726
+ }>, Cause.YieldableError>;
727
+ /**
728
+ * The error when `fold` can not change a node.
729
+ *
730
+ * @stability stable
731
+ * @category errors
732
+ * @since 0.3.0
733
+ */
734
+ export declare class Untranslatable extends Untranslatable_base {}
735
+ /**
736
+ * Changes a condition into another form with an interpreter. Call
737
+ * {@link partial} first: `fold` fails on principal fields and on `Under`
738
+ * nodes, and on predicates when the interpreter has no `predicate` function.
739
+ *
740
+ * @stability stable
741
+ * @category converting
742
+ * @since 0.3.0
743
+ */
744
+ export declare const fold: {
745
+ <A>(interpreter: Interpreter<A>): (condition: Any) => Effect.Effect<A, Untranslatable>;
746
+ <A>(condition: Any, interpreter: Interpreter<A>): Effect.Effect<A, Untranslatable>;
747
+ };
748
+ /**
749
+ * The fields that a condition uses.
750
+ *
751
+ * @stability stable
752
+ * @category getters
753
+ * @since 0.3.0
754
+ */
755
+ export declare const fields: (condition: Ast) => ReadonlyArray<string>;
756
+ /**
757
+ * Values of fields, as a nested object. A field that is not in it may have any
758
+ * value. A value with parts that the search does not look into, such as a
759
+ * date, is an empty object.
760
+ *
761
+ * @stability stable
762
+ * @category models
763
+ * @since 1.0.0
764
+ */
765
+ export interface Example {
766
+ readonly [field: string]: unknown;
767
+ }
768
+ /**
769
+ * The answer of {@link satisfiable}:
770
+ * - `Satisfiable`: some value makes the condition true. `example` is one.
771
+ * - `Unsatisfiable`: no value makes the condition true.
772
+ * - `Unknown`: the search can not say. The condition has a predicate, the
773
+ * search went past its budget, or it set two fields that the Schema ties
774
+ * together, as below a union or a field that may be missing.
775
+ *
776
+ * @stability stable
777
+ * @category models
778
+ * @since 1.0.0
779
+ */
780
+ export type Satisfiability = Data.TaggedEnum<{
781
+ Satisfiable: {
782
+ readonly example: Example;
783
+ };
784
+ Unsatisfiable: {};
785
+ Unknown: {
786
+ readonly reason: "predicate" | "budget" | "schema";
787
+ };
788
+ }>;
789
+ /**
790
+ * The options of {@link satisfiable}.
791
+ *
792
+ * @stability stable
793
+ * @category models
794
+ * @since 1.0.0
795
+ */
796
+ export interface SatisfiableOptions {
797
+ /**
798
+ * The Schema of the values. It gives the domain of each field. Without
799
+ * it, a field may have any value.
800
+ */
801
+ readonly schema?: Schema.Top | undefined;
802
+ /** The most nodes that the search may visit. The default is 1000000. */
803
+ readonly budget?: number | undefined;
804
+ }
805
+ /**
806
+ * Tests if some value makes a condition true, and gives such a value.
807
+ *
808
+ * Call {@link partial} first: the condition must have no principal fields and
809
+ * no `Under` nodes. A condition with a predicate gives `Unknown`.
810
+ *
811
+ * With `options.schema`, each field of the condition must be a plain scalar
812
+ * field of the Schema (see `Resource.isPlainField`). Else the answer is
813
+ * `Unknown` with the reason `"schema"`: for another field, a value may have
814
+ * data that the Schema does not show, so the model of its values is not
815
+ * exact.
816
+ *
817
+ * The answer is exact. Each test of a condition compares one field with
818
+ * constants, so the constants cut the values of each field into a few cells.
819
+ * The search tries one value from each cell, one field at a time. The values
820
+ * of a field come from the Schema in `options.schema`: its literals, `null`
821
+ * for a nullable or optional field, and finite or integer numbers. A field
822
+ * with another Schema check is not a plain field, so the search never tries a
823
+ * value that such a check stops.
824
+ *
825
+ * The problem is as hard as boolean satisfiability, so the search has a
826
+ * budget. Past it, the answer is `Unknown`. A condition with more than 256
827
+ * fields, or with a field path of more than 256 parts, gives `Unknown` too.
828
+ *
829
+ * @stability stable
830
+ * @category evaluation
831
+ * @since 1.0.0
832
+ */
833
+ export declare const satisfiable: {
834
+ (options?: SatisfiableOptions): (condition: Any) => Satisfiability;
835
+ (condition: Any, options?: SatisfiableOptions): Satisfiability;
836
+ };
837
+ /**
838
+ * Gives a condition as a value that `JSON.stringify` keeps with no loss. JSON
839
+ * has no `Infinity`, `-Infinity` or `NaN`: it writes `null` in their place,
840
+ * and a test with `null` has another meaning. Such a number becomes an object
841
+ * that {@link schema} does not decode, so a policy with it fails to load and
842
+ * never gives a wrong answer.
843
+ *
844
+ * @stability stable
845
+ * @category conversions
846
+ * @since 1.0.0
847
+ */
848
+ export declare const toJsonValue: (condition: Ast) => unknown;
849
+ /**
850
+ * The JSON form of a condition with no predicate.
851
+ *
852
+ * @stability stable
853
+ * @category models
854
+ * @since 0.3.0
855
+ */
856
+ export type Encoded = Exclude<Ast, Predicate | And | Or | Not> | EncodedAnd | EncodedOr | EncodedNot;
857
+ /**
858
+ * @stability stable
859
+ * @category models
860
+ * @since 0.3.0
861
+ */
862
+ export interface EncodedAnd {
863
+ readonly _tag: "And";
864
+ readonly conditions: ReadonlyArray<Encoded>;
865
+ }
866
+ /**
867
+ * @stability stable
868
+ * @category models
869
+ * @since 0.3.0
870
+ */
871
+ export interface EncodedOr {
872
+ readonly _tag: "Or";
873
+ readonly conditions: ReadonlyArray<Encoded>;
874
+ }
875
+ /**
876
+ * @stability stable
877
+ * @category models
878
+ * @since 0.3.0
879
+ */
880
+ export interface EncodedNot {
881
+ readonly _tag: "Not";
882
+ readonly condition: Encoded;
883
+ }
884
+ /**
885
+ * A condition with no predicate, that may read any values. It is the type of
886
+ * {@link schema}: a decoded condition reads nothing, so it fits any rule, and
887
+ * any plain condition, such as one from a builder or a residual, may be
888
+ * encoded.
889
+ *
890
+ * @stability stable
891
+ * @category models
892
+ * @since 1.0.0
893
+ */
894
+ export type PlainCondition = Condition<never, never, never, any>;
895
+ /**
896
+ * The Schema of a condition with no predicate. It checks the shape only. Use
897
+ * `Policy.schema` to check the field names against a domain too.
898
+ *
899
+ * @stability stable
900
+ * @category schemas
901
+ * @since 0.3.0
902
+ */
903
+ export declare const schema: Schema.Codec<PlainCondition, Encoded>;
904
+ /**
905
+ * Tests if a value is a whole condition tree: each node has a known tag and
906
+ * the fields of that tag. A predicate node needs a name and a test. A tree
907
+ * deeper than 1000 levels is not taken.
908
+ *
909
+ * @stability stable
910
+ * @category guards
911
+ * @since 1.0.0
912
+ */
913
+ export declare const isAst: (u: unknown) => u is Ast;
914
+ export { Condition_d_exports };
915
+ //# sourceMappingURL=Condition.d.ts.map