Rename routes to controllers

This commit is contained in:
rijkvanzanten
2020-08-15 16:56:45 -06:00
parent c644fa8ba0
commit c2b6b2a090
21 changed files with 20 additions and 20 deletions

View File

@@ -0,0 +1,51 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { nanoid } from 'nanoid';
import { InvalidQueryException, InvalidPayloadException } from '../exceptions';
import argon2 from 'argon2';
const router = Router();
router.get(
'/random/string',
asyncHandler(async (req, res) => {
if (req.query && req.query.length && Number(req.query.length) > 500)
throw new InvalidQueryException(`"length" can't be more than 500 characters`);
const string = nanoid(req.query?.length ? Number(req.query.length) : 32);
return res.json({ data: string });
})
);
router.post(
'/hash',
asyncHandler(async (req, res) => {
if (!req.body?.string) {
throw new InvalidPayloadException(`"string" is required`);
}
const hash = await argon2.hash(req.body.string);
return res.json({ data: hash });
})
);
router.post(
'/hash/verify',
asyncHandler(async (req, res) => {
if (!req.body?.string) {
throw new InvalidPayloadException(`"string" is required`);
}
if (!req.body?.hash) {
throw new InvalidPayloadException(`"hash" is required`);
}
const result = await argon2.verify(req.body.hash, req.body.string);
return res.json({ data: result });
})
);
export default router;