import { http, HttpResponse } from "msw";
export const handlers = [
// GET requests via proxy.php
http.get("/proxy.php", ({ request }) => {
const url = new URL(request.url);
const path = url.searchParams.get("path");
if (path === "/api/v1/areas/list") {
return HttpResponse.json({
status: true,
data: {
areas: [
{ id: 1, type: "room", alias: "kitchen", display_name: "Kitchen", parent_id: 0 },
{ id: 2, type: "room", alias: "hall", display_name: "Hall", parent_id: 0 },
],
total: 2,
},
});
}
if (path === "/api/v1/devices/scanning/setup") {
return HttpResponse.json({
status: true,
data: {
devices: [
{
device_name: "New Device",
device_type: "relay",
ip_address: "192.168.1.50",
mac_address: "A4:CF:12:9B:3F:D2",
firmware_version: "1.0",
status: "setup",
},
],
},
});
}
if (path === "/api/v1/devices/list") {
return HttpResponse.json({
status: true,
data: {
devices: [
{ id: 1, name: "Relay 1", alias: "relay_1", device_type: "relay", device_ip: "192.168.1.10", connection_status: "active" },
],
total: 1,
},
});
}
if (path === "/api/v1/scripts/actions/list") {
return HttpResponse.json({
status: true,
data: {
scripts: [
{ alias: "kitchen_light", name: "Kitchen Light", icon: '<i class="ph ph-lightbulb"></i>', state: "enabled", author: "Test", scope: "KitchenScope" },
{ alias: "hall_light", name: "Hall Light", icon: '<i class="ph ph-lightbulb"></i>', state: "enabled", author: "Test", scope: "HallScope" },
],
total: 2,
},
});
}
if (path === "/api/v1/scripts/regular/list") {
return HttpResponse.json({
status: true,
data: {
scripts: [
{ alias: "auto_off", name: "Auto Off", state: "enabled", filename: "auto_off.php", scope: "KitchenScope" },
],
total: 1,
},
});
}
if (path === "/api/v1/scripts/scopes/list") {
return HttpResponse.json({
status: true,
data: {
scopes: [
{ name: "KitchenScope", filename: "KitchenScope.php", state: "enabled", path: "/srv/.../ControlScripts" },
{ name: "HallScope", filename: "HallScope.php", state: "enabled", path: "/srv/.../ControlScripts" },
],
total: 2,
},
});
}
if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/remove")) {
return HttpResponse.json({ status: true });
}
if (path?.startsWith("/api/v1/scripts/actions/alias/") && (path.endsWith("/enable") || path.endsWith("/disable"))) {
return HttpResponse.json({ status: true });
}
if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/devices")) {
return HttpResponse.json({
status: true,
data: {
devices: [
{ id: 1, name: "Relay 1", alias: "relay_1", device_type: "relay", device_ip: "192.168.1.10", connection_status: "active" },
],
total: 1,
},
});
}
if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/scripts")) {
return HttpResponse.json({
status: true,
data: {
scripts: [
{ alias: "kitchen_light", name: "Kitchen Light", state: "enabled" },
],
total: 1,
},
});
}
if (path?.startsWith("/api/v1/areas/id/") && path.endsWith("/unassign-from-area")) {
return HttpResponse.json({ status: true });
}
return new HttpResponse(null, { status: 404 });
}),
// POST requests via proxy.php
http.post("/proxy.php", async ({ request }) => {
const url = new URL(request.url);
const path = url.searchParams.get("path");
const body = await request.json().catch(() => ({}));
if (path === "/api/v1/areas/new-area") {
return HttpResponse.json({
status: true,
data: {
area: { id: 3, type: body.type, alias: body.alias, display_name: body.display_name, parent_id: 0 },
},
});
}
if (path === "/api/v1/areas/update-display-name") {
return HttpResponse.json({
status: true,
data: { area_id: body.area_id, display_name: body.display_name },
});
}
if (path === "/api/v1/devices/setup/new-device") {
return HttpResponse.json({
status: true,
data: {
device: { id: 2, name: body.name, alias: body.alias, device_type: "relay", device_ip: body.device_ip },
},
});
}
if (path === "/api/v1/scripts/actions/run") {
return HttpResponse.json({
status: true,
data: {
return: {
result: { ok: true },
exec_time: "0.042 seconds",
},
},
});
}
return new HttpResponse(null, { status: 404 });
}),
];