Skip to content

Commit a299a44

Browse files
authored
feat: support saving playground with partial content (#277)
1 parent b072eb5 commit a299a44

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/lambda/gist/gist.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,32 @@ async function getGist({ id, version }) {
5252
return { id, version, files };
5353
}
5454

55+
const placeholderValues = {
56+
js: '// your query',
57+
html: '<!-- your markup -->',
58+
json: '{}',
59+
};
60+
61+
function ensureContent(files) {
62+
for (let name of Object.keys(files)) {
63+
if (files[name].content.length === 0) {
64+
files[name].content = placeholderValues[name.split('.').pop()] || 'o.O';
65+
}
66+
}
67+
68+
return files;
69+
}
70+
5571
/**
5672
* @param id {string}
5773
* @param description {string}
5874
* @param files { object.<string, { content: string }>}
5975
* @returns {Promise<{id: *, version: *, url: string}>}
6076
*/
6177
async function saveGist({ id, description, files }) {
78+
// we can't save empty files to github gists. So we'll make sure they ain't empty.
79+
ensureContent(files);
80+
6281
// if the id is given, we assume an update and thereby just correct the method.
6382
const response = await fetch([ENDPOINT, id].filter(Boolean).join('/'), {
6483
method: id ? 'PATCH' : 'POST',

0 commit comments

Comments
 (0)