@terpjs/contract 0.23.0 → 0.25.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/openapi.json CHANGED
@@ -1121,6 +1121,16 @@
1121
1121
  "title": "SubjectRefRead",
1122
1122
  "type": "object"
1123
1123
  },
1124
+ "TerpRole": {
1125
+ "description": "Every role name this app's modules declare, for the same reason.",
1126
+ "enum": [
1127
+ "admin",
1128
+ "editor",
1129
+ "viewer"
1130
+ ],
1131
+ "title": "TerpRole",
1132
+ "type": "string"
1133
+ },
1124
1134
  "UserAdminUpdate": {
1125
1135
  "additionalProperties": false,
1126
1136
  "description": "Admin edits a user's email / role rank (OCC ``version`` required).\n\nActivation state is changed through the dedicated ``deactivate`` /\n``reactivate`` actions, not this generic patch.",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terpjs/contract",
3
- "version": "0.23.0",
3
+ "version": "0.25.0",
4
4
  "type": "module",
5
5
  "description": "Terp frontend contract \u2014 the OpenAPI-generated TypeScript client, design tokens, and the stack-agnostic module/route/nav + auth types.",
6
6
  "exports": {
package/src/index.ts CHANGED
@@ -7,5 +7,15 @@ export type { components, operations, paths } from "./schema";
7
7
  export { defineModuleManifest } from "./manifest";
8
8
  export { ICON_NAMES } from "./icons";
9
9
  export type { IconName } from "./icons";
10
- export type { ModuleManifest, ModuleRoute, NavGroup, NavItem, RoleName, UiText } from "./manifest";
10
+ export type {
11
+ AccessName,
12
+ ModuleManifest,
13
+ ModuleRoute,
14
+ NavGroup,
15
+ NavItem,
16
+ TerpPermissionName,
17
+ RoleName,
18
+ TerpAccessVocabulary,
19
+ UiText,
20
+ } from "./manifest";
11
21
  export type { AccessToken, Action, AuthSession, Credentials, CurrentUser } from "./auth";
package/src/manifest.ts CHANGED
@@ -12,7 +12,68 @@
12
12
  /** A role name as understood by the app's backend (e.g. "viewer" | "editor" | "admin"). */
13
13
  import type { IconName } from "./icons";
14
14
 
15
- export type RoleName = string;
15
+ /**
16
+ * The app's own access vocabulary, supplied by declaration merging.
17
+ *
18
+ * Empty here on purpose. `@terpjs/contract` is stack- and app-agnostic, so it cannot
19
+ * know what an app's roles and permissions are called — but the app does, because
20
+ * `terp openapi` emits both as enums and the generated `schema.d.ts` turns them into
21
+ * string-literal unions. Six lines in the app hand those unions to this interface:
22
+ *
23
+ * ```ts
24
+ * // frontend/src/access.d.ts
25
+ * import type { components } from "./api/schema";
26
+ * declare module "@terpjs/contract" {
27
+ * interface TerpAccessVocabulary {
28
+ * permission: components["schemas"]["TerpPermission"];
29
+ * role: components["schemas"]["TerpRole"];
30
+ * }
31
+ * }
32
+ * ```
33
+ *
34
+ * They name TYPES rather than values, so unlike the permission literals they replace
35
+ * they cannot drift: regenerate the client and a renamed permission stops
36
+ * type-checking at every manifest and every `useHasPermission` call that used it.
37
+ *
38
+ * An app that declares neither key keeps `string`, which is what every app had before
39
+ * this existed — so adopting it is opt-in and skipping it costs nothing.
40
+ */
41
+ export interface TerpAccessVocabulary {}
42
+
43
+ /**
44
+ * The app's spelling of one access vocabulary, or `string` when it has not declared one.
45
+ *
46
+ * The `extends string` guard matters: an augmentation that supplies something other
47
+ * than a string union (a mistake, or a half-finished generated file) degrades to
48
+ * `string` rather than making every manifest in the app stop compiling for a reason
49
+ * that points at the wrong file.
50
+ */
51
+ export type AccessName<Key extends "permission" | "role"> =
52
+ Key extends keyof TerpAccessVocabulary
53
+ ? TerpAccessVocabulary[Key] extends string
54
+ ? TerpAccessVocabulary[Key]
55
+ : string
56
+ : string;
57
+
58
+ /** Minimum role required, narrowed to the app's own roles when it declares them. */
59
+ export type RoleName = AccessName<"role">;
60
+
61
+ /**
62
+ * A declared permission name, narrowed to the app's own when it declares them.
63
+ *
64
+ * A client gates a route, a nav entry or a control on a permission whose single source
65
+ * is the backend's declaration. Spelled as a bare string it fails in one of two silent
66
+ * directions when the backend renames or re-floors it: it over-gates, and a screen
67
+ * 403s for someone who may use it, or it under-gates, and a link renders while every
68
+ * request behind it fails. Only a hand-written end-to-end test catches either.
69
+ *
70
+ * NAMED `Terp`-first on purpose. `lib.dom.d.ts` declares a GLOBAL `PermissionName`
71
+ * (the browser's `"geolocation" | "notifications" | "push" | …`), so a file that uses
72
+ * the short name and forgets to import it does not fail — it silently type-checks
73
+ * against the browser's vocabulary instead, and the error it eventually produces names
74
+ * a union nobody in the app recognises. Writing this one found exactly that.
75
+ */
76
+ export type TerpPermissionName = AccessName<"permission">;
16
77
 
17
78
  /**
18
79
  * Stable user-interface copy: the source message plus the catalog id every target locale
@@ -52,7 +113,7 @@ export interface ModuleRoute {
52
113
  * unknown or misspelled names are simply absent from the grant list, and an app that mounts
53
114
  * no grant capability has an empty list, which correctly hides everything that names one.
54
115
  */
55
- permission?: string;
116
+ permission?: TerpPermissionName;
56
117
  /**
57
118
  * Query-string keys this route reads, e.g. `["status", "page"]`.
58
119
  *
@@ -104,7 +165,7 @@ export interface NavItem {
104
165
  * unknown or misspelled names are simply absent from the grant list, and an app that mounts
105
166
  * no grant capability has an empty list, which correctly hides everything that names one.
106
167
  */
107
- permission?: string;
168
+ permission?: TerpPermissionName;
108
169
  /**
109
170
  * Match the URL exactly rather than as a segment-aligned prefix.
110
171
  *
package/src/schema.d.ts CHANGED
@@ -906,6 +906,12 @@ export interface components {
906
906
  /** Name */
907
907
  name: string | null;
908
908
  };
909
+ /**
910
+ * TerpRole
911
+ * @description Every role name this app's modules declare, for the same reason.
912
+ * @enum {string}
913
+ */
914
+ TerpRole: "admin" | "editor" | "viewer";
909
915
  /**
910
916
  * UserAdminUpdate
911
917
  * @description Admin edits a user's email / role rank (OCC ``version`` required).