Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions dist/src/routes/apiV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
// External Libs
const express = require("express");
const expressBrute = require("express-brute");
const redisStore = require("express-brute-redis");
// External Files
const account_1 = __importDefault(require("../controllers/account"));
const block_1 = __importDefault(require("../controllers/block"));
const publicBlock_1 = __importDefault(require("../controllers/publicBlock"));
const logger_1 = __importDefault(require("../services/logger"));
// Logger setup
const logger = new logger_1.default('API');
// Express Brute setup (1 request per 1/2 sec)
const store = new redisStore();
const failCallback = (req, res, next, nextValid) => {
const message = `Please wait till ${nextValid} to make future requests`;
res.status(429).send(`Pantry API limit reached. ${message}`);
};
const bruteForce = new expressBrute(store, {
failCallback,
freeRetries: 5,
minWait: 9000,
maxWait: 9000,
lifetime: 10,
refreshTimeoutOnRequest: false,
});
// Router setup
const _apiV1Router = express.Router();
_apiV1Router.post('/create', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
Expand Down Expand Up @@ -64,12 +48,7 @@ _apiV1Router.put('/:pantryID', (req, res) => __awaiter(void 0, void 0, void 0, f
details: error.message });
}
}));
_apiV1Router.get('/:pantryID', bruteForce.getMiddleware({
key: (req, res, next) => {
const { pantryID } = accountParams(req);
next(pantryID);
},
}), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
_apiV1Router.get('/:pantryID', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { pantryID } = accountParams(req);
logger.info('[GET] Get Account', { pantryID });
Expand Down Expand Up @@ -119,12 +98,7 @@ _apiV1Router.put('/:pantryID/basket/:basketName', (req, res) => __awaiter(void 0
details: error.message });
}
}));
_apiV1Router.get('/:pantryID/basket/:basketName', bruteForce.getMiddleware({
key: (req, res, next) => {
const { pantryID } = accountParams(req);
next(pantryID);
},
}), (req, res) => __awaiter(void 0, void 0, void 0, function* () {
_apiV1Router.get('/:pantryID/basket/:basketName', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { pantryID, basketName } = basketParams(req);
logger.info('[GET] Get Basket', { pantryID, basketName });
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@
"deepmerge": "^4.2.2",
"dotenv": "^6.1.0",
"express": "^4.16.3",
"express-brute": "^1.0.1",
"express-brute-redis": "^0.0.1",
"helmet": "^3.22.0",
"pino": "^5.17.0",
"pino-logflare": "^0.3.12",
"redis": "^3.1.1",
"url-loader": "^1.0.1",
"uuid": "^3.4.0",
Expand Down
81 changes: 24 additions & 57 deletions src/routes/apiV1.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// External Libs
import express = require('express')
import expressBrute = require('express-brute')
import redisStore = require('express-brute-redis')

// External Files
import AccountController from '../controllers/account'
Expand All @@ -16,21 +14,6 @@ import { IBlockRequestParams } from '../interfaces/block'
// Logger setup
const logger = new logService('API')

// Express Brute setup (1 request per 1/2 sec)
const store = new redisStore()
const failCallback = (req, res, next, nextValid) => {
const message = `Please wait till ${nextValid} to make future requests`
res.status(429).send(`Pantry API limit reached. ${message}`)
}
const bruteForce = new expressBrute(store, {
failCallback,
freeRetries: 5,
minWait: 9000, // 9s
maxWait: 9000, // 9s
lifetime: 10, // 10s,
refreshTimeoutOnRequest: false,
})

// Router setup
const _apiV1Router = express.Router()

Expand Down Expand Up @@ -63,27 +46,19 @@ _apiV1Router.put('/:pantryID', async (req, res) => {
}
})

_apiV1Router.get('/:pantryID',
bruteForce.getMiddleware({
key: (req, res, next) => {
const { pantryID } = accountParams(req)
next(pantryID)
},
}),
async (req, res) => {
try {
const { pantryID } = accountParams(req)

logger.info('[GET] Get Account', { pantryID })
const _account = await AccountController.get(pantryID)

res.send(_account)
} catch (error) {
res.status(400).json({ error: 'Could not get pantry',
details: error.message })
}
_apiV1Router.get('/:pantryID', async (req, res) => {
try {
const { pantryID } = accountParams(req)

logger.info('[GET] Get Account', { pantryID })
const _account = await AccountController.get(pantryID)

res.send(_account)
} catch (error) {
res.status(400).json({ error: 'Could not get pantry',
details: error.message })
}
)
})

_apiV1Router.delete('/:pantryID', async (req, res) => {
try {
Expand Down Expand Up @@ -129,27 +104,19 @@ _apiV1Router.put('/:pantryID/basket/:basketName', async (req, res) => {
}
})

_apiV1Router.get('/:pantryID/basket/:basketName',
bruteForce.getMiddleware({
key: (req, res, next) => {
const { pantryID } = accountParams(req)
next(pantryID)
},
}),
async (req, res) => {
try {
const { pantryID, basketName } = basketParams(req)

logger.info('[GET] Get Basket', { pantryID, basketName })
const _response = await BlockController.get(pantryID, basketName)

res.send(_response)
} catch (error) {
res.status(400).json({ error: 'Could not get basket',
details: error.message })
}
_apiV1Router.get('/:pantryID/basket/:basketName', async (req, res) => {
try {
const { pantryID, basketName } = basketParams(req)

logger.info('[GET] Get Basket', { pantryID, basketName })
const _response = await BlockController.get(pantryID, basketName)

res.send(_response)
} catch (error) {
res.status(400).json({ error: 'Could not get basket',
details: error.message })
}
)
})

_apiV1Router.delete('/:pantryID/basket/:basketName', async (req, res) => {
try {
Expand Down
Loading
Loading