@oxyhq/contracts 0.21.0 → 0.23.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.
@@ -1,45 +1,351 @@
1
1
  /**
2
- * Account graph wire contracts — organization taxonomy and create-account input.
2
+ * Account graph wire contracts — the account-kind vocabulary, the account
3
+ * category taxonomy, and the create-account input.
3
4
  *
4
- * `organizationCategory` classifies `kind: 'organization'` accounts (agency,
5
- * cooperative, landlord, …) without polluting `User.kind`. Meaningful only when
6
- * `kind === 'organization'`.
5
+ * `accountCategories` classifies a NON-PERSONAL account — what it is about, what
6
+ * it does without polluting `User.kind`. See the block above
7
+ * {@link ACCOUNT_CATEGORY_IDS} for the four rules that govern it.
7
8
  */
8
9
  import { z } from 'zod';
9
- export const ORGANIZATION_CATEGORIES = [
10
+ /**
11
+ * The union is spelled out above and the array proves coverage BOTH ways
12
+ * (`satisfies` here, the `Gap` alias below) — the same shape this package's
13
+ * `ACCOUNT_CATEGORY_IDS` / `TRUST_TIERS` pairs use, and the one
14
+ * `db/schema/users.ts` mirrors to keep the `users_kind_check` CHECK honest.
15
+ *
16
+ * Deriving the union from the array instead would cost nothing here and be paid
17
+ * by consumers: `kind` travels into `@oxyhq/services` through
18
+ * `SwitchableAccount`, where an indexed-access type is materially more
19
+ * expensive to check than a literal union.
20
+ */
21
+ export const ACCOUNT_KINDS = [
22
+ 'personal',
23
+ 'organization',
24
+ 'project',
25
+ 'bot',
26
+ 'channel',
27
+ ];
28
+ export const accountKindSchema = z.enum(ACCOUNT_KINDS);
29
+ export const CHILD_ACCOUNT_KINDS = [
30
+ 'organization',
31
+ 'project',
32
+ 'bot',
33
+ 'channel',
34
+ ];
35
+ export const childAccountKindSchema = z.enum(CHILD_ACCOUNT_KINDS);
36
+ /**
37
+ * Whether an operator may ACT AS an account of this kind — switch the whole app
38
+ * into it (`POST /accounts/:id/switch`) or authorise an app to act as it
39
+ * (an OAuth delegated subject).
40
+ *
41
+ * Two kinds are refused, for opposite reasons:
42
+ *
43
+ * - `personal` is a human login, so assuming it would be impersonation.
44
+ * - `channel` is a CONTENT identity, not an operating one. A channel exists so
45
+ * that posts can be authored BY it; it is never a seat anybody occupies. Its
46
+ * operators act on it through their own membership, and an application
47
+ * publishes to it with its own credential. Refusing act-as is what makes
48
+ * "no login, ever" structural rather than incidental: no session can be
49
+ * minted whose subject is a channel, so no bearer exists that could add an
50
+ * auth method to one (every auth-method write resolves its target from the
51
+ * authenticated subject, never from a parameter).
52
+ *
53
+ * Consumers must gate on this predicate rather than testing `kind === 'personal'`,
54
+ * which silently admits every kind added after it was written.
55
+ */
56
+ export function isActAsEligibleKind(kind) {
57
+ return kind === 'organization' || kind === 'project' || kind === 'bot';
58
+ }
59
+ /**
60
+ * Narrow an unknown value to an {@link AccountKind}.
61
+ *
62
+ * The user-DTO serializers read from structurally-permissive `unknown` sources
63
+ * (a Drizzle row, a Mongo document, an already-formatted object), so each one
64
+ * would otherwise hand-roll this check and they would drift on what counts.
65
+ */
66
+ export function isAccountKind(value) {
67
+ return typeof value === 'string' && ACCOUNT_KINDS.includes(value);
68
+ }
69
+ // ===========================================================================
70
+ // Account categories
71
+ //
72
+ // Four rules govern this taxonomy. Each one is here because the obvious
73
+ // alternative fails silently rather than loudly.
74
+ //
75
+ // 1. AN ID IS AN OPAQUE, IMMUTABLE SLUG — the LABEL is not stored anywhere.
76
+ // Every value below is an identifier that no rename may ever change. The
77
+ // human-readable label lives in each client's own translation catalogue,
78
+ // keyed by the id. Re-labelling `agency` from "Real estate agency" to
79
+ // "Agency" is therefore a one-line locale edit that touches no row; moving
80
+ // the label into the column or the DTO would make the same edit a data
81
+ // migration, and would pin every reader to the language of whoever chose it.
82
+ //
83
+ // 2. THE PRIMARY CATEGORY IS THE FIRST ELEMENT of an ordered list. Not a
84
+ // separate flag, and not a second field: a flag admits two primaries and
85
+ // admits none, and ordering makes both unrepresentable. The cost is that
86
+ // ORDER IS DATA — anything that re-serializes, sorts or de-duplicates this
87
+ // list can change which category is primary without erroring, so no layer
88
+ // between the column and the client may reorder it.
89
+ //
90
+ // 3. THE VOCABULARY IS APPEND-ONLY. `ACCOUNT_CATEGORY_IDS` may gain ids and may
91
+ // never lose one, because rows already carry the ids it holds. Withdrawing a
92
+ // category means listing it in {@link RETIRED_ACCOUNT_CATEGORY_IDS}, which
93
+ // removes it from the picker while leaving it readable and re-writable. The
94
+ // database enforces the same asymmetry: its CHECK is re-evaluated on EVERY
95
+ // update to a row, so narrowing the allowed set makes an unrelated write —
96
+ // saving a bio — fail on any account that had picked the withdrawn value.
97
+ // Measured on a real Postgres, `NOT VALID` included; it does not help.
98
+ //
99
+ // 4. THE SET IS CLOSED. An id nobody can validate is an id no client can render:
100
+ // the label comes from a translation key derived from the id, so an invented
101
+ // value paints as a blank or a raw slug in every app at once. Closing it also
102
+ // costs nothing that rule 3 does not already charge — adding a category needs
103
+ // a migration to widen the CHECK whether or not the enum exists, so an open
104
+ // list would buy back no work, only the validation.
105
+ // ===========================================================================
106
+ /**
107
+ * Every account category, by stable id.
108
+ *
109
+ * Grouped by comment for readability only; the storage, the wire and the picker
110
+ * all treat this as one flat list. `other` is the escape hatch for an account
111
+ * that fits nothing here.
112
+ *
113
+ * TO ADD ONE: append an id (lowercase ASCII, `snake_case`) here, publish
114
+ * `@oxyhq/contracts`, then ship a migration that widens
115
+ * `users_account_categories_check` — never edit an existing migration — and add
116
+ * an `accounts.accountCategory.<id>` label to each client's locales.
117
+ *
118
+ * TO WITHDRAW ONE: leave the id here and add it to
119
+ * {@link RETIRED_ACCOUNT_CATEGORY_IDS}. See rule 3 above.
120
+ */
121
+ export const ACCOUNT_CATEGORY_IDS = [
122
+ // ---- media & public information -----------------------------------------
123
+ 'news',
124
+ 'politics',
125
+ // ---- business & economy --------------------------------------------------
126
+ 'business',
127
+ 'startup',
128
+ 'finance',
129
+ 'crypto',
130
+ 'marketplace',
131
+ 'retail',
132
+ // The four ids the single-valued `organizationCategory` field used to hold,
133
+ // carried forward VERBATIM. Their labels may be rewritten freely; their ids
134
+ // may not, because live rows hold them.
135
+ 'real_estate',
10
136
  'agency',
11
- 'cooperative',
12
137
  'landlord',
138
+ 'cooperative',
139
+ 'architecture',
140
+ // ---- technology ----------------------------------------------------------
141
+ 'technology',
142
+ 'software',
143
+ 'ai',
144
+ 'security',
145
+ 'automation',
146
+ // ---- knowledge -----------------------------------------------------------
147
+ 'science',
148
+ 'education',
149
+ 'books',
150
+ // ---- health --------------------------------------------------------------
151
+ 'health',
152
+ 'fitness',
153
+ // ---- sport & play --------------------------------------------------------
154
+ 'sports',
155
+ 'gaming',
156
+ // ---- culture & entertainment ---------------------------------------------
157
+ //
158
+ // There is deliberately NO generic `entertainment` here, and re-adding one is
159
+ // a regression rather than a gap. It is the only id this list ever carried
160
+ // that was dominated by its own specifics — `film`, `music`, `gaming` and
161
+ // `comedy` all exist — and a generic drawer sitting beside its four
162
+ // concretions collects the lazy pick, which degrades the data for all four at
163
+ // once: the accounts that would have said `film` say `entertainment` instead,
164
+ // and `film` stops meaning what it meant.
165
+ //
166
+ // The other overlaps in this vocabulary are NOT the same case and must not be
167
+ // merged on this reasoning: `sports`/`fitness`, `art`/`photography`,
168
+ // `business`/`startup`, `finance`/`crypto`, `home_garden`/`diy` and
169
+ // `technology`/`software`/`security` are genuinely different audiences, and
170
+ // with a cap of four the granularity is cheap.
171
+ 'music',
172
+ 'film',
173
+ 'podcast',
174
+ 'art',
175
+ 'photography',
176
+ 'comedy',
177
+ // ---- everyday life -------------------------------------------------------
178
+ 'food',
179
+ 'travel',
180
+ 'fashion',
181
+ 'home_garden',
182
+ 'diy',
183
+ 'automotive',
184
+ 'animals',
185
+ 'family',
186
+ // ---- society -------------------------------------------------------------
187
+ 'nonprofit',
188
+ 'government',
189
+ 'community',
190
+ 'activism',
191
+ 'environment',
192
+ 'religion',
193
+ // ---- fallback ------------------------------------------------------------
13
194
  'other',
14
195
  ];
15
- export const organizationCategorySchema = z.enum(ORGANIZATION_CATEGORIES);
196
+ /**
197
+ * Accepts EVERY id, withdrawn ones included — see rule 3.
198
+ *
199
+ * A schema that rejected a withdrawn id would 400 the whole request whenever a
200
+ * client round-trips the categories it was served, so an account that had
201
+ * picked one could no longer save its bio either. That is the same failure the
202
+ * nullable `bio` / `avatar` fix addressed, wearing a different hat.
203
+ */
204
+ export const accountCategoryIdSchema = z.enum(ACCOUNT_CATEGORY_IDS);
205
+ /**
206
+ * Ids withdrawn from the picker. Empty today.
207
+ *
208
+ * A withdrawn id keeps working everywhere it is already stored: it validates,
209
+ * it survives a round-trip save, it still renders from its label key, and it
210
+ * stays PRIMARY if it was primary. Nothing rewrites a stored list — a read-time
211
+ * or migration-time demotion would silently replace a choice its owner made,
212
+ * which is precisely what stable ids exist to prevent. The owner drops it on
213
+ * their next edit; until then it is honoured.
214
+ *
215
+ * What withdrawal changes is only this: the id leaves
216
+ * {@link SELECTABLE_ACCOUNT_CATEGORY_IDS}, so no picker offers it, and
217
+ * {@link newlyAddedRetiredCategories} refuses to let a write ADD it to an
218
+ * account that did not already have it.
219
+ */
220
+ export const RETIRED_ACCOUNT_CATEGORY_IDS = [];
221
+ /** Whether a category may still be OFFERED. A stored one is readable either way. */
222
+ export function isSelectableAccountCategoryId(id) {
223
+ return !RETIRED_ACCOUNT_CATEGORY_IDS.includes(id);
224
+ }
225
+ /** The ids a picker may offer, in declaration order. */
226
+ export const SELECTABLE_ACCOUNT_CATEGORY_IDS = ACCOUNT_CATEGORY_IDS.filter(isSelectableAccountCategoryId);
227
+ /**
228
+ * Which of `next` are withdrawn ids the account did not already carry — i.e.
229
+ * the ones a write must be refused for.
230
+ *
231
+ * `retired` is a parameter rather than a module read so the rule can be
232
+ * exercised against a non-empty set while the production one is empty; a test
233
+ * over `RETIRED_ACCOUNT_CATEGORY_IDS` alone would pass vacuously today and stay
234
+ * passing if the rule were deleted.
235
+ */
236
+ export function newlyAddedRetiredCategories(next, previous, retired) {
237
+ return next.filter((id) => retired.includes(id) && !previous.includes(id));
238
+ }
239
+ /**
240
+ * How many categories one account may carry.
241
+ *
242
+ * Four, not "as many as you like". Three reasons, in the order they bind:
243
+ *
244
+ * - The primary has to MEAN something. At ten categories the first element
245
+ * reads as a sort artifact rather than a choice, and rule 2 above is the
246
+ * entire mechanism by which a primary exists.
247
+ * - The profile RENDERS them as a row of chips; four labels of this length is
248
+ * what fits a phone-width profile header before the row wraps or truncates.
249
+ * - Four is enough to place a genuinely compound account without a tag cloud:
250
+ * a housing cooperative that is also a non-profit serving a local community
251
+ * spends `cooperative`, `nonprofit`, `community`, `real_estate` — and is the
252
+ * most compound real example in the ecosystem.
253
+ *
254
+ * One constant, read by the wire schema, the database CHECK and the picker, so
255
+ * changing it is one edit plus a migration.
256
+ */
257
+ export const MAX_ACCOUNT_CATEGORIES = 4;
258
+ /**
259
+ * An account's categories on the wire. ORDER IS MEANINGFUL — index 0 is the
260
+ * primary (rule 2).
261
+ *
262
+ * A duplicate is REJECTED rather than silently collapsed. De-duplicating would
263
+ * rewrite the caller's list, and any rewrite of this list can move which id sits
264
+ * at index 0 — so the one repair available here is the one that would break the
265
+ * property the list exists to carry. A duplicate only ever comes from a client
266
+ * bug, and a 400 naming the index is how that bug gets found.
267
+ */
268
+ export const accountCategoriesSchema = z
269
+ .array(accountCategoryIdSchema)
270
+ .max(MAX_ACCOUNT_CATEGORIES)
271
+ .superRefine((ids, ctx) => {
272
+ const seen = new Set();
273
+ ids.forEach((id, index) => {
274
+ if (seen.has(id)) {
275
+ ctx.addIssue({
276
+ code: z.ZodIssueCode.custom,
277
+ message: `Duplicate account category "${id}"`,
278
+ path: [index],
279
+ });
280
+ }
281
+ seen.add(id);
282
+ });
283
+ });
284
+ /**
285
+ * Kinds that may carry categories: every kind EXCEPT `personal`.
286
+ *
287
+ * A person has interests, not a sector — and their interests are not a
288
+ * classification anybody else gets to read off their profile. Spelled out
289
+ * positively, like {@link isActAsEligibleKind} and for the same reason: a `kind
290
+ * !== 'personal'` test silently admits every kind invented after it was
291
+ * written, whereas this list forces whoever adds one to decide.
292
+ */
293
+ export const ACCOUNT_CATEGORY_KINDS = [
294
+ 'organization',
295
+ 'project',
296
+ 'bot',
297
+ 'channel',
298
+ ];
299
+ /**
300
+ * Whether an account of this kind may carry categories.
301
+ *
302
+ * The API refuses the write and the `users_account_categories_kind_check`
303
+ * constraint makes it unrepresentable; both derive from
304
+ * {@link ACCOUNT_CATEGORY_KINDS}, so they cannot disagree.
305
+ */
306
+ export function kindAcceptsAccountCategories(kind) {
307
+ return ACCOUNT_CATEGORY_KINDS.includes(kind ?? '');
308
+ }
309
+ /**
310
+ * An account's name on the create/update wire.
311
+ *
312
+ * `displayName` is EXPLICIT and stored, not derived. `first`/`last` model a
313
+ * human name, and composing a display string from them is right for a person —
314
+ * but a non-personal account has a TITLE, not a given and family name. Without
315
+ * this field the only way to name a channel "Notas de Nate" was to put the whole
316
+ * title in `first`, which renders correctly by accident while recording it as
317
+ * somebody's given name.
318
+ *
319
+ * When present it wins over the composed `first`/`last` (see the API's
320
+ * `composeDisplayName`, which already preferred an explicit value — only the
321
+ * storage for one was missing).
322
+ */
16
323
  const accountNameSchema = z
17
324
  .object({
18
325
  first: z.string().trim().max(100).optional(),
19
326
  last: z.string().trim().max(100).optional(),
327
+ displayName: z.string().trim().max(100).optional(),
20
328
  })
21
329
  .optional();
22
330
  /**
23
331
  * POST /accounts — create a non-personal account under the caller's tree.
24
- * `organizationCategory` is accepted only when `kind` is `organization`.
332
+ *
333
+ * No cross-field refinement guards `accountCategories`, and that is not an
334
+ * omission: `kind` here is a CHILD kind, and every child kind is in
335
+ * {@link ACCOUNT_CATEGORY_KINDS}, so `personal` is already unrepresentable on
336
+ * this route. The refinement the single-valued predecessor needed disappeared
337
+ * along with the restriction that made it necessary. A child kind that does NOT
338
+ * accept categories would break that reasoning silently, so
339
+ * `__tests__/accountGraph.test.ts` asserts the two lists agree.
25
340
  */
26
- export const createAccountRequestSchema = z
27
- .object({
341
+ export const createAccountRequestSchema = z.object({
28
342
  parentAccountId: z.string().trim().min(1).optional(),
29
- kind: z.enum(['organization', 'project', 'bot']),
343
+ kind: childAccountKindSchema,
30
344
  username: z.string().trim().min(1).max(100),
31
345
  name: accountNameSchema,
32
346
  bio: z.string().trim().max(500).optional(),
33
347
  avatar: z.string().optional(),
34
348
  description: z.string().trim().max(1000).optional(),
35
- organizationCategory: organizationCategorySchema.optional(),
36
- })
37
- .superRefine((data, ctx) => {
38
- if (data.organizationCategory !== undefined && data.kind !== 'organization') {
39
- ctx.addIssue({
40
- code: z.ZodIssueCode.custom,
41
- message: 'organizationCategory applies only when kind is organization',
42
- path: ['organizationCategory'],
43
- });
44
- }
349
+ /** Ordered, PRIMARY FIRST — see rule 2 above {@link ACCOUNT_CATEGORY_IDS}. */
350
+ accountCategories: accountCategoriesSchema.optional(),
45
351
  });
@@ -45,10 +45,11 @@ export const deviceTokenMintRequestSchema = z.object({
45
45
  });
46
46
  /**
47
47
  * Wire shape of a successful `POST /session/device/token`: the freshly-minted
48
- * short access token for the active account, its expiry, the NEXT rotating
49
- * device secret the client must persist (rotation-in-use — the presented secret
50
- * stays valid for a short grace so multi-tab races don't lock out), and the
51
- * projected device-session state.
48
+ * short access token for the active account, its expiry, the device secret the
49
+ * client must persist (`nextDeviceSecret`on mint this echoes the presented
50
+ * secret unchanged so concurrent refreshes from multiple origins do not race),
51
+ * and the projected device-session state. Sign-in rotates the secret via
52
+ * `issueDeviceSecret`; mint does not.
52
53
  */
53
54
  export const deviceTokenMintResponseSchema = z.object({
54
55
  accessToken: z.string(),
package/dist/esm/index.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * Platform-agnostic — zod is the only runtime dependency. No react/react-native/
10
10
  * expo, no `require()` in the ESM build.
11
11
  */
12
- export { ORGANIZATION_CATEGORIES, organizationCategorySchema, createAccountRequestSchema, } from './accountGraph.js';
12
+ export { ACCOUNT_KINDS, accountKindSchema, CHILD_ACCOUNT_KINDS, childAccountKindSchema, isAccountKind, isActAsEligibleKind, ACCOUNT_CATEGORY_IDS, ACCOUNT_CATEGORY_KINDS, accountCategoriesSchema, accountCategoryIdSchema, isSelectableAccountCategoryId, kindAcceptsAccountCategories, MAX_ACCOUNT_CATEGORIES, newlyAddedRetiredCategories, RETIRED_ACCOUNT_CATEGORY_IDS, SELECTABLE_ACCOUNT_CATEGORY_IDS, createAccountRequestSchema, } from './accountGraph.js';
13
13
  export {
14
14
  // Schemas
15
15
  userNameSchema, userRelationshipSchema, themePreferenceSchema, userResponseSchema, userProfileUpdateSchema, currentUserResponseSchema, deviceLinkedSessionSchema, deviceLinkedSessionsResponseSchema,
@@ -30,7 +30,7 @@
30
30
  */
31
31
  import { z } from 'zod';
32
32
  import { verifiedDomainSchema } from './identity.js';
33
- import { organizationCategorySchema } from './accountGraph.js';
33
+ import { accountCategoriesSchema, accountKindSchema } from './accountGraph.js';
34
34
  export const userNameSchema = z
35
35
  .object({
36
36
  first: z.string().optional(),
@@ -101,10 +101,38 @@ export const userResponseSchema = z
101
101
  */
102
102
  verifiedDomains: z.array(verifiedDomainSchema).optional(),
103
103
  /**
104
- * Real-estate / team taxonomy for `kind: 'organization'` accounts.
105
- * Absent on personal, project, and bot accounts.
104
+ * Account-graph classification what KIND of account this is.
105
+ *
106
+ * ORTHOGONAL to `type` (`local` / `federated` / `agent` / `automated`),
107
+ * which says where the account lives and how it is driven; the two
108
+ * coexist and neither substitutes for the other. A `channel` is a
109
+ * publishing identity nobody can act as, so a consumer that renders
110
+ * authored content reads THIS to tell a channel's post from a person's.
111
+ *
112
+ * Optional because a DTO produced from a source that never carried the
113
+ * column omits it; absent should be read as `personal`, the column's
114
+ * default, not as unknown.
106
115
  */
107
- organizationCategory: organizationCategorySchema.optional(),
116
+ kind: accountKindSchema.optional(),
117
+ /**
118
+ * What this account is about — the field a profile screen RENDERS.
119
+ *
120
+ * **Ordered, primary first.** `accountCategories[0]` is the primary
121
+ * category; there is deliberately no sibling `primaryCategory` field,
122
+ * because two representations of one fact can disagree (see rule 2 in
123
+ * `accountGraph.ts`). Nothing downstream may sort, de-duplicate or
124
+ * otherwise reorder this array.
125
+ *
126
+ * **Ids, never labels.** Each element is a stable slug; the visible text
127
+ * comes from the reader's own translation catalogue, keyed
128
+ * `accounts.accountCategory.<id>`. A label on the wire would paint every
129
+ * profile in the language of whoever picked it.
130
+ *
131
+ * Absent when the account has none — which is every `personal` account,
132
+ * and any non-personal one that has not chosen. A renderer reads
133
+ * `user.accountCategories ?? []`.
134
+ */
135
+ accountCategories: accountCategoriesSchema.optional(),
108
136
  /**
109
137
  * The authenticated viewer's relationship to this profile. Present ONLY
110
138
  * on single-profile fetches (`GET /profiles/username/:username`,
@@ -126,6 +154,12 @@ export const userProfileUpdateSchema = z
126
154
  .object({
127
155
  first: z.string().optional(),
128
156
  last: z.string().optional(),
157
+ /**
158
+ * Explicit display name, stored rather than composed. Wins over
159
+ * `first`/`last` when set; send `''` to clear it and fall back
160
+ * to the composed pair.
161
+ */
162
+ displayName: z.string().optional(),
129
163
  })
130
164
  .optional(),
131
165
  username: z.string().optional(),