refactor: let `send` build output

pull/2478/head
Luke Edwards 6 years ago
parent 092d0be525
commit 999b1f4cdb

@ -1,9 +1,10 @@
import send from '@polka/send';
export function get(req, res) { export function get(req, res) {
if (!req.session || !req.session.passport || !req.session.passport.user) { if (!req.session || !req.session.passport || !req.session.passport.user) {
res.send('null'); return send(res, 200, 'null');
return;
} }
const { id, username, displayName, photo } = req.session.passport && req.session.passport.user; const { id, username, displayName, photo } = req.session.passport.user;
res.send({ id, username, displayName, photo }); send(res, 200, { id, username, displayName, photo });
} }

@ -1,3 +1,4 @@
import send from '@polka/send';
import get_posts from './_posts.js'; import get_posts from './_posts.js';
let lookup; let lookup;
@ -6,18 +7,16 @@ export function get(req, res) {
if (!lookup || process.env.NODE_ENV !== 'production') { if (!lookup || process.env.NODE_ENV !== 'production') {
lookup = new Map(); lookup = new Map();
get_posts().forEach(post => { get_posts().forEach(post => {
lookup.set(post.slug, JSON.stringify(post)); lookup.set(post.slug, post);
}); });
} }
if (lookup.has(req.params.slug)) { const post = lookup.get(req.params.slug);
res.set({
'Content-Type': 'application/json', if (post) {
'Cache-Control': `max-age=${5 * 60 * 1e3}` // 5 minutes res.setHeader('Cache-Control', `max-age=${5 * 60 * 1e3}`); // 5 minutes
}); send(res, 200, post);
res.end(lookup.get(req.params.slug));
} else { } else {
res.statusCode = 404; send(res, 404, { message: 'not found' });
res.end(JSON.stringify({ message: 'not found' }));
} }
} }

@ -1,3 +1,4 @@
import send from '@polka/send';
import get_posts from './_posts.js'; import get_posts from './_posts.js';
let json; let json;
@ -16,9 +17,8 @@ export function get(req, res) {
json = JSON.stringify(posts); json = JSON.stringify(posts);
} }
res.set({ send(res, 200, json, {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Cache-Control': `max-age=${5 * 60 * 1e3}` // 5 minutes 'Cache-Control': `max-age=${5 * 60 * 1e3}` // 5 minutes
}); });
res.end(json); }
}

@ -1,3 +1,4 @@
import send from '@polka/send';
import get_posts from '../blog/_posts.js'; import get_posts from '../blog/_posts.js';
const months = ',Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','); 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]+</gm, '<').trim(); `.replace(/>[^\S]+/gm, '>').replace(/[^\S]+</gm, '<').trim();
export function get(req, res) { export function get(req, res) {
res.set({ send(res, 200, rss, {
'Cache-Control': `max-age=${30 * 60 * 1e3}`, 'Cache-Control': `max-age=${30 * 60 * 1e3}`,
'Content-Type': 'application/rss+xml' 'Content-Type': 'application/rss+xml'
}); });
res.end(rss);
} }

@ -1,15 +1,12 @@
import send from '@polka/send';
import get_sections from './_sections.js'; import get_sections from './_sections.js';
let json; let json;
export function get(req, res) { export function get(req, res) {
if (!json || process.env.NODE_ENV !== 'production') { if (!json || process.env.NODE_ENV !== 'production') {
json = JSON.stringify(get_sections()); json = get_sections();
} }
res.set({ send(res, 200, json);
'Content-Type': 'application/json' }
});
res.end(json);
}

@ -1,3 +1,4 @@
import send from '@polka/send';
import { get_example } from './_examples.js'; import { get_example } from './_examples.js';
const cache = new Map(); const cache = new Map();
@ -6,20 +7,17 @@ export function get(req, res) {
const { slug } = req.params; const { slug } = req.params;
try { try {
if (!cache.has(slug) || process.env.NODE_ENV !== 'production') { let example = cache.get(slug);
cache.set(slug, JSON.stringify(get_example(slug)));
}
res.writeHead(200, { if (!example || process.env.NODE_ENV !== 'production') {
'Content-Type': 'application/json' example = get_example(slug);
}); cache.set(slug, example);
}
res.end(cache.get(slug)); send(res, 200, example);
} catch (err) { } catch (err) {
res.writeHead(404, { send(res, 404, {
'Content-Type': 'application/json' error: 'not found'
}); });
res.end(JSON.stringify({ error: 'not found' }));
} }
} }

@ -1,3 +1,4 @@
import send from '@polka/send';
import { get_examples } from './_examples.js'; import { get_examples } from './_examples.js';
let cached; let cached;
@ -5,19 +6,13 @@ let cached;
export function get(req, res) { export function get(req, res) {
try { try {
if (!cached || process.env.NODE_ENV !== 'production') { if (!cached || process.env.NODE_ENV !== 'production') {
cached = JSON.stringify(get_examples()); cached = get_examples();
} }
res.writeHead(200, { send(res, 200, cached);
'Content-Type': 'application/json'
});
res.end(cached);
} catch (e) { } catch (e) {
res.writeHead(e.status || 500, { send(res, e.status || 500, {
'Content-Type': 'application/json' message: e.message
}); });
res.end(JSON.stringify({ message: e.message }));
} }
} }

