@classytic/access 0.2.0 → 0.4.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.
@@ -25,27 +25,33 @@ declare const grantSchema: z.ZodObject<{
25
25
  currency: z.ZodOptional<z.ZodString>;
26
26
  metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
27
27
  renew: z.ZodOptional<z.ZodBoolean>;
28
- }, z.core.$strip>;
29
- /** POST /access/check — read-only gate. */
28
+ quotaMode: z.ZodOptional<z.ZodEnum<{
29
+ reset: "reset";
30
+ add: "add";
31
+ }>>;
32
+ }, z.core.$strict>;
33
+ /** GET /access/entitlements/check — the COMPLETE identity tuple; unknown keys refused. */
30
34
  declare const checkSchema: z.ZodObject<{
31
35
  subjectRef: z.ZodString;
36
+ subjectModel: z.ZodString;
32
37
  resourceRef: z.ZodString;
33
- resourceType: z.ZodOptional<z.ZodString>;
38
+ resourceType: z.ZodString;
34
39
  scopeKey: z.ZodOptional<z.ZodString>;
35
- }, z.core.$strip>;
36
- /** POST /access/entitlements/:id/consume — spend metered units. */
40
+ }, z.core.$strict>;
41
+ /** POST /access/entitlements/:id/action consume — spend metered units under a key. */
37
42
  declare const consumeSchema: z.ZodObject<{
43
+ key: z.ZodString;
38
44
  amount: z.ZodDefault<z.ZodNumber>;
39
- }, z.core.$strip>;
40
- /** POST /access/entitlements/:id/extend — renew the window. */
45
+ }, z.core.$strict>;
46
+ /** POST /access/entitlements/:id/action extend — exactly one of the two. */
41
47
  declare const extendSchema: z.ZodObject<{
42
48
  accessExpiresAt: z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>;
43
49
  addMs: z.ZodOptional<z.ZodNumber>;
44
- }, z.core.$strip>;
45
- /** POST /access/entitlements/:id/{suspend,revoke,refund} */
50
+ }, z.core.$strict>;
51
+ /** POST /access/entitlements/:id/action {suspend,revoke,refund} */
46
52
  declare const reasonSchema: z.ZodObject<{
47
53
  reason: z.ZodOptional<z.ZodString>;
48
- }, z.core.$strip>;
54
+ }, z.core.$strict>;
49
55
  type GrantBody = z.infer<typeof grantSchema>;
50
56
  type CheckBody = z.infer<typeof checkSchema>;
51
57
  type ConsumeBody = z.infer<typeof consumeSchema>;
@@ -3,11 +3,11 @@ import { CURRENCY_PATTERN } from "@classytic/primitives/currency";
3
3
  import { z } from "zod";
4
4
  //#region src/schemas/entitlement.ts
5
5
  /**
6
- * Zod validators — `@classytic/access/schemas/entitlement`. Route bodies use
7
- * `z.coerce.date()` (rule 25) so ISO strings auto-convert to `Date`.
6
+ * Wire validators — `@classytic/access/schemas/entitlement`. Strict: an unknown key is refused,
7
+ * never stripped. Dates are `z.coerce.date()` so ISO strings arrive as `Date`.
8
8
  */
9
9
  /** POST /access/entitlements — grant access. */
