import type { Context, Next } from "jsr:@hono/hono@4"; export type ClientType = "datastar" | "cli" | "shell" | "browser"; export function determineClientType(c: Context): ClientType { const accept = c.req.header("accept") || ""; const userAgent = c.req.header("user-agent") || ""; if (c.req.header("datastar-request") === "true") { return "datastar"; } if ( accept.includes("application/json") || userAgent.includes("curl") || userAgent.includes("AuthYesCLI") ) { // If it's explicitly asking for JSON, or it's a CLI tool like curl/our custom CLI if (accept.includes("application/json")) { return "cli"; } // curl without explicit accept json might just want text/plain shell strings return "shell"; } if (accept.includes("text/html")) { return "browser"; } // Default to cli/json if unknown return "cli"; } export function contentNegotiation() { return async (c: Context, next: Next) => { const clientType = determineClientType(c); c.set("clientType", clientType); await next(); }; }