From e51b18117e61504f7f662921abee7a2d31c850be Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Sun, 21 Apr 2019 22:09:02 -0700 Subject: [PATCH] refactor: let `send` build output --- site/src/routes/auth/me.json.js | 11 +++--- site/src/routes/blog/[slug].json.js | 19 +++++---- site/src/routes/blog/index.json.js | 6 +-- site/src/routes/blog/rss.xml.js | 4 +- site/src/routes/docs/index.json.js | 11 ++---- site/src/routes/examples/[slug].json.js | 20 +++++----- site/src/routes/examples/index.json.js | 17 +++----- site/src/routes/gist/[id].js | 39 ++++++------------- site/src/routes/gist/create.js | 23 +++-------- site/src/routes/tutorial/[slug]/index.json.js | 22 +++++------ site/src/routes/tutorial/index.json.js | 17 +++----- 11 files changed, 71 insertions(+), 118 deletions(-) diff --git a/site/src/routes/auth/me.json.js b/site/src/routes/auth/me.json.js index 2c9cebe765..44aa7c9a6f 100644 --- a/site/src/routes/auth/me.json.js +++ b/site/src/routes/auth/me.json.js @@ -1,9 +1,10 @@ +import send from '@polka/send'; + export function get(req, res) { if (!req.session || !req.session.passport || !req.session.passport.user) { - res.send('null'); - return; + return send(res, 200, 'null'); } - const { id, username, displayName, photo } = req.session.passport && req.session.passport.user; - res.send({ id, username, displayName, photo }); -} \ No newline at end of file + const { id, username, displayName, photo } = req.session.passport.user; + send(res, 200, { id, username, displayName, photo }); +} diff --git a/site/src/routes/blog/[slug].json.js b/site/src/routes/blog/[slug].json.js index ebc191f2da..ee41d5826d 100644 --- a/site/src/routes/blog/[slug].json.js +++ b/site/src/routes/blog/[slug].json.js @@ -1,3 +1,4 @@ +import send from '@polka/send'; import get_posts from './_posts.js'; let lookup; @@ -6,18 +7,16 @@ export function get(req, res) { if (!lookup || process.env.NODE_ENV !== 'production') { lookup = new Map(); get_posts().forEach(post => { - lookup.set(post.slug, JSON.stringify(post)); + lookup.set(post.slug, post); }); } - if (lookup.has(req.params.slug)) { - res.set({ - 'Content-Type': 'application/json', - 'Cache-Control': `max-age=${5 * 60 * 1e3}` // 5 minutes - }); - res.end(lookup.get(req.params.slug)); + const post = lookup.get(req.params.slug); + + if (post) { + res.setHeader('Cache-Control', `max-age=${5 * 60 * 1e3}`); // 5 minutes + send(res, 200, post); } else { - res.statusCode = 404; - res.end(JSON.stringify({ message: 'not found' })); + send(res, 404, { message: 'not found' }); } -} \ No newline at end of file +} diff --git a/site/src/routes/blog/index.json.js b/site/src/routes/blog/index.json.js index b2609505a4..75180e6e87 100644 --- a/site/src/routes/blog/index.json.js +++ b/site/src/routes/blog/index.json.js @@ -1,3 +1,4 @@ +import send from '@polka/send'; import get_posts from './_posts.js'; let json; @@ -16,9 +17,8 @@ export function get(req, res) { json = JSON.stringify(posts); } - res.set({ + send(res, 200, json, { 'Content-Type': 'application/json', 'Cache-Control': `max-age=${5 * 60 * 1e3}` // 5 minutes }); - res.end(json); -} \ No newline at end of file +} diff --git a/site/src/routes/blog/rss.xml.js b/site/src/routes/blog/rss.xml.js index e41e1131f0..974806ad44 100644 --- a/site/src/routes/blog/rss.xml.js +++ b/site/src/routes/blog/rss.xml.js @@ -1,3 +1,4 @@ +import send from '@polka/send'; import get_posts from '../blog/_posts.js'; const months = ',Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','); @@ -34,9 +35,8 @@ const rss = ` `.replace(/>[^\S]+/gm, '>').replace(/[^\S]+