@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,84 +0,0 @@
1
- // A `load` function of a requirement is kept in an annotation, so its error
2
- // type and its service type are erased to `unknown`. The language service
3
- // check for `unknown` in an Effect is off here.
4
- // @effect-diagnostics anyUnknownInErrorContext:off
5
- import * as Context from "effect/Context"
6
- import * as Effect from "effect/Effect"
7
- import * as Layer from "effect/Layer"
8
- import * as Option from "effect/Option"
9
- import { HttpRouter } from "effect/http"
10
- import type * as Authorizer from "../Authorizer.ts"
11
- import type * as HttpApi from "../HttpApi.ts"
12
- import { GroupRequired, Required } from "../HttpApi.ts"
13
-
14
- /** @internal */
15
- export const requires = (
16
- _authorizer: unknown,
17
- action: string,
18
- kind: string,
19
- load?: HttpApi.Requirement["load"],
20
- ): HttpApi.Requirement =>
21
- Object.freeze(load === undefined ? { action, kind } : { action, kind, load })
22
-
23
- /** @internal */
24
- export const layer = (
25
- middleware: Context.ServiceClass<any, string, any>,
26
- authorizer: Authorizer.AnyServiceClass,
27
- ) =>
28
- Layer.effect(
29
- middleware,
30
- Effect.gen(function* () {
31
- const service = yield* authorizer
32
- const services = yield* Effect.context<never>()
33
- return Effect.fnUntraced(function* (
34
- httpEffect: Effect.Effect<unknown, unknown, unknown>,
35
- options: {
36
- readonly endpoint: {
37
- readonly name: string
38
- readonly method: string
39
- readonly annotations: Context.Context<never>
40
- }
41
- readonly group: {
42
- readonly identifier: string
43
- readonly annotations: Context.Context<never>
44
- }
45
- },
46
- ) {
47
- const requirements = Option.orElse(
48
- Context.getOption(options.endpoint.annotations, Required),
49
- () =>
50
- Option.map(
51
- Context.getOption(options.group.annotations, GroupRequired),
52
- (rules) =>
53
- options.endpoint.method === "GET" ? rules.read : rules.write,
54
- ),
55
- )
56
- if (Option.isNone(requirements)) {
57
- return yield* Effect.die(
58
- new Error(
59
- `The endpoint ${options.group.identifier}.${options.endpoint.name} does not say what it needs. Give it the HttpApi.Required annotation.`,
60
- ),
61
- )
62
- }
63
- for (const requirement of requirements.value) {
64
- if (requirement.load === undefined) {
65
- yield* service.check({
66
- action: requirement.action,
67
- kind: requirement.kind,
68
- } as never)
69
- } else {
70
- const params = yield* HttpRouter.params
71
- const value = yield* Effect.provideContext(
72
- requirement.load(params),
73
- services,
74
- )
75
- yield* service.check({
76
- action: requirement.action,
77
- resource: value,
78
- } as never)
79
- }
80
- }
81
- return yield* httpEffect
82
- })
83
- }),
84
- )