@backmatter/core 0.0.21 → 0.0.23

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.21",
3
+ "version": "0.0.23",
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
@@ -71,7 +71,10 @@ export type DevMode = {
71
71
  export type HostSessionReason = "invalid" | "anonymous" | "unavailable";
72
72
 
73
73
  export type HostPreviewSession = {
74
+ /** Draft preview is on (checkout cookie + draft mode). */
74
75
  enabled: boolean;
76
+ /** Signed into the site session but viewing the published site. */
77
+ signedIn?: boolean;
75
78
  branch?: string;
76
79
  tree?: string;
77
80
  reason?: HostSessionReason;
@@ -103,16 +106,21 @@ export async function fetchHostPreviewSession(origin = ""): Promise<HostPreviewS
103
106
  if (!res.ok) return { enabled: false, reason: "unavailable" };
104
107
  const data = (await res.json()) as {
105
108
  mode?: string;
109
+ signedIn?: boolean;
106
110
  branch?: string;
107
111
  tree?: string;
108
112
  reason?: unknown;
109
113
  };
110
- if (data.mode !== "preview") return { enabled: false, reason: hostSessionReason(data.reason) };
111
- return {
112
- enabled: true,
113
- ...(typeof data.branch === "string" && data.branch ? { branch: data.branch } : {}),
114
- ...(typeof data.tree === "string" && data.tree ? { tree: data.tree } : {}),
115
- };
114
+ const branch =
115
+ typeof data.branch === "string" && data.branch ? { branch: data.branch } : {};
116
+ const tree = typeof data.tree === "string" && data.tree ? { tree: data.tree } : {};
117
+ if (data.mode === "preview") {
118
+ return { enabled: true, signedIn: true, ...branch, ...tree };
119
+ }
120
+ if (data.signedIn === true) {
121
+ return { enabled: false, signedIn: true, ...branch, ...tree };
122
+ }
123
+ return { enabled: false, reason: hostSessionReason(data.reason) };
116
124
  } catch {
117
125
  return { enabled: false, reason: "unavailable" };
118
126
  }
@@ -131,6 +139,34 @@ export async function clearHostPreviewSession(origin = ""): Promise<boolean> {
131
139
  }
132
140
  }
133
141
 
142
+ /** Turn off draft preview but keep the site login session and bar marker. */
143
+ export async function setHostSiteLive(origin = ""): Promise<boolean> {
144
+ try {
145
+ const res = await fetch(`${origin.replace(/\/$/, "")}/api/backmatter/preview?live=1`, {
146
+ method: "POST",
147
+ credentials: "same-origin",
148
+ cache: "no-store",
149
+ });
150
+ return res.ok;
151
+ } catch {
152
+ return false;
153
+ }
154
+ }
155
+
156
+ /** Re-enable draft preview for the current site session checkout. */
157
+ export async function setHostSitePreview(origin = ""): Promise<boolean> {
158
+ try {
159
+ const res = await fetch(`${origin.replace(/\/$/, "")}/api/backmatter/preview?preview=1`, {
160
+ method: "POST",
161
+ credentials: "same-origin",
162
+ cache: "no-store",
163
+ });
164
+ return res.ok;
165
+ } catch {
166
+ return false;
167
+ }
168
+ }
169
+
134
170
  export type CmsSessionUser = { id: string; email: string; name: string };
135
171
 
136
172
  /** Cookie session on the CMS API. Fail closed: empty URL, 401, errors → null. */