guesty-mcp-server 0.3.0

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.
@@ -0,0 +1,20 @@
1
+ import { readFileSync, writeFileSync, mkdirSync } from "fs";
2
+ import { join, dirname } from "path";
3
+ import { fileURLToPath } from "url";
4
+
5
+ const __dirname = dirname(fileURLToPath(import.meta.url));
6
+ const CACHE_FILE = join(__dirname, "..", ".token-cache.json");
7
+
8
+ export function readCache() {
9
+ try {
10
+ return JSON.parse(readFileSync(CACHE_FILE, "utf8"));
11
+ } catch {
12
+ return null;
13
+ }
14
+ }
15
+
16
+ export function writeCache(token, expiresIn) {
17
+ try {
18
+ writeFileSync(CACHE_FILE, JSON.stringify({ token, expiry: Date.now() + (expiresIn - 60) * 1000 }));
19
+ } catch { /* ignore */ }
20
+ }
@@ -0,0 +1,83 @@
1
+ import { createServer } from "http";
2
+
3
+ /**
4
+ * Guesty Webhook Handler
5
+ * Receives real-time events from Guesty:
6
+ * - New reservation created
7
+ * - Guest message received
8
+ * - Review posted
9
+ * - Reservation updated/canceled
10
+ * - Check-in/check-out events
11
+ */
12
+
13
+ const WEBHOOK_PORT = parseInt(process.env.WEBHOOK_PORT || "3001", 10);
14
+
15
+ const eventHandlers = new Map();
16
+
17
+ export function onEvent(eventType, handler) {
18
+ if (!eventHandlers.has(eventType)) {
19
+ eventHandlers.set(eventType, []);
20
+ }
21
+ eventHandlers.get(eventType).push(handler);
22
+ }
23
+
24
+ function emit(eventType, data) {
25
+ const handlers = eventHandlers.get(eventType) || [];
26
+ const allHandlers = eventHandlers.get("*") || [];
27
+ for (const h of [...handlers, ...allHandlers]) {
28
+ try {
29
+ h(data);
30
+ } catch (e) {
31
+ console.error(`Webhook handler error for ${eventType}:`, e.message);
32
+ }
33
+ }
34
+ }
35
+
36
+ export function startWebhookServer() {
37
+ const server = createServer(async (req, res) => {
38
+ if (req.method !== "POST") {
39
+ res.writeHead(405);
40
+ res.end("Method Not Allowed");
41
+ return;
42
+ }
43
+
44
+ let body = "";
45
+ for await (const chunk of req) body += chunk;
46
+
47
+ try {
48
+ const event = JSON.parse(body);
49
+ const eventType = event.event || event.type || "unknown";
50
+
51
+ console.log(`[webhook] Received: ${eventType}`);
52
+
53
+ emit(eventType, event);
54
+
55
+ res.writeHead(200, { "Content-Type": "application/json" });
56
+ res.end(JSON.stringify({ received: true }));
57
+ } catch (e) {
58
+ console.error("[webhook] Parse error:", e.message);
59
+ res.writeHead(400);
60
+ res.end("Bad Request");
61
+ }
62
+ });
63
+
64
+ server.listen(WEBHOOK_PORT, () => {
65
+ console.log(`[webhook] Guesty webhook server listening on port ${WEBHOOK_PORT}`);
66
+ });
67
+
68
+ return server;
69
+ }
70
+
71
+ /**
72
+ * Supported Guesty webhook events:
73
+ * - reservation.created
74
+ * - reservation.updated
75
+ * - reservation.canceled
76
+ * - reservation.checkin
77
+ * - reservation.checkout
78
+ * - conversation.new_message
79
+ * - review.created
80
+ * - listing.updated
81
+ * - task.created
82
+ * - task.completed
83
+ */