@backmatter/core 0.0.39 → 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.39",
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": {
@@ -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)) ?? {};