diff --git a/pages/api/albums/[album].js b/pages/api/albums/[album].js index 3431567b..0b0cd571 100644 --- a/pages/api/albums/[album].js +++ b/pages/api/albums/[album].js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import Album from '../../../src/core/Album' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const id = req.query.album @@ -15,4 +16,4 @@ export default async (req, res) => { error: `Album ${id} not found` }) } -} +}) diff --git a/pages/api/albums/index.js b/pages/api/albums/index.js index d610f214..70bf386d 100644 --- a/pages/api/albums/index.js +++ b/pages/api/albums/index.js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import Album from '../../../src/core/Album' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const data = await Album.find({}, { sort: { date: -1 } }) @@ -15,4 +16,4 @@ export default async (req, res) => { error: `Article not found` }) } -} +}) diff --git a/pages/api/articles/index.js b/pages/api/articles/index.js index 01650097..35347033 100644 --- a/pages/api/articles/index.js +++ b/pages/api/articles/index.js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import { findArticles } from '../../../src/api/articles' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const { query } = req @@ -18,4 +19,4 @@ export default async (req, res) => { error: `Article ${id} not found` }) } -} +}) diff --git a/pages/api/articles/urls.js b/pages/api/articles/urls.js index 628194e6..8ed535b0 100644 --- a/pages/api/articles/urls.js +++ b/pages/api/articles/urls.js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import Article from '../../../src/core/Article' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const data = await Article.find({}) @@ -17,4 +18,4 @@ export default async (req, res) => { error: `Not found` }) } -} +}) diff --git a/pages/api/files/[document].js b/pages/api/files/[document].js index 67542634..8d8367fb 100644 --- a/pages/api/files/[document].js +++ b/pages/api/files/[document].js @@ -1,8 +1,9 @@ import { connect } from '../../../src/core/db' import Document from '../../../src/core/Document' import { encodeFile } from '../../../src/api/files' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const id = req.query.document @@ -16,4 +17,4 @@ export default async (req, res) => { error: `Document ${id} not found` }) } -} +}) diff --git a/pages/api/files/index.js b/pages/api/files/index.js index 9fcd4798..2438a703 100644 --- a/pages/api/files/index.js +++ b/pages/api/files/index.js @@ -1,8 +1,9 @@ import { connect } from '../../../src/core/db' import Document from '../../../src/core/Document' import { encodeFile } from '../../../src/api/files' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const documents = await Document.find({hidden: false}) @@ -20,4 +21,4 @@ export default async (req, res) => { error: `Documents not found` }) } -} +}) diff --git a/pages/api/files/urls.js b/pages/api/files/urls.js index 91fa04eb..68aa0c6e 100644 --- a/pages/api/files/urls.js +++ b/pages/api/files/urls.js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import Document from '../../../src/core/Document' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const data = await Document.find({}) @@ -17,4 +18,4 @@ export default async (req, res) => { error: `Not found` }) } -} +}) diff --git a/pages/api/image.js b/pages/api/image.js index e6ae826e..b7c2d270 100644 --- a/pages/api/image.js +++ b/pages/api/image.js @@ -1,8 +1,9 @@ import { connect } from '../../src/core/db' import Image from '../../src/core/Image' import { encodeImage } from '../../src/api/image' +import { withMiddleware } from '../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const file = req.query.file @@ -16,4 +17,4 @@ export default async (req, res) => { error: `Image ${file} not found` }) } -} +}) diff --git a/pages/api/page.js b/pages/api/page.js index b17717de..b9b5d5b0 100644 --- a/pages/api/page.js +++ b/pages/api/page.js @@ -2,8 +2,9 @@ import { connect } from '../../src/core/db' import Page from '../../src/core/Page' import { getPathWithNoTrailingSlash } from '../../src/lib/url' import { encodePage } from '../../src/api/page' +import { withMiddleware } from '../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const page = getPathWithNoTrailingSlash(req.query.page) @@ -17,4 +18,4 @@ export default async (req, res) => { error: `Image ${page} not found` }) } -} +}) diff --git a/pages/api/pages/index.js b/pages/api/pages/index.js index fae73e68..2977f0d2 100644 --- a/pages/api/pages/index.js +++ b/pages/api/pages/index.js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import Page from '../../../src/core/Page' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const items = await Page.find({}) @@ -16,4 +17,4 @@ export default async (req, res) => { error: `Article ${id} not found` }) } -} +}) diff --git a/pages/api/pages/urls.js b/pages/api/pages/urls.js index 629d5a0a..c26272d0 100644 --- a/pages/api/pages/urls.js +++ b/pages/api/pages/urls.js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import Page from '../../../src/core/Page' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const data = await Page.find({}) @@ -17,4 +18,4 @@ export default async (req, res) => { error: `Not found` }) } -} +}) diff --git a/pages/api/persons/[person].js b/pages/api/persons/[person].js index 4e887812..d5fb7d4e 100644 --- a/pages/api/persons/[person].js +++ b/pages/api/persons/[person].js @@ -1,8 +1,9 @@ import { connect } from '../../../src/core/db' import Teacher from '../../../src/core/Teacher' import { encodePerson } from '../../../src/api/persons' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const id = req.query.person @@ -16,4 +17,4 @@ export default async (req, res) => { error: `Article ${id} not found` }) } -} +}) diff --git a/pages/api/persons/index.js b/pages/api/persons/index.js index 0bfcbc00..a942e281 100644 --- a/pages/api/persons/index.js +++ b/pages/api/persons/index.js @@ -2,8 +2,9 @@ import { connect } from '../../../src/core/db' import Teacher from '../../../src/core/Teacher' import { prioritySort } from '../../../src/lib/sort' import { encodePerson } from '../../../src/api/persons' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const order = [ @@ -29,4 +30,4 @@ export default async (req, res) => { error: `Article ${id} not found` }) } -} +}) diff --git a/pages/api/persons/urls.js b/pages/api/persons/urls.js index da82658f..d08abdbc 100644 --- a/pages/api/persons/urls.js +++ b/pages/api/persons/urls.js @@ -1,7 +1,8 @@ import { connect } from '../../../src/core/db' import Teacher from '../../../src/core/Teacher' +import { withMiddleware } from '../../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const data = await Teacher.find({}) @@ -17,4 +18,4 @@ export default async (req, res) => { error: `Not found` }) } -} +}) diff --git a/pages/api/schedule.js b/pages/api/schedule.js index 3186007e..00b0a11c 100644 --- a/pages/api/schedule.js +++ b/pages/api/schedule.js @@ -1,7 +1,8 @@ import { connect } from '../../src/core/db' import { findSchedule } from '../../src/core/schedule' +import { withMiddleware } from '../../src/middlewares/withMiddleware' -export default async (req, res) => { +export default withMiddleware(async (req, res) => { await connect() const period = req.query.period @@ -16,4 +17,4 @@ export default async (req, res) => { error: `Schedule ${period}:${semester} not found` }) } -} +}) diff --git a/src/middlewares/withMiddleware.js b/src/middlewares/withMiddleware.js new file mode 100644 index 00000000..925e3849 --- /dev/null +++ b/src/middlewares/withMiddleware.js @@ -0,0 +1,3 @@ +export const withMiddleware = handler => async (req, res) => { + return handler(req, res); +};