primitive-admin 1.3.0-alpha.0 → 1.3.0-alpha.2

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 (148) hide show
  1. package/README.md +45 -0
  2. package/dist/src/commands/admins.js +91 -0
  3. package/dist/src/commands/admins.js.map +1 -1
  4. package/dist/src/commands/auth-passkeys.d.ts +7 -0
  5. package/dist/src/commands/auth-passkeys.js +99 -0
  6. package/dist/src/commands/auth-passkeys.js.map +1 -0
  7. package/dist/src/commands/auth-sessions.js +7 -28
  8. package/dist/src/commands/auth-sessions.js.map +1 -1
  9. package/dist/src/commands/auth.js +32 -6
  10. package/dist/src/commands/auth.js.map +1 -1
  11. package/dist/src/commands/db3-types.d.ts +27 -0
  12. package/dist/src/commands/db3-types.js +288 -2
  13. package/dist/src/commands/db3-types.js.map +1 -1
  14. package/dist/src/commands/db3.d.ts +16 -2
  15. package/dist/src/commands/db3.js +614 -5
  16. package/dist/src/commands/db3.js.map +1 -1
  17. package/dist/src/commands/documents.js +7 -0
  18. package/dist/src/commands/documents.js.map +1 -1
  19. package/dist/src/commands/functions.js +8 -3
  20. package/dist/src/commands/functions.js.map +1 -1
  21. package/dist/src/commands/hosting.js +177 -14
  22. package/dist/src/commands/hosting.js.map +1 -1
  23. package/dist/src/commands/metadata.js +14 -6
  24. package/dist/src/commands/metadata.js.map +1 -1
  25. package/dist/src/commands/secrets.d.ts +38 -0
  26. package/dist/src/commands/secrets.js +102 -4
  27. package/dist/src/commands/secrets.js.map +1 -1
  28. package/dist/src/commands/spent-tokens.d.ts +19 -0
  29. package/dist/src/commands/spent-tokens.js +79 -0
  30. package/dist/src/commands/spent-tokens.js.map +1 -0
  31. package/dist/src/commands/sync-app-settings.d.ts +145 -0
  32. package/dist/src/commands/sync-app-settings.js +341 -8
  33. package/dist/src/commands/sync-app-settings.js.map +1 -1
  34. package/dist/src/commands/sync.d.ts +62 -15
  35. package/dist/src/commands/sync.js +437 -48
  36. package/dist/src/commands/sync.js.map +1 -1
  37. package/dist/src/commands/usage.d.ts +24 -0
  38. package/dist/src/commands/usage.js +54 -0
  39. package/dist/src/commands/usage.js.map +1 -1
  40. package/dist/src/commands/users.js +41 -1
  41. package/dist/src/commands/users.js.map +1 -1
  42. package/dist/src/lib/admin-login-tables.d.ts +31 -0
  43. package/dist/src/lib/admin-login-tables.js +75 -0
  44. package/dist/src/lib/admin-login-tables.js.map +1 -0
  45. package/dist/src/lib/api-client.d.ts +241 -0
  46. package/dist/src/lib/api-client.js +203 -10
  47. package/dist/src/lib/api-client.js.map +1 -1
  48. package/dist/src/lib/app-settings-descriptor.d.ts +42 -3
  49. package/dist/src/lib/app-settings-descriptor.js +114 -1
  50. package/dist/src/lib/app-settings-descriptor.js.map +1 -1
  51. package/dist/src/lib/auth-flow.d.ts +13 -0
  52. package/dist/src/lib/auth-flow.js +23 -2
  53. package/dist/src/lib/auth-flow.js.map +1 -1
  54. package/dist/src/lib/command-groups.js +2 -0
  55. package/dist/src/lib/command-groups.js.map +1 -1
  56. package/dist/src/lib/config-object-descriptor.js +6 -1
  57. package/dist/src/lib/config-object-descriptor.js.map +1 -1
  58. package/dist/src/lib/credentials-store.d.ts +19 -0
  59. package/dist/src/lib/credentials-store.js +66 -2
  60. package/dist/src/lib/credentials-store.js.map +1 -1
  61. package/dist/src/lib/db-codegen/dbTypeIR.d.ts +2 -0
  62. package/dist/src/lib/db-codegen/dbTypeIR.js +3 -1
  63. package/dist/src/lib/db-codegen/dbTypeIR.js.map +1 -1
  64. package/dist/src/lib/db-type-records.d.ts +20 -1
  65. package/dist/src/lib/db-type-records.js +17 -4
  66. package/dist/src/lib/db-type-records.js.map +1 -1
  67. package/dist/src/lib/db3-export.d.ts +43 -8
  68. package/dist/src/lib/db3-export.js +133 -13
  69. package/dist/src/lib/db3-export.js.map +1 -1
  70. package/dist/src/lib/db3-json-fields.d.ts +49 -0
  71. package/dist/src/lib/db3-json-fields.js +195 -0
  72. package/dist/src/lib/db3-json-fields.js.map +1 -0
  73. package/dist/src/lib/db3-type-archive.d.ts +59 -0
  74. package/dist/src/lib/db3-type-archive.js +95 -0
  75. package/dist/src/lib/db3-type-archive.js.map +1 -0
  76. package/dist/src/lib/db3-type-models.d.ts +15 -1
  77. package/dist/src/lib/db3-type-models.js +147 -16
  78. package/dist/src/lib/db3-type-models.js.map +1 -1
  79. package/dist/src/lib/document-export-record-log.d.ts +7 -0
  80. package/dist/src/lib/document-export-record-log.js +9 -0
  81. package/dist/src/lib/document-export-record-log.js.map +1 -1
  82. package/dist/src/lib/document-ingest-artifact.d.ts +6 -0
  83. package/dist/src/lib/document-ingest-artifact.js +20 -1
  84. package/dist/src/lib/document-ingest-artifact.js.map +1 -1
  85. package/dist/src/lib/function-authorize-check.js +5 -0
  86. package/dist/src/lib/function-authorize-check.js.map +1 -1
  87. package/dist/src/lib/function-authorize-view.d.ts +9 -0
  88. package/dist/src/lib/function-authorize-view.js +27 -4
  89. package/dist/src/lib/function-authorize-view.js.map +1 -1
  90. package/dist/src/lib/function-collect.d.ts +28 -0
  91. package/dist/src/lib/function-collect.js +129 -2
  92. package/dist/src/lib/function-collect.js.map +1 -1
  93. package/dist/src/lib/function-db-types.js +31 -2
  94. package/dist/src/lib/function-db-types.js.map +1 -1
  95. package/dist/src/lib/function-grants-preflight.d.ts +99 -0
  96. package/dist/src/lib/function-grants-preflight.js +154 -2
  97. package/dist/src/lib/function-grants-preflight.js.map +1 -1
  98. package/dist/src/lib/function-lint.d.ts +29 -1
  99. package/dist/src/lib/function-lint.js +208 -0
  100. package/dist/src/lib/function-lint.js.map +1 -1
  101. package/dist/src/lib/function-sync.d.ts +73 -0
  102. package/dist/src/lib/function-sync.js +107 -2
  103. package/dist/src/lib/function-sync.js.map +1 -1
  104. package/dist/src/lib/generated-command-status.js +46 -8
  105. package/dist/src/lib/generated-command-status.js.map +1 -1
  106. package/dist/src/lib/generated-config-surfaces.d.ts +351 -9
  107. package/dist/src/lib/generated-config-surfaces.js +1009 -24
  108. package/dist/src/lib/generated-config-surfaces.js.map +1 -1
  109. package/dist/src/lib/generated-sdk-types.d.ts +1 -1
  110. package/dist/src/lib/generated-sdk-types.js +1 -1
  111. package/dist/src/lib/generated-sdk-types.js.map +1 -1
  112. package/dist/src/lib/hosting-trust-step.d.ts +67 -0
  113. package/dist/src/lib/hosting-trust-step.js +279 -0
  114. package/dist/src/lib/hosting-trust-step.js.map +1 -0
  115. package/dist/src/lib/hosting-trust.d.ts +77 -0
  116. package/dist/src/lib/hosting-trust.js +194 -0
  117. package/dist/src/lib/hosting-trust.js.map +1 -0
  118. package/dist/src/lib/primitive-declaration.js +89 -10
  119. package/dist/src/lib/primitive-declaration.js.map +1 -1
  120. package/dist/src/lib/refresh-admin-credentials.d.ts +34 -4
  121. package/dist/src/lib/refresh-admin-credentials.js +85 -29
  122. package/dist/src/lib/refresh-admin-credentials.js.map +1 -1
  123. package/dist/src/lib/refresh-stored-login.d.ts +85 -0
  124. package/dist/src/lib/refresh-stored-login.js +233 -0
  125. package/dist/src/lib/refresh-stored-login.js.map +1 -0
  126. package/dist/src/lib/row-field-rename.d.ts +18 -0
  127. package/dist/src/lib/row-field-rename.js +32 -0
  128. package/dist/src/lib/row-field-rename.js.map +1 -0
  129. package/dist/src/lib/schema-codegen/generated-field-grammar.d.ts +13 -2
  130. package/dist/src/lib/schema-codegen/generated-field-grammar.js +24 -1
  131. package/dist/src/lib/schema-codegen/generated-field-grammar.js.map +1 -1
  132. package/dist/src/lib/schema-defs-module.d.ts +29 -0
  133. package/dist/src/lib/schema-defs-module.js +103 -0
  134. package/dist/src/lib/schema-defs-module.js.map +1 -0
  135. package/dist/src/lib/swift-codegen/sharedGenerator.d.ts +6 -0
  136. package/dist/src/lib/swift-codegen/sharedGenerator.js +74 -0
  137. package/dist/src/lib/swift-codegen/sharedGenerator.js.map +1 -1
  138. package/dist/src/lib/toml-list-edit.d.ts +33 -0
  139. package/dist/src/lib/toml-list-edit.js +481 -0
  140. package/dist/src/lib/toml-list-edit.js.map +1 -0
  141. package/dist/src/lib/toml-scalar-edit.d.ts +9 -0
  142. package/dist/src/lib/toml-scalar-edit.js +67 -1
  143. package/dist/src/lib/toml-scalar-edit.js.map +1 -1
  144. package/dist/src/lib/usage-meters.d.ts +16 -0
  145. package/dist/src/lib/usage-meters.js +52 -0
  146. package/dist/src/lib/usage-meters.js.map +1 -0
  147. package/dist/src/types/index.d.ts +9 -0
  148. package/package.json +2 -2
