Ajouter des endpoints
Exposez vos plugins via des endpoints HTTP.
Créer un endpoint
Fichier : plugins/api.ts
Pour créer un endpoint, vous pouvez utiliser Express ou un simple serveur Node.js :
import { createServer } from "http";
import { analyzeText } from "./text-analyzer";
const server = createServer(async (req, res) => {
if (req.method === "POST" && req.url === "/api/analyze") {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", async () => {
try {
const input = JSON.parse(body);
const result = await analyzeText(input);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ result }));
} catch (error) {
res.writeHead(400);
res.end(JSON.stringify({ error: String(error) }));
}
});
} else {
res.writeHead(404);
res.end("Not found");
}
});
server.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
Tester l'endpoint
Pour tester votre endpoint :
curl -X POST http://localhost:3000/api/analyze \
-H "Content-Type: application/json" \
-d '{"text":"Hello world","countWords":true}'
Bonnes pratiques
- Utilisez des paths clairs (
/api/resource) - Retournez du JSON structuré
- Validez les inputs avec Zod
- Gérez les erreurs correctement