@backmatter/core 0.0.20 → 0.0.21

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/preview.ts +20 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backmatter/core",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/src/preview.ts CHANGED
@@ -67,6 +67,16 @@ export type DevMode = {
67
67
  tree?: string;
68
68
  };
69
69
 
70
+ /** Why the host session endpoint refused to open the bar. */
71
+ export type HostSessionReason = "invalid" | "anonymous" | "unavailable";
72
+
73
+ export type HostPreviewSession = {
74
+ enabled: boolean;
75
+ branch?: string;
76
+ tree?: string;
77
+ reason?: HostSessionReason;
78
+ };
79
+
70
80
  export function resolveDevMode(session: {
71
81
  enabled: boolean;
72
82
  branch?: string;
@@ -79,28 +89,32 @@ export function resolveDevMode(session: {
79
89
  };
80
90
  }
81
91
 
82
- export async function fetchHostPreviewSession(
83
- origin = "",
84
- ): Promise<{ enabled: boolean; branch?: string; tree?: string }> {
92
+ function hostSessionReason(value: unknown): HostSessionReason {
93
+ if (value === "invalid" || value === "unavailable") return value;
94
+ return "anonymous";
95
+ }
96
+
97
+ export async function fetchHostPreviewSession(origin = ""): Promise<HostPreviewSession> {
85
98
  try {
86
99
  const res = await fetch(`${origin.replace(/\/$/, "")}/api/backmatter/preview?session=1`, {
87
100
  credentials: "same-origin",
88
101
  cache: "no-store",
89
102
  });
90
- if (!res.ok) return { enabled: false };
103
+ if (!res.ok) return { enabled: false, reason: "unavailable" };
91
104
  const data = (await res.json()) as {
92
105
  mode?: string;
93
106
  branch?: string;
94
107
  tree?: string;
108
+ reason?: unknown;
95
109
  };
96
- if (data.mode !== "preview") return { enabled: false };
110
+ if (data.mode !== "preview") return { enabled: false, reason: hostSessionReason(data.reason) };
97
111
  return {
98
112
  enabled: true,
99
113
  ...(typeof data.branch === "string" && data.branch ? { branch: data.branch } : {}),
100
114
  ...(typeof data.tree === "string" && data.tree ? { tree: data.tree } : {}),
101
115
  };
102
116
  } catch {
103
- return { enabled: false };
117
+ return { enabled: false, reason: "unavailable" };
104
118
  }
105
119
  }
106
120