@nmnmcc/ability 0.3.0 → 1.0.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.
Files changed (66) hide show
  1. package/README.md +199 -31
  2. package/dist/Authorizer.d.ts +64 -39
  3. package/dist/Authorizer.d.ts.map +1 -1
  4. package/dist/Authorizer.js +291 -6
  5. package/dist/Authorizer.js.map +1 -1
  6. package/dist/Condition.d.ts +444 -65
  7. package/dist/Condition.d.ts.map +1 -1
  8. package/dist/Condition.js +1007 -25
  9. package/dist/Condition.js.map +1 -1
  10. package/dist/Domain.d.ts +27 -17
  11. package/dist/Domain.d.ts.map +1 -1
  12. package/dist/Domain.js +52 -10
  13. package/dist/Domain.js.map +1 -1
  14. package/dist/HttpApi.d.ts +84 -40
  15. package/dist/HttpApi.d.ts.map +1 -1
  16. package/dist/HttpApi.js +103 -13
  17. package/dist/HttpApi.js.map +1 -1
  18. package/dist/Policy.d.ts +314 -56
  19. package/dist/Policy.d.ts.map +1 -1
  20. package/dist/Policy.js +1215 -38
  21. package/dist/Policy.js.map +1 -1
  22. package/dist/Principal.js +2 -2
  23. package/dist/Principal.js.map +1 -1
  24. package/dist/Resource.d.ts +171 -20
  25. package/dist/Resource.d.ts.map +1 -1
  26. package/dist/Resource.js +788 -15
  27. package/dist/Resource.js.map +1 -1
  28. package/dist/Rule.d.ts +115 -29
  29. package/dist/Rule.d.ts.map +1 -1
  30. package/dist/Rule.js +219 -12
  31. package/dist/Rule.js.map +1 -1
  32. package/dist/index.js +4 -4
  33. package/package.json +2 -2
  34. package/src/Authorizer.ts +625 -61
  35. package/src/Condition.ts +2403 -112
  36. package/src/Domain.ts +80 -23
  37. package/src/HttpApi.ts +225 -25
  38. package/src/Policy.ts +2836 -96
  39. package/src/Principal.ts +2 -2
  40. package/src/Resource.ts +1544 -29
  41. package/src/Rule.ts +463 -45
  42. package/dist/internal/authorizer.d.ts +0 -28
  43. package/dist/internal/authorizer.d.ts.map +0 -1
  44. package/dist/internal/authorizer.js +0 -153
  45. package/dist/internal/authorizer.js.map +0 -1
  46. package/dist/internal/condition.d.ts +0 -11
  47. package/dist/internal/condition.d.ts.map +0 -1
  48. package/dist/internal/condition.js +0 -393
  49. package/dist/internal/condition.js.map +0 -1
  50. package/dist/internal/domain.js +0 -34
  51. package/dist/internal/domain.js.map +0 -1
  52. package/dist/internal/httpApi.js +0 -41
  53. package/dist/internal/httpApi.js.map +0 -1
  54. package/dist/internal/policy.js +0 -524
  55. package/dist/internal/policy.js.map +0 -1
  56. package/dist/internal/resource.js +0 -66
  57. package/dist/internal/resource.js.map +0 -1
  58. package/dist/internal/rule.js +0 -131
  59. package/dist/internal/rule.js.map +0 -1
  60. package/src/internal/authorizer.ts +0 -271
  61. package/src/internal/condition.ts +0 -607
  62. package/src/internal/domain.ts +0 -56
  63. package/src/internal/httpApi.ts +0 -84
  64. package/src/internal/policy.ts +0 -815
  65. package/src/internal/resource.ts +0 -98
  66. package/src/internal/rule.ts +0 -203
@@ -1,19 +1,7 @@
1
1
  import * as Schema from "effect/Schema";
2
2
  declare namespace Resource_d_exports {
3
- export { Action, Any, Class, Data, Descriptor, FieldRef, Kind, Options, PathSegment, Resource, Struct, TypeId, Value, descriptor, isResource, make };
3
+ export { Action, Any, Class, Data, Descriptor, FieldDomain, FieldRef, Kind, Options, PathSegment, Resource, Struct, Value, anyDomain, descriptor, domainOf, fieldPaths, hasGetter, hasMatchingGetter, hasMatchingPath, hasNoValue, hasPath, hasValue, isPlainField, isResource, kindOf, make };
4
4
  }
