@backmatter/core 0.0.20 → 0.0.22

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.20",
3
+ "version": "0.0.22",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/src/config.ts CHANGED
@@ -3,6 +3,12 @@
3
3
  // here; they are discovered from the `models/` folder.
4
4
  import { detectGitBranch } from "./sync-schema";
5
5
 
6
+ /** Options for `<DevBar />` on the host site. */
7
+ export type DevBarHostConfig = {
8
+ /** Draw a viewport ring and top pill while the site session is in preview. */
9
+ previewFrame?: boolean;
10
+ };
11
+
6
12
  export type BackmatterDirs = {
7
13
  /** Folder with one `defineModel` default export per file. Default: "models". */
8
14
  models?: string;
@@ -45,6 +51,7 @@ export type BackmatterConfig = {
45
51
  /** Locale used by routes without a `{locale}` placeholder. Default: "en". */
46
52
  defaultLocale?: string;
47
53
  dirs?: BackmatterDirs;
54
+ devBar?: DevBarHostConfig;
48
55
  };
49
56
 
50
57
  export type ResolvedConfig = Omit<BackmatterConfig, "dirs" | "defaultLocale"> & {
@@ -211,6 +218,7 @@ export function resolveConfig(
211
218
  siteRoot: config.siteRoot,
212
219
  locales: config.locales,
213
220
  defaultLocale: config.defaultLocale ?? DEFAULT_LOCALE,
221
+ devBar: config.devBar,
214
222
  dirs: {
215
223
  models: config.dirs?.models ?? DEFAULT_DIRS.models,
216
224
  blocks: config.dirs?.blocks ?? DEFAULT_DIRS.blocks,
package/src/index.ts CHANGED
@@ -51,7 +51,7 @@ export { defineSource } from "./source";
51
51
  export type { DocFormat } from "./doc-format";
52
52
  export { modelFormat, parseDoc, serializeDoc } from "./doc-format";
53
53
 
54
- export type { BackmatterConfig, BackmatterDirs, ResolvedConfig } from "./config";
54
+ export type { BackmatterConfig, BackmatterDirs, DevBarHostConfig, ResolvedConfig } from "./config";
55
55
  export {
56
56
  defineConfig,
57
57
  resolveConfig,
package/src/preview.ts CHANGED
@@ -67,6 +67,19 @@ 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
+ /** Draft preview is on (checkout cookie + draft mode). */
75
+ enabled: boolean;
76
+ /** Signed into the site session but viewing the published site. */
77
+ signedIn?: boolean;
78
+ branch?: string;
79
+ tree?: string;
80
+ reason?: HostSessionReason;
81
+ };
82
+
70
83
  export function resolveDevMode(session: {
71
84
  enabled: boolean;
72
85
  branch?: string;
@@ -79,28 +92,36 @@ export function resolveDevMode(session: {
79
92
  };
80
93
  }
81
94
 
82
- export async function fetchHostPreviewSession(
83
- origin = "",
84
- ): Promise<{ enabled: boolean; branch?: string; tree?: string }> {
95
+ function hostSessionReason(value: unknown): HostSessionReason {
96
+ if (value === "invalid" || value === "unavailable") return value;
97
+ return "anonymous";
98
+ }
99
+
100
+ export async function fetchHostPreviewSession(origin = ""): Promise<HostPreviewSession> {
85
101
  try {
86
102
  const res = await fetch(`${origin.replace(/\/$/, "")}/api/backmatter/preview?session=1`, {
87
103
  credentials: "same-origin",
88
104
  cache: "no-store",
89
105
  });
90
- if (!res.ok) return { enabled: false };
106
+ if (!res.ok) return { enabled: false, reason: "unavailable" };
91
107
  const data = (await res.json()) as {
92
108
  mode?: string;
93
109
  branch?: string;
94
110
  tree?: string;
111
+ reason?: unknown;
95
112
  };
96
- if (data.mode !== "preview") return { enabled: false };
97
- return {
98
- enabled: true,
99
- ...(typeof data.branch === "string" && data.branch ? { branch: data.branch } : {}),
100
- ...(typeof data.tree === "string" && data.tree ? { tree: data.tree } : {}),
101
- };
113
+ const branch =
114
+ typeof data.branch === "string" && data.branch ? { branch: data.branch } : {};
115
+ const tree = typeof data.tree === "string" && data.tree ? { tree: data.tree } : {};
116
+ if (data.mode === "preview") {
117
+ return { enabled: true, signedIn: true, ...branch, ...tree };
118
+ }
119
+ if (data.signedIn === true) {
120
+ return { enabled: false, signedIn: true, ...branch, ...tree };
121
+ }
122
+ return { enabled: false, reason: hostSessionReason(data.reason) };
102
123
  } catch {
103
- return { enabled: false };
124
+ return { enabled: false, reason: "unavailable" };
104
125
  }
105
126
  }
106
127
 
@@ -117,6 +138,34 @@ export async function clearHostPreviewSession(origin = ""): Promise<boolean> {
117
138
  }
118
139
  }
119
140
 
141
+ /** Turn off draft preview but keep the site login session and bar marker. */
142
+ export async function setHostSiteLive(origin = ""): Promise<boolean> {
143
+ try {
144
+ const res = await fetch(`${origin.replace(/\/$/, "")}/api/backmatter/preview?live=1`, {
145
+ method: "POST",
146
+ credentials: "same-origin",
147
+ cache: "no-store",
148
+ });
149
+ return res.ok;
150
+ } catch {
151
+ return false;
152
+ }
153
+ }
154
+
155
+ /** Re-enable draft preview for the current site session checkout. */
156
+ export async function setHostSitePreview(origin = ""): Promise<boolean> {
157
+ try {
158
+ const res = await fetch(`${origin.replace(/\/$/, "")}/api/backmatter/preview?preview=1`, {
159
+ method: "POST",
160
+ credentials: "same-origin",
161
+ cache: "no-store",
162
+ });
163
+ return res.ok;
164
+ } catch {
165
+ return false;
166
+ }
167
+ }
168
+
120
169
  export type CmsSessionUser = { id: string; email: string; name: string };
121
170
 
122
171
  /** Cookie session on the CMS API. Fail closed: empty URL, 401, errors → null. */