Lint rules and flow annotations for rest of the files

This commit is contained in:
Jori Lallo
2017-11-15 22:56:40 -08:00
parent c9b710cbee
commit 0441e92d08
36 changed files with 77 additions and 51 deletions

View File

@ -1,3 +1,4 @@
// @flow
import Router from 'koa-router';
import httpErrors from 'http-errors';

View File

@ -1,3 +1,4 @@
// @flow
import Router from 'koa-router';
import httpErrors from 'http-errors';
import { Document, User } from '../models';

View File

@ -1,3 +1,4 @@
// @flow
import bodyParser from 'koa-bodyparser';
import Koa from 'koa';
import Router from 'koa-router';

View File

@ -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<void>
) {
await next();
const ok = ctx.status < 400;
// $FlowFixMe
ctx.body = {
...ctx.body,
status: ctx.status,

View File

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

View File

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

View File

@ -1,3 +1,4 @@
// @flow
import uuid from 'uuid';
import Router from 'koa-router';

View File

@ -1,3 +1,4 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from 'fetch-test-server';
import app from '..';