@classytic/access 0.1.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.
- package/CHANGELOG.md +22 -0
- package/README.md +140 -0
- package/dist/domain/access-decision.d.mts +66 -0
- package/dist/domain/access-decision.mjs +47 -0
- package/dist/event-catalog-BGFNnlNJ.d.mts +137 -0
- package/dist/event-catalog-BVD_rFlc.mjs +111 -0
- package/dist/events/event-catalog.d.mts +2 -0
- package/dist/events/event-catalog.mjs +2 -0
- package/dist/index.d.mts +362 -0
- package/dist/index.mjs +854 -0
- package/dist/schemas/common.d.mts +19 -0
- package/dist/schemas/common.mjs +18 -0
- package/dist/schemas/entitlement.d.mts +56 -0
- package/dist/schemas/entitlement.mjs +44 -0
- package/package.json +87 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/schemas/common.d.ts
|
|
4
|
+
/** Polymorphic ref (subject / resource / origin id). */
|
|
5
|
+
declare const refSchema: z.ZodString;
|
|
6
|
+
/** A model/type tag — a letter-led identifier ('user', 'course', 'Order'). */
|
|
7
|
+
declare const modelSchema: z.ZodString;
|
|
8
|
+
declare const scopeKeySchema: z.ZodString;
|
|
9
|
+
declare const metadataSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
10
|
+
declare const sourceSchema: z.ZodEnum<{
|
|
11
|
+
purchase: "purchase";
|
|
12
|
+
subscription: "subscription";
|
|
13
|
+
trial: "trial";
|
|
14
|
+
grant: "grant";
|
|
15
|
+
manual: "manual";
|
|
16
|
+
system: "system";
|
|
17
|
+
}>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { metadataSchema, modelSchema, refSchema, scopeKeySchema, sourceSchema };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
//#region src/schemas/common.ts
|
|
3
|
+
/** Polymorphic ref (subject / resource / origin id). */
|
|
4
|
+
const refSchema = z.string().min(1).max(256);
|
|
5
|
+
/** A model/type tag — a letter-led identifier ('user', 'course', 'Order'). */
|
|
6
|
+
const modelSchema = z.string().min(1).max(100).regex(/^[A-Za-z][A-Za-z0-9_-]*$/, "must start with a letter and contain only alphanumerics, _ or -");
|
|
7
|
+
const scopeKeySchema = z.string().max(256);
|
|
8
|
+
const metadataSchema = z.record(z.string(), z.unknown());
|
|
9
|
+
const sourceSchema = z.enum([
|
|
10
|
+
"purchase",
|
|
11
|
+
"subscription",
|
|
12
|
+
"trial",
|
|
13
|
+
"grant",
|
|
14
|
+
"manual",
|
|
15
|
+
"system"
|
|
16
|
+
]);
|
|
17
|
+
//#endregion
|
|
18
|
+
export { metadataSchema, modelSchema, refSchema, scopeKeySchema, sourceSchema };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/schemas/entitlement.d.ts
|
|
4
|
+
/** POST /access/entitlements — grant access. */
|
|
5
|
+
declare const grantSchema: z.ZodObject<{
|
|
6
|
+
subjectRef: z.ZodString;
|
|
7
|
+
subjectModel: z.ZodString;
|
|
8
|
+
subjectEmail: z.ZodOptional<z.ZodString>;
|
|
9
|
+
resourceRef: z.ZodString;
|
|
10
|
+
resourceType: z.ZodString;
|
|
11
|
+
scopeKey: z.ZodOptional<z.ZodString>;
|
|
12
|
+
source: z.ZodEnum<{
|
|
13
|
+
purchase: "purchase";
|
|
14
|
+
subscription: "subscription";
|
|
15
|
+
trial: "trial";
|
|
16
|
+
grant: "grant";
|
|
17
|
+
manual: "manual";
|
|
18
|
+
system: "system";
|
|
19
|
+
}>;
|
|
20
|
+
notBefore: z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>;
|
|
21
|
+
accessExpiresAt: z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>;
|
|
22
|
+
usageLimit: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
23
|
+
originRef: z.ZodOptional<z.ZodString>;
|
|
24
|
+
originModel: z.ZodOptional<z.ZodString>;
|
|
25
|
+
amount: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
|
|
26
|
+
currency: z.ZodOptional<z.ZodString>;
|
|
27
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
28
|
+
renew: z.ZodOptional<z.ZodBoolean>;
|
|
29
|
+
}, z.core.$strip>;
|
|
30
|
+
/** POST /access/check — read-only gate. */
|
|
31
|
+
declare const checkSchema: z.ZodObject<{
|
|
32
|
+
subjectRef: z.ZodString;
|
|
33
|
+
resourceRef: z.ZodString;
|
|
34
|
+
resourceType: z.ZodOptional<z.ZodString>;
|
|
35
|
+
scopeKey: z.ZodOptional<z.ZodString>;
|
|
36
|
+
}, z.core.$strip>;
|
|
37
|
+
/** POST /access/entitlements/:id/consume — spend metered units. */
|
|
38
|
+
declare const consumeSchema: z.ZodObject<{
|
|
39
|
+
amount: z.ZodDefault<z.ZodNumber>;
|
|
40
|
+
}, z.core.$strip>;
|
|
41
|
+
/** POST /access/entitlements/:id/extend — renew the window. */
|
|
42
|
+
declare const extendSchema: z.ZodObject<{
|
|
43
|
+
accessExpiresAt: z.ZodOptional<z.ZodNullable<z.ZodCoercedDate<unknown>>>;
|
|
44
|
+
addMs: z.ZodOptional<z.ZodNumber>;
|
|
45
|
+
}, z.core.$strip>;
|
|
46
|
+
/** POST /access/entitlements/:id/{suspend,revoke,refund} */
|
|
47
|
+
declare const reasonSchema: z.ZodObject<{
|
|
48
|
+
reason: z.ZodOptional<z.ZodString>;
|
|
49
|
+
}, z.core.$strip>;
|
|
50
|
+
type GrantBody = z.infer<typeof grantSchema>;
|
|
51
|
+
type CheckBody = z.infer<typeof checkSchema>;
|
|
52
|
+
type ConsumeBody = z.infer<typeof consumeSchema>;
|
|
53
|
+
type ExtendBody = z.infer<typeof extendSchema>;
|
|
54
|
+
type ReasonBody = z.infer<typeof reasonSchema>;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { CheckBody, ConsumeBody, ExtendBody, GrantBody, ReasonBody, checkSchema, consumeSchema, extendSchema, grantSchema, reasonSchema };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { metadataSchema, modelSchema, refSchema, scopeKeySchema, sourceSchema } from "./common.mjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
//#region src/schemas/entitlement.ts
|
|
4
|
+
/**
|
|
5
|
+
* Zod validators — `@classytic/access/schemas/entitlement`. Route bodies use
|
|
6
|
+
* `z.coerce.date()` (rule 25) so ISO strings auto-convert to `Date`.
|
|
7
|
+
*/
|
|
8
|
+
/** POST /access/entitlements — grant access. */
|
|
9
|
+
const grantSchema = z.object({
|
|
10
|
+
subjectRef: refSchema,
|
|
11
|
+
subjectModel: modelSchema,
|
|
12
|
+
subjectEmail: z.string().email().optional(),
|
|
13
|
+
resourceRef: refSchema,
|
|
14
|
+
resourceType: modelSchema,
|
|
15
|
+
scopeKey: scopeKeySchema.optional(),
|
|
16
|
+
source: sourceSchema,
|
|
17
|
+
notBefore: z.coerce.date().nullable().optional(),
|
|
18
|
+
accessExpiresAt: z.coerce.date().nullable().optional(),
|
|
19
|
+
usageLimit: z.number().int().min(0).nullable().optional(),
|
|
20
|
+
originRef: refSchema.optional(),
|
|
21
|
+
originModel: modelSchema.optional(),
|
|
22
|
+
amount: z.number().int().nullable().optional(),
|
|
23
|
+
currency: z.string().length(3).optional(),
|
|
24
|
+
metadata: metadataSchema.optional(),
|
|
25
|
+
renew: z.boolean().optional()
|
|
26
|
+
});
|
|
27
|
+
/** POST /access/check — read-only gate. */
|
|
28
|
+
const checkSchema = z.object({
|
|
29
|
+
subjectRef: refSchema,
|
|
30
|
+
resourceRef: refSchema,
|
|
31
|
+
resourceType: modelSchema.optional(),
|
|
32
|
+
scopeKey: scopeKeySchema.optional()
|
|
33
|
+
});
|
|
34
|
+
/** POST /access/entitlements/:id/consume — spend metered units. */
|
|
35
|
+
const consumeSchema = z.object({ amount: z.number().int().min(1).default(1) });
|
|
36
|
+
/** POST /access/entitlements/:id/extend — renew the window. */
|
|
37
|
+
const extendSchema = z.object({
|
|
38
|
+
accessExpiresAt: z.coerce.date().nullable().optional(),
|
|
39
|
+
addMs: z.number().int().min(1).optional()
|
|
40
|
+
}).refine((v) => v.accessExpiresAt !== void 0 || v.addMs !== void 0, { message: "provide accessExpiresAt or addMs" });
|
|
41
|
+
/** POST /access/entitlements/:id/{suspend,revoke,refund} */
|
|
42
|
+
const reasonSchema = z.object({ reason: z.string().max(500).optional() });
|
|
43
|
+
//#endregion
|
|
44
|
+
export { checkSchema, consumeSchema, extendSchema, grantSchema, reasonSchema };
|
package/package.json
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@classytic/access",
|
|
3
|
+
"version": "0.1.0",
|
|
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
|
+
"author": "Classytic",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=22"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"default": "./dist/index.mjs"
|
|
19
|
+
},
|
|
20
|
+
"./decision": {
|
|
21
|
+
"types": "./dist/domain/access-decision.d.mts",
|
|
22
|
+
"default": "./dist/domain/access-decision.mjs"
|
|
23
|
+
},
|
|
24
|
+
"./events": {
|
|
25
|
+
"types": "./dist/events/event-catalog.d.mts",
|
|
26
|
+
"default": "./dist/events/event-catalog.mjs"
|
|
27
|
+
},
|
|
28
|
+
"./schemas/*": {
|
|
29
|
+
"types": "./dist/schemas/*.d.mts",
|
|
30
|
+
"default": "./dist/schemas/*.mjs"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"main": "./dist/index.mjs",
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"CHANGELOG.md"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsdown",
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"test": "vitest run --project unit --project integration --project scenarios",
|
|
44
|
+
"test:unit": "vitest run --project unit",
|
|
45
|
+
"test:integration": "vitest run --project integration",
|
|
46
|
+
"test:scenarios": "vitest run --project scenarios",
|
|
47
|
+
"test:watch": "vitest --project unit --project integration",
|
|
48
|
+
"test:all": "vitest run",
|
|
49
|
+
"prepublishOnly": "npm run typecheck && npm run build",
|
|
50
|
+
"push": "classytic-push"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@classytic/mongokit": ">=3.16.0",
|
|
54
|
+
"@classytic/primitives": ">=0.7.1",
|
|
55
|
+
"@classytic/repo-core": ">=0.6.0",
|
|
56
|
+
"mongoose": ">=9.4.1",
|
|
57
|
+
"zod": ">=4.0.0"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@classytic/dev-tools": "^0.2.0",
|
|
61
|
+
"@classytic/mongokit": "^3.16.1",
|
|
62
|
+
"@classytic/primitives": "^0.7.2",
|
|
63
|
+
"@classytic/repo-core": "^0.6.0",
|
|
64
|
+
"@types/node": "^22.19.21",
|
|
65
|
+
"@vitest/coverage-v8": "^4.1.9",
|
|
66
|
+
"mongodb-memory-server": "^11.2.0",
|
|
67
|
+
"mongoose": "^9.4.1",
|
|
68
|
+
"tsdown": "^0.22.3",
|
|
69
|
+
"typescript": "^6.0.3",
|
|
70
|
+
"vitest": "^4.1.9",
|
|
71
|
+
"zod": "^4.3.6"
|
|
72
|
+
},
|
|
73
|
+
"keywords": [
|
|
74
|
+
"access",
|
|
75
|
+
"entitlement",
|
|
76
|
+
"authorization",
|
|
77
|
+
"grant",
|
|
78
|
+
"membership",
|
|
79
|
+
"subscription-access",
|
|
80
|
+
"download-access",
|
|
81
|
+
"api-access",
|
|
82
|
+
"feature-flag",
|
|
83
|
+
"session",
|
|
84
|
+
"mongokit",
|
|
85
|
+
"classytic"
|
|
86
|
+
]
|
|
87
|
+
}
|