File size: 1,728 Bytes
ad867fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
require("dotenv").config();
const express = require("express");
const fetch = require("node-fetch");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const CODESANDBOX_API = "https://api.codesandbox.io/graphql";
const BEARER_TOKEN = process.env.CODESANDBOX_TOKEN;
const querySandboxes = `
query {
me {
sandboxes {
nodes {
id
title
createdAt
}
}
}
}
`;
const createSandboxMutation = `
mutation {
createSandbox(input: { title: "New Sandbox from API" }) {
sandbox {
id
title
}
}
}
`;
async function graphqlRequest(query) {
const res = await fetch(CODESANDBOX_API, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${BEARER_TOKEN}`
},
body: JSON.stringify({ query })
});
const data = await res.json();
return data;
}
app.get("/", async (req, res) => {
const data = await graphqlRequest(querySandboxes);
const sandboxes = data?.data?.me?.sandboxes?.nodes || [];
let html = `
<h2>π§ͺ CodeSandbox Manager</h2>
<form action="/create" method="POST">
<button>Create New Sandbox</button>
</form>
<h3>π¦ Existing Sandboxes:</h3>
<ul>
`;
sandboxes.forEach(s => {
html += `<li><b>${s.title}</b> (ID: ${s.id}) β Created: ${new Date(s.createdAt).toLocaleString()}</li>`;
});
html += "</ul>";
res.send(html);
});
app.post("/create", async (req, res) => {
const result = await graphqlRequest(createSandboxMutation);
res.redirect("/");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`CodeSandbox Manager UI running at http://localhost:${PORT}`);
});
|