@proveanything/smartlinks 2.1.16 → 2.1.18

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 (60) hide show
  1. package/dist/adminViews.d.ts +85 -0
  2. package/dist/adminViews.js +259 -0
  3. package/dist/api/errors.d.ts +10 -0
  4. package/dist/api/errors.js +17 -0
  5. package/dist/api/index.d.ts +3 -0
  6. package/dist/api/index.js +3 -0
  7. package/dist/api/operations.d.ts +36 -0
  8. package/dist/api/operations.js +56 -0
  9. package/dist/api/order.d.ts +12 -1
  10. package/dist/api/order.js +15 -0
  11. package/dist/api/partner.d.ts +7 -1
  12. package/dist/api/partner.js +9 -0
  13. package/dist/api/userGroups.d.ts +78 -0
  14. package/dist/api/userGroups.js +124 -0
  15. package/dist/docs/API_SUMMARY.md +699 -46
  16. package/dist/docs/admin-views.md +125 -0
  17. package/dist/docs/app-manifest.md +2 -0
  18. package/dist/docs/app-objects.md +23 -0
  19. package/dist/docs/operations.md +281 -0
  20. package/dist/docs/overview.md +3 -0
  21. package/dist/docs/server-functions.md +106 -0
  22. package/dist/docs/tag-ranges.md +8 -0
  23. package/dist/docs/user-groups.md +168 -0
  24. package/dist/docs/utils.md +17 -0
  25. package/dist/http.js +4 -0
  26. package/dist/iframeResponder.d.ts +15 -0
  27. package/dist/iframeResponder.js +39 -1
  28. package/dist/index.d.ts +2 -0
  29. package/dist/index.js +2 -0
  30. package/dist/openapi.yaml +1426 -276
  31. package/dist/testing/index.d.ts +29 -0
  32. package/dist/testing/index.js +178 -1
  33. package/dist/types/appManifest.d.ts +170 -0
  34. package/dist/types/appObjects.d.ts +22 -1
  35. package/dist/types/claimSet.d.ts +2 -0
  36. package/dist/types/clientLog.d.ts +46 -0
  37. package/dist/types/clientLog.js +1 -0
  38. package/dist/types/containers.d.ts +8 -0
  39. package/dist/types/iframeResponder.d.ts +16 -1
  40. package/dist/types/index.d.ts +3 -0
  41. package/dist/types/index.js +3 -0
  42. package/dist/types/operations.d.ts +598 -0
  43. package/dist/types/operations.js +8 -0
  44. package/dist/types/order.d.ts +35 -6
  45. package/dist/types/userGroups.d.ts +103 -0
  46. package/dist/types/userGroups.js +5 -0
  47. package/dist/utils/conditions.d.ts +7 -0
  48. package/dist/utils/conditions.js +16 -9
  49. package/docs/API_SUMMARY.md +699 -46
  50. package/docs/admin-views.md +125 -0
  51. package/docs/app-manifest.md +2 -0
  52. package/docs/app-objects.md +23 -0
  53. package/docs/operations.md +281 -0
  54. package/docs/overview.md +3 -0
  55. package/docs/server-functions.md +106 -0
  56. package/docs/tag-ranges.md +8 -0
  57. package/docs/user-groups.md +168 -0
  58. package/docs/utils.md +17 -0
  59. package/openapi.yaml +1426 -276
  60. package/package.json +1 -1
