From 0441e92d08689b412f0985ec88c6eb97a7e384c8 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Wed, 15 Nov 2017 22:56:40 -0800 Subject: [PATCH] Lint rules and flow annotations for rest of the files --- .eslintignore | 1 + .eslintrc | 1 + server/api/apiKeys.js | 1 + server/api/hooks.js | 1 + server/api/index.js | 1 + server/api/middlewares/apiWrapper.js | 11 +++++++++-- server/api/middlewares/authentication.js | 13 ++++++++++--- server/api/middlewares/pagination.js | 17 ++++++++++++++--- server/api/user.js | 1 + server/api/user.test.js | 1 + server/errors.js | 3 ++- server/middlewares/methodOverride.js | 10 ++++++++-- server/middlewares/subdomainRedirect.js | 10 ++++++++-- server/migrations/20160619080644-initial.js | 2 -- .../20160622043741-add-parent-document.js | 2 -- server/migrations/20160626063409-add-indexes.js | 2 -- .../migrations/20160626175224-add-revisions.js | 2 -- .../migrations/20160711071958-search-index.js | 2 -- .../migrations/20160726061511-atlas-creator.js | 2 -- ...20160812145029-document-atlas-soft-delete.js | 2 -- .../20160814083127-paranoia-indeces.js | 2 -- .../20160814095336-add-document-createdById.js | 2 -- ...160814111419-add-document-collaboratorIds.js | 2 -- .../20160815142720-app-collection-urlId.js | 2 -- .../20160816082738-add-revision-index.js | 2 -- .../20160824062457-add-apikey-indeces.js | 2 -- server/models/ApiKey.js | 1 + server/models/User.test.js | 1 + server/pages/About.js | 4 +--- server/pages/Pricing.js | 3 ++- server/presenters/apiKey.js | 6 +++++- server/presenters/user.js | 4 ++-- server/presenters/user.test.js | 2 +- server/redis.js | 1 + server/utils/renderpage.js | 4 +--- server/utils/updates.js | 5 ++++- 36 files changed, 77 insertions(+), 51 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..c39b8693 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +server/migrations/*.js \ No newline at end of file diff --git a/.eslintrc b/.eslintrc index 239991f4..845c418d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -36,6 +36,7 @@ "prettier/prettier": [ "error", { + "printWidth": 80, "trailingComma": "es5", "singleQuote": true } diff --git a/server/api/apiKeys.js b/server/api/apiKeys.js index 0a60efa8..4b6e5c84 100644 --- a/server/api/apiKeys.js +++ b/server/api/apiKeys.js @@ -1,3 +1,4 @@ +// @flow import Router from 'koa-router'; import httpErrors from 'http-errors'; diff --git a/server/api/hooks.js b/server/api/hooks.js index 65b29b82..999c579d 100644 --- a/server/api/hooks.js +++ b/server/api/hooks.js @@ -1,3 +1,4 @@ +// @flow import Router from 'koa-router'; import httpErrors from 'http-errors'; import { Document, User } from '../models'; diff --git a/server/api/index.js b/server/api/index.js index 4801cd1c..5a93e31e 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -1,3 +1,4 @@ +// @flow import bodyParser from 'koa-bodyparser'; import Koa from 'koa'; import Router from 'koa-router'; diff --git a/server/api/middlewares/apiWrapper.js b/server/api/middlewares/apiWrapper.js index bf49bc7a..96f44276 100644 --- a/server/api/middlewares/apiWrapper.js +++ b/server/api/middlewares/apiWrapper.js @@ -1,9 +1,16 @@ -export default function apiWrapper(_options) { - return async function apiWrapperMiddleware(ctx, next) { +// @flow +import { type Context } from 'koa'; + +export default function apiWrapper() { + return async function apiWrapperMiddleware( + ctx: Context, + next: () => Promise + ) { await next(); const ok = ctx.status < 400; + // $FlowFixMe ctx.body = { ...ctx.body, status: ctx.status, diff --git a/server/api/middlewares/authentication.js b/server/api/middlewares/authentication.js index 232dc38f..e68f7ff0 100644 --- a/server/api/middlewares/authentication.js +++ b/server/api/middlewares/authentication.js @@ -1,10 +1,15 @@ +// @flow import httpErrors from 'http-errors'; import JWT from 'jsonwebtoken'; +import { type Context } from 'koa'; import { User, ApiKey } from '../../models'; -export default function auth({ require = true } = {}) { - return async function authMiddleware(ctx, next) { +export default function auth({ require = true }: { require?: boolean } = {}) { + return async function authMiddleware( + ctx: Context, + next: () => Promise + ) { let token; const authorizationHeader = ctx.request.get('authorization'); @@ -25,6 +30,7 @@ export default function auth({ require = true } = {}) { ); } } + // $FlowFixMe } else if (ctx.body.token) { token = ctx.body.token; } else if (ctx.request.query.token) { @@ -38,7 +44,7 @@ export default function auth({ require = true } = {}) { if (token) { let user; - if (token.match(/^[\w]{38}$/)) { + if (String(token).match(/^[\w]{38}$/)) { // API key let apiKey; try { @@ -83,6 +89,7 @@ export default function auth({ require = true } = {}) { ctx.state.token = token; ctx.state.user = user; + // $FlowFixMe ctx.cache[user.id] = user; } diff --git a/server/api/middlewares/pagination.js b/server/api/middlewares/pagination.js index d0e94048..8c693ebc 100644 --- a/server/api/middlewares/pagination.js +++ b/server/api/middlewares/pagination.js @@ -1,8 +1,13 @@ +// @flow import httpErrors from 'http-errors'; import querystring from 'querystring'; +import { type Context } from 'koa'; -export default function pagination(options) { - return async function paginationMiddleware(ctx, next) { +export default function pagination(options?: Object) { + return async function paginationMiddleware( + ctx: Context, + next: () => Promise + ) { const opts = { defaultLimit: 15, maxLimit: 100, @@ -11,7 +16,9 @@ export default function pagination(options) { let query = ctx.request.query; let body = ctx.request.body; + // $FlowFixMe let limit = parseInt(query.limit || body.limit, 10); + // $FlowFixMe let offset = parseInt(query.offset || body.offset, 10); limit = isNaN(limit) ? opts.defaultLimit : limit; offset = isNaN(offset) ? 0 : offset; @@ -27,9 +34,13 @@ export default function pagination(options) { offset: offset, }; + // $FlowFixMe query.limit = ctx.state.pagination.limit; + // $FlowFixMe query.offset = ctx.state.pagination.offset + query.limit; - ctx.state.pagination.nextPath = `/api${ctx.request.path}?${querystring.stringify(query)}`; + ctx.state.pagination.nextPath = `/api${ + ctx.request.path + }?${querystring.stringify(query)}`; return next(); }; diff --git a/server/api/user.js b/server/api/user.js index 64b3676f..4ed8db81 100644 --- a/server/api/user.js +++ b/server/api/user.js @@ -1,3 +1,4 @@ +// @flow import uuid from 'uuid'; import Router from 'koa-router'; diff --git a/server/api/user.test.js b/server/api/user.test.js index 765d76a9..847f3319 100644 --- a/server/api/user.test.js +++ b/server/api/user.test.js @@ -1,3 +1,4 @@ +/* eslint-disable flowtype/require-valid-file-annotation */ import TestServer from 'fetch-test-server'; import app from '..'; diff --git a/server/errors.js b/server/errors.js index 23ffd9f3..03bfe2f3 100644 --- a/server/errors.js +++ b/server/errors.js @@ -1,6 +1,7 @@ +// @flow import httpErrors from 'http-errors'; -const apiError = (code, id, message) => { +const apiError = (code: number, id: string, message: string) => { return httpErrors(code, message, { id }); }; diff --git a/server/middlewares/methodOverride.js b/server/middlewares/methodOverride.js index 0c49e2a9..030b6d66 100644 --- a/server/middlewares/methodOverride.js +++ b/server/middlewares/methodOverride.js @@ -1,8 +1,14 @@ +// @flow import queryString from 'query-string'; +import { type Context } from 'koa'; -export default function methodOverride(_options) { - return async function methodOverrideMiddleware(ctx, next) { +export default function methodOverride() { + return async function methodOverrideMiddleware( + ctx: Context, + next: () => Promise + ) { if (ctx.method === 'POST') { + // $FlowFixMe ctx.body = ctx.request.body; } else if (ctx.method === 'GET') { ctx.method = 'POST'; // eslint-disable-line diff --git a/server/middlewares/subdomainRedirect.js b/server/middlewares/subdomainRedirect.js index c1c41468..2ae238ff 100644 --- a/server/middlewares/subdomainRedirect.js +++ b/server/middlewares/subdomainRedirect.js @@ -1,5 +1,11 @@ -export default function subdomainRedirect(options) { - return async function subdomainRedirectMiddleware(ctx, next) { +// @flow +import { type Context } from 'koa'; + +export default function subdomainRedirect() { + return async function subdomainRedirectMiddleware( + ctx: Context, + next: () => Promise + ) { if (ctx.headers.host === 'getoutline.com') { ctx.redirect(`https://www.${ctx.headers.host}${ctx.path}`); } else { diff --git a/server/migrations/20160619080644-initial.js b/server/migrations/20160619080644-initial.js index 4a113bc1..d03ba39a 100644 --- a/server/migrations/20160619080644-initial.js +++ b/server/migrations/20160619080644-initial.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.createTable('teams', { diff --git a/server/migrations/20160622043741-add-parent-document.js b/server/migrations/20160622043741-add-parent-document.js index 415e826b..8fc0fd98 100644 --- a/server/migrations/20160622043741-add-parent-document.js +++ b/server/migrations/20160622043741-add-parent-document.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addColumn('documents', 'parentDocumentId', { diff --git a/server/migrations/20160626063409-add-indexes.js b/server/migrations/20160626063409-add-indexes.js index a3878ff2..5e8042c8 100644 --- a/server/migrations/20160626063409-add-indexes.js +++ b/server/migrations/20160626063409-add-indexes.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addIndex('documents', ['urlId']); diff --git a/server/migrations/20160626175224-add-revisions.js b/server/migrations/20160626175224-add-revisions.js index 4a9f46c0..f3d85af6 100644 --- a/server/migrations/20160626175224-add-revisions.js +++ b/server/migrations/20160626175224-add-revisions.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.createTable('revisions', { diff --git a/server/migrations/20160711071958-search-index.js b/server/migrations/20160711071958-search-index.js index 7ac7862d..6b70ab1d 100644 --- a/server/migrations/20160711071958-search-index.js +++ b/server/migrations/20160711071958-search-index.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { const searchDocument = ` diff --git a/server/migrations/20160726061511-atlas-creator.js b/server/migrations/20160726061511-atlas-creator.js index 41ed9425..e2f567c3 100644 --- a/server/migrations/20160726061511-atlas-creator.js +++ b/server/migrations/20160726061511-atlas-creator.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addColumn('atlases', 'creatorId', { diff --git a/server/migrations/20160812145029-document-atlas-soft-delete.js b/server/migrations/20160812145029-document-atlas-soft-delete.js index 6f2864e9..6c59a803 100644 --- a/server/migrations/20160812145029-document-atlas-soft-delete.js +++ b/server/migrations/20160812145029-document-atlas-soft-delete.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addColumn('atlases', 'deletedAt', { diff --git a/server/migrations/20160814083127-paranoia-indeces.js b/server/migrations/20160814083127-paranoia-indeces.js index 48853314..2d8338a2 100644 --- a/server/migrations/20160814083127-paranoia-indeces.js +++ b/server/migrations/20160814083127-paranoia-indeces.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { // Remove old indeces diff --git a/server/migrations/20160814095336-add-document-createdById.js b/server/migrations/20160814095336-add-document-createdById.js index 1c53a069..cb70ac09 100644 --- a/server/migrations/20160814095336-add-document-createdById.js +++ b/server/migrations/20160814095336-add-document-createdById.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addColumn('documents', 'createdById', { diff --git a/server/migrations/20160814111419-add-document-collaboratorIds.js b/server/migrations/20160814111419-add-document-collaboratorIds.js index da733208..4eab7090 100644 --- a/server/migrations/20160814111419-add-document-collaboratorIds.js +++ b/server/migrations/20160814111419-add-document-collaboratorIds.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addColumn('documents', 'collaboratorIds', { diff --git a/server/migrations/20160815142720-app-collection-urlId.js b/server/migrations/20160815142720-app-collection-urlId.js index bb8b0c70..243395d0 100644 --- a/server/migrations/20160815142720-app-collection-urlId.js +++ b/server/migrations/20160815142720-app-collection-urlId.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addColumn('atlases', 'urlId', { diff --git a/server/migrations/20160816082738-add-revision-index.js b/server/migrations/20160816082738-add-revision-index.js index 2ac17f65..c907c6d8 100644 --- a/server/migrations/20160816082738-add-revision-index.js +++ b/server/migrations/20160816082738-add-revision-index.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addIndex('revisions', ['documentId']); diff --git a/server/migrations/20160824062457-add-apikey-indeces.js b/server/migrations/20160824062457-add-apikey-indeces.js index c30226a5..b5803243 100644 --- a/server/migrations/20160824062457-add-apikey-indeces.js +++ b/server/migrations/20160824062457-add-apikey-indeces.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { up: function(queryInterface, Sequelize) { queryInterface.addIndex('apiKeys', ['secret', 'deletedAt']); diff --git a/server/models/ApiKey.js b/server/models/ApiKey.js index dc8af364..d2e11591 100644 --- a/server/models/ApiKey.js +++ b/server/models/ApiKey.js @@ -1,3 +1,4 @@ +// @flow import { DataTypes, sequelize } from '../sequelize'; import randomstring from 'randomstring'; diff --git a/server/models/User.test.js b/server/models/User.test.js index f9326f89..8db8dd1c 100644 --- a/server/models/User.test.js +++ b/server/models/User.test.js @@ -1,3 +1,4 @@ +/* eslint-disable flowtype/require-valid-file-annotation */ import { flushdb, seed } from '../test/support'; beforeEach(flushdb); diff --git a/server/pages/About.js b/server/pages/About.js index 086f4ca8..06635104 100644 --- a/server/pages/About.js +++ b/server/pages/About.js @@ -12,9 +12,7 @@ export default function About() {

