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