refactor: let `send` build output

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

@ -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 });
}
const { id, username, displayName, photo } = req.session.passport.user;
send(res, 200, { id, username, displayName, photo });
}

@ -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' });
}
}
}

@ -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);
}
}

@ -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]+</gm, '<').trim();
export function get(req, res) {
res.set({
send(res, 200, rss, {
'Cache-Control': `max-age=${30 * 60 * 1e3}`,
'Content-Type': 'application/rss+xml'
});
res.end(rss);
}

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

@ -1,3 +1,4 @@
import send from '@polka/send';
import { get_example } from './_examples.js';
const cache = new Map();
@ -6,20 +7,17 @@ export function get(req, res) {
const { slug } = req.params;
try {
if (!cache.has(slug) || process.env.NODE_ENV !== 'production') {
cache.set(slug, JSON.stringify(get_example(slug)));
}
let example = cache.get(slug);
res.writeHead(200, {
'Content-Type': 'application/json'
});
if (!example || process.env.NODE_ENV !== 'production') {
example = get_example(slug);
cache.set(slug, example);
}
res.end(cache.get(slug));
send(res, 200, example);
} catch (err) {
res.writeHead(404, {
'Content-Type': 'application/json'
send(res, 404, {
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';
let cached;
@ -5,19 +6,13 @@ let cached;
export function get(req, res) {
try {
if (!cached || process.env.NODE_ENV !== 'production') {
cached = JSON.stringify(get_examples());
cached = get_examples();
}
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(cached);
send(res, 200, cached);
} catch (e) {
res.writeHead(e.status || 500, {
'Content-Type': 'application/json'
send(res, e.status || 500, {
message: e.message
});
res.end(JSON.stringify({ message: e.message }));
}
}
}

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

@ -1,15 +1,12 @@
import fetch from 'node-fetch';
import { body } from './_utils.js';
import send from '@polka/send';
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;
return send(res, 403, { error: 'unauthorized' });
}
try {
@ -45,26 +42,18 @@ export async function post(req, res) {
})
});
res.writeHead(r.status, {
'Content-Type': 'application/json'
});
const gist = await r.json();
res.end(JSON.stringify({
send(res, r.status, {
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({
} catch (err) {
send(res, 500, {
error: err.message
}));
});
}
}

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

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

Loading…
Cancel
Save