Skip to content

Commit

Permalink
Merge pull request #87 from telostat/85-add-a-version-endpoint
Browse files Browse the repository at this point in the history
Add version endpoint, cleanup upload files and migrate eslint config
  • Loading branch information
fkoksal authored Nov 6, 2024
2 parents 30081e4 + d21e447 commit 9b6b41c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
34 changes: 34 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default [...compat.extends(
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:prettier/recommended",
), {
languageOptions: {
parser: tsParser,
ecmaVersion: 2018,
sourceType: "module",
},

rules: {
"@typescript-eslint/no-unused-vars": ["warn", {
argsIgnorePattern: "^_",
}],

"@typescript-eslint/ban-ts-ignore": 0,
"@typescript-eslint/no-explicit-any": 0,
},
}];
2 changes: 2 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
},
"devDependencies": {
"@commitlint/cli": "^19.5.0",
"@eslint/js":"^9.14.0",
"@eslint/eslintrc":"^3.1.0",
"@types/express": "^5.0.0",
"@types/html-to-text": "^9.0.4",
"@types/jest": "^29.5.14",
Expand Down
19 changes: 18 additions & 1 deletion src/applications/restapi/endpoints/sendmail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request, Response } from 'express';
import { unlink } from 'node:fs';
import { Address, Attachments, Recipient, RecipientType } from '../../../commons';
import { templatedMjmlEmailer } from '../../../implementations/programs';
import { SimpleSMTPConfig } from '../../../implementations/senders';
Expand Down Expand Up @@ -65,6 +66,13 @@ function readRequest(request: Request): Promise<Input> {
const mjmTemplateReader = readFile(mjmTemplate.path, { encoding: 'utf8' });
const txtTemplateReader = txtTemplate ? readFile(txtTemplate.path, { encoding: 'utf8' }) : undefined;

// Delete uploaded metadata and templates
[metadata, mjmTemplate, txtTemplate].forEach((element) => {
unlink(`./${element.path}`, (err) => {
if (err) throw err;
});
});

// Attempt to build the request input:
return Promise.all([metadataReader, mjmTemplateReader, txtTemplateReader]).then(
([metadataC, mjmTemplate, txtTemplate]) => {
Expand Down Expand Up @@ -128,7 +136,16 @@ export async function sendmail(request: Request, response: Response): Promise<vo

// Run the Mailess program and return:
program(input.subject, input.from, input.recipients, input.context, input.attachments).then(
(ret) => response.send({ status: 'SUCCESS', data: ret }),
(ret) => {
// Delet uploaded attachments
input.attachments.forEach((element) => {
unlink(`./${element.path}`, (err) => {
if (err) throw err;
});
});

response.send({ status: 'SUCCESS', data: ret });
},
(err) => {
if (err instanceof MailessError) {
response.status(400).send({ status: 'ERROR', data: err.message });
Expand Down
12 changes: 12 additions & 0 deletions src/applications/restapi/endpoints/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request, Response } from 'express';
import pjson from '../../../../package.json';

/**
* Provides the endpoint functionality.
*
* @param request HTTP request.
* @param response HTTP response.
*/
export async function version(_request: Request, response: Response): Promise<void> {
response.json({ version: pjson.version });
}
4 changes: 3 additions & 1 deletion src/applications/restapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from 'express';
import multer from 'multer';
import { sendmail } from './endpoints/sendmail';
import { notification } from './endpoints/notification';
import { sendmail } from './endpoints/sendmail';
import { version } from './endpoints/version';

/**
* Provides an encoding for named file uploads.
Expand Down Expand Up @@ -67,6 +68,7 @@ export function runApplication(config: AppConfig): void {
]);

// Define endpoints:
app.get('/version', version);
app.post('/sendmail', uploadsMiddleware, sendmail);
app.post('/notification', uploadsMiddleware, notification);

Expand Down

0 comments on commit 9b6b41c

Please sign in to comment.