@backmatter/core 0.0.38 → 0.0.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backmatter/core",
3
- "version": "0.0.38",
3
+ "version": "0.0.40",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/src/fields.ts CHANGED
@@ -49,10 +49,24 @@ export type FieldDef<
49
49
  step?: number;
50
50
  where?: RelationWhere;
51
51
  multiple?: true;
52
+ /** For blocks fields: how Studio's "Add block" picker lays out the blocks. */
53
+ picker?: BlockPickerView;
52
54
  /** Guidance for editors and the AI: what goes here, length, tone. Never rendered. */
53
55
  description?: string;
54
56
  };
55
57
 
58
+ /**
59
+ * Layout of Studio's "Add block" picker. "grouped" opens on the block groups,
60
+ * "flat" shows every block at once, "auto" groups only when there are many.
61
+ */
62
+ export type BlockPickerView = "auto" | "grouped" | "flat";
63
+
64
+ const PICKER_VIEWS: readonly BlockPickerView[] = ["auto", "grouped", "flat"];
65
+
66
+ function isPickerView(value: unknown): value is BlockPickerView {
67
+ return PICKER_VIEWS.includes(value as BlockPickerView);
68
+ }
69
+
56
70
  export type FieldSnapshot = {
57
71
  kind: FieldKind;
58
72
  required?: boolean;
@@ -68,6 +82,7 @@ export type FieldSnapshot = {
68
82
  step?: number;
69
83
  where?: RelationWhere;
70
84
  multiple?: true;
85
+ picker?: BlockPickerView;
71
86
  description?: string;
72
87
  };
73
88
 
@@ -491,7 +506,13 @@ const constructors = {
491
506
  boolean: <const R extends boolean = false>(opts?: Opts<R>) => base("boolean", opts),
492
507
  relation,
493
508
  link,
494
- blocks: <const R extends boolean = false>(opts?: Opts<R>) => base("blocks", opts),
509
+ blocks: <const R extends boolean = false>(opts?: Opts<R> & { picker?: BlockPickerView }) => {
510
+ if (opts?.picker !== undefined && !isPickerView(opts.picker)) {
511
+ throw new Error('field.blocks picker must be "auto", "grouped" or "flat"');
512
+ }
513
+ const def = base("blocks", { required: opts?.required, input: opts?.input });
514
+ return opts?.picker ? { ...def, picker: opts.picker } : def;
515
+ },
495
516
  object: objectField,
496
517
  array: arrayField,
497
518
  };
@@ -524,6 +545,7 @@ export function serializeField(def: FieldDef): FieldSnapshot {
524
545
  ...(def.models ? { models: [...def.models] } : {}),
525
546
  ...(def.where ? { where: def.where } : {}),
526
547
  ...(def.multiple ? { multiple: true as const } : {}),
548
+ ...(def.picker ? { picker: def.picker } : {}),
527
549
  ...(def.description?.trim() ? { description: def.description.trim() } : {}),
528
550
  };
529
551
  if (def.fields) {
@@ -607,6 +629,7 @@ export function parseFieldSnapshot(input: unknown): FieldSnapshot {
607
629
  if (where) {
608
630
  snap.where = where;
609
631
  }
632
+ if (isPickerView(value.picker)) snap.picker = value.picker;
610
633
  if (typeof value.description === "string" && value.description.trim()) {
611
634
  snap.description = value.description.trim();
612
635
  }
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export type {
6
6
  FieldDef,
7
7
  FieldKind,
8
8
  FieldSnapshot,
9
+ BlockPickerView,
9
10
  } from "./fields";
10
11
  export { field, serializeField, parseFieldSnapshot } from "./fields";
11
12
  export {
@@ -12,12 +12,16 @@ export const MEDIA_USAGE = `Usage:
12
12
  backmatter media url <id> [--width <px>] [--origin <url>]
13
13
 
14
14
  login opens the browser, where you confirm the code and pick one workspace.
15
+ It calls --api, else BACKMATTER_API_URL, else the folder's linked api, else the
16
+ api of your saved logins when they share one, else https://api.backmatter.app.
15
17
  It links this folder to that workspace in .backmatter/workspace.json and keeps
16
18
  the token, which works only in that workspace, in ~/.backmatter/credentials.json.
17
19
  media upload and list use --workspace, else BACKMATTER_WORKSPACE, else
18
20
  BACKMATTER_WORKSPACE_ID, else the folder's linked workspace.
19
21
  media url prints a CDN url. --width <px> appends width and format=auto.`;
20
22
 
23
+ const HOSTED_API = "https://api.backmatter.app";
24
+
21
25
  type WorkspaceRef = { id: string; name: string; slug: string };
22
26
  /** One workspace's token. The token works in that workspace only. */
23
27
  type Login = { api: string; workspace: WorkspaceRef; token: string };
@@ -78,7 +82,13 @@ export async function runMediaCli(
78
82
  async function login(argv: string[], io: Io): Promise<number> {
79
83
  const flags = readFlags(argv);
80
84
  const link = await findFolderLink(io.cwd);
81
- const api = (flags.api || io.env.BACKMATTER_API_URL || link?.link.api || "http://localhost:3001").replace(/\/$/, "");
85
+ const api = (
86
+ flags.api ||
87
+ io.env.BACKMATTER_API_URL ||
88
+ link?.link.api ||
89
+ savedApi(await readCredentials(io.home)) ||
90
+ HOSTED_API
91
+ ).replace(/\/$/, "");
82
92
  const web = (flags.web || io.env.BACKMATTER_WEB_ORIGIN || "http://localhost:5173").replace(/\/$/, "");
83
93
  const workspace =
84
94
  flags.workspace ||
@@ -87,12 +97,15 @@ async function login(argv: string[], io: Io): Promise<number> {
87
97
  (link && link.link.api === api ? link.link.workspaceId : undefined);
88
98
  const pollSecret = crypto.randomUUID() + crypto.randomUUID();
89
99
  const fetchImpl = io.fetch ?? fetch;
90
- const started = await fetchImpl(`${api}/cli/login`, {
100
+ const startUrl = `${api}/cli/login`;
101
+ const started = await fetchImpl(startUrl, {
91
102
  method: "POST",
92
103
  headers: { "content-type": "application/json" },
93
104
  body: JSON.stringify({ pollSecret, workspace, client: io.hostname }),
105
+ }).catch((err: unknown) => {
106
+ throw new Error(`login failed: could not reach ${startUrl} (${err instanceof Error ? err.message : String(err)})`);
94
107
  });
95
- if (!started.ok) throw new Error(`login failed: ${started.status}`);
108
+ if (!started.ok) throw new Error(`login failed: ${started.status} from ${startUrl}`);
96
109
  const body = (await started.json()) as { id: string; userCode?: string; authorizeUrl: string };
97
110
  const authorizeUrl = body.authorizeUrl || `${web}/cli/authorize?id=${encodeURIComponent(body.id)}`;
98
111
  (io.open ?? openBrowser)(authorizeUrl);
@@ -245,6 +258,16 @@ async function readCredentials(home: string): Promise<Credentials | null> {
245
258
  return readJson<Credentials>(credentialsPath(home));
246
259
  }
247
260
 
261
+ /** The api every saved login uses, when they agree on one. */
262
+ function savedApi(creds: Credentials | null): string | undefined {
263
+ const apis = new Set(
264
+ [...(creds?.logins ?? []).map((entry) => entry.api), creds?.api]
265
+ .filter((api): api is string => !!api)
266
+ .map((api) => api.replace(/\/$/, "")),
267
+ );
268
+ return apis.size === 1 ? [...apis][0] : undefined;
269
+ }
270
+
248
271
  /** Keeps one token per api and workspace, replacing an older one for the same pair. */
249
272
  async function saveLogin(home: string, login: Login): Promise<void> {
250
273
  const creds = (await readCredentials(home)) ?? {};