5
- /**
6
- * @stability stable
7
- * @category type IDs
8
- * @since 0.3.0
9
- */
10
- export declare const TypeId: "~@nmnmcc/ability/Resource";
11
- /**
12
- * @stability stable
13
- * @category type IDs
14
- * @since 0.3.0
15
- */
16
- export type TypeId = typeof TypeId;
17
5
  /**
18
6
  * One part of a resource path. A string is a fixed part. A field reference
19
7
  * takes the part from a field of the value.
@@ -47,7 +35,18 @@ export interface Descriptor {
47
35
  readonly path: ReadonlyArray<PathSegment> | undefined;
48
36
  /** The paths of the data fields, with dots for nested fields. */
49
37
  readonly fieldPaths: ReadonlyArray<string>;
50
- /** The Schema of the values, with the `_tag` field. */
38
+ /**
39
+ * The paths in {@link fieldPaths} of fields that hold more than one value,
40
+ * such as a record, a map or an array. The parts of such a field are not
41
+ * in `fieldPaths`, but a rule may name them.
42
+ */
43
+ readonly openPaths: ReadonlyArray<string>;
44
+ /**
45
+ * A struct Schema of the data of the values, with the `_tag` field. It is a
46
+ * struct also for a `Resource.Class`. The class Schema must not be read
47
+ * before the class is made, because Effect then binds the class Schema to
48
+ * the base class, and the values lose the getters of the class.
49
+ */
51
50
  readonly schema: Schema.Top;
52
51
  }
53
52
  /**
@@ -61,7 +60,7 @@ export interface Descriptor {
61
60
  export interface Resource<Kind extends string, Action extends string, Data extends object, Value extends {
62
61
  readonly _tag: Kind;
63
62
  }> {
64
- readonly [TypeId]: Descriptor;
63
+ readonly descriptor: Descriptor;
65
64
  readonly "~Resource"?: {
66
65
  readonly Kind: Kind;
67
66
  readonly Action: Action;
@@ -125,8 +124,152 @@ export interface Options<Actions extends ReadonlyArray<string>, Fields extends S
125
124
  * Gives the path of the resource. The function gets one reference for each
126
125
  * top-level field.
127
126
  */