@ -1,5 +1,6 @@
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import { body } from './_utils.js'; import { body } from './_utils.js';
import send from '@polka/send';
export async function get(req, res) { export async function get(req, res) {
const { id } = req.params; const { id } = req.params;
@ -14,22 +15,18 @@ export async function get(req, res) {
headers headers
}); });
res.writeHead(r.status, {
'Content-Type': 'application/json'
});
const result = await r.json(); const result = await r.json();
if (r.status === 200) { if (r.status === 200) {
res.end(JSON.stringify({ send(res, 200, {
id: result.id, id: result.id,
description: result.description, description: result.description,
owner: result.owner, owner: result.owner,
html_url: result.html_url, html_url: result.html_url,
files: result.files files: result.files
})); });
} else { } else {
res.end(JSON.stringify(result)); send(res, r.status, result);
} }
} }
@ -37,11 +34,7 @@ export async function patch(req, res) {
const user = req.session && req.session.passport && req.session.passport.user; const user = req.session && req.session.passport && req.session.passport.user;
if (!user) { if (!user) {
res.writeHead(403, { return send(res, 403, { error: 'unauthorized' });
'Content-Type': 'application/json'
});
res.end(JSON.stringify({ error: 'unauthorized' }));
return;
} }
try { try {
@ -58,24 +51,16 @@ export async function patch(req, res) {
}) })
}); });
res.writeHead(r.status, {
'Content-Type': 'application/json'
});
if (r.status === 200) { if (r.status === 200) {
res.end(JSON.stringify({ send(res, 200, { ok: true });
ok: true
}));
} else { } else {
res.end(await r.text()); send(res, r.status, await r.text(), {
'Content-Type': 'application/json'
});
} }
} catch (err) { } catch (err) {
res.writeHead(500, { send(res, 500, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
error: err.message error: err.message
})); });
} }
} }

@ -1,15 +1,12 @@
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import { body } from './_utils.js'; import { body } from './_utils.js';
import send from '@polka/send';
export async function post(req, res) { export async function post(req, res) {
const user = req.session.passport && req.session.passport.user; const user = req.session.passport && req.session.passport.user;
if (!user) { if (!user) {
res.writeHead(403, { return send(res, 403, { error: 'unauthorized' });
'Content-Type': 'application/json'
});
res.end(JSON.stringify({ error: 'unauthorized' }));
return;
} }
try { try {
@ -45,26 +42,18 @@ export async function post(req, res) {
}) })
}); });
res.writeHead(r.status, {
'Content-Type': 'application/json'
});
const gist = await r.json(); const gist = await r.json();
res.end(JSON.stringify({ send(res, r.status, {
id: gist.id, id: gist.id,
description: gist.description, description: gist.description,
owner: gist.owner, owner: gist.owner,
html_url: gist.html_url, html_url: gist.html_url,
files: gist.files files: gist.files
}));
} catch (err) {
res.writeHead(500, {
'Content-Type': 'application/json'
}); });
} catch (err) {
res.end(JSON.stringify({ send(res, 500, {
error: err.message error: err.message
})); });
} }
} }

@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import marked from 'marked'; import marked from 'marked';
import PrismJS from 'prismjs'; import PrismJS from 'prismjs';
import send from '@polka/send';
import { extract_frontmatter, extract_metadata, langs, link_renderer } from '../../../utils/markdown'; import { extract_frontmatter, extract_metadata, langs, link_renderer } from '../../../utils/markdown';
const cache = new Map(); const cache = new Map();
@ -94,20 +95,15 @@ function get_tutorial(slug) {
export function get(req, res) { export function get(req, res) {
const { slug } = req.params; const { slug } = req.params;
if (!cache.has(slug) || process.env.NODE_ENV !== 'production') { let tut = cache.get(slug);
cache.set(slug, JSON.stringify(get_tutorial(slug))); if (!tut || process.env.NODE_ENV !== 'production') {
tut = get_tutorial(slug);
cache.set(slug, tut);
} }
const json = cache.get(slug); if (tut) {
send(res, 200, tut);
res.set({
'Content-Type': 'application/json'
});
if (json) {
res.end(json);
} else { } else {
res.statusCode = 404; send(res, 404, { message: 'not found' });
res.end(JSON.stringify({ message: 'not found' }));
} }
} }

@ -1,4 +1,5 @@
import * as fs from 'fs'; import * as fs from 'fs';
import send from '@polka/send';
import { extract_frontmatter } from '../../utils/markdown'; import { extract_frontmatter } from '../../utils/markdown';
let json; let json;
@ -50,21 +51,13 @@ function get_sections() {
export function get(req, res) { export function get(req, res) {
try { try {
if (!json || process.env.NODE_ENV !== 'production') { if (!json || process.env.NODE_ENV !== 'production') {
json = JSON.stringify(get_sections()); json = get_sections();
} }
res.set({ send(res, 200, json);
'Content-Type': 'application/json'
});
res.end(json);
} catch (err) { } catch (err) {
res.writeHead(500, { send(res, 500, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
message: err.message message: err.message
})); });
} }
} }

Loading…
Cancel
Save