+ hudozka-middlewares, + hudozka-api-v1

This commit is contained in:
tmshv committed 2016-03-26 21:53:20 +03:00
1 parent 6347e4db35
commit 35cc7c0caa
17 files changed
+175 -61

No files matched your search

-31
View File
@@ -1,31 +0,0 @@
import {put} from 'koa-route';
import compose from 'koa-compose';
import {ensureServiceAuth} from '../middlewares/ensureServiceAuth';
import {ejson} from '../middlewares/ejson';
import query from 'koa-query';
import {toBoolean} from 'koa-query';
export function add(auth, store) {
return put('/timeline', compose([
ensureServiceAuth(),
ejson(),
query({
save: toBoolean()
}),
async(ctx) => {
let doSave = ctx.request.query.save || true;
let post = ctx.request.body;
try {
if(!doSave) store.timeline.disableSaving();
let result = await store.timeline.add(post);
if(!doSave) store.timeline.enableSaving();
ctx.body = result;
} catch (e) {
ctx.status = 400;
ctx.body = e;
}
}
]));
}
-8
View File
@@ -1,8 +0,0 @@
import {add as feedAdd} from './feed';
import compose from 'koa-compose';
export default function (auth, store) {
return compose([
feedAdd(auth, store)
]);
};
-15
View File
@@ -1,15 +0,0 @@
import {parse} from 'ejson';
export function ejson() {
return async (ctx, next) => {
let type = ctx.request.headers['content-type'];
let data = ctx.request.body;
if (type === 'application/ejson'){
data = typeof data === 'string' ? data : JSON.stringify(data);
ctx.request.body = parse(data);
}
await next();
};
}
-14
View File
@@ -1,14 +0,0 @@
import {checkAuth} from '../core/service';
export function ensureServiceAuth() {
return async (ctx, next) => {
let service = ctx.request.headers['x-service-name'];
let token = ctx.request.headers['x-service-token'];
if (!checkAuth(service, token)) {
ctx.status = 401;
} else {
await next();
}
};
}
-35
View File
@@ -1,35 +0,0 @@
let contains = (list, i) => list.indexOf(i) > -1;
export function queryValue(params) {
let keys = Object.keys(params);
return async (ctx, next) => {
let query = ctx.request.query;
ctx.request.query = Object.keys(query)
.reduce((q, i) => {
if(contains(keys, i)){
let fn = params[i];
q[i] = fn(q[i]);
}
return q;
}, query);
await next();
};
}
export function booleanify(){
return function(value){
if(value == 'true') return true;
if(value == '1') return true;
if(value == 'false') return false;
if(value == '0') return false;
return Boolean(value);
}
}
export function arrayify(sep){
return function(value){
return value.split(sep);
}
}
+2 -2
View File
@@ -11,10 +11,10 @@ import helmet from 'koa-helmet';
import mount from 'koa-mount';
import bodyParser from 'koa-bodyparser';
import api_v1 from 'hudozka-api-v1';
import config from './config';
import {redirectionTable} from './config';
import {routes, queryObject} from './routes';
import api from './api';
import {redirect} from './routes/redirect';
import {checkAuth} from './core/service';
@@ -49,7 +49,7 @@ export default function(store){
}
function apis(store){
return mount('/api/v1', api(checkAuth, store));
return mount('/api/v1', api_v1(checkAuth, store));
}
function prerenderRmFragment(){