About Outline

-

- Just a proof of concept for multiple pages. -

+

Just a proof of concept for multiple pages.

); diff --git a/server/pages/Pricing.js b/server/pages/Pricing.js index 0f0084ba..c2dc9c5c 100644 --- a/server/pages/Pricing.js +++ b/server/pages/Pricing.js @@ -13,7 +13,8 @@ export default function Pricing() {

Pricing

- Explore Outline with a 14 day trial, free forever for teams smaller than 5. + Explore Outline with a 14 day trial, free forever for teams smaller + than 5.

diff --git a/server/presenters/apiKey.js b/server/presenters/apiKey.js index 71dfccc4..17eb24ee 100644 --- a/server/presenters/apiKey.js +++ b/server/presenters/apiKey.js @@ -1,4 +1,8 @@ -function present(ctx, key) { +// @flow +import { type Context } from 'koa'; +import { ApiKey } from '../models'; + +function present(ctx: Context, key: ApiKey) { return { id: key.id, name: key.name, diff --git a/server/presenters/user.js b/server/presenters/user.js index 1d064ace..4e9a7e3d 100644 --- a/server/presenters/user.js +++ b/server/presenters/user.js @@ -8,8 +8,8 @@ function present(ctx: Object, user: User) { id: user.id, username: user.username, name: user.name, - avatarUrl: user.avatarUrl || - (user.slackData ? user.slackData.image_192 : null), + avatarUrl: + user.avatarUrl || (user.slackData ? user.slackData.image_192 : null), }; } diff --git a/server/presenters/user.test.js b/server/presenters/user.test.js index 672a0a1b..9804496b 100644 --- a/server/presenters/user.test.js +++ b/server/presenters/user.test.js @@ -1,5 +1,5 @@ +/* eslint-disable flowtype/require-valid-file-annotation */ import presentUser from './user'; - import ctx from '../../__mocks__/ctx'; it('presents a user', async () => { diff --git a/server/redis.js b/server/redis.js index 837a4a99..11309366 100644 --- a/server/redis.js +++ b/server/redis.js @@ -1,3 +1,4 @@ +// @flow import redis from 'redis'; import redisLock from 'redis-lock'; diff --git a/server/utils/renderpage.js b/server/utils/renderpage.js index 7a8c7399..e970fa46 100644 --- a/server/utils/renderpage.js +++ b/server/utils/renderpage.js @@ -10,9 +10,7 @@ const sheet = new ServerStyleSheet(); export default function renderpage(ctx: Object, children: React$Element<*>) { const html = ReactDOMServer.renderToString( - - {children} - + {children} ); diff --git a/server/utils/updates.js b/server/utils/updates.js index 18d950b5..a730dac3 100644 --- a/server/utils/updates.js +++ b/server/utils/updates.js @@ -16,7 +16,10 @@ export default async () => { 'SECRET_KEY or URL env var is not set' ); const secret = process.env.SECRET_KEY.slice(0, 6) + process.env.URL; - const id = crypto.createHash('sha256').update(secret).digest('hex'); + const id = crypto + .createHash('sha256') + .update(secret) + .digest('hex'); const [ userCount,