@@ -1,7 +1,7 @@
1
1
  import { Option } from "commander";
2
2
  import { commandStatus } from "../lib/command-status.js";
3
3
  import { ApiClient } from "../lib/api-client.js";
4
- import { resolveAppId } from "../lib/config.js";
4
+ import { getServerUrl, resolveAppId } from "../lib/config.js";
5
5
  import { error, flushOutput, formatDate, formatTable, info, json, result as printResult, success, warn } from "../lib/output.js";
6
6
  import { confirmPrompt } from "../lib/confirm-prompt.js";
7
7
  import { normalizeCliListEnvelope, wholeListEnvelope } from "../lib/paginate.js";
@@ -151,10 +151,51 @@ function printDb3(row) {
151
151
  printResult("Sync", row.sync === false ? "off" : "on");
152
152
  printResult("Type", row.type ?? "none");
153
153
  }
154
+ /** The levels a db3 grant, link or request names. */
155
+ const SHARING_LEVELS = ["reader", "read-write"];
156
+ /** `value` as a sharing level, or exit 1 naming `flag` before any request. */
157
+ function parseSharingLevel(value, flag) {
158
+ if (SHARING_LEVELS.includes(value))
159
+ return value;
160
+ refuseList(`${flag} must be "reader" or "read-write".`);
161
+ }
162
+ /** Exactly one of `--user-id` and `--email`, or exit 1 before any request. */
163
+ function parseUserTarget(options) {
164
+ if (options.userId !== undefined && options.email === undefined)
165
+ return { userId: options.userId };
166
+ if (options.email !== undefined && options.userId === undefined)
167
+ return { email: options.email };
168
+ refuseList("Name the user with exactly one of --user-id <id> and --email <email>.");
169
+ }
170
+ /** Ask `question` unless `-y`: false when the operator cancels; exit 1 when stdin cannot ask. */
171
+ async function confirmed(options, question) {
172
+ if (options.yes)
173
+ return true;
174
+ try {
175
+ if (await confirmPrompt(question))
176
+ return true;
177
+ }
178
+ catch (err) {
179
+ error(err.message);
180
+ process.exit(1);
181
+ }
182
+ info("Cancelled.");
183
+ return false;
184
+ }
185
+ /** A sharing list's rows printed as a table, or its "No … found." line. */
186
+ function printSharingTable(rows, columns, empty) {
187
+ if (rows.length === 0) {
188
+ info(empty);
189
+ return;
190
+ }
191
+ console.log(formatTable(rows, columns));
192
+ }
154
193
  /**
155
194
  * `primitive db3` (#4696, #4804): a db3's lifecycle (`create`, `get`,
156
- * `list`, `delete`, `stats`), its sync, app access and custody, its indexes,
157
- * and its records, schema and room through the `db3/:id/*` routes.
195
+ * `list`, `delete`, `stats`), its sync, app access and custody, its sharing
196
+ * (#5922: link access, user and group grants, pending invitations, access
197
+ * requests and a user's effective access), its indexes, and its records,
198
+ * schema and room through the `db3/:id/*` routes.
158
199
  *
159
200
  * The records, schema and inspect verbs are the `documents` verbs' twins:
160
201
  * they share their action code (`./documents.ts`) through the db3 routes
@@ -164,7 +205,7 @@ function printDb3(row) {
164
205
  export function registerDb3Commands(program) {
165
206
  const db3 = program
166
207
  .command("db3")
167
- .description("Work with db3s: create, read, list and delete them, their sync, app access and custody, indexes, records, schema and room");
208
+ .description("Work with db3s: create, read, list and delete them, their sync, sharing, app access and custody, indexes, records, schema and room");
168
209
  commandStatus(db3, { stability: "beta" });
169
210
  // ---- Indexes ----
170
211
  const indexes = db3.command("indexes").description("List, register and drop a db3's indexes");
@@ -616,6 +657,47 @@ export function registerDb3Commands(program) {
616
657
  fail(err);
617
658
  }
618
659
  });
660
+ records
661
+ .command("clear")
662
+ .description("Remove every record of a db3 model in one operation (experimental)")
663
+ .argument("<db3-id>", "db3 ID")
664
+ .argument("<model-name>", "Model name")
665
+ .option("--app <app-id>", "App ID")
666
+ .option("--drop", "Also remove the model itself from an untyped db3 (its declaration, observed fields and indexes)")
667
+ .option("-y, --yes", "Skip confirmation prompt")
668
+ .option("--json", "Output as JSON")
669
+ .action(async (db3Id, modelName, options) => {
670
+ const resolvedAppId = resolveAppId(undefined, options);
671
+ if (!options.yes) {
672
+ let confirmed;
673
+ try {
674
+ confirmed = await confirmPrompt(`Remove every record of ${modelName} from db3 ${db3Id}${options.drop ? ", and the model itself" : ""}? This cannot be undone.`);
675
+ }
676
+ catch (err) {
677
+ error(err.message);
678
+ process.exit(1);
679
+ }
680
+ if (!confirmed) {
681
+ info("Cancelled.");
682
+ return;
683
+ }
684
+ }
685
+ try {
686
+ const answer = await new ApiClient().clearDb3Model(resolvedAppId, db3Id, modelName, options.drop ? { drop: true } : {});
687
+ if (options.json) {
688
+ json(answer);
689
+ return;
690
+ }
691
+ if (answer.lsn === null)
692
+ success(`${modelName} held no records; nothing was written`);
693
+ else
694
+ success(`Cleared ${answer.rows} records of ${modelName} from db3 ${db3Id} (log number ${answer.lsn}, epoch ${answer.epoch})` +
695
+ (answer.dropped ? "; the model was dropped" : ""));
696
+ }
697
+ catch (err) {
698
+ fail(err);
699
+ }
700
+ });
619
701
  // ---- Schema and room: the `documents schema` and `documents inspect` twins ----
620
702
  db3
621
703
  .command("schema")
@@ -915,6 +997,13 @@ Custody:
915
997
  printResult("Your Permission", row.permission);
916
998
  if (row.accessSource)
917
999
  printResult("Access Source", row.accessSource);
1000
+ if (row.publicAccess) {
1001
+ printResult("Public Access", row.publicAccess);
1002
+ if (row.publicAccessUpdatedBy)
1003
+ printResult("Public Access Set By", row.publicAccessUpdatedBy);
1004
+ if (row.publicAccessUpdatedAt)
1005
+ printResult("Public Access Set", formatDate(row.publicAccessUpdatedAt));
1006
+ }
918
1007
  }
919
1008
  catch (err) {
920
1009
  fail(err);
@@ -1179,6 +1268,511 @@ Custody:
1179
1268
  fail(err);
1180
1269
  }
1181
1270
  });
1271
+ // ---- Public access (#5842) ----
1272
+ const publicAccess = db3
1273
+ .command("public-access")
1274
+ .description("Change whether a db3 may be served to callers with no sharing level (app owners and admins only)");
1275
+ publicAccess
1276
+ .command("set")
1277
+ .description("Mark the db3 public with reader, or clear it with none (app owners and admins only)")
1278
+ .argument("<db3-id>", "db3 ID")
1279
+ .argument("<level>", "reader or none")
1280
+ .option("--app <app-id>", "App ID")
1281
+ .option("--json", "Output as JSON")
1282
+ .addHelpText("after", `
1283
+ Public access is not a sharing level: it lets nobody open, sync, list or read
1284
+ the db3 through a db3 route. While the app's signedOutAccess setting is on, a
1285
+ server function may serve a public db3 to any caller, signed out included.
1286
+ \`primitive db3 get\` prints the stored value.
1287
+ `)
1288
+ .action(async (db3Id, level, options) => {
1289
+ if (level !== "reader" && level !== "none") {
1290
+ error('The level must be "reader" or "none".');
1291
+ process.exit(1);
1292
+ }
1293
+ const resolvedAppId = resolveAppId(undefined, options);
1294
+ try {
1295
+ const answer = await new ApiClient().setDb3PublicAccess(resolvedAppId, db3Id, level === "none" ? null : "reader");
1296
+ if (options.json) {
1297
+ json(answer);
1298
+ return;
1299
+ }
1300
+ success(`Public access on ${db3Id}: ${answer.level ?? "none"}${answer.changed ? "" : " (unchanged)"}.`);
1301
+ if (answer.level === "reader" && answer.signedOutAccess === false) {
1302
+ info("It takes effect once the app's signedOutAccess setting is on ([app] signedOutAccess = true in app.toml).");
1303
+ }
1304
+ }
1305
+ catch (err) {
1306
+ fail(err);
1307
+ }
1308
+ });
1309
+ publicAccess
1310
+ .command("blob-url")
1311
+ .description("Print the stable public URL of a blob in a public db3 (makes no request)")
1312
+ .argument("<db3-id>", "db3 ID")
1313
+ .argument("<blob-id>", "Blob ID")
1314
+ .option("--app <app-id>", "App ID")
1315
+ .option("--json", "Output as JSON")
1316
+ .addHelpText("after", `
1317
+ Anyone can load the URL, signed in or not, while the db3 is public and the
1318
+ app's signedOutAccess setting is on; every other case answers 404. The URL
1319
+ never changes, and its answer may be cached for five minutes. In a web app,
1320
+ client.db3.publicBlobUrl(db3Id, blobId) builds the same URL.
1321
+ `)
1322
+ .action((db3Id, blobId, options) => {
1323
+ const resolvedAppId = resolveAppId(undefined, options);
1324
+ const url = publicBlobUrl(getServerUrl(), resolvedAppId, db3Id, blobId);
1325
+ if (options.json) {
1326
+ json({ url });
1327
+ return;
1328
+ }
1329
+ console.log(url);
1330
+ });
1331
+ // ---- Sharing (#5922): link access, grants, invitations, requests, effective access ----
1332
+ const linkAccess = db3.command("link-access").description("Show or change who in the app may open a db3 by its id alone");
1333
+ linkAccess
1334
+ .command("get")
1335
+ .description("Print reader, read-write or none")
1336
+ .argument("<db3-id>", "db3 ID")
1337
+ .option("--app <app-id>", "App ID")
1338
+ .option("--json", "Output as JSON")
1339
+ .action(async (db3Id, options) => {
1340
+ const resolvedAppId = resolveAppId(undefined, options);
1341
+ try {
1342
+ const answer = await new ApiClient().getDb3LinkAccess(resolvedAppId, db3Id);
1343
+ if (options.json) {
1344
+ json(answer);
1345
+ return;
1346
+ }
1347
+ console.log(answer.linkAccess ?? "none");
1348
+ }
1349
+ catch (err) {
1350
+ fail(err);
1351
+ }
1352
+ });
1353
+ linkAccess
1354
+ .command("set")
1355
+ .description("Let any member of the app with the db3's id open it at a level")
1356
+ .argument("<db3-id>", "db3 ID")
1357
+ .requiredOption("--level <level>", "reader or read-write")
1358
+ .option("--app <app-id>", "App ID")
1359
+ .option("--json", "Output as JSON")
1360
+ .action(async (db3Id, options) => {
1361
+ const level = parseSharingLevel(options.level, "--level");
1362
+ const resolvedAppId = resolveAppId(undefined, options);
1363
+ try {
1364
+ const answer = await new ApiClient().setDb3LinkAccess(resolvedAppId, db3Id, level);
1365
+ if (options.json) {
1366
+ json(answer);
1367
+ return;
1368
+ }
1369
+ success(`Link access on ${db3Id}: ${answer?.linkAccess ?? level}.`);
1370
+ }
1371
+ catch (err) {
1372
+ fail(err);
1373
+ }
1374
+ });
1375
+ linkAccess
1376
+ .command("clear")
1377
+ .description("Turn link access off; grants are untouched")
1378
+ .argument("<db3-id>", "db3 ID")
1379
+ .option("--app <app-id>", "App ID")
1380
+ .option("--json", "Output as JSON")
1381
+ .action(async (db3Id, options) => {
1382
+ const resolvedAppId = resolveAppId(undefined, options);
1383
+ try {
1384
+ const answer = await new ApiClient().clearDb3LinkAccess(resolvedAppId, db3Id);
1385
+ if (options.json) {
1386
+ json(answer);
1387
+ return;
1388
+ }
1389
+ success(`Link access on ${db3Id} cleared.`);
1390
+ }
1391
+ catch (err) {
1392
+ fail(err);
1393
+ }
1394
+ });
1395
+ const permissions = db3.command("permissions").description("List, grant and revoke users' grants on a db3");
1396
+ permissions
1397
+ .command("list")
1398
+ .description("List the users granted the db3 and their levels")
1399
+ .argument("<db3-id>", "db3 ID")
1400
+ .option("--app <app-id>", "App ID")
1401
+ .option("--json", "Output as JSON")
1402
+ .action(async (db3Id, options) => {
1403
+ const resolvedAppId = resolveAppId(undefined, options);
1404
+ try {
1405
+ const page = wholeListEnvelope(await new ApiClient().listDb3Permissions(resolvedAppId, db3Id));
1406
+ if (options.json) {
1407
+ json(page);
1408
+ return;
1409
+ }
1410
+ printSharingTable(page.items, [
1411
+ { header: "USER_ID", key: "userId" },
1412
+ { header: "EMAIL", key: "email" },
1413
+ { header: "PERMISSION", key: "permission" },
1414
+ { header: "GRANTED", key: "grantedAt", format: formatDate },
1415
+ ], "No permissions found.");
1416
+ }
1417
+ catch (err) {
1418
+ fail(err);
1419
+ }
1420
+ });
1421
+ permissions
1422
+ .command("grant")
1423
+ .description("Grant one user a level, by user ID or by email (an address not yet in the app gets a pending invitation)")
1424
+ .argument("<db3-id>", "db3 ID")
1425
+ .option("--user-id <id>", "The user's ID")
1426
+ .option("--email <email>", "The user's email address")
1427
+ .requiredOption("--permission <level>", "reader or read-write")
1428
+ .option("--send-email", "Email the user about the grant")
1429
+ .option("--url <url>", "The link the email carries (needs --send-email; defaults to the app's base URL)")
1430
+ .option("--note <text>", "A note the email carries (needs --send-email)")
1431
+ .option("--app <app-id>", "App ID")
1432
+ .option("--json", "Output as JSON")
1433
+ .action(async (db3Id, options) => {
1434
+ const target = parseUserTarget(options);
1435
+ const permission = parseSharingLevel(options.permission, "--permission");
1436
+ if (!options.sendEmail && (options.url !== undefined || options.note !== undefined)) {
1437
+ refuseList("--url and --note go in the grant's email: pass --send-email with them.");
1438
+ }
1439
+ const resolvedAppId = resolveAppId(undefined, options);
1440
+ try {
1441
+ const answer = await new ApiClient().grantDb3Permission(resolvedAppId, db3Id, { ...target, permission }, options.sendEmail
1442
+ ? {
1443
+ sendEmail: true,
1444
+ ...(options.url !== undefined ? { documentUrl: options.url } : {}),
1445
+ ...(options.note !== undefined ? { note: options.note } : {}),
1446
+ }
1447
+ : undefined);
1448
+ if (options.json) {
1449
+ json(answer);
1450
+ return;
1451
+ }
1452
+ const who = "userId" in target ? target.userId : target.email;
1453
+ const pending = (answer?.results ?? []).filter((r) => r?.status === "pending_signup");
1454
+ if (pending.length === 0) {
1455
+ success(`Granted ${permission} on ${db3Id} to ${who}.`);
1456
+ return;
1457
+ }
1458
+ for (const row of pending) {
1459
+ info(`${row.email} has not joined the app yet: the ${row.permission ?? permission} grant is pending until they sign up.`);
1460
+ printResult("Invitation", row.invitationId);
1461
+ }
1462
+ }
1463
+ catch (err) {
1464
+ fail(err);
1465
+ }
1466
+ });
1467
+ permissions
1468
+ .command("revoke")
1469
+ .description("Revoke one user's grant, by user ID or by email")
1470
+ .argument("<db3-id>", "db3 ID")
1471
+ .option("--user-id <id>", "The user's ID")
1472
+ .option("--email <email>", "The user's email address")
1473
+ .option("--app <app-id>", "App ID")
1474
+ .option("-y, --yes", "Skip confirmation prompt")
1475
+ .option("--json", "Output as JSON")
1476
+ .action(async (db3Id, options) => {
1477
+ const target = parseUserTarget(options);
1478
+ const who = "userId" in target ? target.userId : target.email;
1479
+ const resolvedAppId = resolveAppId(undefined, options);
1480
+ if (!(await confirmed(options, `Revoke ${who}'s grant on db3 ${db3Id}?`)))
1481
+ return;
1482
+ try {
1483
+ const answer = await new ApiClient().revokeDb3Permission(resolvedAppId, db3Id, target);
1484
+ if (options.json) {
1485
+ json(answer);
1486
+ return;
1487
+ }
1488
+ success(`Revoked ${who}'s grant on ${db3Id}.`);
1489
+ }
1490
+ catch (err) {
1491
+ fail(err);
1492
+ }
1493
+ });
1494
+ const groupPermissions = db3.command("group-permissions").description("List, grant and revoke groups' grants on a db3");
1495
+ groupPermissions
1496
+ .command("list")
1497
+ .description("List the groups granted the db3 and their levels")
1498
+ .argument("<db3-id>", "db3 ID")
1499
+ .option("--include-system", "Also list the grants of the platform's system groups")
1500
+ .option("--app <app-id>", "App ID")
1501
+ .option("--json", "Output as JSON")
1502
+ .action(async (db3Id, options) => {
1503
+ const resolvedAppId = resolveAppId(undefined, options);
1504
+ try {
1505
+ const page = wholeListEnvelope(await new ApiClient().listDb3GroupPermissions(resolvedAppId, db3Id, { includeSystem: options.includeSystem === true }));
1506
+ if (options.json) {
1507
+ json(page);
1508
+ return;
1509
+ }
1510
+ const rows = page.items.map((row) => ({
1511
+ ...row,
1512
+ system: typeof row.groupType === "string" && row.groupType.startsWith("_") ? "yes" : "no",
1513
+ }));
1514
+ printSharingTable(rows, [
1515
+ { header: "GROUP_ID", key: "groupId" },
1516
+ { header: "PERMISSION", key: "permission" },
1517
+ { header: "GRANTED", key: "grantedAt", format: formatDate },
1518
+ ...(options.includeSystem ? [{ header: "SYSTEM", key: "system" }] : []),
1519
+ ], "No group permissions found.");
1520
+ }
1521
+ catch (err) {
1522
+ fail(err);
1523
+ }
1524
+ });
1525
+ groupPermissions
1526
+ .command("grant")
1527
+ .description("Grant a group, by its ID, a level: every current and future member holds it")
1528
+ .argument("<db3-id>", "db3 ID")
1529
+ .requiredOption("--group-id <id>", "The group's ID")
1530
+ .requiredOption("--permission <level>", "reader or read-write")
1531
+ .option("--app <app-id>", "App ID")
1532
+ .option("--json", "Output as JSON")
1533
+ .action(async (db3Id, options) => {
1534
+ const permission = parseSharingLevel(options.permission, "--permission");
1535
+ const resolvedAppId = resolveAppId(undefined, options);
1536
+ try {
1537
+ const answer = await new ApiClient().grantDb3GroupPermission(resolvedAppId, db3Id, options.groupId, permission);
1538
+ if (options.json) {
1539
+ json(answer);
1540
+ return;
1541
+ }
1542
+ success(`Granted ${permission} on ${db3Id} to group ${options.groupId}.`);
1543
+ }
1544
+ catch (err) {
1545
+ fail(err);
1546
+ }
1547
+ });
1548
+ groupPermissions
1549
+ .command("revoke")
1550
+ .description("Revoke a group's grant")
1551
+ .argument("<db3-id>", "db3 ID")
1552
+ .argument("<group-id>", "The group's ID")
1553
+ .option("--app <app-id>", "App ID")
1554
+ .option("-y, --yes", "Skip confirmation prompt")
1555
+ .option("--json", "Output as JSON")
1556
+ .action(async (db3Id, groupId, options) => {
1557
+ const resolvedAppId = resolveAppId(undefined, options);
1558
+ if (!(await confirmed(options, `Revoke group ${groupId}'s grant on db3 ${db3Id}?`)))
1559
+ return;
1560
+ try {
1561
+ const answer = await new ApiClient().revokeDb3GroupPermission(resolvedAppId, db3Id, groupId);
1562
+ if (options.json) {
1563
+ json(answer);
1564
+ return;
1565
+ }
1566
+ success(`Revoked group ${groupId}'s grant on ${db3Id}.`);
1567
+ }
1568
+ catch (err) {
1569
+ fail(err);
1570
+ }
1571
+ });
1572
+ const pendingInvitations = db3
1573
+ .command("pending-invitations")
1574
+ .description("List and revoke grants waiting for an email address to join the app");
1575
+ pendingInvitations
1576
+ .command("list")
1577
+ .description("List the db3's pending invitations")
1578
+ .argument("<db3-id>", "db3 ID")
1579
+ .option("--app <app-id>", "App ID")
1580
+ .option("--json", "Output as JSON")
1581
+ .action(async (db3Id, options) => {
1582
+ const resolvedAppId = resolveAppId(undefined, options);
1583
+ try {
1584
+ const answer = await new ApiClient().listDb3PendingInvitations(resolvedAppId, db3Id);
1585
+ const page = wholeListEnvelope(answer?.items);
1586
+ if (options.json) {
1587
+ json(page);
1588
+ return;
1589
+ }
1590
+ printSharingTable(page.items, [
1591
+ { header: "INVITATION_ID", key: "invitationId" },
1592
+ { header: "EMAIL", key: "email" },
1593
+ { header: "PERMISSION", key: "permission" },
1594
+ { header: "INVITED", key: "createdAt", format: formatDate },
1595
+ { header: "EXPIRES", key: "expiresAt", format: formatDate },
1596
+ ], "No pending invitations found.");
1597
+ }
1598
+ catch (err) {
1599
+ fail(err);
1600
+ }
1601
+ });
1602
+ pendingInvitations
1603
+ .command("revoke")
1604
+ .description("Cancel a pending invitation's grants on this db3; the app invitation and its other shares stay")
1605
+ .argument("<db3-id>", "db3 ID")
1606
+ .argument("<invitation-id>", "The invitation's ID, from pending-invitations list")
1607
+ .option("--app <app-id>", "App ID")
1608
+ .option("-y, --yes", "Skip confirmation prompt")
1609
+ .option("--json", "Output as JSON")
1610
+ .action(async (db3Id, invitationId, options) => {
1611
+ const resolvedAppId = resolveAppId(undefined, options);
1612
+ if (!(await confirmed(options, `Revoke pending invitation ${invitationId} on db3 ${db3Id}?`)))
1613
+ return;
1614
+ try {
1615
+ const answer = await new ApiClient().revokeDb3PendingInvitation(resolvedAppId, db3Id, invitationId);
1616
+ if (options.json) {
1617
+ json(answer);
1618
+ return;
1619
+ }
1620
+ success(`Revoked the pending invitation for ${answer?.email ?? invitationId} on ${db3Id}.`);
1621
+ }
1622
+ catch (err) {
1623
+ fail(err);
1624
+ }
1625
+ });
1626
+ const accessRequests = db3.command("access-requests").description("List, file and resolve requests for access to a db3");
1627
+ accessRequests
1628
+ .command("list")
1629
+ .description("List the db3's pending access requests")
1630
+ .argument("<db3-id>", "db3 ID")
1631
+ .option("--app <app-id>", "App ID")
1632
+ .option("--json", "Output as JSON")
1633
+ .action(async (db3Id, options) => {
1634
+ const resolvedAppId = resolveAppId(undefined, options);
1635
+ try {
1636
+ const page = wholeListEnvelope(await new ApiClient().listDb3AccessRequests(resolvedAppId, db3Id));
1637
+ if (options.json) {
1638
+ json(page);
1639
+ return;
1640
+ }
1641
+ printSharingTable(page.items.map((row) => ({ ...row, message: row.message ?? "" })), [
1642
+ { header: "REQUEST_ID", key: "requestId" },
1643
+ { header: "REQUESTER", key: "requesterId" },
1644
+ { header: "PERMISSION", key: "requestedPermission" },
1645
+ { header: "MESSAGE", key: "message" },
1646
+ { header: "CREATED", key: "createdAt", format: formatDate },
1647
+ ], "No access requests found.");
1648
+ }
1649
+ catch (err) {
1650
+ fail(err);
1651
+ }
1652
+ });
1653
+ accessRequests
1654
+ .command("create")
1655
+ .description("Ask the db3's owners for access as the signed-in user (a member's login; an admin already has access)")
1656
+ .argument("<db3-id>", "db3 ID")
1657
+ .requiredOption("--permission <level>", "reader or read-write")
1658
+ .option("--message <text>", "A message to the owners")
1659
+ .option("--url <url>", "The link the owners' email carries")
1660
+ .option("--review-url <url>", "The link to review the request (defaults to --url)")
1661
+ .option("--no-send-email", "Do not email the owners")
1662
+ .option("--app <app-id>", "App ID")
1663
+ .option("--json", "Output as JSON")
1664
+ .action(async (db3Id, options) => {
1665
+ const permission = parseSharingLevel(options.permission, "--permission");
1666
+ const resolvedAppId = resolveAppId(undefined, options);
1667
+ try {
1668
+ const { answer, created } = await new ApiClient().createDb3AccessRequest(resolvedAppId, db3Id, {
1669
+ permission,
1670
+ ...(options.message !== undefined ? { message: options.message } : {}),
1671
+ ...(options.url !== undefined ? { documentUrl: options.url } : {}),
1672
+ ...(options.reviewUrl !== undefined ? { reviewUrl: options.reviewUrl } : {}),
1673
+ ...(options.sendEmail === false ? { sendEmail: false } : {}),
1674
+ });
1675
+ if (options.json) {
1676
+ json(answer);
1677
+ return;
1678
+ }
1679
+ success(created
1680
+ ? `Access request created for ${permission} on ${db3Id}.`
1681
+ : `Your pending access request on ${db3Id} was updated to ${permission}.`);
1682
+ printResult("Request", answer?.request?.requestId);
1683
+ }
1684
+ catch (err) {
1685
+ fail(err);
1686
+ }
1687
+ });
1688
+ accessRequests
1689
+ .command("approve")
1690
+ .description("Approve a request: grants the requested level, or --permission")
1691
+ .argument("<db3-id>", "db3 ID")
1692
+ .argument("<request-id>", "The request's ID, from access-requests list")
1693
+ .option("--permission <level>", "reader or read-write, in place of the requested level")
1694
+ .option("--url <url>", "The link the requester's email carries")
1695
+ .option("--app <app-id>", "App ID")
1696
+ .option("--json", "Output as JSON")
1697
+ .action(async (db3Id, requestId, options) => {
1698
+ const permission = options.permission !== undefined ? parseSharingLevel(options.permission, "--permission") : undefined;
1699
+ const resolvedAppId = resolveAppId(undefined, options);
1700
+ try {
1701
+ const answer = await new ApiClient().approveDb3AccessRequest(resolvedAppId, db3Id, requestId, {
1702
+ ...(permission !== undefined ? { permission } : {}),
1703
+ ...(options.url !== undefined ? { documentUrl: options.url } : {}),
1704
+ });
1705
+ if (options.json) {
1706
+ json(answer);
1707
+ return;
1708
+ }
1709
+ success(`Approved request ${requestId}: granted ${answer?.request?.grantedPermission ?? permission ?? "the requested level"} on ${db3Id}.`);
1710
+ }
1711
+ catch (err) {
1712
+ fail(err);
1713
+ }
1714
+ });
1715
+ accessRequests
1716
+ .command("deny")
1717
+ .description("Deny a request; the requester may ask again")
1718
+ .argument("<db3-id>", "db3 ID")
1719
+ .argument("<request-id>", "The request's ID, from access-requests list")
1720
+ .option("--url <url>", "The link the requester's email carries")
1721
+ .option("--app <app-id>", "App ID")
1722
+ .option("--json", "Output as JSON")
1723
+ .action(async (db3Id, requestId, options) => {
1724
+ const resolvedAppId = resolveAppId(undefined, options);
1725
+ try {
1726
+ const answer = await new ApiClient().denyDb3AccessRequest(resolvedAppId, db3Id, requestId, options.url !== undefined ? { documentUrl: options.url } : {});
1727
+ if (options.json) {
1728
+ json(answer);
1729
+ return;
1730
+ }
1731
+ success(`Denied request ${requestId} on ${db3Id}.`);
1732
+ }
1733
+ catch (err) {
1734
+ fail(err);
1735
+ }
1736
+ });
1737
+ const access = db3.command("access").description("Show a user's effective access to a db3");
1738
+ access
1739
+ .command("get")
1740
+ .description("Show a user's level on the db3 and where it comes from: a grant, a group, the link, app access or ownership")
1741
+ .argument("<db3-id>", "db3 ID")
1742
+ .requiredOption("--user-id <id>", "The user's ID")
1743
+ .option("--app <app-id>", "App ID")
1744
+ .option("--json", "Output as JSON")
1745
+ .action(async (db3Id, options) => {
1746
+ const resolvedAppId = resolveAppId(undefined, options);
1747
+ try {
1748
+ const answer = await new ApiClient().validateDb3AccessForUser(resolvedAppId, db3Id, options.userId);
1749
+ const row = {
1750
+ db3Id,
1751
+ userId: options.userId,
1752
+ hasAccess: answer?.hasAccess === true,
1753
+ permission: answer?.permission ?? null,
1754
+ accessSource: answer?.accessSource ?? null,
1755
+ appRole: answer?.appRole ?? null,
1756
+ };
1757
+ if (options.json) {
1758
+ json(row);
1759
+ return;
1760
+ }
1761
+ console.log(formatTable([{ ...row, hasAccess: String(row.hasAccess), permission: row.permission ?? "-", accessSource: row.accessSource ?? "-", appRole: row.appRole ?? "-" }], [
1762
+ { header: "DB3", key: "db3Id" },
1763
+ { header: "USER", key: "userId" },
1764
+ { header: "ACCESS", key: "hasAccess" },
1765
+ { header: "PERMISSION", key: "permission" },
1766
+ { header: "SOURCE", key: "accessSource" },
1767
+ { header: "APP_ROLE", key: "appRole" },
1768
+ ]));
1769
+ if (!row.hasAccess)
1770
+ info(`${row.userId} has no access to ${db3Id}.`);
1771
+ }
1772
+ catch (err) {
1773
+ fail(err);
1774
+ }
1775
+ });
1182
1776
  // ---- Custody (#4800, #4804): through the admin API ----
1183
1777
  db3
1184
1778
  .command("transfer")
@@ -1383,7 +1977,7 @@ async function importAction(dir, options) {
1383
1977
  return 1;
1384
1978
  }
1385
1979
  }
1386
- const outcome = await loadDb3Rows(client, resolvedAppId, db3Id, root, (line) => info(line));
1980
+ const outcome = await loadDb3Rows(client, resolvedAppId, db3Id, root, (line) => info(line), manifest.declaration);
1387
1981
  const session = outcome?.session ?? null;
1388
1982
  if (outcome && outcome.exitCode !== 0) {
1389
1983
  if (outcome.message)
@@ -1441,4 +2035,19 @@ async function importAction(dir, options) {
1441
2035
  return 1;
1442
2036
  }
1443
2037
  }
2038
+ /**
2039
+ * The public blob route's URL, as `client.db3.publicBlobUrl` builds it: the
2040
+ * API host, then `/app/<appId>/api/public/db3/<db3Id>/blobs/<blobId>` with
2041
+ * every id percent-encoded.
2042
+ *
2043
+ * @param apiUrl - The environment's API host.
2044
+ * @param appId - The app.
2045
+ * @param db3Id - The public db3.
2046
+ * @param blobId - The blob.
2047
+ * @returns The URL.
2048
+ */
2049
+ export function publicBlobUrl(apiUrl, appId, db3Id, blobId) {
2050
+ const base = apiUrl.replace(/\/+$/, "");
2051
+ return `${base}/app/${encodeURIComponent(appId)}/api/public/db3/${encodeURIComponent(db3Id)}/blobs/${encodeURIComponent(blobId)}`;
2052
+ }
1444
2053
  //# sourceMappingURL=db3.js.map