From 72f9f95997d2ae24e5641d96a35476e60611d64f Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Mon, 8 Nov 2021 15:31:04 +0000 Subject: [PATCH] refactor: use TypeScript for some middleware Refs #398 --- package-lock.json | 35 +++++++++++++++++++ package.json | 3 ++ .../middleware/{community.js => community.ts} | 11 +++--- src/backend/middleware/{mail.js => mail.ts} | 12 ++++++- .../middleware/{persona.js => persona.ts} | 11 +++--- src/backend/middleware/{user.js => user.ts} | 11 +++--- src/backend/server.ts | 8 ++--- 7 files changed, 74 insertions(+), 17 deletions(-) rename src/backend/middleware/{community.js => community.ts} (73%) rename src/backend/middleware/{mail.js => mail.ts} (76%) rename src/backend/middleware/{persona.js => persona.ts} (73%) rename src/backend/middleware/{user.js => user.ts} (75%) diff --git a/package-lock.json b/package-lock.json index 3985eeb8..af7eec01 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6916,6 +6916,17 @@ "integrity": "sha512-aC6eqz0NYnHJQ1CkE8RcIJ2fU50id4DWNNk7blt/6nlLT2rMxfWy3LNMbpQWbmULZuqcUPI+Wq5PTNdJtELtaw==", "dev": true }, + "@types/email-templates": { + "version": "8.0.4", + "resolved": "https://registry.npmjs.org/@types/email-templates/-/email-templates-8.0.4.tgz", + "integrity": "sha512-HYvVoyG8qS6PrimZZOS4wMrtQ9MelKEl0sOpi4zVpz2Ds74v+UvWckIFz3NyGyTwAR1okMbwJkApgR2GL/ALjg==", + "dev": true, + "requires": { + "@types/html-to-text": "*", + "@types/nodemailer": "*", + "juice": "^7.0.0" + } + }, "@types/express": { "version": "4.17.11", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz", @@ -6975,6 +6986,12 @@ "hoist-non-react-statics": "^3.3.0" } }, + "@types/html-to-text": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/@types/html-to-text/-/html-to-text-8.0.1.tgz", + "integrity": "sha512-0B/OifmJYmk5r9z9+KJtGWOF0LEjbTN4D2QeCh+mAw81JkJwC83NvNWUZFEqRT5PpnjX7vX0ab1SMGcwCs3Lag==", + "dev": true + }, "@types/http-assert": { "version": "1.5.3", "resolved": "https://registry.npmjs.org/@types/http-assert/-/http-assert-1.5.3.tgz", @@ -7224,6 +7241,24 @@ } } }, + "@types/nodemailer": { + "version": "6.4.4", + "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.4.4.tgz", + "integrity": "sha512-Ksw4t7iliXeYGvIQcSIgWQ5BLuC/mljIEbjf615svhZL10PE9t+ei8O9gDaD3FPCasUJn9KTLwz2JFJyiiyuqw==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, + "@types/nodemailer-sendgrid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@types/nodemailer-sendgrid/-/nodemailer-sendgrid-1.0.0.tgz", + "integrity": "sha512-7D3Ddd7vN4tTDNGUkv31zH+2Wr6a37V0ryk0v5dyyBow/NA5+Jx5L3HvN1xq2ArZ2DbL2/z0tR9rfJtnJZK/Gw==", + "dev": true, + "requires": { + "@types/nodemailer": "*" + } + }, "@types/normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz", diff --git a/package.json b/package.json index 8893f7da..dfdcd088 100644 --- a/package.json +++ b/package.json @@ -163,6 +163,7 @@ "@parcel/transformer-typescript-tsc": "2.0.0-beta.2", "@parcel/validator-typescript": "2.0.0-beta.2", "@types/doi-regex": "^0.1.0", + "@types/email-templates": "^8.0.4", "@types/faker": "^5.5.9", "@types/identifiers-arxiv": "^0.1.0", "@types/jest": "^27.0.2", @@ -177,6 +178,8 @@ "@types/koa__cors": "^2.2.3", "@types/node": "^14.17.32", "@types/node-fetch": "^2.5.12", + "@types/nodemailer": "^6.4.4", + "@types/nodemailer-sendgrid": "^1.0.0", "@types/parcel-env": "^0.0.1", "@types/quill": "^1.3.10", "@types/react-dom": "^16.9.14", diff --git a/src/backend/middleware/community.js b/src/backend/middleware/community.ts similarity index 73% rename from src/backend/middleware/community.js rename to src/backend/middleware/community.ts index bf122098..ccb30129 100644 --- a/src/backend/middleware/community.js +++ b/src/backend/middleware/community.ts @@ -1,11 +1,14 @@ +import { Middleware } from 'koa'; + /** * Middleware shim to get current community and add it to context. - * - * @param {Object} ctx - the koa context object - * @param {function} next - continue to next middleware */ -const currentCommunity = () => { +type Community = { + community: string | null +} + +const currentCommunity = (): Middleware => { return async (ctx, next) => { const path = ctx.request.path.replace(/^\/+|\/+$/g, '').split('/'); if (path[0] === 'api' && path[2] === 'communities') { diff --git a/src/backend/middleware/mail.js b/src/backend/middleware/mail.ts similarity index 76% rename from src/backend/middleware/mail.js rename to src/backend/middleware/mail.ts index f4f62fbf..45bc1047 100644 --- a/src/backend/middleware/mail.js +++ b/src/backend/middleware/mail.ts @@ -1,3 +1,4 @@ +import { DefaultState, Middleware } from 'koa'; import path from 'path'; import Email from 'email-templates'; import nodemailer from 'nodemailer'; @@ -7,7 +8,16 @@ import sendgridTransport from 'nodemailer-sendgrid'; const TEMPLATE_DIR = path.resolve('dist', 'backend', 'templates', 'email'); -export const mailWrapper = config => { +type Config = { + emailAddress: string, + emailSendgridKey: string, +} + +type Mailer = { + mail: Email; +} + +export const mailWrapper = (config: Config): Middleware => { const transport = nodemailer.createTransport( sendgridTransport({ apiKey: config.emailSendgridKey, diff --git a/src/backend/middleware/persona.js b/src/backend/middleware/persona.ts similarity index 73% rename from src/backend/middleware/persona.js rename to src/backend/middleware/persona.ts index 3692c9ab..1e5e2a3d 100644 --- a/src/backend/middleware/persona.js +++ b/src/backend/middleware/persona.ts @@ -1,11 +1,14 @@ +import { Middleware } from 'koa'; + /** * Middleware shim to get current persona and add it to context. - * - * @param {Object} ctx - the koa context object - * @param {function} next - continue to next middleware */ -const currentPersona = () => { +type Persona = { + persona: string | null; +} + +const currentPersona = (): Middleware => { return async (ctx, next) => { const path = ctx.request.path.replace(/^\/+|\/+$/g, '').split('/'); if (path[0] === 'api' && path[2] === 'personas') { diff --git a/src/backend/middleware/user.js b/src/backend/middleware/user.ts similarity index 75% rename from src/backend/middleware/user.js rename to src/backend/middleware/user.ts index 91a4c987..b6ec84f6 100644 --- a/src/backend/middleware/user.js +++ b/src/backend/middleware/user.ts @@ -1,11 +1,14 @@ +import { Middleware } from 'koa'; + /** * Middleware shim to get current user and add it to context. - * - * @param {Object} ctx - the koa context object - * @param {function} next - continue to next middleware */ -const currentUser = () => { +type User = { + identity: string | null; +} + +const currentUser = (): Middleware => { return async (ctx, next) => { const path = ctx.request.path.replace(/^\/+|\/+$/g, '').split('/'); if (path[0] === 'api' && path[2] === 'users') { diff --git a/src/backend/server.ts b/src/backend/server.ts index da0ca96d..c537368d 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -29,10 +29,10 @@ import { createError } from './utils/http-errors'; // Our middlewares import authWrapper from './middleware/auth.js'; // authorization/user roles -import currentCommunity from './middleware/community.js'; -import currentPersona from './middleware/persona.js'; -import currentUser from './middleware/user.js'; -import { mailWrapper } from './middleware/mail.js'; +import currentCommunity from './middleware/community'; +import currentPersona from './middleware/persona'; +import currentUser from './middleware/user'; +import { mailWrapper } from './middleware/mail'; // Our models import {