Update index.js
Browse files
index.js
CHANGED
@@ -9,26 +9,13 @@ app.use(express.json());
|
|
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: "
|
29 |
sandbox {
|
30 |
id
|
31 |
title
|
|
|
32 |
}
|
33 |
}
|
34 |
}
|
@@ -43,37 +30,47 @@ async function graphqlRequest(query) {
|
|
43 |
},
|
44 |
body: JSON.stringify({ query })
|
45 |
});
|
46 |
-
const data = await res.json();
|
47 |
-
return data;
|
48 |
-
}
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
|
|
56 |
<form action="/create" method="POST">
|
57 |
-
<button
|
58 |
</form>
|
59 |
-
|
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 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
});
|
75 |
|
76 |
const PORT = process.env.PORT || 3000;
|
77 |
app.listen(PORT, () => {
|
78 |
-
console.log(
|
79 |
});
|
|
|
9 |
const CODESANDBOX_API = "https://api.codesandbox.io/graphql";
|
10 |
const BEARER_TOKEN = process.env.CODESANDBOX_TOKEN;
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
const createSandboxMutation = `
|
13 |
mutation {
|
14 |
+
createSandbox(input: { title: "API Sandbox" }) {
|
15 |
sandbox {
|
16 |
id
|
17 |
title
|
18 |
+
publicId
|
19 |
}
|
20 |
}
|
21 |
}
|
|
|
30 |
},
|
31 |
body: JSON.stringify({ query })
|
32 |
});
|
|
|
|
|
|
|
33 |
|
34 |
+
const json = await res.json();
|
35 |
+
if (json.errors) {
|
36 |
+
throw new Error(JSON.stringify(json.errors));
|
37 |
+
}
|
38 |
+
|
39 |
+
return json.data;
|
40 |
+
}
|
41 |
|
42 |
+
app.get("/", (req, res) => {
|
43 |
+
res.send(`
|
44 |
+
<h2>π CodeSandbox Creator</h2>
|
45 |
<form action="/create" method="POST">
|
46 |
+
<button type="submit">π Create New Sandbox</button>
|
47 |
</form>
|
48 |
+
`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
});
|
50 |
|
51 |
app.post("/create", async (req, res) => {
|
52 |
+
try {
|
53 |
+
const result = await graphqlRequest(createSandboxMutation);
|
54 |
+
const sandbox = result.createSandbox.sandbox;
|
55 |
+
const link = `https://codesandbox.io/s/${sandbox.publicId}`;
|
56 |
+
|
57 |
+
res.send(`
|
58 |
+
<h3>β
Sandbox Created!</h3>
|
59 |
+
<p>Title: <b>${sandbox.title}</b></p>
|
60 |
+
<p>Open: <a href="${link}" target="_blank">${link}</a></p>
|
61 |
+
<a href="/">β¬
Back</a>
|
62 |
+
`);
|
63 |
+
} catch (error) {
|
64 |
+
console.error(error);
|
65 |
+
res.send(`
|
66 |
+
<h3>β Failed to create sandbox</h3>
|
67 |
+
<pre>${error.message}</pre>
|
68 |
+
<a href="/">β¬
Back</a>
|
69 |
+
`);
|
70 |
+
}
|
71 |
});
|
72 |
|
73 |
const PORT = process.env.PORT || 3000;
|
74 |
app.listen(PORT, () => {
|
75 |
+
console.log(`π App running at http://localhost:${PORT}`);
|
76 |
});
|