@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,687 @@
1
+ import { Descriptor } from "./Resource.js";
2
+ import { Builder, Condition, Encoded, Example, FieldSelector, Reads, Variance } from "./Condition.js";
3
+ import { ActionOf, Any as Any$1, DataOf, Kind, Principal, ValueOf } from "./Domain.js";
4
+ import { Any as Any$2, Builder as Builder$1, Error as Error$1, OpaqueOf as OpaqueOf$1, Rule, Services as Services$1 } from "./Rule.js";
5
+ import { Decision } from "./Decision.js";
6
+ import * as Context from "effect/Context";
7
+ import * as Effect from "effect/Effect";
8
+ import * as Schema from "effect/Schema";
9
+ import * as Data from "effect/Data";
10
+ import * as Pipeable from "effect/Pipeable";
11
+ declare namespace Policy_d_exports {
12
+ export { Algorithm, AllNode, Any, AnyKindRequest, AnyNode, AnyRequest, CeilingNode, Coverage, DomainOf, EncodedNode, EncodedRule, Error, Explanation, FieldlessRequest, FieldlessTarget, FirstOfNode, KindRequest, KindTarget, Node, OpaqueOf, Options, Policy, Request, ResidualReads, Residuals, RulesNode, Scope, Services, Step, Target, TypedNode, all, any, ceiling, coverage, decide, decideSync, define, empty, explain, firstOf, fitsKind, isPolicy, permittedFields, provideContext, residual, residuals, rules, schema, scopeHasValue, scopeIsEmpty };
13
+ }
14
+ /**
15
+ * @stability stable
16
+ * @category models
17
+ * @since 0.3.0
18
+ */
19
+ export type Algorithm = "denyOverrides" | "permitOverrides" | "firstApplicable" | "lastApplicable";
20
+ /**
21
+ * @stability stable
22
+ * @category models
23
+ * @since 0.3.0
24
+ */
25
+ export interface RulesNode {
26
+ readonly _tag: "Rules";
27
+ readonly algorithm: Algorithm;
28
+ readonly rules: ReadonlyArray<Any$2>;
29
+ }
30
+ /**
31
+ * @stability stable
32
+ * @category models
33
+ * @since 0.3.0
34
+ */
35
+ export interface AllNode {
36
+ readonly _tag: "All";
37
+ readonly policies: ReadonlyArray<Node>;
38
+ }
39
+ /**
40
+ * @stability stable
41
+ * @category models
42
+ * @since 0.3.0
43
+ */
44
+ export interface AnyNode {
45
+ readonly _tag: "Any";
46
+ readonly policies: ReadonlyArray<Node>;
47
+ }
48
+ /**
49
+ * @stability stable
50
+ * @category models
51
+ * @since 0.3.0
52
+ */
53
+ export interface FirstOfNode {
54
+ readonly _tag: "FirstOf";
55
+ readonly policies: ReadonlyArray<Node>;
56
+ }
57
+ /**
58
+ * @stability stable
59
+ * @category models
60
+ * @since 0.3.0
61
+ */
62
+ export interface CeilingNode {
63
+ readonly _tag: "Ceiling";
64
+ readonly cap: Node;
65
+ readonly policy: Node;
66
+ }
67
+ /**
68
+ * The nodes of a policy tree.
69
+ *
70
+ * @stability stable
71
+ * @category models
72
+ * @since 0.3.0
73
+ */
74
+ export type Node = RulesNode | AllNode | AnyNode | FirstOfNode | CeilingNode;
75
+ /**
76
+ * The tree of a policy with the types of its predicates and its domain `D`.
77
+ * Each rule and each child has the types too, and `"~Node"` is only in the types. A node made by
78
+ * hand does not have it. So the node of one policy, or a part of it, can not
79
+ * go into a policy with other types.
80
+ *
81
+ * @stability stable
82
+ * @category models
83
+ * @since 1.0.0
84
+ */
85
+ export type TypedNode<E = never, R = never, O = never, D = any> = ({
86
+ readonly _tag: "Rules";
87
+ readonly algorithm: Algorithm;
88
+ readonly rules: ReadonlyArray<Rule<E, R, O, D, never>>;
89
+ } | {
90
+ readonly _tag: "All" | "Any" | "FirstOf";
91
+ readonly policies: ReadonlyArray<TypedNode<E, R, O, D>>;
92
+ } | {
93
+ readonly _tag: "Ceiling";
94
+ readonly cap: TypedNode<E, R, O, D>;
95
+ readonly policy: TypedNode<E, R, O, D>;
96
+ }) & {
97
+ readonly "~Node": Variance<E, R, O>;
98
+ };
99
+ /**
100
+ * A policy for the domain `D`. `E`, `R` and `O` come from the conditions of
101
+ * its rules. See `Condition.Condition`.
102
+ *
103
+ * @stability stable
104
+ * @category models
105
+ * @since 0.3.0
106
+ */
107
+ export interface Policy<D extends Any$1, E = never, R = never, O = never> extends Pipeable.Pipeable {
108
+ readonly domain: D;
109
+ readonly node: TypedNode<E, R, O, D>;
110
+ readonly "~Policy": Variance<E, R, O>;
111
+ /**
112
+ * Only in the types. A policy reads the values and the principal of its
113
+ * domain, so a policy of one domain is not a policy of a wider domain.
114
+ */
115
+ readonly "~Domain": (domain: D) => D;
116
+ }
117
+ /**
118
+ * Any policy.
119
+ *
120
+ * @stability stable
121
+ * @category models
122
+ * @since 0.3.0
123
+ */
124
+ export type Any = Policy<any, any, any, any>;
125
+ /**
126
+ * The domain of a policy.
127
+ *
128
+ * @stability stable
129
+ * @category utility types
130
+ * @since 0.3.0
131
+ */
132
+ export type DomainOf<P> = P extends Policy<infer D, any, any, any> ? D : never;
133
+ /**
134
+ * The error type of a policy.
135
+ *
136
+ * @stability stable
137
+ * @category utility types
138
+ * @since 0.3.0
139
+ */
140
+ export type Error<P> = P extends {
141
+ readonly "~Policy"?: Variance<infer E, infer _R, infer _O>;
142
+ } ? E : never;
143
+ /**
144
+ * The service type of a policy.
145
+ *
146
+ * @stability stable
147
+ * @category utility types
148
+ * @since 0.3.0
149
+ */
150
+ export type Services<P> = P extends {
151
+ readonly "~Policy"?: Variance<infer _E, infer R, infer _O>;
152
+ } ? R : never;
153
+ /**
154
+ * `Condition.Opaque` when a policy has a predicate.
155
+ *
156
+ * @stability stable
157
+ * @category utility types
158
+ * @since 0.3.0
159
+ */
160
+ export type OpaqueOf<P> = P extends {
161
+ readonly "~Policy"?: Variance<infer _E, infer _R, infer O>;
162
+ } ? O : never;
163
+ /**
164
+ * One act on one resource value, for each kind of the domain. The action must
165
+ * be an action of the kind, and the field must be a field of the kind.
166
+ *
167
+ * @stability stable
168
+ * @category models
169
+ * @since 0.3.0
170
+ */
171
+ export type Target<D> = { [K in Kind<D>]: {
172
+ readonly action: ActionOf<D, K>;
173
+ readonly resource: ValueOf<D, K>;
174
+ readonly field?: FieldSelector<DataOf<D, K>>;
175
+ /** A target has a resource value or a kind, never both. */
176
+ readonly kind?: never;
177
+ }; }[Kind<D>];
178
+ /**
179
+ * A {@link Target} with no field. Each kind keeps its own actions.
180
+ *
181
+ * @stability stable
182
+ * @category models
183
+ * @since 0.3.0
184
+ */
185
+ export type FieldlessTarget<D> = { [K in Kind<D>]: {
186
+ readonly action: ActionOf<D, K>;
187
+ readonly resource: ValueOf<D, K>;
188
+ readonly kind?: never;
189
+ }; }[Kind<D>];
190
+ /**
191
+ * A scope: a part of a kind, as a condition on its values. It gets a builder
192
+ * with the types of the kind and of the principal, like the condition of a
193
+ * rule. A scope is plain data, so it can not have a predicate.
194
+ *
195
+ * @stability stable
196
+ * @category models
197
+ * @since 1.0.0
198
+ */
199
+ export type Scope<D, K extends string> = (builder: Builder<DataOf<D, K>, ValueOf<D, K>, Principal<D>>) => Condition<never, never, never, Reads<ValueOf<D, K>, Principal<D>>>;
200
+ /**
201
+ * One act on one kind of resource, with no value. With `within`, the target is
202
+ * only the values of the kind in the scope. Without it, the target is every
203
+ * value of the kind.
204
+ *
205
+ * @stability stable
206
+ * @category models
207
+ * @since 0.3.0
208
+ */
209
+ export type KindTarget<D> = { [K in Kind<D>]: {
210
+ readonly action: ActionOf<D, K>;
211
+ readonly kind: K;
212
+ readonly field?: FieldSelector<DataOf<D, K>>;
213
+ /** The part of the kind. See {@link Scope}. */
214
+ readonly within?: Scope<D, K>;
215
+ /** A target has a resource value or a kind, never both. */
216
+ readonly resource?: never;
217
+ }; }[Kind<D>];
218
+ /**
219
+ * A request: a principal, an action, and a resource value.
220
+ *
221
+ * @stability stable
222
+ * @category models
223
+ * @since 0.3.0
224
+ */
225
+ export type Request<D> = Target<D> & {
226
+ readonly principal: Principal<D>;
227
+ };
228
+ /**
229
+ * A {@link Request} with no field.
230
+ *
231
+ * @stability stable
232
+ * @category models
233
+ * @since 0.3.0
234
+ */
235
+ export type FieldlessRequest<D> = FieldlessTarget<D> & {
236
+ readonly principal: Principal<D>;
237
+ };
238
+ /**
239
+ * A request about a kind of resource, with no value.
240
+ *
241
+ * @stability stable
242
+ * @category models
243
+ * @since 0.3.0
244
+ */
245
+ export type KindRequest<D> = KindTarget<D> & {
246
+ readonly principal: Principal<D>;
247
+ };
248
+ /**
249
+ * A request with no types.
250
+ *
251
+ * @stability stable
252
+ * @category models
253
+ * @since 0.3.0
254
+ */
255
+ export interface AnyRequest {
256
+ readonly principal: object;
257
+ readonly action: string;
258
+ readonly resource: object;
259
+ readonly field?: string | undefined;
260
+ }
261
+ /**
262
+ * One rule that a decision tested. `path` gives the place of the rule in the
263
+ * policy tree: the index of each child, then the index of the rule.
264
+ *
265
+ * @stability stable
266
+ * @category models
267
+ * @since 0.3.0
268
+ */
269
+ export interface Step {
270
+ readonly path: ReadonlyArray<number>;
271
+ readonly rule: Any$2;
272
+ readonly applies: boolean;
273
+ }
274
+ /**
275
+ * A decision, and the rules that were tested to make it, in order. Keep it in
276
+ * an audit log.
277
+ *
278
+ * @stability stable
279
+ * @category models
280
+ * @since 0.3.0
281
+ */
282
+ export interface Explanation {
283
+ readonly decision: Decision;
284
+ readonly steps: ReadonlyArray<Step>;
285
+ }
286
+ /**
287
+ * The options of a list of rules.
288
+ *
289
+ * @stability stable
290
+ * @category models
291
+ * @since 0.3.0
292
+ */
293
+ export interface Options {
294
+ /** The default is `denyOverrides`. */
295
+ readonly algorithm?: Algorithm;
296
+ }
297
+ /**
298
+ * Makes a policy from a list of rules.
299
+ *
300
+ * @stability stable
301
+ * @category constructors
302
+ * @since 0.3.0
303
+ */
304
+ export declare const rules: <D extends Any$1, Rules extends Rule<any, any, any, D, never>>(domain: D, rules: Iterable<Rules>, options?: Options) => Policy<D, Error$1<Rules>, Services$1<Rules>, OpaqueOf$1<Rules>>;
305
+ /**
306
+ * Makes a policy from a generator function that gives rules. It is pure and it
307
+ * gives its result at once.
308
+ *
309
+ * @example
310
+ * ```ts
311
+ * const policy = Policy.define(App)(function* (_) {
312
+ * yield* _.allow("read", Bot)
313
+ * yield* _.allow(["update", "delete"], Bot, {
314
+ * when: ($) => $.eq("ownerUserId", $.principal("userId")),
315
+ * })
316
+ * })
317
+ * ```
318
+ *
319
+ * @stability stable
320
+ * @category constructors
321
+ * @since 0.3.0
322
+ */
323
+ export declare const define: <D extends Any$1>(domain: D, options?: Options) => <Rules extends Rule<any, any, any, D, never>, A>(body: (builder: Builder$1<D>) => Generator<Rules, A, unknown>) => Policy<D, Error$1<Rules>, Services$1<Rules>, OpaqueOf$1<Rules>>;
324
+ /**
325
+ * A policy with no rules. It is not applicable to any request.
326
+ *
327
+ * @stability stable
328
+ * @category constructors
329
+ * @since 0.3.0
330
+ */
331
+ export declare const empty: <D extends Any$1>(domain: D) => Policy<D>;
332
+ type Combined<D extends Any$1, Ps extends ReadonlyArray<Any>> = Policy<D, Error<Ps[number]>, Services<Ps[number]>, OpaqueOf<Ps[number]>>;
333
+ /**
334
+ * Every policy must allow, and one deny is enough to deny. With some policies
335
+ * not applicable and none that deny, the result is not applicable.
336
+ *
337
+ * @stability stable
338
+ * @category combinators
339
+ * @since 0.3.0
340
+ */
341
+ export declare const all: <const Ps extends readonly [Policy<any, any, any, any>, ...Array<Any>]>(policies: Ps) => Combined<DomainOf<Ps[0]>, Ps>;
342
+ /**
343
+ * One allow is enough. Without an allow, one deny gives a deny.
344
+ *
345
+ * @stability stable
346
+ * @category combinators
347
+ * @since 0.3.0
348
+ */
349
+ export declare const any: <const Ps extends readonly [Policy<any, any, any, any>, ...Array<Any>]>(policies: Ps) => Combined<DomainOf<Ps[0]>, Ps>;
350
+ /**
351
+ * The first policy that allows or denies gives the answer.
352
+ *
353
+ * @stability stable
354
+ * @category combinators
355
+ * @since 0.3.0
356
+ */
357
+ export declare const firstOf: <const Ps extends readonly [Policy<any, any, any, any>, ...Array<Any>]>(policies: Ps) => Combined<DomainOf<Ps[0]>, Ps>;
358
+ /**
359
+ * Puts a limit on a policy. A deny from `cap` always wins. An allow from `cap`
360
+ * does nothing.
361
+ *
362
+ * @stability stable
363
+ * @category combinators
364
+ * @since 0.3.0
365
+ */
366
+ export declare const ceiling: {
367
+ <D extends Any$1, E2, R2, O2>(cap: Policy<D, E2, R2, O2>): <E, R, O>(policy: Policy<D, E, R, O>) => Policy<D, E | E2, R | R2, O | O2>;
368
+ <D extends Any$1, E, R, O, E2, R2, O2>(policy: Policy<D, E, R, O>, cap: Policy<D, E2, R2, O2>): Policy<D, E | E2, R | R2, O | O2>;
369
+ };
370
+ /**
371
+ * Gives the services in `context` to every predicate of a policy, so the
372
+ * policy needs no services when it runs.
373
+ *
374
+ * @stability stable
375
+ * @category services
376
+ * @since 1.0.0
377
+ */
378
+ export declare const provideContext: {
379
+ <R>(context: Context.Context<R>): <D extends Any$1, E, O>(policy: Policy<D, E, R, O>) => Policy<D, E, never, O>;
380
+ <D extends Any$1, E, R, O>(policy: Policy<D, E, R, O>, context: Context.Context<R>): Policy<D, E, never, O>;
381
+ };
382
+ /**
383
+ * Tests if a request fits its kind: the action is an action of the kind, and
384
+ * the field, if there is one, is a field of the kind. The types check this,
385
+ * but a value with a union type, or a value from outside, may get past them.
386
+ * A rule for every action (`"*"`) must not cover an action that the kind does
387
+ * not have, so such a request is not applicable.
388
+ *
389
+ * @stability stable
390
+ * @category predicates
391
+ * @since 1.0.0
392
+ */
393
+ export declare const fitsKind: (descriptor: Descriptor, request: {
394
+ readonly action: string;
395
+ readonly field?: string | undefined;
396
+ }) => boolean;
397
+ /**
398
+ * Gives the decision of a policy for a request.
399
+ *
400
+ * @stability stable
401
+ * @category evaluation
402
+ * @since 0.3.0
403
+ */
404
+ export declare const decide: {
405
+ <D extends Any$1 = never>(request: Request<D>): <E, R, O>(policy: Policy<D, E, R, O>) => Effect.Effect<Decision, E, R>;
406
+ <D extends Any$1, E, R, O>(policy: Policy<D, E, R, O>, request: Request<NoInfer<D>>): Effect.Effect<Decision, E, R>;
407
+ };
408
+ /**
409
+ * Gives the decision of a policy with no predicates, at once.
410
+ *
411
+ * @stability stable
412
+ * @category evaluation
413
+ * @since 0.3.0
414
+ */
415
+ export declare const decideSync: {
416
+ <D extends Any$1 = never>(request: Request<D>): (policy: Policy<D>) => Decision;
417
+ <D extends Any$1>(policy: Policy<D>, request: Request<NoInfer<D>>): Decision;
418
+ };
419
+ /**
420
+ * Gives the decision of a policy and the rules that were tested to make it.
421
+ *
422
+ * One node may be a child of many nodes. A node with no predicate that the
423
+ * request may use gives the same decision each time, so its rules are tested,
424
+ * and are in the steps, only the first time. So a small tree with many shared
425
+ * nodes gives a short list of steps.
426
+ *
427
+ * @stability stable
428
+ * @category evaluation
429
+ * @since 0.3.0
430
+ */
431
+ export declare const explain: {
432
+ <D extends Any$1 = never>(request: Request<D>): <E, R, O>(policy: Policy<D, E, R, O>) => Effect.Effect<Explanation, E, R>;
433
+ <D extends Any$1, E, R, O>(policy: Policy<D, E, R, O>, request: Request<NoInfer<D>>): Effect.Effect<Explanation, E, R>;
434
+ };
435
+ /**
436
+ * Gives the fields of a resource value that the policy lets a request use. It
437
+ * tests each field of the resource Schema, with dots for nested fields.
438
+ *
439
+ * A field that holds more than one value, such as a record, is left out when a
440
+ * deny rule names a part of it. The list can not name that part, and the whole
441
+ * field would show it.
442
+ *
443
+ * @stability stable
444
+ * @category evaluation
445
+ * @since 0.3.0
446
+ */
447
+ export declare const permittedFields: {
448
+ <D extends Any$1 = never>(request: FieldlessRequest<D>): <E, R, O>(policy: Policy<D, E, R, O>) => Effect.Effect<ReadonlyArray<string>, E, R>;
449
+ <D extends Any$1, E, R, O>(policy: Policy<D, E, R, O>, request: FieldlessRequest<NoInfer<D>>): Effect.Effect<ReadonlyArray<string>, E, R>;
450
+ };
451
+ /**
452
+ * The two residual conditions of a request. See {@link residuals}.
453
+ *
454
+ * @stability stable
455
+ * @category models
456
+ * @since 1.0.0
457
+ */
458
+ export interface Residuals<E = never, R = never, O = never, I = unknown> {
459
+ /** The condition on resource values for which the policy allows. */
460
+ readonly allow: Condition<E, R, O, I>;
461
+ /** The condition on resource values for which the policy denies. */
462
+ readonly deny: Condition<E, R, O, I>;
463
+ }
464
+ /**
465
+ * A request about a kind of resource, with no types.
466
+ *
467
+ * @stability stable
468
+ * @category models
469
+ * @since 1.0.0
470
+ */
471
+ export interface AnyKindRequest {
472
+ readonly principal: object;
473
+ readonly action: string;
474
+ readonly kind: string;
475
+ readonly field?: string | undefined;
476
+ /**
477
+ * A scope needs the types of the domain for its builder. With the request
478
+ * first, the domain is not known yet, so give the policy first.
479
+ */
480
+ readonly within?: never;
481
+ }
482
+ /**
483
+ * `unknown` when the request `Req` is a request of the type `T`, and `never`
484
+ * when it is not. With the request first and the policy later, the request is
485
+ * checked when the policy, and so the domain, is known. For a union, each
486
+ * member must be such a request.
487
+ */
488
+ type Fits<Req, T> = [Req] extends [T] ? unknown : never;
489
+ /**
490
+ * What a residual condition reads: a value of the kind `K`. The principal is
491
+ * already in it. A residual is for one kind, so when `K` is a union of kinds,
492
+ * the value must be a value of each of them. No value is, so such a residual
493
+ * can not be evaluated on a value of the wrong kind.
494
+ *
495
+ * @stability stable
496
+ * @category utility types
497
+ * @since 1.0.0
498
+ */
499
+ export type ResidualReads<D, K> = Reads<(K extends unknown ? (value: ValueOf<D, K & string>) => void : never) extends ((value: infer V) => void) ? V : never, unknown>;
500
+ /** A request about a kind with no types, with the scope of a target. */
501
+ interface ScopedRequest {
502
+ readonly principal: object;
503
+ readonly action: string;
504
+ readonly kind: string;
505
+ readonly field?: string | undefined;
506
+ readonly within?: unknown;
507
+ }
508
+ /**
509
+ * Gives the condition on resource values for which the policy allows a
510
+ * request. It has no principal fields and no `Under` nodes, so you can give it
511
+ * to `Condition.fold`. It is `True` when the policy allows every value of the
512
+ * kind, and `False` when it allows none.
513
+ *
514
+ * With a scope (`within`), the condition is the scope and the residual
515
+ * together, so it is false for every value outside the scope. Give the policy
516
+ * first to use a scope. The condition comes from its parts, with no search. To
517
+ * know if it allows all of the scope or none of it, use {@link coverage}.
518
+ *
519
+ * When a rule that the request may use has a predicate, the condition is one
520
+ * predicate with the name `"policy"`. It runs the whole policy for each value,
521
+ * so its answers and its failures are the same as those of {@link decide}. It
522
+ * takes the principal with it. Give it resource values with their `_tag`. It
523
+ * is false for a value of another kind.
524
+ *
525
+ * A policy tree may use the condition of a child two times, so a small policy
526
+ * may give a very large condition. When the condition would have more than
527
+ * 10000 nodes or 500 levels, this function throws an error. In an
528
+ * `Authorizer`, the error is
529
+ * a defect, so the check fails.
530
+ *
531
+ * @stability stable
532
+ * @category evaluation
533
+ * @since 0.3.0
534
+ */
535
+ export declare const residual: {
536
+ <const Req extends AnyKindRequest>(request: Req): <D extends Any$1, E, R, O>(policy: Policy<D, E, R, O> & Fits<Req, KindRequest<D>>) => Condition<E, R, O, ResidualReads<D, Req["kind"]>>;
537
+ <D extends Any$1, E, R, O, const Req extends KindRequest<NoInfer<D>>>(policy: Policy<D, E, R, O>, request: Req): Condition<E, R, O, ResidualReads<D, Req["kind"]>>;
538
+ };
539
+ /**
540
+ * Gives both residual conditions of a request: the values that the policy
541
+ * allows, and the values that it denies. A value in neither is not
542
+ * applicable. See {@link residual}.
543
+ *
544
+ * @stability stable
545
+ * @category evaluation
546
+ * @since 1.0.0
547
+ */
548
+ export declare const residuals: {
549
+ <const Req extends AnyKindRequest>(request: Req): <D extends Any$1, E, R, O>(policy: Policy<D, E, R, O> & Fits<Req, KindRequest<D>>) => Residuals<E, R, O, ResidualReads<D, Req["kind"]>>;
550
+ <D extends Any$1, E, R, O, const Req extends KindRequest<NoInfer<D>>>(policy: Policy<D, E, R, O>, request: Req): Residuals<E, R, O, ResidualReads<D, Req["kind"]>>;
551
+ };
552
+ /**
553
+ * How much of a scope a policy allows. In each case, `condition` is the exact
554
+ * condition on the values in the scope that the policy allows: give it to
555
+ * `Condition.fold` for a list query.
556
+ * - `All`: the policy allows every value in the scope. `condition` is the
557
+ * scope (`True` with no scope).
558
+ * - `None`: the policy allows no value in the scope. `condition` is `False`.
559
+ * An empty scope is `None` too, so a check on a scope that has a mistake,
560
+ * such as a principal field with no value, never passes.
561
+ * - `Some`: the policy allows some values in the scope and not others.
562
+ * `allowed` is an example of a value that it allows, and `refused` of a
563
+ * value in the scope that it does not allow.
564
+ * - `Unknown`: the answer needs a predicate, or the search went past its
565
+ * budget.
566
+ *
567
+ * @stability stable
568
+ * @category models
569
+ * @since 1.0.0
570
+ */
571
+ export type Coverage<E = never, R = never, O = never, I = unknown> = Data.TaggedEnum<{
572
+ All: {
573
+ readonly condition: Condition<E, R, O, I>;
574
+ };
575
+ None: {
576
+ readonly condition: Condition<E, R, O, I>;
577
+ };
578
+ Some: {
579
+ readonly condition: Condition<E, R, O, I>;
580
+ readonly allowed: Example;
581
+ readonly refused: Example;
582
+ };
583
+ Unknown: {
584
+ readonly condition: Condition<E, R, O, I>;
585
+ readonly reason: "predicate" | "budget" | "schema";
586
+ };
587
+ }>;
588
+ /**
589
+ * Tests if the scope of a request has a value of the Schema of its kind. The
590
+ * answer is `true` only when the search finds such a value. When the search
591
+ * can not say, the answer is `false`. So a "yes" from it is safe to act on,
592
+ * as for the question "may the decider answer for this scope?".
593
+ *
594
+ * @stability stable
595
+ * @category evaluation
596
+ * @since 1.0.0
597
+ */
598
+ export declare const scopeHasValue: (policy: Any, request: ScopedRequest) => boolean;
599
+ /**
600
+ * Tests if the scope of a request has no value of the Schema of its kind. A
601
+ * scope with a mistake, such as two values for one field, is empty. With no
602
+ * scope, the answer is `false` when the kind has a value. When the search can
603
+ * not say, the answer is `false`.
604
+ *
605
+ * @stability stable
606
+ * @category evaluation
607
+ * @since 1.0.0
608
+ */
609
+ export declare const scopeIsEmpty: (policy: Any, request: ScopedRequest) => boolean;
610
+ /**
611
+ * Tells how much of a scope the policy allows: all of it, none of it, or some
612
+ * of it. See {@link Coverage}. With no scope (`within`), the scope is every
613
+ * value of the kind.
614
+ *
615
+ * The answer comes from {@link Condition.satisfiable} with the Schema of the
616
+ * resource, so it is exact for the values of the Schema. When a condition
617
+ * tests a field that is not a plain scalar (see `Resource.isPlainField`),
618
+ * such as a field with a check other than a finite or integer check, the
619
+ * answer is `Unknown`, and a check on the kind says no.
620
+ *
621
+ * `Some` means that some value of the Schema is allowed. It does not mean that
622
+ * such a value is in your database. To know that, run `condition` as a query.
623
+ *
624
+ * @stability stable
625
+ * @category evaluation
626
+ * @since 1.0.0
627
+ */
628
+ export declare const coverage: {
629
+ <const Req extends AnyKindRequest>(request: Req): <D extends Any$1, E, R, O>(policy: Policy<D, E, R, O> & Fits<Req, KindRequest<D>>) => Coverage<E, R, O, ResidualReads<D, Req["kind"]>>;
630
+ <D extends Any$1, E, R, O, const Req extends KindRequest<NoInfer<D>>>(policy: Policy<D, E, R, O>, request: Req): Coverage<E, R, O, ResidualReads<D, Req["kind"]>>;
631
+ };
632
+ /**
633
+ * The JSON form of a rule.
634
+ *
635
+ * @stability stable
636
+ * @category models
637
+ * @since 0.3.0
638
+ */
639
+ export interface EncodedRule {
640
+ readonly _tag: "Allow" | "Deny";
641
+ readonly actions: ReadonlyArray<string>;
642
+ readonly resources: ReadonlyArray<string>;
643
+ readonly when?: Encoded;
644
+ readonly fields?: ReadonlyArray<string>;
645
+ readonly reason?: string;
646
+ readonly id?: string;
647
+ readonly meta?: unknown;
648
+ }
649
+ /**
650
+ * The JSON form of a policy.
651
+ *
652
+ * @stability stable
653
+ * @category models
654
+ * @since 0.3.0
655
+ */
656
+ export type EncodedNode = {
657
+ readonly _tag: "Rules";
658
+ readonly algorithm: Algorithm;
659
+ readonly rules: ReadonlyArray<EncodedRule>;
660
+ } | {
661
+ readonly _tag: "All" | "Any" | "FirstOf";
662
+ readonly policies: ReadonlyArray<EncodedNode>;
663
+ } | {
664
+ readonly _tag: "Ceiling";
665
+ readonly cap: EncodedNode;
666
+ readonly policy: EncodedNode;
667
+ };
668
+ /**
669
+ * Tests if a value is a policy.
670
+ *
671
+ * @stability stable
672
+ * @category guards
673
+ * @since 0.3.0
674
+ */
675
+ export declare const isPolicy: (u: unknown) => u is Any;
676
+ /**
677
+ * The Schema of the policies of a domain with no predicates. Decoding checks
678
+ * every kind, action, field and principal field against the domain, and the
679
+ * `meta` of each rule against the `meta` Schema of the domain.
680
+ *
681
+ * @stability stable
682
+ * @category schemas
683
+ * @since 0.3.0
684
+ */
685
+ export declare const schema: <D extends Any$1>(domain: D) => Schema.Codec<Policy<D>, EncodedNode>;
686
+ export { Policy_d_exports };
687
+ //# sourceMappingURL=Policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Policy.d.ts","names":["Rule.Any","Rule.Rule","Condition.Variance","Domain.Any","Domain.Kind","Domain.ActionOf","Domain.ValueOf","Domain.DataOf","Condition.FieldSelector","Domain.Principal","Condition.Builder","Condition.Reads","Condition.Condition","Decision.Decision","Rule.Error","Rule.Services","Rule.OpaqueOf","Rule.Builder","Resource.Descriptor","Condition.Example","Condition.Encoded"],"sources":["../src/Policy.ts"],"mappings":";;;;;;;;;;;;;;;;;;YAoEY;;;;;;iBAQK;WACP;WACA,WAAW;WACX,OAAO,cAAcA;;;;;;;iBAQd;WACP;WACA,UAAU,cAAc;;;;;;;iBAQjB;WACP;WACA,UAAU,cAAc;;;;;;;iBAQjB;WACP;WACA,UAAU,cAAc;;;;;;;iBAQjB;WACP;WACA,KAAK;WACL,QAAQ;;;;;;;;;YAUN,OAAO,YAAY,UAAU,UAAU,cAAc;;;;;;;;;;;YAYrD,UAAU,WAAW,WAAW,WAAW;WAE3C;WACA,WAAW;WAIX,OAAO,cAAcC,KAAU,GAAG,GAAG,GAAG;;WAGxC;WACA,UAAU,cAAc,UAAU,GAAG,GAAG,GAAG;;WAG3C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