128
- readonly path?: (fields: { readonly [K in keyof Fields & string]: FieldRef<K>; }) => ReadonlyArray<PathSegment>;
127
+ readonly path?: (fields: { readonly [K in keyof Fields & string]: FieldRef<K>; }) => ReadonlyArray<string | FieldRef<keyof Fields & string>>;
128
+ }
129
+ /**
130
+ * Tests if a path names a getter of a class Schema, such as a getter of a
131
+ * `Schema.Class` or a `Principal.Class`, at its root or in a nested field. A
132
+ * condition reads such a field, and the types of a condition take it, so the
133
+ * check of a policy takes it too. It goes into the fields of a struct, the
134
+ * values of a record, the members of a union, `Schema.suspend`, and a class
135
+ * with an annotation or a check.
136
+ *
137
+ * @stability stable
138
+ * @category predicates
139
+ * @since 1.0.0
140
+ */
141
+ export declare const hasGetter: (schema: unknown, path: string) => boolean;
142
+ /**
143
+ * Tests if a Schema has a getter of a class at a path whose parts fit
144
+ * `parts`, as `hasGetter` does for one path. A part fits a name when `fits`
145
+ * says so, as a part with `*` fits many names. A part `**` stands for any
146
+ * number of parts, also none.
147
+ *
148
+ * @stability stable
149
+ * @category predicates
150
+ * @since 1.0.0
151
+ */
152
+ export declare const hasMatchingGetter: (schema: unknown, parts: ReadonlyArray<string>, fits: (part: string, name: string) => boolean) => boolean;
153
+ /**
154
+ * Gives the paths of all the fields of a Schema, with dots for nested fields.
155
+ * A nested struct gives its own path and the paths of its fields. Arrays are
156
+ * left out. It throws when a field name has a dot or is empty, because such a
157
+ * name would give two fields one path, or a path with an empty part.
158
+ *
159
+ * @stability stable
160
+ * @category getters
161
+ * @since 1.0.0
162
+ */
163
+ export declare const fieldPaths: (schema: Schema.Top) => ReadonlyArray<string>;
164
+ /**
165
+ * Tests if a path names a plain scalar field of a Schema: a string, a number,
166
+ * a boolean, a literal, `null`, or an optional field or a union of these. Its
167
+ * only checks may be integer and finite checks: another check, such as
168
+ * `isGreaterThan(0)`, stops values that the search does not know about. On
169
+ * the way, each part must be a required field of a plain struct with no
170
+ * checks, with a name that is not a name of `Object.prototype`.
171
+ *
172
+ * `Policy.coverage` and `Condition.satisfiable` answer `All`, `None` or
173
+ * `Some` only when each field of the condition is such a field. Their model of
174
+ * the values of a field is exact for these Schemas. For other Schemas, such as
175
+ * classes, records, unions of structs or declarations, a value may have data
176
+ * that the Schema does not show, and the answer is `Unknown`.
177
+ *
178
+ * @stability stable
179
+ * @category predicates
180
+ * @since 1.0.0
181
+ */
182
+ export declare const isPlainField: (schema: Schema.Top, path: string) => boolean;
183
+ /**
184
+ * Tests if a path names a field of a Schema that holds a value with no named
185
+ * fields: a scalar, or a value such as a record or an array. It goes into
186
+ * records by their keys, and into recursive Schemas as far as the path goes.
187
+ * So it finds paths that {@link fieldPaths} does not list.
188
+ *
189
+ * @stability stable
190
+ * @category predicates
191
+ * @since 1.0.0
192
+ */
193
+ export declare const hasPath: (schema: Schema.Top, path: string) => boolean;
194
+ /**
195
+ * Tests if a Schema has a field whose path fits a pattern. `parts` are the
196
+ * parts of the pattern, split at the dots. A part `**` stands for any number
197
+ * of parts, also none. Another part fits a name when `fits` says so, as a
198
+ * part with `*` fits many names. For a record, the walk asks if the key
199
+ * Schema takes a key that fits the part. For a template literal key Schema,
200
+ * with parts that are literals and strings, the answer is exact. For another key Schema, the walk tries sample keys, so it may miss a
201
+ * key, and then the answer is `false`.
202
+ *
203
+ * @stability stable
204
+ * @category predicates
205
+ * @since 1.0.0
206
+ */
207
+ export declare const hasMatchingPath: (schema: Schema.Top, parts: ReadonlyArray<string>, fits: (part: string, name: string) => boolean) => boolean;
208
+ /**
209
+ * Tests if a Schema surely has a value: each required field, at each depth,
210
+ * has a type that has values, with no checks other than integer and finite
211
+ * checks on numbers. A check such as a minimum length may stop every value,
212
+ * and the library can not see that, so such a Schema gives `false`. So does a
213
+ * required field that holds the struct itself again.
214
+ *
215
+ * @stability stable
216
+ * @category predicates
217
+ * @since 1.0.0
218
+ */
219
+ export declare const hasValue: (schema: Schema.Top) => boolean;
220
+ /**
221
+ * Tests if a Schema has no value at all: a required field of it, at some
222
+ * depth, has no value, as `Schema.Never` has none. A required field that
223
+ * holds the struct itself again, with no way to stop, has no finite value
224
+ * either. Other checks, such as a minimum and a maximum that do not meet, are
225
+ * not seen, so a "no" does not prove that the Schema has a value.
226
+ *
227
+ * @stability stable
228
+ * @category predicates
229
+ * @since 1.0.0
230
+ */
231
+ export declare const hasNoValue: (schema: Schema.Top) => boolean;
232
+ /**
233
+ * The values that one field of a resource may hold, as a condition sees them.
234
+ * A missing field and `undefined` are `null`. The domain may be larger than
235
+ * the real set of values: Schema checks other than finite and integer checks
236
+ * are left out. A larger domain only makes a check say "no" more often.
237
+ *
238
+ * @stability stable
239
+ * @category models
240
+ * @since 1.0.0
241
+ */
242
+ export interface FieldDomain {
243
+ /** The field may be `null` or missing. */
244
+ readonly null: boolean;
245
+ /** The field may hold a value with parts, or a value that is not a scalar. */
246
+ readonly object: boolean;
247
+ /** The field may hold `true` and `false`. */
248
+ readonly boolean: boolean;
249
+ /** The field may hold any string. */
250
+ readonly string: boolean;
251
+ /** The numbers that the field may hold. */
252
+ readonly number: "none" | "integer" | "finite" | "any";
253
+ /** Single values that the field may hold, other than the ones above. */
254
+ readonly literals: ReadonlyArray<string | number | boolean>;
129
255
  }
