Skip to content

Commit d360f7b

Browse files
committed
fix: bump deps, fixed middleware order
1 parent 466313c commit d360f7b

File tree

3 files changed

+533
-673
lines changed

3 files changed

+533
-673
lines changed

index.js

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,8 @@ class API {
3838
...config
3939
};
4040

41-
const { logger } = this.config;
42-
43-
let storeIPAddress = false;
44-
45-
if (this.config.storeIPAddress)
46-
storeIPAddress = new StoreIPAddress({
47-
logger,
48-
...this.config.storeIPAddress
49-
});
50-
5141
const cabin = new Cabin({
52-
logger,
42+
logger: this.config.logger,
5343
...this.config.cabin
5444
});
5545

@@ -58,14 +48,16 @@ class API {
5848

5949
// listen for error and log events emitted by app
6050
app.on('error', (err, ctx) => {
61-
ctx.logger[err.status && err.status < 500 ? 'warn' : 'error'](err);
51+
const level = err.status && err.status < 500 ? 'warn' : 'error';
52+
if (ctx.logger) ctx.logger[level](err);
53+
else cabin[level](err);
6254
});
63-
app.on('log', logger.log);
55+
app.on('log', cabin.log);
6456

6557
// initialize redis
6658
const client = new Redis(
6759
this.config.redis,
68-
logger,
60+
cabin,
6961
this.config.redisMonitor
7062
);
7163

@@ -74,19 +66,40 @@ class API {
7466
// later on with `server.close()`
7567
let server;
7668

77-
// override koa's undocumented error handler
78-
// <https://github.com/sindresorhus/eslint-plugin-unicorn/issues/174>
79-
app.context.onerror = errorHandler;
80-
8169
// allow middleware to access redis client
8270
app.context.client = client;
8371

72+
// override koa's undocumented error handler
73+
app.context.onerror = errorHandler(false, cabin);
74+
8475
// only trust proxy if enabled
8576
app.proxy = boolean(process.env.TRUST_PROXY);
8677

8778
// specify that this is our api (used by error handler)
8879
app.context.api = true;
8980

81+
// allow before hooks to get setup
82+
if (_.isFunction(this.config.hookBeforeSetup))
83+
this.config.hookBeforeSetup(app);
84+
85+
// rate limiting
86+
if (this.config.rateLimit)
87+
app.use(
88+
ratelimit({
89+
...this.config.rateLimit,
90+
db: client
91+
})
92+
);
93+
94+
// basic auth
95+
if (this.config.auth) app.use(auth(this.config.auth));
96+
97+
// configure timeout
98+
if (this.config.timeout) {
99+
const timeout = new Timeout(this.config.timeout);
100+
app.use(timeout.middleware);
101+
}
102+
90103
// adds request received hrtime and date symbols to request object
91104
// (which is used by Cabin internally to add `request.timestamp` to logs
92105
app.use(requestReceived);
@@ -104,21 +117,10 @@ class API {
104117
if (this.config.i18n) {
105118
const i18n = this.config.i18n.config
106119
? this.config.i18n
107-
: new I18N({ ...this.config.i18n, logger });
120+
: new I18N({ ...this.config.i18n, logger: cabin });
108121
app.use(i18n.middleware);
109122
}
110123

111-
if (this.config.auth) app.use(auth(this.config.auth));
112-
113-
// rate limiting
114-
if (this.config.rateLimit)
115-
app.use(
116-
ratelimit({
117-
...this.config.rateLimit,
118-
db: client
119-
})
120-
);
121-
122124
// conditional-get
123125
app.use(conditional());
124126

@@ -137,20 +139,20 @@ class API {
137139
// pretty-printed json responses
138140
app.use(json());
139141

140-
// 404 handler
141-
app.use(koa404Handler);
142-
143142
// passport
144143
if (this.config.passport) app.use(this.config.passport.initialize());
145144

146-
// configure timeout
147-
if (this.config.timeout) {
148-
const timeout = new Timeout(this.config.timeout);
149-
app.use(timeout.middleware);
145+
// store the user's last ip address in the background
146+
if (this.config.storeIPAddress) {
147+
const storeIPAddress = new StoreIPAddress({
148+
logger: cabin,
149+
...this.config.storeIPAddress
150+
});
151+
app.use(storeIPAddress.middleware);
150152
}
151153

152-
// store the user's last ip address in the background
153-
if (storeIPAddress) app.use(storeIPAddress.middleware);
154+
// 404 handler
155+
app.use(koa404Handler);
154156

155157
// allow before hooks to get setup
156158
if (_.isFunction(this.config.hookBeforeRoutes))

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
2121
],
2222
"dependencies": {
23-
"@koa/router": "^9.3.1",
24-
"@ladjs/i18n": "^4.0.0",
23+
"@koa/router": "^9.4.0",
24+
"@ladjs/i18n": "^5.0.0",
2525
"@ladjs/redis": "^1.0.7",
2626
"@ladjs/shared-config": "^3.0.9",
2727
"@ladjs/store-ip-address": "^0.0.7",
2828
"boolean": "3.0.1",
29-
"cabin": "^8.0.4",
29+
"cabin": "^8.0.7",
3030
"express-request-id": "^1.4.1",
3131
"findhit-proxywrap": "^0.3.12",
3232
"kcors": "^2.2.2",
3333
"koa": "^2.13.0",
3434
"koa-404-handler": "^0.0.2",
3535
"koa-basic-auth": "^4.0.0",
36-
"koa-better-error-handler": "^5.0.0",
36+
"koa-better-error-handler": "^6.0.1",
3737
"koa-better-timeout": "^0.0.6",
3838
"koa-bodyparser": "^4.3.0",
3939
"koa-compress": "^5.0.1",
@@ -43,27 +43,27 @@
4343
"koa-json": "^2.0.2",
4444
"koa-no-trailing-slash": "^2.1.0",
4545
"koa-simple-ratelimit": "^5.0.0",
46-
"lodash": "^4.17.19",
46+
"lodash": "^4.17.20",
4747
"request-received": "^0.0.3",
4848
"response-time": "^2.3.2"
4949
},
5050
"devDependencies": {
51-
"@commitlint/cli": "^9.1.1",
52-
"@commitlint/config-conventional": "^9.1.1",
53-
"ava": "^3.10.1",
51+
"@commitlint/cli": "^9.1.2",
52+
"@commitlint/config-conventional": "^9.1.2",
53+
"ava": "^3.12.1",
5454
"codecov": "^3.7.2",
5555
"cross-env": "^7.0.2",
56-
"eslint": "^7.5.0",
56+
"eslint": "^7.7.0",
5757
"eslint-config-xo-lass": "^1.0.3",
5858
"fixpack": "^3.0.6",
5959
"husky": "^4.2.5",
60-
"lint-staged": "10.2.11",
61-
"mongoose": "^5.9.25",
60+
"lint-staged": "10.2.13",
61+
"mongoose": "^5.10.2",
6262
"nyc": "^15.1.0",
6363
"remark-cli": "^8.0.1",
64-
"remark-preset-github": "^2.0.2",
64+
"remark-preset-github": "^3.0.0",
6565
"supertest": "^4.0.2",
66-
"xo": "^0.32.1"
66+
"xo": "^0.33.0"
6767
},
6868
"engines": {
6969
"node": ">=8.3"

0 commit comments

Comments
 (0)