@@ -0,0 +1,168 @@
1
+ # User groups
2
+
3
+ A **user group** is a named list of people in a collection: "QC Controllers", "Warehouse", "Approved
4
+ partners". Use them to say who may do what, for staff who aren't collection admins and for third
5
+ parties alike.
6
+
7
+ > There is no separate "roles" object. A role is a user group: "Sam has the QC role" and "Sam is in
8
+ > the QC Controllers user group" are the same fact.
9
+
10
+ ## The three things you do with them
11
+
12
+ | You want to… | Use |
13
+ |---|---|
14
+ | **Protect** an action, so only some people can do it | `userGroups` on the server function's declaration |
15
+ | **Decide** something finer inside a function | `ctx.caller.userGroups` |
16
+ | **Show or hide** a button or a screen | `userGroups.mine(collectionId)`, or a `group` condition |
17
+
18
+ Only the first two protect anything. Hiding a button doesn't stop someone calling the function behind
19
+ it, so limit the function as well.
20
+
21
+ ## A user group
22
+
23
+ ```ts
24
+ {
25
+ id: 'k3Jd…', // random, never changes
26
+ key: 'qc-controller', // the name code uses; set once, kept through a rename
27
+ name: 'QC Controllers', // what people see; free to change
28
+ description: 'Sign off finished batches',
29
+ members: ['uid_1', 'uid_2'] // user ids
30
+ }
31
+ ```
32
+
33
+ - **Refer to user groups by `key`** in code and manifests, never by name or id.
34
+ - A key is lowercase letters, digits and hyphens. It's made from the name when you don't give one
35
+ ("QC Controllers" → `qc-controllers`), and it's unique in the collection.
36
+ - Members are people who sign in: on the site's own login, or with a SmartLinks account.
37
+ - Collection admins pass every user group check. They don't need adding.
38
+
39
+ ## Limit a server function
40
+
41
+ In the manifest (see [server-functions.md](server-functions.md)):
42
+
43
+ ```jsonc
44
+ {
45
+ "name": "releaseBatch",
46
+ "trigger": { "type": "http", "methods": ["POST"] },
47
+ "visibility": "public",
48
+ "authority": "collection",
49
+ "elevated": true,
50
+ "userGroups": ["qc-controller", "manager"], // in at least ONE of these
51
+ "capabilities": ["sl:records:write"]
52
+ }
53
+ ```
54
+
55
+ The platform checks this before any of your code runs:
56
+
57
+ | Caller | Result |
58
+ |---|---|
59
+ | In one of the user groups, or a collection admin | The function runs |
60
+ | Signed in, in none of them | `403 { error: 'NOT_IN_USER_GROUP' }` |
61
+ | Not signed in | `401 { error: 'SIGN_IN_REQUIRED' }` |
62
+
63
+ - `http` functions only: `event` and `cron` functions have no caller.
64
+ - `userGroups` doesn't replace `elevated: true` on a `public` + `collection` function. Declare both.
65
+
66
+ ## Decide inside a function
67
+
68
+ Every function sees the caller's keys:
69
+
70
+ ```ts
71
+ export async function adjustQuantity(ctx, event) {
72
+ const isManager = ctx.caller.userGroups.includes('manager')
73
+ if (event.body.delta > 100 && !isManager) {
74
+ return new Response(JSON.stringify({ error: 'Over 100 needs a manager' }), { status: 403 })
75
+ }
76
+ // …
77
+ }
78
+ ```
79
+
80
+ `ctx.caller.userGroups` is `[]` when nobody is signed in, and for `event` / `cron` runs.
81
+
82
+ ## Show or hide in a screen
83
+
84
+ ```ts
85
+ import { userGroups } from '@proveanything/smartlinks'
86
+
87
+ const { userGroups: mine } = await userGroups.mine(collectionId)
88
+ const showReleaseButton = mine.includes('qc-controller')
89
+ ```
90
+
91
+ - `mine()` returns `{ userGroups: string[], userGroupIds: string[] }`: keys for code, and ids for
92
+ older saved conditions. Both are empty when nobody is signed in.
93
+ - It is never cached, so a change an admin makes shows on the next call.
94
+ - A person can only ask about themselves.
95
+
96
+ With conditions (see [utils.md](utils.md)):
97
+
98
+ ```ts
99
+ const { userGroups: keys, userGroupIds } = await userGroups.mine(collectionId)
100
+
101
+ await utils.validateCondition({
102
+ condition: { type: 'and', conditions: [{ type: 'user', userType: 'group', groupIds: ['qc-controller'] }] },
103
+ user: { valid: true, uid, groups: [...keys, ...userGroupIds] },
104
+ })
105
+ ```
106
+
107
+ ## Manage them (admin)
108
+
109
+ ```ts
110
+ const qc = await userGroups.create(collectionId, { name: 'QC Controllers' }) // key: 'qc-controllers'
111
+
112
+ await userGroups.addMember(collectionId, qc.id, { email: 'sam@example.com', name: 'Sam' })
113
+ await userGroups.addMember(collectionId, qc.id, { userId: 'uid_2' })
114
+ await userGroups.removeMember(collectionId, qc.id, 'uid_2')
115
+
116
+ await userGroups.members(collectionId, qc.id) // [{ uid, name, email }]
117
+ await userGroups.ofUser(collectionId, 'uid_1') // [{ id, key, name }]
118
+ await userGroups.list(collectionId)
119
+ await userGroups.update(collectionId, qc.id, { name: 'Quality control' }) // key stays 'qc-controllers'
120
+ await userGroups.remove(collectionId, qc.id)
121
+ ```
122
+
123
+ - **Add and remove one person at a time** with `addMember` / `removeMember`. Saving a whole `members`
124
+ list through `update` can overwrite a change someone else made at the same moment.
125
+ - Adding by email finds the person in the collection's logins, or adds them. No welcome email is sent.
126
+ - A change takes up to a minute to reach function checks.
127
+
128
+ Errors: `KEY_INVALID`, `KEY_TAKEN` (409), `KEY_FIXED` (409, a key can't be changed once set),
129
+ `MISSING_FIELDS`, `NOT_FOUND`.
130
+
131
+ ## Who changed what
132
+
133
+ Every change is recorded: a user group created, renamed or deleted, and each person added or removed,
134
+ with the admin who did it and when.
135
+
136
+ ```ts
137
+ await userGroups.history(collectionId, { userGroupId: qc.id }) // one user group
138
+ await userGroups.history(collectionId, { userId: 'uid_1' }) // everything about one person
139
+ // [{ at, type: 'usergroup.member-added', actorId: 'uid_admin', data: { key: 'qc-controllers', userId: 'uid_1', … } }]
140
+ ```
141
+
142
+ It covers the last 30 days unless you pass `from` / `to` (up to 400 days), and takes about a second.
143
+ Entries hold ids only, never names or emails.
144
+
145
+ ## In a site
146
+
147
+ It works the same way. A site's functions run in the site's own worker, and the site's function
148
+ runtime applies `userGroups` from `public/app.manifest.json` before your handler runs, with the same
149
+ `401` / `403` answers. `ctx.caller.userId` and `ctx.caller.userGroups` are filled in for a signed-in
150
+ visitor.
151
+
152
+ A site made before this existed has no `src/site/lib/fn-access.ts`. There, `userGroups` in the
153
+ manifest is ignored until the site is updated.
154
+
155
+ ## Testing
156
+
157
+ ```ts
158
+ import { createFunctionTestContext } from '@proveanything/smartlinks/testing'
159
+
160
+ const ctx = createFunctionTestContext({
161
+ def: { trigger: { type: 'http' }, capabilities: ['sl:records:write'] },
162
+ caller: { userId: 'sam', userGroups: ['qc-controller'] },
163
+ })
164
+ ctx.caller.userGroups // ['qc-controller']
165
+ ```
166
+
167
+ The test context gives your handler the caller you describe. It doesn't apply the declaration's
168
+ `userGroups` limit: that check happens on the platform before your handler is called.
@@ -479,6 +479,23 @@ await utils.validateCondition({
479
479
 
480
480
  User types: `'valid'`, `'invalid'`, `'owner'`, `'admin'`, `'group'`
481
481
 
482
+ `'group'` passes when the signed-in user is in at least one of the named [user groups](user-groups.md).
483
+ Name them in `groupIds` (keys or ids), and pass the user's own in `user.groups`:
484
+
485
+ ```typescript
486
+ const { userGroups: keys, userGroupIds } = await userGroups.mine(collectionId)
487
+
488
+ await utils.validateCondition({
489
+ condition: {
490
+ type: 'and',
491
+ conditions: [{ type: 'user', userType: 'group', groupIds: ['qc-controller'] }]
492
+ },
493
+ user: { valid: true, uid: 'user123', groups: [...keys, ...userGroupIds] }
494
+ })
495
+ ```
496
+
497
+ A condition only shows or hides. To protect an action, limit the server function behind it.
498
+
482
499
  #### Date & Time Conditions
483
500
  Show/hide content based on dates:
484
501
 
package/dist/http.js CHANGED
@@ -222,6 +222,10 @@ function shouldSkipCache(path) {
222
222
  // Tag ranges change as tags are programmed, and carry the tags' keys — never cached, never persisted.
223
223
  if (/\/claimSet\/[^/]+\/ranges(\/|\?|$)/i.test(path))
224
224
  return true;
225
+ // User groups say who may do what: a change (someone added or removed) must show at once, and
226
+ // `mine` is about one person.
227
+ if (/\/userGroups?(\/|\?|$)/i.test(path))
228
+ return true;
225
229
  return false;
226
230
  }
227
231
  /** Evict the oldest (LRU) entry when the cache is at capacity. */
@@ -39,6 +39,8 @@ export declare class IframeResponder {
39
39
  private uploads;
40
40
  private activeStreams;
41
41
  private isInitialLoad;
42
+ /** The app follows `smartlinks:navigate` (it said so with `smartlinks:navigable`). */
43
+ private navigable;
42
44
  private messageHandler;
43
45
  private resizeHandler;
44
46
  private appUrl;
@@ -50,6 +52,19 @@ export declare class IframeResponder {
50
52
  * Returns the src URL to set on the iframe.
51
53
  */
52
54
  attach(iframe: HTMLIFrameElement): Promise<string>;
55
+ /**
56
+ * Ask the app to open one of its admin views (or a hash path) without reloading. Returns false when
57
+ * the app hasn't said it follows navigate (an older build): reload the frame at the new path instead.
58
+ * See docs/admin-views.md.
59
+ */
60
+ navigate(req: {
61
+ view?: string;
62
+ path?: string;
63
+ params?: Record<string, string>;
64
+ highlight?: string;
65
+ }): boolean;
66
+ /** True once the app has said it follows `smartlinks:navigate`. */
67
+ get isNavigable(): boolean;
53
68
  /**
54
69
  * Update cached data (e.g., after user logs in).
55
70
  */
@@ -43,6 +43,8 @@ export class IframeResponder {
43
43
  this.uploads = new Map();
44
44
  this.activeStreams = new Map();
45
45
  this.isInitialLoad = true;
46
+ /** The app follows `smartlinks:navigate` (it said so with `smartlinks:navigable`). */
47
+ this.navigable = false;
46
48
  this.messageHandler = null;
47
49
  this.resizeHandler = null;
48
50
  this.appUrl = null;
@@ -89,6 +91,28 @@ export class IframeResponder {
89
91
  const src = this.buildIframeSrc();
90
92
  return src;
91
93
  }
94
+ /**
95
+ * Ask the app to open one of its admin views (or a hash path) without reloading. Returns false when
96
+ * the app hasn't said it follows navigate (an older build): reload the frame at the new path instead.
97
+ * See docs/admin-views.md.
98
+ */
99
+ navigate(req) {
100
+ var _a;
101
+ const win = (_a = this.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow;
102
+ if (!this.navigable || !win)
103
+ return false;
104
+ try {
105
+ win.postMessage(Object.assign({ type: 'smartlinks:navigate' }, req), '*');
106
+ return true;
107
+ }
108
+ catch (_b) {
109
+ return false;
110
+ }
111
+ }
112
+ /** True once the app has said it follows `smartlinks:navigate`. */
113
+ get isNavigable() {
114
+ return this.navigable;
115
+ }
92
116
  /**
93
117
  * Update cached data (e.g., after user logs in).
94
118
  */
@@ -356,7 +380,7 @@ export class IframeResponder {
356
380
  // Standard Iframe Messages
357
381
  // ===========================================================================
358
382
  async handleStandardMessage(data, event) {
359
- var _a, _b, _c, _d, _e, _f, _g;
383
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
360
384
  switch (data.type) {
361
385
  case 'smartlinks:resize': {
362
386
  const contentHeight = (_a = data.payload) === null || _a === void 0 ? void 0 : _a.height;
@@ -424,6 +448,20 @@ export class IframeResponder {
424
448
  }
425
449
  break;
426
450
  }
451
+ case 'smartlinks:navigable': {
452
+ this.navigable = true;
453
+ const p = data.payload || {};
454
+ (_j = (_h = this.options).onNavigable) === null || _j === void 0 ? void 0 : _j.call(_h, {
455
+ views: Array.isArray(p.views) ? p.views.filter((v) => typeof v === 'string') : [],
456
+ chrome: p.chrome === 'none' ? 'none' : 'full',
457
+ });
458
+ break;
459
+ }
460
+ case 'smartlinks:navigate-result': {
461
+ const p = data.payload || {};
462
+ (_l = (_k = this.options).onNavigateResult) === null || _l === void 0 ? void 0 : _l.call(_k, Object.assign({ ok: p.ok === true, view: typeof p.view === 'string' ? p.view : null, path: typeof p.path === 'string' ? p.path : null }, (typeof p.reason === 'string' ? { reason: p.reason } : {})));
463
+ break;
464
+ }
427
465
  }
428
466
  }
429
467
  // ===========================================================================
package/dist/index.d.ts CHANGED
@@ -3,6 +3,8 @@ export * from "./api/index.js";
3
3
  export * from "./types/index.js";
4
4
  export { iframe } from "./iframe.js";
5
5
  export { readContext } from "./context.js";
6
+ export * as adminViews from "./adminViews.js";
7
+ export type { NavigateRequest, NavigateTarget } from "./adminViews.js";
6
8
  export { widgetStyleVars, widgetStyleControls, readableOn, WIDGET_STYLE_VARS } from "./widgetStyle.js";
7
9
  export type { WidgetStyling } from "./widgetStyle.js";
8
10
  export type { SmartLinksContext } from "./context.js";
package/dist/index.js CHANGED
@@ -7,6 +7,8 @@ export * from "./types/index.js";
7
7
  export { iframe } from "./iframe.js";
8
8
  // App context reader (props → hash → search), for pages and containers alike
9
9
  export { readContext } from "./context.js";
10
+ import * as adminViews_1 from "./adminViews.js";
11
+ export { adminViews_1 as adminViews };
10
12
  export { widgetStyleVars, widgetStyleControls, readableOn, WIDGET_STYLE_VARS } from "./widgetStyle.js";
11
13
  import * as cache_1 from './cache.js';
12
14
  export { cache_1 as cache };