mirror of https://github.com/sveltejs/svelte
parent
559a2e7200
commit
7390bcdfb5
@ -0,0 +1,73 @@
|
||||
import fetch from 'node-fetch';
|
||||
import { body } from './_utils.js';
|
||||
|
||||
export async function get(req, res) {
|
||||
const { id } = req.params;
|
||||
|
||||
const r = await fetch(`https://api.github.com/gists/${id}`);
|
||||
|
||||
res.writeHead(r.status, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
const result = await r.json();
|
||||
|
||||
if (r.status === 200) {
|
||||
res.end(JSON.stringify({
|
||||
id: result.id,
|
||||
description: result.description,
|
||||
owner: result.owner,
|
||||
html_url: result.html_url,
|
||||
files: result.files
|
||||
}));
|
||||
} else {
|
||||
res.end(JSON.stringify(result));
|
||||
}
|
||||
}
|
||||
|
||||
export async function patch(req, res) {
|
||||
const user = req.session.passport && req.session.passport.user;
|
||||
|
||||
if (!user) {
|
||||
res.writeHead(403, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'unauthorized' }));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const { description, files } = await body(req);
|
||||
|
||||
const r = await fetch(`https://api.github.com/gists/${req.params.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Authorization: `token ${user.token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
description,
|
||||
files
|
||||
})
|
||||
});
|
||||
|
||||
res.writeHead(r.status, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
if (r.status === 200) {
|
||||
res.end(JSON.stringify({
|
||||
ok: true
|
||||
}));
|
||||
} else {
|
||||
res.end(await r.text());
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
res.end(JSON.stringify({
|
||||
error: err.message
|
||||
}));
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
export function body(req) {
|
||||
return new Promise((fulfil, reject) => {
|
||||
let str = '';
|
||||
|
||||
req.on('error', reject);
|
||||
|
||||
req.on('data', chunk => {
|
||||
str += chunk;
|
||||
});
|
||||
|
||||
req.on('end', () => {
|
||||
try {
|
||||
fulfil(JSON.parse(str));
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
import fetch from 'node-fetch';
|
||||
import { body } from './_utils.js';
|
||||
|
||||
export async function post(req, res) {
|
||||
const user = req.session.passport && req.session.passport.user;
|
||||
|
||||
if (!user) {
|
||||
res.writeHead(403, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'unauthorized' }));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const { name, components, json5 } = await body(req);
|
||||
|
||||
const files = {
|
||||
'meta.json': {
|
||||
content: JSON.stringify({
|
||||
svelte: true
|
||||
}, null, ' ')
|
||||
},
|
||||
'README.md': {
|
||||
content: `Created with [svelte.technology/repl](https://svelte.technology/repl)`
|
||||
}
|
||||
};
|
||||
components.forEach(component => {
|
||||
const file = `${component.name}.${component.type}`;
|
||||
if (!component.source.trim()) {
|
||||
throw new Error(`GitHub does not allow saving gists with empty files - ${file}`);
|
||||
}
|
||||
files[file] = { content: component.source };
|
||||
});
|
||||
|
||||
if (json5) {
|
||||
files['data.json5'] = { content: json5 };
|
||||
}
|
||||
|
||||
const r = await fetch(`https://api.github.com/gists`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `token ${user.token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
description: name,
|
||||
files,
|
||||
public: false
|
||||
})
|
||||
});
|
||||
|
||||
res.writeHead(r.status, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
const gist = await r.json();
|
||||
|
||||
res.end(JSON.stringify({
|
||||
id: gist.id,
|
||||
description: gist.description,
|
||||
owner: gist.owner,
|
||||
html_url: gist.html_url,
|
||||
files: gist.files
|
||||
}));
|
||||
} catch (err) {
|
||||
res.writeHead(500, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
res.end(JSON.stringify({
|
||||
error: err.message
|
||||
}));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue