Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit eaacf25

Browse files
authored
Improve middleware (#345)
1 parent 7833b81 commit eaacf25

File tree

11 files changed

+131
-102
lines changed

11 files changed

+131
-102
lines changed

src/app/middleware/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const cacheControl = require('./cache-control');
2+
const cors = require('./cors');
3+
const error = require('./error');
4+
const invalidUrl = require('./invalid-url');
5+
const metricGranularity = require('./metric-granularity');
6+
const pagination = require('./pagination');
7+
const timePeriod = require('./time-period');
8+
9+
module.exports = {
10+
cacheControl,
11+
cors,
12+
error,
13+
invalidUrl,
14+
metricGranularity,
15+
pagination,
16+
timePeriod,
17+
};

src/app/middleware/validate-granularity.js renamed to src/app/middleware/metric-granularity.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const _ = require('lodash');
22

33
const { GRANULARITY, TIME_PERIOD } = require('../../constants');
4+
const determineGranularityForTimePeriod = require('../../metrics/determine-granularity-for-time-period');
45
const InvalidParameterError = require('../errors/invalid-parameter-error');
56

67
const VALID_VALUES_BY_PERIOD = {
@@ -12,12 +13,17 @@ const VALID_VALUES_BY_PERIOD = {
1213
};
1314

1415
const createMiddleware = paramNames => async (context, next) => {
15-
const { request } = context;
16+
const { params, request } = context;
1617

17-
const period = _.get(request, `query.${paramNames.period}`);
18+
const period = _.get(params, paramNames.period);
1819
const granularity = _.get(request, `query.${paramNames.granularity}`);
1920

2021
if (granularity === undefined) {
22+
_.set(
23+
context,
24+
['params', paramNames.granularity],
25+
determineGranularityForTimePeriod(period),
26+
);
2127
await next();
2228
return;
2329
}
@@ -31,6 +37,8 @@ const createMiddleware = paramNames => async (context, next) => {
3137
);
3238
}
3339

40+
_.set(context, ['params', paramNames.granularity], granularity);
41+
3442
await next();
3543
};
3644

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const _ = require('lodash');
33
const { TIME_PERIOD } = require('../../constants');
44
const InvalidParameterError = require('../errors/invalid-parameter-error');
55

6-
const createMiddleware = paramName => async (context, next) => {
6+
const createMiddleware = (paramName, defaultValue) => async (context, next) => {
77
const { request } = context;
88

9-
const period = _.get(request, `query.${paramName}`);
9+
const period = _.get(request, `query.${paramName}`, defaultValue);
1010
const validValues = _.values(TIME_PERIOD);
1111

1212
if (period !== undefined && !validValues.includes(period)) {
@@ -16,6 +16,8 @@ const createMiddleware = paramName => async (context, next) => {
1616
);
1717
}
1818

19+
_.set(context, ['params', paramName], period);
20+
1921
await next();
2022
};
2123

src/app/routes/v1/fills.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Router = require('koa-router');
66
const Fill = require('../../../model/fill');
77
const getRelayerLookupId = require('../../../relayers/get-relayer-lookup-id');
88
const InvalidParameterError = require('../../errors/invalid-parameter-error');
9-
const pagination = require('../../middleware/pagination');
9+
const middleware = require('../../middleware');
1010
const reverseMapStatus = require('../../../fills/reverse-map-status');
1111
const searchFills = require('../../../fills/search-fills');
1212
const transformFill = require('./util/transform-fill');
@@ -41,7 +41,11 @@ const createRouter = () => {
4141

4242
router.get(
4343
'/',
44-
pagination({ defaultLimit: 20, maxLimit: 50, maxPage: Infinity }),
44+
middleware.pagination({
45+
defaultLimit: 20,
46+
maxLimit: 50,
47+
maxPage: Infinity,
48+
}),
4549
async ({ pagination: { limit, page }, request, response }, next) => {
4650
const { address, bridgeAddress, status, token } = request.query;
4751
const relayerId = request.query.relayer;

src/app/routes/v1/metrics.js

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,22 @@ const getRelayerMetrics = require('../../../metrics/get-relayer-metrics');
1212
const getTokenMetrics = require('../../../metrics/get-token-metrics');
1313
const getTraderMetrics = require('../../../metrics/get-trader-metrics');
1414
const InvalidParameterError = require('../../errors/invalid-parameter-error');
15+
const middleware = require('../../middleware');
1516
const MissingParameterError = require('../../errors/missing-parameter-error');
1617
const Token = require('../../../model/token');
17-
const validatePeriod = require('../../middleware/validate-period');
18-
const validateGranularity = require('../../middleware/validate-granularity');
1918

2019
const createRouter = () => {
2120
const router = new Router({ prefix: '/metrics' });
2221

2322
router.get(
2423
'/network',
25-
validatePeriod('period'),
26-
validateGranularity({ period: 'period', granularity: 'granularity' }),
27-
async ({ request, response }, next) => {
28-
const period = request.query.period || TIME_PERIOD.MONTH;
29-
const granularity =
30-
request.query.granularity === undefined
31-
? determineGranularityForTimePeriod(period)
32-
: request.query.granularity;
33-
24+
middleware.timePeriod('period', TIME_PERIOD.MONTH),
25+
middleware.metricGranularity({
26+
period: 'period',
27+
granularity: 'granularity',
28+
}),
29+
async ({ params, response }, next) => {
30+
const { granularity, period } = params;
3431
const metrics = await getNetworkMetrics(period, granularity);
3532

3633
response.body = metrics;
@@ -41,8 +38,8 @@ const createRouter = () => {
4138

4239
router.get(
4340
'/token',
44-
validatePeriod('period'),
45-
async ({ request, response }, next) => {
41+
middleware.timePeriod('period', TIME_PERIOD.MONTH),
42+
async ({ params, request, response }, next) => {
4643
const tokenAddress = request.query.token;
4744

4845
if (tokenAddress === undefined) {
@@ -58,9 +55,8 @@ const createRouter = () => {
5855
);
5956
}
6057

61-
const period = request.query.period || TIME_PERIOD.MONTH;
58+
const { period } = params;
6259
const granularity = determineGranularityForTimePeriod(period);
63-
6460
const metrics = await getTokenMetrics(token, period, granularity);
6561

6662
response.body = metrics;
@@ -71,9 +67,12 @@ const createRouter = () => {
7167

7268
router.get(
7369
'/trader',
74-
validatePeriod('period'),
75-
validateGranularity({ period: 'period', granularity: 'granularity' }),
76-
async ({ request, response }, next) => {
70+
middleware.timePeriod('period', TIME_PERIOD.MONTH),
71+
middleware.metricGranularity({
72+
period: 'period',
73+
granularity: 'granularity',
74+
}),
75+
async ({ params, request, response }, next) => {
7776
const { address } = request.query;
7877

7978
if (_.isEmpty(address)) {
@@ -89,13 +88,7 @@ const createRouter = () => {
8988
);
9089
}
9190

92-
const period = request.query.period || TIME_PERIOD.MONTH;
93-
94-
const granularity =
95-
request.query.granularity === undefined
96-
? determineGranularityForTimePeriod(period)
97-
: request.query.granularity;
98-
91+
const { granularity, period } = params;
9992
const metrics = await getTraderMetrics(address, period, granularity);
10093

10194
response.body = metrics;
@@ -106,10 +99,12 @@ const createRouter = () => {
10699

107100
router.get(
108101
'/relayer',
109-
validatePeriod('period'),
110-
validateGranularity({ period: 'period', granularity: 'granularity' }),
111-
async ({ request, response }, next) => {
112-
const period = request.query.period || TIME_PERIOD.MONTH;
102+
middleware.timePeriod('period', TIME_PERIOD.MONTH),
103+
middleware.metricGranularity({
104+
period: 'period',
105+
granularity: 'granularity',
106+
}),
107+
async ({ params, request, response }, next) => {
113108
const relayerId = request.query.relayer;
114109

115110
if (relayerId === undefined) {
@@ -125,11 +120,7 @@ const createRouter = () => {
125120
);
126121
}
127122

128-
const granularity =
129-
request.query.granularity === undefined
130-
? determineGranularityForTimePeriod(period)
131-
: request.query.granularity;
132-
123+
const { granularity, period } = params;
133124
const metrics = await getRelayerMetrics(
134125
relayerLookupId,
135126
period,
@@ -144,15 +135,13 @@ const createRouter = () => {
144135

145136
router.get(
146137
'/protocol',
147-
validatePeriod('period'),
148-
validateGranularity({ period: 'period', granularity: 'granularity' }),
149-
async ({ request, response }, next) => {
150-
const period = request.query.period || TIME_PERIOD.MONTH;
151-
const granularity =
152-
request.query.granularity === undefined
153-
? determineGranularityForTimePeriod(period)
154-
: request.query.granularity;
155-
138+
middleware.timePeriod('period', TIME_PERIOD.MONTH),
139+
middleware.metricGranularity({
140+
period: 'period',
141+
granularity: 'granularity',
142+
}),
143+
async ({ params, response }, next) => {
144+
const { granularity, period } = params;
156145
const metrics = await getProtocolMetrics(period, granularity);
157146

158147
response.body = metrics;
@@ -163,15 +152,13 @@ const createRouter = () => {
163152

164153
router.get(
165154
'/active-trader',
166-
validatePeriod('period'),
167-
validateGranularity({ period: 'period', granularity: 'granularity' }),
168-
async ({ request, response }, next) => {
169-
const period = request.query.period || TIME_PERIOD.MONTH;
170-
const granularity =
171-
request.query.granularity === undefined
172-
? determineGranularityForTimePeriod(period)
173-
: request.query.granularity;
174-
155+
middleware.timePeriod('period', TIME_PERIOD.MONTH),
156+
middleware.metricGranularity({
157+
period: 'period',
158+
granularity: 'granularity',
159+
}),
160+
async ({ params, response }, next) => {
161+
const { granularity, period } = params;
175162
const metrics = await getActiveTraderMetrics(period, granularity);
176163

177164
response.body = metrics;

src/app/routes/v1/protocols.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ const getDatesForTimePeriod = require('../../../util/get-dates-for-time-period')
55
const getProtocolsWith24HourStats = require('../../../protocols/get-protocols-with-24-hour-stats');
66
const getProtocolsWithStatsForDates = require('../../../protocols/get-protocols-with-stats-for-dates');
77
const InvalidParameterError = require('../../errors/invalid-parameter-error');
8-
const pagination = require('../../middleware/pagination');
9-
const validatePeriod = require('../../middleware/validate-period');
8+
const middleware = require('../../middleware');
109

1110
const createRouter = () => {
1211
const router = new Router();
1312

1413
router.get(
1514
'/protocols',
16-
pagination({ defaultLimit: 20, maxLimit: 50, maxPage: Infinity }),
17-
validatePeriod('statsPeriod'),
18-
async ({ pagination: { limit, page }, request, response }, next) => {
15+
middleware.pagination({
16+
defaultLimit: 20,
17+
maxLimit: 50,
18+
maxPage: Infinity,
19+
}),
20+
middleware.timePeriod('statsPeriod', TIME_PERIOD.DAY),
21+
async ({ pagination, params, request, response }, next) => {
22+
const { limit, page } = pagination;
1923
const { sortBy } = request.query;
20-
const statsPeriod = request.query.statsPeriod || TIME_PERIOD.DAY;
24+
const { statsPeriod } = params;
2125
const { dateFrom, dateTo } = getDatesForTimePeriod(statsPeriod);
2226

2327
if (

src/app/routes/v1/relayers.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ const { TIME_PERIOD } = require('../../../constants');
44
const getDatesForTimePeriod = require('../../../util/get-dates-for-time-period');
55
const getRelayersWith24HourStats = require('../../../relayers/get-relayers-with-24-hour-stats');
66
const getRelayersWithStatsForDates = require('../../../relayers/get-relayers-with-stats-for-dates');
7-
const pagination = require('../../middleware/pagination');
8-
const validatePeriod = require('../../middleware/validate-period');
7+
const middleware = require('../../middleware');
98

109
const createRouter = () => {
1110
const router = new Router();
1211

1312
router.get(
1413
'/relayers',
15-
pagination({ defaultLimit: 20, maxLimit: 50, maxPage: Infinity }),
16-
validatePeriod('statsPeriod'),
17-
async ({ pagination: { limit, page }, request, response }, next) => {
18-
const statsPeriod = request.query.statsPeriod || TIME_PERIOD.DAY;
14+
middleware.pagination({
15+
defaultLimit: 20,
16+
maxLimit: 50,
17+
maxPage: Infinity,
18+
}),
19+
middleware.timePeriod('statsPeriod', TIME_PERIOD.DAY),
20+
async ({ pagination, params, response }, next) => {
21+
const { limit, page } = pagination;
22+
const { statsPeriod } = params;
1923
const { dateFrom, dateTo } = getDatesForTimePeriod(statsPeriod);
20-
2124
const { relayers, resultCount } =
2225
statsPeriod === TIME_PERIOD.DAY
2326
? await getRelayersWith24HourStats({

src/app/routes/v1/stats.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ const compute24HourNetworkStats = require('../../../stats/compute-24-hour-networ
66
const computeNetworkStatsForDates = require('../../../stats/compute-network-stats-for-dates');
77
const compute24HourTraderStats = require('../../../stats/compute-24-hour-trader-stats');
88
const computeTraderStatsForDates = require('../../../stats/compute-trader-stats-for-dates');
9-
const validatePeriod = require('../../middleware/validate-period');
9+
const middleware = require('../../middleware');
1010

1111
const createRouter = () => {
1212
const router = new Router({ prefix: '/stats' });
1313

1414
router.get(
1515
'/network',
16-
validatePeriod('period'),
17-
async ({ request, response }, next) => {
18-
const period = request.query.period || TIME_PERIOD.DAY;
16+
middleware.timePeriod('period', TIME_PERIOD.DAY),
17+
async ({ params, response }, next) => {
18+
const { period } = params;
1919
const { dateFrom, dateTo } = getDatesForTimePeriod(period);
2020
const stats =
2121
period === TIME_PERIOD.DAY
@@ -30,9 +30,9 @@ const createRouter = () => {
3030

3131
router.get(
3232
'/relayer',
33-
validatePeriod('period'),
34-
async ({ request, response }, next) => {
35-
const period = request.query.period || TIME_PERIOD.DAY;
33+
middleware.timePeriod('period', TIME_PERIOD.DAY),
34+
async ({ params, response }, next) => {
35+
const { period } = params;
3636
const { dateFrom, dateTo } = getDatesForTimePeriod(period);
3737
const stats =
3838
period === TIME_PERIOD.DAY
@@ -50,9 +50,9 @@ const createRouter = () => {
5050

5151
router.get(
5252
'/trader',
53-
validatePeriod('period'),
54-
async ({ request, response }, next) => {
55-
const period = request.query.period || TIME_PERIOD.DAY;
53+
middleware.timePeriod('period', TIME_PERIOD.DAY),
54+
async ({ params, response }, next) => {
55+
const { period } = params;
5656
const { dateFrom, dateTo } = getDatesForTimePeriod(period);
5757
const stats =
5858
period === TIME_PERIOD.DAY

0 commit comments

Comments
 (0)