@backmatter/core 0.0.21 → 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.21",
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
@@ -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;
@@ -107,12 +110,16 @@ export async function fetchHostPreviewSession(origin = ""): Promise<HostPreviewS
107
110
  tree?: string;
108
111
  reason?: unknown;
109
112
  };
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
- };
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) };
116
123
  } catch {
117
124
  return { enabled: false, reason: "unavailable" };
118
125
  }
@@ -131,6 +138,34 @@ export async function clearHostPreviewSession(origin = ""): Promise<boolean> {
131
138
  }
132
139
  }
133
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
+
134
169
  export type CmsSessionUser = { id: string; email: string; name: string };
135
170
 
136
171
  /** Cookie session on the CMS API. Fail closed: empty URL, 401, errors → null. */