256
+ /**
257
+ * The domain of a field with no Schema: any value.
258
+ *
259
+ * @stability stable
260
+ * @category constants
261
+ * @since 1.0.0
262
+ */
263
+ export declare const anyDomain: FieldDomain;
264
+ /**
265
+ * Gives the domain of a field of a Schema, by its path with dots. A path that
266
+ * the Schema does not name gives `null`, as a condition reads such a field.
267
+ *
268
+ * @stability stable
269
+ * @category getters
270
+ * @since 1.0.0
271
+ */
272
+ export declare const domainOf: (schema: Schema.Top, path: string) => FieldDomain;
130
273
  /**
131
274
  * The type of a class made by {@link Class}.
132
275
  *
@@ -149,16 +292,16 @@ export type Class<Self, Kind extends string, Actions extends ReadonlyArray<strin
149
292
  * import { Schema } from "effect"
150
293
  * import { Resource } from "@nmnmcc/ability"
151
294
  *
152
- * class Bot extends Resource.Class<Bot>()("Bot", {
295
+ * class Document extends Resource.Class<Document>()("Document", {
153
296
  * actions: ["read", "update"],
154
297
  * fields: {
155
298
  * id: Schema.String,
156
- * ownerUserId: Schema.NullOr(Schema.String),
299
+ * teamId: Schema.NullOr(Schema.String),
157
300
  * },
158
- * path: (f) => ["user", f.ownerUserId, "bot", f.id],
301
+ * path: (f) => ["team", f.teamId, "doc", f.id],
159
302
  * }) {}
160
303
  *
161
- * const bot = new Bot({ id: "b1", ownerUserId: "u1" })
304
+ * const document = new Document({ id: "d1", teamId: "t1" })
162
305
  * ```
163
306
  *
164
307
  * @stability stable
@@ -209,5 +352,13 @@ export declare const isResource: (u: unknown) => u is Any;
209
352
  * @since 0.3.0
210
353
  */
211
354
  export declare const descriptor: (resource: Any) => Descriptor;
355
+ /**
356
+ * Gives the kind name of a resource value, from its `_tag` field.
357
+ *
358
+ * @stability stable
359
+ * @category getters
360
+ * @since 1.0.0
361
+ */
362
+ export declare const kindOf: (value: unknown) => string | undefined;
212
363
  export { Resource_d_exports };
213
364
  //# sourceMappingURL=Resource.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Resource.d.ts","names":[],"sources":["../src/Resource.ts"],"mappings":";;;;;;;;;qBA+Ba;;;;;;YAOD,gBAAgB;;;;;;;;;YAUhB,uBAAuB;;;;;;;;iBASlB,SAAS;WAChB;WACA,OAAO;;;;;;;;;iBAUA;WACP;WACA,SAAS;WACT,MAAM,cAAc;;WAEpB,YAAY;;WAEZ,QAAQ,OAAO;;;;;;;;;;iBAWR,SAChB,qBACA,uBACA,qBACA;WAAyB,MAAM;;YAErB,SAAS;WACV;aACC,MAAM;aACN,QAAQ;aACR,MAAM;aACN,OAAO;;;;;;;;;;YAWN,MAAM;WAA4C;;;;;;;;;YASlD,KAAK,KAAK,UAAU,eAAe,oBAAoB;;;;;;;;YASvD,OAAO,KAAK,UAAU,oBAAoB,eAAe;;;;;;;;YASzD,KAAK,KAAK,UAAU,yBAAyB,UAAU;;;;;;;;YASvD,MAAM,KAAK,UAAU,8BAA8B,KAAK;;;;;;;;iBASnD,QAChB,gBAAgB,uBAChB,eAAe,OAAO,OAAO;WAEpB,SAAS;WACT,QAAQ;;;;;WAKR,QAAQ,oBACN,WAAW,kBAAkB,SAAS,UAC3C,cAAc;;;;;;;;;YAUT,MACX,MACA,qBACA,gBAAgB,uBAChB,eAAe,OAAO,OAAO,UAC1B,OAAO,MAAM,MAAM,OAAO,aAAa,MAAM,eAChD,SACC,MACA,iBACA,OAAO,OAAO,KAAK,SACnB;WAAkB,MAAM;;WAEf,MAAM;WACN,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA4BP,QAAQ,cACpB,+BAEM,2BACA,gBAAgB,6BAChB,eAAe,OAAO,OAAO,QAEnC,MAAM,MACN,SAAS,Q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