feat(settings): per-integration Test button with live API checks

Each integration card now has a Test button that re-runs its
connectivity check on demand and updates the badge in place.

- Refactors checkIntegrationStatus into checkIntegration(name) so a
  single integration can be tested without firing the others.
- Adds POST /api/settings/test for the on-demand check.
- Replaces the "key exists ⇒ connected" placeholder for Tavily,
  Gemini, and Nextdoor with real API calls (Tavily search, Gemini
  models list, Nextdoor advertiser fetch). 401/403 surface as
  "Invalid API key" / "Token expired or invalid" so a stale
  credential is obvious instead of pretending to be healthy.
- Fixes Postiz auth header (was Bearer, the rest of the codebase
  passes the API key bare — matches lib/postiz.ts now).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Trey T
2026-05-03 21:03:16 -05:00
parent 63698333ff
commit 55fb221faa
3 changed files with 203 additions and 87 deletions
+126 -76
View File
@@ -102,82 +102,132 @@ export async function saveSetting(key: SettingKey, value: string) {
});
}
/**
* Check connectivity status for each integration.
*/
export async function checkIntegrationStatus(): Promise<Record<string, { connected: boolean; error?: string }>> {
const settings = await getAllSettings();
const status: Record<string, { connected: boolean; error?: string }> = {};
export type IntegrationName = "claude" | "postiz" | "tavily" | "gemini" | "nextdoor";
export type IntegrationStatus = { connected: boolean; error?: string };
// Claude OAuth token — validate by hitting the messages API with a 1-token call.
// A valid OAuth token returns 200; an expired/invalid one returns 401.
if (settings.CLAUDE_CODE_OAUTH_TOKEN) {
try {
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"anthropic-version": "2023-06-01",
"anthropic-beta": "oauth-2025-04-20",
Authorization: `Bearer ${settings.CLAUDE_CODE_OAUTH_TOKEN}`,
},
body: JSON.stringify({
model: "claude-haiku-4-5-20251001",
max_tokens: 1,
messages: [{ role: "user", content: "." }],
}),
signal: AbortSignal.timeout(10000),
});
if (res.ok) {
status.claude = { connected: true };
} else if (res.status === 401) {
status.claude = { connected: false, error: "Token expired or invalid" };
} else {
status.claude = { connected: false, error: `HTTP ${res.status}` };
}
} catch (e) {
status.claude = { connected: false, error: e instanceof Error ? e.message : "Connection failed" };
}
} else {
status.claude = { connected: false, error: "Not configured" };
function errorMessage(e: unknown): string {
if (e instanceof Error) {
if (e.name === "TimeoutError" || e.name === "AbortError") return "Timed out";
return e.message;
}
// Postiz
if (settings.POSTIZ_URL && settings.POSTIZ_API_KEY) {
try {
const res = await fetch(`${settings.POSTIZ_URL}/public/v1/integrations`, {
headers: { Authorization: `Bearer ${settings.POSTIZ_API_KEY}` },
signal: AbortSignal.timeout(5000),
});
status.postiz = { connected: res.ok };
if (!res.ok) status.postiz.error = `HTTP ${res.status}`;
} catch (e) {
status.postiz = { connected: false, error: e instanceof Error ? e.message : "Connection failed" };
}
} else {
status.postiz = { connected: false, error: "Not configured" };
}
// Tavily
if (settings.TAVILY_API_KEY) {
status.tavily = { connected: true }; // No ping endpoint, just check if key exists
} else {
status.tavily = { connected: false, error: "Not configured" };
}
// Gemini
if (settings.GEMINI_API_KEY) {
status.gemini = { connected: true };
} else {
status.gemini = { connected: false, error: "Not configured" };
}
// Nextdoor
if (settings.NEXTDOOR_API_TOKEN && settings.NEXTDOOR_ADVERTISER_ID) {
status.nextdoor = { connected: true };
} else {
status.nextdoor = { connected: false, error: "Not configured" };
}
return status;
return "Connection failed";
}
/**
* Live-test a single integration by making a real API call.
*/
export async function checkIntegration(name: IntegrationName): Promise<IntegrationStatus> {
const settings = await getAllSettings();
switch (name) {
case "claude": {
// OAuth token — validate by hitting the messages API with a 1-token call.
if (!settings.CLAUDE_CODE_OAUTH_TOKEN) return { connected: false, error: "Not configured" };
try {
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"anthropic-version": "2023-06-01",
"anthropic-beta": "oauth-2025-04-20",
Authorization: `Bearer ${settings.CLAUDE_CODE_OAUTH_TOKEN}`,
},
body: JSON.stringify({
model: "claude-haiku-4-5-20251001",
max_tokens: 1,
messages: [{ role: "user", content: "." }],
}),
signal: AbortSignal.timeout(10000),
});
if (res.ok) return { connected: true };
if (res.status === 401) return { connected: false, error: "Token expired or invalid" };
return { connected: false, error: `HTTP ${res.status}` };
} catch (e) {
return { connected: false, error: errorMessage(e) };
}
}
case "postiz": {
if (!settings.POSTIZ_URL || !settings.POSTIZ_API_KEY) return { connected: false, error: "Not configured" };
try {
const res = await fetch(`${settings.POSTIZ_URL}/public/v1/integrations`, {
headers: { Authorization: settings.POSTIZ_API_KEY },
signal: AbortSignal.timeout(5000),
});
if (res.ok) return { connected: true };
return { connected: false, error: `HTTP ${res.status}` };
} catch (e) {
return { connected: false, error: errorMessage(e) };
}
}
case "tavily": {
if (!settings.TAVILY_API_KEY) return { connected: false, error: "Not configured" };
try {
const res = await fetch("https://api.tavily.com/search", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
api_key: settings.TAVILY_API_KEY,
query: "ping",
max_results: 1,
}),
signal: AbortSignal.timeout(8000),
});
if (res.ok) return { connected: true };
if (res.status === 401 || res.status === 403) return { connected: false, error: "Invalid API key" };
return { connected: false, error: `HTTP ${res.status}` };
} catch (e) {
return { connected: false, error: errorMessage(e) };
}
}
case "gemini": {
if (!settings.GEMINI_API_KEY) return { connected: false, error: "Not configured" };
try {
const res = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models?key=${encodeURIComponent(settings.GEMINI_API_KEY)}`,
{ signal: AbortSignal.timeout(8000) }
);
if (res.ok) return { connected: true };
if (res.status === 400 || res.status === 403) return { connected: false, error: "Invalid API key" };
return { connected: false, error: `HTTP ${res.status}` };
} catch (e) {
return { connected: false, error: errorMessage(e) };
}
}
case "nextdoor": {
if (!settings.NEXTDOOR_API_TOKEN || !settings.NEXTDOOR_ADVERTISER_ID) {
return { connected: false, error: "Not configured" };
}
try {
const res = await fetch(
`https://api.nextdoor.com/v1/advertiser/${encodeURIComponent(settings.NEXTDOOR_ADVERTISER_ID)}`,
{
headers: { Authorization: `Bearer ${settings.NEXTDOOR_API_TOKEN}` },
signal: AbortSignal.timeout(8000),
}
);
if (res.ok) return { connected: true };
if (res.status === 401 || res.status === 403) return { connected: false, error: "Token expired or invalid" };
if (res.status === 404) return { connected: false, error: "Advertiser ID not found" };
return { connected: false, error: `HTTP ${res.status}` };
} catch (e) {
return { connected: false, error: errorMessage(e) };
}
}
}
}
const ALL_INTEGRATIONS: IntegrationName[] = ["claude", "postiz", "tavily", "gemini", "nextdoor"];
/**
* Check connectivity status for every integration in parallel.
*/
export async function checkIntegrationStatus(): Promise<Record<string, IntegrationStatus>> {
const results = await Promise.all(
ALL_INTEGRATIONS.map(async (name) => [name, await checkIntegration(name)] as const)
);
return Object.fromEntries(results);
}