10
- const grantSchema = z.object({
10
+ const grantSchema = z.strictObject({
11
11
  subjectRef: refSchema,
12
12
  subjectModel: modelSchema,
13
13
  subjectEmail: z.string().email().optional(),
@@ -23,23 +23,28 @@ const grantSchema = z.object({
23
23
  amount: z.number().int().nullable().optional(),
24
24
  currency: z.string().regex(CURRENCY_PATTERN, "ISO 4217 (3 uppercase letters)").optional(),
25
25
  metadata: metadataSchema.optional(),
26
- renew: z.boolean().optional()
26
+ renew: z.boolean().optional(),
27
+ quotaMode: z.enum(["reset", "add"]).optional()
27
28
  });
28
- /** POST /access/check — read-only gate. */
29
- const checkSchema = z.object({
29
+ /** GET /access/entitlements/check — the COMPLETE identity tuple; unknown keys refused. */
30
+ const checkSchema = z.strictObject({
30
31
  subjectRef: refSchema,
32
+ subjectModel: modelSchema,
31
33
  resourceRef: refSchema,
32
- resourceType: modelSchema.optional(),
34
+ resourceType: modelSchema,
33
35
  scopeKey: scopeKeySchema.optional()
34
36
  });
35
- /** POST /access/entitlements/:id/consume — spend metered units. */
36
- const consumeSchema = z.object({ amount: z.number().int().min(1).default(1) });
37
- /** POST /access/entitlements/:id/extend — renew the window. */
38
- const extendSchema = z.object({
37
+ /** POST /access/entitlements/:id/action consume — spend metered units under a key. */
38
+ const consumeSchema = z.strictObject({
39
+ key: z.string().trim().min(1).max(256),
40
+ amount: z.number().int().min(1).default(1)
41
+ });
42
+ /** POST /access/entitlements/:id/action extend — exactly one of the two. */
43
+ const extendSchema = z.strictObject({
39
44
  accessExpiresAt: z.coerce.date().nullable().optional(),
40
45
  addMs: z.number().int().min(1).optional()
41
- }).refine((v) => v.accessExpiresAt !== void 0 || v.addMs !== void 0, { message: "provide accessExpiresAt or addMs" });
42
- /** POST /access/entitlements/:id/{suspend,revoke,refund} */
43
- const reasonSchema = z.object({ reason: z.string().max(500).optional() });
46
+ }).refine((v) => v.accessExpiresAt !== void 0 !== (v.addMs !== void 0), { message: "provide exactly one of accessExpiresAt or addMs" });
47
+ /** POST /access/entitlements/:id/action {suspend,revoke,refund} */
48
+ const reasonSchema = z.strictObject({ reason: z.string().max(500).optional() });
44
49
  //#endregion
45
50
  export { checkSchema, consumeSchema, extendSchema, grantSchema, reasonSchema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classytic/access",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Access ENTITLEMENT engine - time-bound (or lifetime) grants of access to any resource: course enrollment, file download, API access, membership, feature flag, any service. Like a session, but durable: the grant is a permanent record, the RIGHT to access is COMPUTED (never TTL-deleted). Single responsibility: it answers 'does this subject currently hold a live grant to this resource', NOT the purchase (@classytic/order), the money (@classytic/revenue), or the catalog item (@classytic/catalog). Powered by @classytic/mongokit.",
5
5
  "author": "Classytic",
6
6
  "license": "MIT",
@@ -14,18 +14,22 @@
14
14
  },
15
15
  "exports": {
16
16
  ".": {
17
+ "@classytic/source": "./src/index.ts",
17
18
  "types": "./dist/index.d.mts",
18
19
  "default": "./dist/index.mjs"
19
20
  },
20
21
  "./decision": {
22
+ "@classytic/source": "./src/domain/access-decision.ts",
21
23
  "types": "./dist/domain/access-decision.d.mts",
22
24
  "default": "./dist/domain/access-decision.mjs"
23
25
  },
24
26
  "./events": {
27
+ "@classytic/source": "./src/events/event-catalog.ts",
25
28
  "types": "./dist/events/event-catalog.d.mts",
26
29
  "default": "./dist/events/event-catalog.mjs"
27
30
  },
28
31
  "./schemas/*": {
32
+ "@classytic/source": "./src/schemas/*.ts",
29
33
  "types": "./dist/schemas/*.d.mts",
30
34
  "default": "./dist/schemas/*.mjs"
31
35
  }
@@ -50,21 +54,21 @@
50
54
  "push": "classytic-push"
51
55
  },
52
56
  "peerDependencies": {
53
- "@classytic/mongokit": ">=3.33.0",
54
- "@classytic/primitives": ">=0.23.0",
55
- "@classytic/repo-core": ">=0.24.0",
56
- "mongoose": ">=9.4.1",
57
+ "@classytic/mongokit": ">=3.41.0",
58
+ "@classytic/primitives": ">=0.31.1",
59
+ "@classytic/repo-core": ">=0.31.0",
60
+ "mongoose": ">=9.11.0",
57
61
  "zod": ">=4.0.0"
58
62
  },
59
63
  "devDependencies": {
60
64
  "@classytic/dev-tools": "^0.2.0",
61
- "@classytic/mongokit": "^3.33.0",
62
- "@classytic/primitives": ">=0.23.0",
63
- "@classytic/repo-core": ">=0.24.0",
65
+ "@classytic/mongokit": "^3.41.0",
66
+ "@classytic/primitives": ">=0.31.1",
67
+ "@classytic/repo-core": ">=0.31.0",
64
68
  "@types/node": "^22.19.21",
65
69
  "@vitest/coverage-v8": "^4.1.9",
66
70
  "mongodb-memory-server": "^11.2.0",
67
- "mongoose": "^9.4.1",
71
+ "mongoose": "^9.11.1",
68
72
  "publint": "^0.3.23",
69
73
  "tsdown": "^0.22.14",
70
74
  "typescript": "^6.0.3",
@@ -1,111 +0,0 @@
1
- import { createEvent } from "@classytic/primitives/events";
2
- import { z } from "zod";
3
- //#region src/events/event-constants.ts
4
- /**
5
- * ACCESS_EVENTS — domain event constants. Prefix `access:` per
6
- * PACKAGE_RULES §12. Hosts subscribe glob-style so typos are compile errors.
7
- */
8
- const ACCESS_EVENTS = {
9
- GRANTED: "access:granted",
10
- EXTENDED: "access:extended",
11
- SUSPENDED: "access:suspended",
12
- RESUMED: "access:resumed",
13
- REVOKED: "access:revoked",
14
- EXPIRED: "access:expired",
15
- EXHAUSTED: "access:exhausted",
16
- REFUNDED: "access:refunded"
17
- };
18
- //#endregion
19
- //#region src/events/event-catalog.ts
20
- /**
21
- * Zod-source event catalog (PACKAGE_RULES rule 18). One Zod schema per event
22
- * is the source of truth; hosts wire `accessEventDefinitions` into arc's
23
- * `EventRegistry`. Timestamps are `z.iso.datetime()` (rule 25).
24
- *
25
- * Every access event carries the same entitlement snapshot — a host subscriber
26
- * reacts to the lifecycle without re-fetching (grant a perk on `access:granted`,
27
- * email a "renew" nudge on `access:expired`, refill credits on `access:extended`).
28
- */
29
- function defineAccessEvent(input) {
30
- const { name, description, version = 1, zodSchema } = input;
31
- return {
32
- name,
33
- version,
34
- description,
35
- zodSchema,
36
- schema: z.toJSONSchema(zodSchema),
37
- create(payload, meta) {
38
- return createEvent(name, payload, {
39
- resource: "entitlement",
40
- source: "access",
41
- ...meta
42
- });
43
- }
44
- };
45
- }
46
- /** The entitlement snapshot every access event carries. */
47
- const entitlementEventSchema = z.object({
48
- entitlementId: z.string(),
49
- organizationId: z.string().optional(),
50
- subjectRef: z.string(),
51
- subjectModel: z.string(),
52
- resourceRef: z.string(),
53
- resourceType: z.string(),
54
- scopeKey: z.string(),
55
- status: z.string(),
56
- accessExpiresAt: z.iso.datetime().optional(),
57
- usageLimit: z.number().optional(),
58
- usageUsed: z.number().optional()
59
- });
60
- const Granted = defineAccessEvent({
61
- name: ACCESS_EVENTS.GRANTED,
62
- description: "A subject was granted access to a resource for a window (or lifetime).",
63
- zodSchema: entitlementEventSchema
64
- });
65
- const Extended = defineAccessEvent({
66
- name: ACCESS_EVENTS.EXTENDED,
67
- description: "An access window was pushed out / quota refreshed (a renewal or top-up).",
68
- zodSchema: entitlementEventSchema
69
- });
70
- const Suspended = defineAccessEvent({
71
- name: ACCESS_EVENTS.SUSPENDED,
72
- description: "Access was paused without ending it (recoverable via access:resumed).",
73
- zodSchema: entitlementEventSchema
74
- });
75
- const Resumed = defineAccessEvent({
76
- name: ACCESS_EVENTS.RESUMED,
77
- description: "A suspended grant was reactivated.",
78
- zodSchema: entitlementEventSchema
79
- });
80
- const Revoked = defineAccessEvent({
81
- name: ACCESS_EVENTS.REVOKED,
82
- description: "Access was pulled permanently (admin action / dispute). Window cut to now.",
83
- zodSchema: entitlementEventSchema
84
- });
85
- const Expired = defineAccessEvent({
86
- name: ACCESS_EVENTS.EXPIRED,
87
- description: "The access window lapsed and the sweeper marked the grant expired (renewable).",
88
- zodSchema: entitlementEventSchema
89
- });
90
- const Exhausted = defineAccessEvent({
91
- name: ACCESS_EVENTS.EXHAUSTED,
92
- description: "A metered grant reached its quota and is consumed (downloads/API calls spent).",
93
- zodSchema: entitlementEventSchema
94
- });
95
- const Refunded = defineAccessEvent({
96
- name: ACCESS_EVENTS.REFUNDED,
97
- description: "A grant was refunded (payment reversed). Access cut to now.",
98
- zodSchema: entitlementEventSchema
99
- });
100
- const accessEventDefinitions = [
101
- Granted,
102
- Extended,
103
- Suspended,
104
- Resumed,
105
- Revoked,
106
- Expired,
107
- Exhausted,
108
- Refunded
109
- ];
110
- //#endregion
111
- export { Refunded as a, Suspended as c, Granted as i, accessEventDefinitions as l, Expired as n, Resumed as o, Extended as r, Revoked as s, Exhausted as t, ACCESS_EVENTS as u };
@@ -1,136 +0,0 @@
1
- import { DomainEvent, EventMeta } from "@classytic/primitives/events";
2
- import { z } from "zod";
3
- //#region src/events/event-constants.d.ts
4
- /**
5
- * ACCESS_EVENTS — domain event constants. Prefix `access:` per
6
- * PACKAGE_RULES §12. Hosts subscribe glob-style so typos are compile errors.
7
- */
8
- declare const ACCESS_EVENTS: {
9
- readonly GRANTED: "access:granted";
10
- readonly EXTENDED: "access:extended";
11
- readonly SUSPENDED: "access:suspended";
12
- readonly RESUMED: "access:resumed";
13
- readonly REVOKED: "access:revoked";
14
- readonly EXPIRED: "access:expired";
15
- readonly EXHAUSTED: "access:exhausted";
16
- readonly REFUNDED: "access:refunded";
17
- };
18
- type AccessEventName = (typeof ACCESS_EVENTS)[keyof typeof ACCESS_EVENTS];
19
- //#endregion
20
- //#region src/events/event-catalog.d.ts
21
- type AccessEventSchema = Record<string, unknown>;
22
- interface AccessEventDefinition<TSchema extends z.ZodType = z.ZodType> {
23
- name: AccessEventName;
24
- version: number;
25
- description: string;
26
- zodSchema: TSchema;
27
- schema: AccessEventSchema;
28
- create(payload: z.infer<TSchema>, meta?: Partial<EventMeta>): DomainEvent<z.infer<TSchema>>;
29
- }
30
- declare const Granted: AccessEventDefinition<z.ZodObject<{
31
- entitlementId: z.ZodString;
32
- organizationId: z.ZodOptional<z.ZodString>;
33
- subjectRef: z.ZodString;
34
- subjectModel: z.ZodString;
35
- resourceRef: z.ZodString;
36
- resourceType: z.ZodString;
37
- scopeKey: z.ZodString;
38
- status: z.ZodString;
39
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
40
- usageLimit: z.ZodOptional<z.ZodNumber>;
41
- usageUsed: z.ZodOptional<z.ZodNumber>;
42
- }, z.core.$strip>>;
43
- declare const Extended: AccessEventDefinition<z.ZodObject<{
44
- entitlementId: z.ZodString;
45
- organizationId: z.ZodOptional<z.ZodString>;
46
- subjectRef: z.ZodString;
47
- subjectModel: z.ZodString;
48
- resourceRef: z.ZodString;
49
- resourceType: z.ZodString;
50
- scopeKey: z.ZodString;
51
- status: z.ZodString;
52
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
53
- usageLimit: z.ZodOptional<z.ZodNumber>;
54
- usageUsed: z.ZodOptional<z.ZodNumber>;
55
- }, z.core.$strip>>;
56
- declare const Suspended: AccessEventDefinition<z.ZodObject<{
57
- entitlementId: z.ZodString;
58
- organizationId: z.ZodOptional<z.ZodString>;
59
- subjectRef: z.ZodString;
60
- subjectModel: z.ZodString;
61
- resourceRef: z.ZodString;
62
- resourceType: z.ZodString;
63
- scopeKey: z.ZodString;
64
- status: z.ZodString;
65
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
66
- usageLimit: z.ZodOptional<z.ZodNumber>;
67
- usageUsed: z.ZodOptional<z.ZodNumber>;
68
- }, z.core.$strip>>;
69
- declare const Resumed: AccessEventDefinition<z.ZodObject<{
70
- entitlementId: z.ZodString;
71
- organizationId: z.ZodOptional<z.ZodString>;
72
- subjectRef: z.ZodString;
73
- subjectModel: z.ZodString;
74
- resourceRef: z.ZodString;
75
- resourceType: z.ZodString;
76
- scopeKey: z.ZodString;
77
- status: z.ZodString;
78
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
79
- usageLimit: z.ZodOptional<z.ZodNumber>;
80
- usageUsed: z.ZodOptional<z.ZodNumber>;
81
- }, z.core.$strip>>;
82
- declare const Revoked: AccessEventDefinition<z.ZodObject<{
83
- entitlementId: z.ZodString;
84
- organizationId: z.ZodOptional<z.ZodString>;
85
- subjectRef: z.ZodString;
86
- subjectModel: z.ZodString;
87
- resourceRef: z.ZodString;
88
- resourceType: z.ZodString;
89
- scopeKey: z.ZodString;
90
- status: z.ZodString;
91
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
92
- usageLimit: z.ZodOptional<z.ZodNumber>;
93
- usageUsed: z.ZodOptional<z.ZodNumber>;
94
- }, z.core.$strip>>;
95
- declare const Expired: AccessEventDefinition<z.ZodObject<{
96
- entitlementId: z.ZodString;
97
- organizationId: z.ZodOptional<z.ZodString>;
98
- subjectRef: z.ZodString;
99
- subjectModel: z.ZodString;
100
- resourceRef: z.ZodString;
101
- resourceType: z.ZodString;
102
- scopeKey: z.ZodString;
103
- status: z.ZodString;
104
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
105
- usageLimit: z.ZodOptional<z.ZodNumber>;
106
- usageUsed: z.ZodOptional<z.ZodNumber>;
107
- }, z.core.$strip>>;
108
- declare const Exhausted: AccessEventDefinition<z.ZodObject<{
109
- entitlementId: z.ZodString;
110
- organizationId: z.ZodOptional<z.ZodString>;
111
- subjectRef: z.ZodString;
112
- subjectModel: z.ZodString;
113
- resourceRef: z.ZodString;
114
- resourceType: z.ZodString;
115
- scopeKey: z.ZodString;
116
- status: z.ZodString;
117
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
118
- usageLimit: z.ZodOptional<z.ZodNumber>;
119
- usageUsed: z.ZodOptional<z.ZodNumber>;
120
- }, z.core.$strip>>;
121
- declare const Refunded: AccessEventDefinition<z.ZodObject<{
122
- entitlementId: z.ZodString;
123
- organizationId: z.ZodOptional<z.ZodString>;
124
- subjectRef: z.ZodString;
125
- subjectModel: z.ZodString;
126
- resourceRef: z.ZodString;
127
- resourceType: z.ZodString;
128
- scopeKey: z.ZodString;
129
- status: z.ZodString;
130
- accessExpiresAt: z.ZodOptional<z.ZodISODateTime>;
131
- usageLimit: z.ZodOptional<z.ZodNumber>;
132
- usageUsed: z.ZodOptional<z.ZodNumber>;
133
- }, z.core.$strip>>;
134
- declare const accessEventDefinitions: ReadonlyArray<AccessEventDefinition>;
135
- //#endregion
136
- export { Extended as a, Resumed as c, accessEventDefinitions as d, ACCESS_EVENTS as f, Expired as i, Revoked as l, AccessEventSchema as n, Granted as o, AccessEventName as p, Exhausted as r, Refunded as s, AccessEventDefinition as t, Suspended as u };