Skip to content

Commit

Permalink
refactor: use TypeScript for some middleware
Browse files Browse the repository at this point in the history
Refs #398
  • Loading branch information
thewilkybarkid committed Nov 8, 2021
1 parent fdfd4b8 commit 72f9f95
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Community> => {
return async (ctx, next) => {
const path = ctx.request.path.replace(/^\/+|\/+$/g, '').split('/');
if (path[0] === 'api' && path[2] === 'communities') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DefaultState, Middleware } from 'koa';
import path from 'path';
import Email from 'email-templates';
import nodemailer from 'nodemailer';
Expand All @@ -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<DefaultState, Mailer> => {
const transport = nodemailer.createTransport(
sendgridTransport({
apiKey: config.emailSendgridKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Persona> => {
return async (ctx, next) => {
const path = ctx.request.path.replace(/^\/+|\/+$/g, '').split('/');
if (path[0] === 'api' && path[2] === 'personas') {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<User> => {
return async (ctx, next) => {
const path = ctx.request.path.replace(/^\/+|\/+$/g, '').split('/');
if (path[0] === 'api' && path[2] === 'users') {
Expand Down
8 changes: 4 additions & 4 deletions src/backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 72f9f95

Please sign in to comment.