[chore] added prettier

This commit is contained in:
Jori Lallo
2017-04-26 21:47:03 -07:00
parent fcdeb67bc5
commit 08b1609440
53 changed files with 1983 additions and 928 deletions

View File

@@ -8,10 +8,8 @@ import { ApiKey } from '../models';
const router = new Router();
router.post('apiKeys.create', auth(), async (ctx) => {
const {
name,
} = ctx.body;
router.post('apiKeys.create', auth(), async ctx => {
const { name } = ctx.body;
ctx.assertPresent(name, 'name is required');
const user = ctx.state.user;
@@ -26,15 +24,13 @@ router.post('apiKeys.create', auth(), async (ctx) => {
};
});
router.post('apiKeys.list', auth(), pagination(), async (ctx) => {
router.post('apiKeys.list', auth(), pagination(), async ctx => {
const user = ctx.state.user;
const keys = await ApiKey.findAll({
where: {
userId: user.id,
},
order: [
['createdAt', 'DESC'],
],
order: [['createdAt', 'DESC']],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
@@ -49,10 +45,8 @@ router.post('apiKeys.list', auth(), pagination(), async (ctx) => {
};
});
router.post('apiKeys.delete', auth(), async (ctx) => {
const {
id,
} = ctx.body;
router.post('apiKeys.delete', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;

View File

@@ -9,7 +9,7 @@ import { User, Team } from '../models';
const router = new Router();
router.post('auth.signup', async (ctx) => {
router.post('auth.signup', async ctx => {
const { username, name, email, password } = ctx.request.body;
ctx.assertPresent(username, 'name is required');
@@ -19,11 +19,19 @@ router.post('auth.signup', async (ctx) => {
ctx.assertPresent(password, 'password is required');
if (await User.findOne({ where: { email } })) {
throw apiError(400, 'user_exists_with_email', 'User already exists with this email');
throw apiError(
400,
'user_exists_with_email',
'User already exists with this email'
);
}
if (await User.findOne({ where: { username } })) {
throw apiError(400, 'user_exists_with_username', 'User already exists with this username');
throw apiError(
400,
'user_exists_with_username',
'User already exists with this username'
);
}
const user = await User.create({
@@ -33,13 +41,15 @@ router.post('auth.signup', async (ctx) => {
password,
});
ctx.body = { data: {
user: await presentUser(ctx, user),
accessToken: user.getJwtToken(),
} };
ctx.body = {
data: {
user: await presentUser(ctx, user),
accessToken: user.getJwtToken(),
},
};
});
router.post('auth.login', async (ctx) => {
router.post('auth.login', async ctx => {
const { username, password } = ctx.request.body;
ctx.assertPresent(username, 'username/email is required');
@@ -47,10 +57,9 @@ router.post('auth.login', async (ctx) => {
let user;
if (username) {
user = await User.findOne({ where: Sequelize.or(
{ email: username },
{ username },
) });
user = await User.findOne({
where: Sequelize.or({ email: username }, { username }),
});
} else {
throw apiError(400, 'invalid_credentials', 'username or email is invalid');
}
@@ -67,13 +76,15 @@ router.post('auth.login', async (ctx) => {
throw apiError(400, 'invalid_password', 'Invalid password');
}
ctx.body = { data: {
user: await presentUser(ctx, user),
accessToken: user.getJwtToken(),
} };
ctx.body = {
data: {
user: await presentUser(ctx, user),
accessToken: user.getJwtToken(),
},
};
});
router.post('auth.slack', async (ctx) => {
router.post('auth.slack', async ctx => {
const { code } = ctx.body;
ctx.assertPresent(code, 'code is required');
@@ -86,7 +97,9 @@ router.post('auth.slack', async (ctx) => {
let data;
try {
const response = await fetch(`https://slack.com/api/oauth.access?${querystring.stringify(body)}`);
const response = await fetch(
`https://slack.com/api/oauth.access?${querystring.stringify(body)}`
);
data = await response.json();
} catch (e) {
throw httpErrors.BadRequest();
@@ -97,7 +110,11 @@ router.post('auth.slack', async (ctx) => {
// Temp to block
const allowedSlackDomains = process.env.ALLOWED_SLACK_DOMAINS.split(',');
if (!allowedSlackDomains.includes(data.team.domain)) {
throw apiError(400, 'invalid_slack_team', 'Atlas is currently in private beta');
throw apiError(
400,
'invalid_slack_team',
'Atlas is currently in private beta'
);
}
// User
@@ -138,14 +155,16 @@ router.post('auth.slack', async (ctx) => {
await team.createFirstAtlas(user.id);
}
ctx.body = { data: {
user: await presentUser(ctx, user),
team: await presentTeam(ctx, team),
accessToken: user.getJwtToken(),
} };
ctx.body = {
data: {
user: await presentUser(ctx, user),
team: await presentTeam(ctx, team),
accessToken: user.getJwtToken(),
},
};
});
router.post('auth.slackCommands', async (ctx) => {
router.post('auth.slackCommands', async ctx => {
const { code } = ctx.body;
ctx.assertPresent(code, 'code is required');
@@ -158,7 +177,9 @@ router.post('auth.slackCommands', async (ctx) => {
let data;
try {
const response = await fetch(`https://slack.com/api/oauth.access?${querystring.stringify(body)}`);
const response = await fetch(
`https://slack.com/api/oauth.access?${querystring.stringify(body)}`
);
data = await response.json();
} catch (e) {
throw httpErrors.BadRequest();
@@ -167,5 +188,4 @@ router.post('auth.slackCommands', async (ctx) => {
if (!data.ok) throw httpErrors.BadRequest(data.error);
});
export default router;

View File

@@ -37,7 +37,6 @@ describe('#auth.signup', async () => {
expect(body).toMatchSnapshot();
});
it('should require valid email', async () => {
const res = await server.post('/api/auth.signup', {
body: {

View File

@@ -9,12 +9,8 @@ import { Atlas } from '../models';
const router = new Router();
router.post('collections.create', auth(), async (ctx) => {
const {
name,
description,
type,
} = ctx.body;
router.post('collections.create', auth(), async ctx => {
const { name, description, type } = ctx.body;
ctx.assertPresent(name, 'name is required');
const user = ctx.state.user;
@@ -32,7 +28,7 @@ router.post('collections.create', auth(), async (ctx) => {
};
});
router.post('collections.info', auth(), async (ctx) => {
router.post('collections.info', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
@@ -51,25 +47,24 @@ router.post('collections.info', auth(), async (ctx) => {
};
});
router.post('collections.list', auth(), pagination(), async (ctx) => {
router.post('collections.list', auth(), pagination(), async ctx => {
const user = ctx.state.user;
const collections = await Atlas.findAll({
where: {
teamId: user.teamId,
},
order: [
['updatedAt', 'DESC'],
],
order: [['updatedAt', 'DESC']],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
// Atlases
let data = [];
await Promise.all(collections.map(async (atlas) => {
return data.push(await presentCollection(ctx, atlas, true));
}));
await Promise.all(
collections.map(async atlas => {
return data.push(await presentCollection(ctx, atlas, true));
})
);
data = _.orderBy(data, ['updatedAt'], ['desc']);
@@ -79,7 +74,7 @@ router.post('collections.list', auth(), pagination(), async (ctx) => {
};
});
router.post('collections.updateNavigationTree', auth(), async (ctx) => {
router.post('collections.updateNavigationTree', auth(), async ctx => {
const { id, tree } = ctx.body;
ctx.assertPresent(id, 'id is required');

View File

@@ -12,7 +12,7 @@ import { Document, Atlas } from '../models';
const router = new Router();
const getDocumentForId = async (id) => {
const getDocumentForId = async id => {
try {
let document;
if (isUUID(id)) {
@@ -38,7 +38,7 @@ const getDocumentForId = async (id) => {
};
// FIXME: This really needs specs :/
router.post('documents.info', auth(), async (ctx) => {
router.post('documents.info', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const document = await getDocumentForId(id);
@@ -69,7 +69,7 @@ router.post('documents.info', auth(), async (ctx) => {
}
});
router.post('documents.search', auth(), async (ctx) => {
router.post('documents.search', auth(), async ctx => {
const { query } = ctx.body;
ctx.assertPresent(query, 'query is required');
@@ -78,12 +78,16 @@ router.post('documents.search', auth(), async (ctx) => {
const documents = await Document.searchForUser(user, query);
const data = [];
await Promise.all(documents.map(async (document) => {
data.push(await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
}));
}));
await Promise.all(
documents.map(async document => {
data.push(
await presentDocument(ctx, document, {
includeCollection: true,
includeCollaborators: true,
})
);
})
);
ctx.body = {
pagination: ctx.state.pagination,
@@ -91,14 +95,8 @@ router.post('documents.search', auth(), async (ctx) => {
};
});
router.post('documents.create', auth(), async (ctx) => {
const {
collection,
title,
text,
parentDocument,
} = ctx.body;
router.post('documents.create', auth(), async ctx => {
const { collection, title, text, parentDocument } = ctx.body;
ctx.assertPresent(collection, 'collection is required');
ctx.assertPresent(title, 'title is required');
ctx.assertPresent(text, 'text is required');
@@ -115,7 +113,7 @@ router.post('documents.create', auth(), async (ctx) => {
const document = await (() => {
return new Promise(resolve => {
lock(ownerCollection.id, 10000, async (done) => {
lock(ownerCollection.id, 10000, async done => {
// FIXME: should we validate the existance of parentDocument?
let parentDocumentObj = {};
if (parentDocument && ownerCollection.type === 'atlas') {
@@ -158,12 +156,8 @@ router.post('documents.create', auth(), async (ctx) => {
};
});
router.post('documents.update', auth(), async (ctx) => {
const {
id,
title,
text,
} = ctx.body;
router.post('documents.update', auth(), async ctx => {
const { id, title, text } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(title, 'title is required');
ctx.assertPresent(text, 'text is required');
@@ -171,7 +165,8 @@ router.post('documents.update', auth(), async (ctx) => {
const user = ctx.state.user;
const document = await getDocumentForId(id);
if (!document || document.teamId !== user.teamId) throw httpErrors.BadRequest();
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
// Update document
document.title = title;
@@ -194,23 +189,22 @@ router.post('documents.update', auth(), async (ctx) => {
};
});
router.post('documents.delete', auth(), async (ctx) => {
const {
id,
} = ctx.body;
router.post('documents.delete', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await getDocumentForId(id);
const collection = await Atlas.findById(document.atlasId);
if (!document || document.teamId !== user.teamId) throw httpErrors.BadRequest();
if (!document || document.teamId !== user.teamId)
throw httpErrors.BadRequest();
// TODO: Add locking
if (collection.type === 'atlas') {
// Don't allow deletion of root docs
if (!document.parentDocumentId) {
throw httpErrors.BadRequest('Unable to delete atlas\'s root document');
throw httpErrors.BadRequest("Unable to delete atlas's root document");
}
// Delete all chilren

View File

@@ -4,17 +4,14 @@ import { Document, User } from '../models';
const router = new Router();
router.post('hooks.slack', async (ctx) => {
const {
token,
user_id,
text,
} = ctx.body;
router.post('hooks.slack', async ctx => {
const { token, user_id, text } = ctx.body;
ctx.assertPresent(token, 'token is required');
ctx.assertPresent(user_id, 'user_id is required');
ctx.assertPresent(text, 'text is required');
if (token !== process.env.SLACK_VERIFICATION_TOKEN) throw httpErrors.BadRequest('Invalid token');
if (token !== process.env.SLACK_VERIFICATION_TOKEN)
throw httpErrors.BadRequest('Invalid token');
const user = await User.find({
where: {
@@ -31,7 +28,9 @@ router.post('hooks.slack', async (ctx) => {
const results = [];
let number = 1;
for (const document of documents) {
results.push(`${number}. <${process.env.URL}${document.getUrl()}|${document.title}>`);
results.push(
`${number}. <${process.env.URL}${document.getUrl()}|${document.title}>`
);
number += 1;
}

View File

@@ -1,20 +1,17 @@
import uuid from 'uuid';
import Router from 'koa-router';
import {
makePolicy,
signPolicy,
} from '../utils/s3';
import { makePolicy, signPolicy } from '../utils/s3';
import auth from './middlewares/authentication';
import { presentUser } from '../presenters';
const router = new Router();
router.post('user.info', auth(), async (ctx) => {
router.post('user.info', auth(), async ctx => {
ctx.body = { data: await presentUser(ctx, ctx.state.user) };
});
router.post('user.s3Upload', auth(), async (ctx) => {
router.post('user.s3Upload', auth(), async ctx => {
const { filename, kind, size } = ctx.body;
ctx.assertPresent(filename, 'filename is required');
ctx.assertPresent(kind, 'kind is required');
@@ -24,25 +21,27 @@ router.post('user.s3Upload', auth(), async (ctx) => {
const key = `${s3Key}/${filename}`;
const policy = makePolicy();
ctx.body = { data: {
maxUploadSize: process.env.AWS_S3_UPLOAD_MAX_SIZE,
uploadUrl: process.env.AWS_S3_UPLOAD_BUCKET_URL,
form: {
AWSAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
'Cache-Control': 'max-age=31557600',
'Content-Type': kind,
key,
acl: 'public-read',
signature: signPolicy(policy),
policy,
ctx.body = {
data: {
maxUploadSize: process.env.AWS_S3_UPLOAD_MAX_SIZE,
uploadUrl: process.env.AWS_S3_UPLOAD_BUCKET_URL,
form: {
AWSAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
'Cache-Control': 'max-age=31557600',
'Content-Type': kind,
key,
acl: 'public-read',
signature: signPolicy(policy),
policy,
},
asset: {
contentType: kind,
url: `${process.env.AWS_S3_UPLOAD_BUCKET_URL}${s3Key}/${filename}`,
name: filename,
size,
},
},
asset: {
contentType: kind,
url: `${process.env.AWS_S3_UPLOAD_BUCKET_URL}${s3Key}/${filename}`,
name: filename,
size,
},
} };
};
});
export default router;