sanity-plugin-seo-copilot 0.2.14 → 0.2.15

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,61 +1,64 @@
1
- {
2
- "name": "sanity-plugin-seo-copilot",
3
- "version": "0.2.14",
4
- "description": "AI-powered SEO writing assistant for Sanity Studio",
5
- "keywords": [
6
- "sanity",
7
- "sanity-plugin",
8
- "seo",
9
- "copilot"
10
- ],
11
- "license": "UNLICENSED",
12
- "author": "MJH Life Sciences",
13
- "exports": {
14
- ".": {
15
- "source": "./src/index.ts",
16
- "import": "./dist/index.mjs",
17
- "require": "./dist/index.js",
18
- "default": "./dist/index.js"
19
- },
20
- "./package.json": "./package.json"
21
- },
22
- "main": "./dist/index.js",
23
- "module": "./dist/index.mjs",
24
- "types": "./dist/index.d.ts",
25
- "files": [
26
- "dist",
27
- "sanity.json",
28
- "src",
29
- "v2-incompatible.js"
30
- ],
31
- "scripts": {
32
- "build": "tsup",
33
- "prepublishOnly": "npm run build"
34
- },
35
- "dependencies": {
36
- "react-markdown": "^10.1.0",
37
- "remark-gfm": "^4.0.1"
38
- },
39
- "devDependencies": {
40
- "@sanity/icons": "^3.7.4",
41
- "@sanity/ui": "^3.1.13",
42
- "@types/react": "^19.0.0",
43
- "@types/react-dom": "^19.2.3",
44
- "react": "^19.2.4",
45
- "react-dom": "^19.2.4",
46
- "sanity": "^5.13.0",
47
- "styled-components": "^6.3.11",
48
- "tsup": "^8.0.0",
49
- "typescript": "^5.9.3"
50
- },
51
- "peerDependencies": {
52
- "@sanity/icons": "^3.0.0",
53
- "@sanity/ui": "^2.0.0 || ^3.0.0",
54
- "react": "^18.0.0 || ^19.0.0",
55
- "sanity": "^3.0.0 || ^5.0.0",
56
- "styled-components": "^6.0.0"
57
- },
58
- "engines": {
59
- "node": ">=18"
60
- }
61
- }
1
+ {
2
+ "name": "sanity-plugin-seo-copilot",
3
+ "version": "0.2.15",
4
+ "description": "AI-powered SEO writing assistant for Sanity Studio",
5
+ "keywords": [
6
+ "sanity",
7
+ "sanity-plugin",
8
+ "seo",
9
+ "copilot"
10
+ ],
11
+ "license": "UNLICENSED",
12
+ "author": "MJH Life Sciences",
13
+ "exports": {
14
+ ".": {
15
+ "source": "./src/index.ts",
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.js",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "main": "./dist/index.js",
23
+ "module": "./dist/index.mjs",
24
+ "types": "./dist/index.d.ts",
25
+ "files": [
26
+ "dist",
27
+ "sanity.json",
28
+ "src",
29
+ "v2-incompatible.js",
30
+ "!src/**/*.test.ts"
31
+ ],
32
+ "scripts": {
33
+ "build": "tsup",
34
+ "prepublishOnly": "npm run build",
35
+ "test": "vitest run"
36
+ },
37
+ "dependencies": {
38
+ "react-markdown": "^10.1.0",
39
+ "remark-gfm": "^4.0.1"
40
+ },
41
+ "devDependencies": {
42
+ "@sanity/icons": "^3.7.4",
43
+ "@sanity/ui": "^3.1.13",
44
+ "@types/react": "^19.0.0",
45
+ "@types/react-dom": "^19.2.3",
46
+ "react": "^19.2.4",
47
+ "react-dom": "^19.2.4",
48
+ "sanity": "^5.13.0",
49
+ "styled-components": "^6.3.11",
50
+ "tsup": "^8.0.0",
51
+ "typescript": "^5.9.3",
52
+ "vitest": "^4.1.11"
53
+ },
54
+ "peerDependencies": {
55
+ "@sanity/icons": "^3.0.0",
56
+ "@sanity/ui": "^2.0.0 || ^3.0.0",
57
+ "react": "^18.0.0 || ^19.0.0",
58
+ "sanity": "^3.0.0 || ^5.0.0",
59
+ "styled-components": "^6.0.0"
60
+ },
61
+ "engines": {
62
+ "node": ">=18"
63
+ }
64
+ }
@@ -7,6 +7,7 @@ import { LoadingBars } from "./SEOCopilotPanel";
7
7
  import { MJH_GOLD, MJH_BLUE } from "./styles";
8
8
  import { usePersistentState, loadLocal } from "../lib/persistence";
9
9
  import type { PrimaryKeywordCandidate, DocumentFields } from "../lib/types";
10
+ import { jsonHeaders } from "../lib/apiKey";
10
11
 
11
12
  function loadLocalArray<T>(key: string): T[] {
12
13
  return loadLocal<T[]>(key, []);
@@ -538,7 +539,7 @@ function SuggestedKeywordsCard({
538
539
  : undefined;
539
540
  const res = await fetch(`${apiUrl}/api/seo-copilot/keyword-help`, {
540
541
  method: "POST",
541
- headers: { "Content-Type": "application/json" },
542
+ headers: jsonHeaders(),
542
543
  // Clip to 5,000 chars: the backend only reads the first 4,000 anyway, so sending
543
544
  // the full article needlessly bloats the request on long pieces.
544
545
  body: JSON.stringify({ keyword: term, content: text.slice(0, 5000), publication, previousSuggestions }),
@@ -574,7 +575,7 @@ function SuggestedKeywordsCard({
574
575
  try {
575
576
  const res = await fetch(`${apiUrl}/api/seo-copilot/keywords`, {
576
577
  method: "POST",
577
- headers: { "Content-Type": "application/json" },
578
+ headers: jsonHeaders(),
578
579
  body: JSON.stringify({ keywords: [trimmed] }),
579
580
  });
580
581
  if (res.ok) {
@@ -670,7 +671,7 @@ function SuggestedKeywordsCard({
670
671
  const existingTerms = allKeywords.map((kw) => kw.term);
671
672
  const res = await fetch(`${apiUrl}/api/seo-copilot/more-keywords`, {
672
673
  method: "POST",
673
- headers: { "Content-Type": "application/json" },
674
+ headers: jsonHeaders(),
674
675
  body: JSON.stringify({ content: text, existingKeywords: existingTerms, publication }),
675
676
  });
676
677
  if (res.ok) {
package/src/index.ts CHANGED
@@ -1,16 +1,18 @@
1
- import { definePlugin } from "sanity";
2
- import { seoCopilotInspector } from "./inspector";
3
-
4
- export interface SEOCopilotConfig {
5
- apiUrl: string;
6
- publication?: string;
7
- }
8
-
9
- export const seoCopilot = definePlugin<SEOCopilotConfig>((config) => ({
10
- name: "seo-copilot",
11
- document: {
12
- inspectors: (prev) => {
13
- return [...prev, seoCopilotInspector(config)];
14
- },
15
- },
16
- }));
1
+ import { definePlugin } from "sanity";
2
+ import { seoCopilotInspector } from "./inspector";
3
+
4
+ export interface SEOCopilotConfig {
5
+ apiUrl: string;
6
+ publication?: string;
7
+ }
8
+
9
+ export const seoCopilot = definePlugin<SEOCopilotConfig>((config) => {
10
+ return {
11
+ name: "seo-copilot",
12
+ document: {
13
+ inspectors: (prev) => {
14
+ return [...prev, seoCopilotInspector(config)];
15
+ },
16
+ },
17
+ };
18
+ });
package/src/lib/api.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { SEOAnalysis, DeepAnalysis, ChatMessage, DocumentFields, ContentSuggestions, LinkingSuggestion, LinkCheckResult } from "./types";
2
+ import { authHeaders, jsonHeaders } from "./apiKey";
2
3
 
3
4
  /**
4
5
  * Safely parse JSON from a response that may have been truncated by
@@ -35,7 +36,7 @@ export async function fetchAnalysis(
35
36
  ): Promise<SEOAnalysis | DeepAnalysis> {
36
37
  const response = await fetch(`${apiUrl}/api/seo-copilot/analyze`, {
37
38
  method: "POST",
38
- headers: { "Content-Type": "application/json" },
39
+ headers: jsonHeaders(),
39
40
  body: JSON.stringify({ content, mode, publication, seedKeywords, documentFields, focusKeyword, secondaryKeyword }),
40
41
  signal,
41
42
  });
@@ -68,7 +69,7 @@ export async function fetchChatStream(
68
69
  ): Promise<ReadableStream<Uint8Array> | null> {
69
70
  const response = await fetch(`${apiUrl}/api/seo-copilot/chat`, {
70
71
  method: "POST",
71
- headers: { "Content-Type": "application/json" },
72
+ headers: jsonHeaders(),
72
73
  body: JSON.stringify({ messages, documentContext, publication, keywordPanelData }),
73
74
  signal,
74
75
  });
@@ -89,7 +90,7 @@ export async function fetchQuestions(
89
90
  ): Promise<{ questions: { question: string; volume: number; cpc: number; competition: number }[]; apiUnitsUsed: number }> {
90
91
  const response = await fetch(`${apiUrl}/api/seo-copilot/questions`, {
91
92
  method: "POST",
92
- headers: { "Content-Type": "application/json" },
93
+ headers: jsonHeaders(),
93
94
  body: JSON.stringify({ keyword }),
94
95
  signal,
95
96
  });
@@ -122,7 +123,7 @@ export async function submitFeedback(
122
123
  ): Promise<void> {
123
124
  const res = await fetch(`${apiUrl}/api/seo-copilot/feedback`, {
124
125
  method: "POST",
125
- headers: { "Content-Type": "application/json" },
126
+ headers: jsonHeaders(),
126
127
  body: JSON.stringify(data),
127
128
  });
128
129
  // Surface HTTP failures (500, 429, ...) so the caller can show a real error instead of a
@@ -137,7 +138,7 @@ export async function lookupKeywords(
137
138
  ): Promise<{ results: { keyword: string; volume: number; cpc: number; competition: number }[]; apiUnitsUsed: number }> {
138
139
  const response = await fetch(`${apiUrl}/api/seo-copilot/keywords`, {
139
140
  method: "POST",
140
- headers: { "Content-Type": "application/json" },
141
+ headers: jsonHeaders(),
141
142
  body: JSON.stringify({ keywords }),
142
143
  signal,
143
144
  });
@@ -158,7 +159,8 @@ export async function loadChatHistory(
158
159
  ): Promise<ChatMessage[]> {
159
160
  try {
160
161
  const res = await fetch(
161
- `${apiUrl}/api/seo-copilot/chat-history?userId=${encodeURIComponent(userId)}&documentId=${encodeURIComponent(documentId)}`
162
+ `${apiUrl}/api/seo-copilot/chat-history?userId=${encodeURIComponent(userId)}&documentId=${encodeURIComponent(documentId)}`,
163
+ { headers: authHeaders() }
162
164
  );
163
165
  if (!res.ok) return [];
164
166
  const data = await res.json();
@@ -179,7 +181,7 @@ export async function fetchContentSuggestions(
179
181
  try {
180
182
  const res = await fetch(`${apiUrl}/api/seo-copilot/content-suggestions`, {
181
183
  method: "POST",
182
- headers: { "Content-Type": "application/json" },
184
+ headers: jsonHeaders(),
183
185
  body: JSON.stringify({ content, primaryKeyword, publication }),
184
186
  signal,
185
187
  });
@@ -209,7 +211,7 @@ export async function fetchLinkingSuggestions(
209
211
  try {
210
212
  const res = await fetch(`${apiUrl}/api/seo-copilot/linking-suggestions`, {
211
213
  method: "POST",
212
- headers: { "Content-Type": "application/json" },
214
+ headers: jsonHeaders(),
213
215
  body: JSON.stringify({ content, primaryKeyword, supportingKeywords, currentDocumentId, publication }),
214
216
  signal,
215
217
  });
@@ -229,7 +231,7 @@ export async function checkLinks(apiUrl: string, urls: string[]): Promise<LinkCh
229
231
  try {
230
232
  const res = await fetch(`${apiUrl}/api/seo-copilot/check-links`, {
231
233
  method: "POST",
232
- headers: { "Content-Type": "application/json" },
234
+ headers: jsonHeaders(),
233
235
  body: JSON.stringify({ urls }),
234
236
  });
235
237
  if (!res.ok) return [];
@@ -251,7 +253,7 @@ export async function saveChatHistory(
251
253
  try {
252
254
  await fetch(`${apiUrl}/api/seo-copilot/chat-history`, {
253
255
  method: "POST",
254
- headers: { "Content-Type": "application/json" },
256
+ headers: jsonHeaders(),
255
257
  body: JSON.stringify({ userId, documentId, messages, publication }),
256
258
  });
257
259
  } catch {
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Shared API key for the SEO Copilot backend.
3
+ *
4
+ * Every /api/seo-copilot/* route requires a matching `x-api-key` header
5
+ * (see PR #33). It is injected into every request from here so call sites do
6
+ * not have to thread it through their signatures.
7
+ *
8
+ * This is NOT a secret and there is deliberately no way to configure it. The
9
+ * plugin runs in the browser and ships as a public npm package, so any value
10
+ * placed here is readable by anyone. An earlier revision exposed a
11
+ * `seoCopilot({ apiKey })` option documented as "supply the value of
12
+ * SEO_COPILOT_API_KEY from the backend deployment", which invited operators to
13
+ * paste the genuine server credential into a public bundle. The option was
14
+ * removed rather than documented around: the only key a browser client can
15
+ * hold is one that is already public.
16
+ *
17
+ * Must stay in lockstep with SHARED_CLIENT_KEY in the backend's
18
+ * src/lib/seo-copilot/sharedKey.ts. The backend test pins this exact literal.
19
+ */
20
+ export const SHARED_CLIENT_KEY = "seo-copilot-public-client-key-v1";
21
+
22
+ /** Auth headers for a backend call. */
23
+ export function authHeaders(): Record<string, string> {
24
+ return { "x-api-key": SHARED_CLIENT_KEY };
25
+ }
26
+
27
+ /** Standard JSON request headers plus auth. */
28
+ export function jsonHeaders(): Record<string, string> {
29
+ return { "Content-Type": "application/json", ...authHeaders() };
30
+ }