@ladjs/api 7.1.1 → 8.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +23 -25
- package/package.json +17 -16
package/index.js
CHANGED
|
@@ -19,17 +19,20 @@ const etag = require('koa-etag');
|
|
|
19
19
|
const json = require('koa-json');
|
|
20
20
|
const koa404Handler = require('koa-404-handler');
|
|
21
21
|
const koaConnect = require('koa-connect');
|
|
22
|
+
const ms = require('ms');
|
|
22
23
|
const multimatch = require('multimatch');
|
|
24
|
+
const ratelimit = require('@ladjs/koa-simple-ratelimit');
|
|
23
25
|
const removeTrailingSlashes = require('koa-no-trailing-slash');
|
|
24
26
|
const requestId = require('express-request-id');
|
|
25
27
|
const requestReceived = require('request-received');
|
|
26
28
|
const responseTime = require('response-time');
|
|
27
29
|
const sharedConfig = require('@ladjs/shared-config');
|
|
28
30
|
const { boolean } = require('boolean');
|
|
29
|
-
|
|
31
|
+
|
|
32
|
+
const RATE_LIMIT_EXCEEDED = `Rate limit exceeded, retry in %s.`;
|
|
30
33
|
|
|
31
34
|
class API {
|
|
32
|
-
constructor(config) {
|
|
35
|
+
constructor(config, client) {
|
|
33
36
|
this.config = {
|
|
34
37
|
...sharedConfig('API'),
|
|
35
38
|
rateLimitIgnoredGlobs: [],
|
|
@@ -41,9 +44,17 @@ class API {
|
|
|
41
44
|
...this.config.cabin
|
|
42
45
|
});
|
|
43
46
|
|
|
47
|
+
// initialize redis
|
|
48
|
+
this.client = client
|
|
49
|
+
? client
|
|
50
|
+
: new Redis(this.config.redis, cabin, this.config.redisMonitor);
|
|
51
|
+
|
|
44
52
|
// initialize the app
|
|
45
53
|
const app = new Koa();
|
|
46
54
|
|
|
55
|
+
// allow middleware to access redis client
|
|
56
|
+
app.context.client = this.client;
|
|
57
|
+
|
|
47
58
|
// listen for error and log events emitted by app
|
|
48
59
|
app.on('error', (err, ctx) => {
|
|
49
60
|
const level = err.status && err.status < 500 ? 'warn' : 'error';
|
|
@@ -52,16 +63,6 @@ class API {
|
|
|
52
63
|
});
|
|
53
64
|
app.on('log', cabin.log);
|
|
54
65
|
|
|
55
|
-
// initialize redis
|
|
56
|
-
const client = new Redis(
|
|
57
|
-
this.config.redis,
|
|
58
|
-
cabin,
|
|
59
|
-
this.config.redisMonitor
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
// allow middleware to access redis client
|
|
63
|
-
app.context.client = client;
|
|
64
|
-
|
|
65
66
|
// override koa's undocumented error handler
|
|
66
67
|
app.context.onerror = errorHandler(false, cabin);
|
|
67
68
|
|
|
@@ -111,19 +112,18 @@ class API {
|
|
|
111
112
|
|
|
112
113
|
return ratelimit({
|
|
113
114
|
...this.config.rateLimit,
|
|
114
|
-
db: client
|
|
115
|
+
db: this.client,
|
|
116
|
+
logger: cabin,
|
|
117
|
+
errorMessage(exp) {
|
|
118
|
+
const fn =
|
|
119
|
+
typeof ctx.request.t === 'function' ? ctx.request.t : util.format;
|
|
120
|
+
// NOTE: ms does not support i18n localization
|
|
121
|
+
return fn(RATE_LIMIT_EXCEEDED, ms(exp, { long: true }));
|
|
122
|
+
}
|
|
115
123
|
})(ctx, next);
|
|
116
124
|
});
|
|
117
125
|
}
|
|
118
126
|
|
|
119
|
-
if (this.config.rateLimit)
|
|
120
|
-
app.use(
|
|
121
|
-
ratelimit({
|
|
122
|
-
...this.config.rateLimit,
|
|
123
|
-
db: client
|
|
124
|
-
})
|
|
125
|
-
);
|
|
126
|
-
|
|
127
127
|
// remove trailing slashes
|
|
128
128
|
app.use(removeTrailingSlashes());
|
|
129
129
|
|
|
@@ -177,15 +177,13 @@ class API {
|
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
// start server on either http or https
|
|
180
|
-
|
|
180
|
+
this.server =
|
|
181
181
|
this.config.protocol === 'https'
|
|
182
182
|
? https.createServer(this.config.ssl, app.callback())
|
|
183
183
|
: http.createServer(app.callback());
|
|
184
184
|
|
|
185
|
-
// expose app
|
|
185
|
+
// expose the app
|
|
186
186
|
this.app = app;
|
|
187
|
-
this.server = server;
|
|
188
|
-
this.client = client;
|
|
189
187
|
|
|
190
188
|
// bind listen/close to this
|
|
191
189
|
this.listen = this.listen.bind(this);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ladjs/api",
|
|
3
3
|
"description": "API server for Lad",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "8.0.1",
|
|
5
5
|
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
|
|
6
6
|
"ava": {
|
|
7
7
|
"failFast": true,
|
|
@@ -21,13 +21,14 @@
|
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@koa/router": "^10.1.1",
|
|
24
|
-
"@ladjs/i18n": "^7.2.
|
|
24
|
+
"@ladjs/i18n": "^7.2.6",
|
|
25
|
+
"@ladjs/koa-simple-ratelimit": "^3.0.0",
|
|
25
26
|
"@ladjs/redis": "^1.0.7",
|
|
26
|
-
"@ladjs/shared-config": "^6.0.
|
|
27
|
+
"@ladjs/shared-config": "^6.0.2",
|
|
27
28
|
"@ladjs/store-ip-address": "^0.0.7",
|
|
28
|
-
"boolean": "^3.
|
|
29
|
-
"cabin": "^9.1.
|
|
30
|
-
"express-request-id": "
|
|
29
|
+
"boolean": "^3.2.0",
|
|
30
|
+
"cabin": "^9.1.2",
|
|
31
|
+
"express-request-id": "1.4.1",
|
|
31
32
|
"kcors": "^2.2.2",
|
|
32
33
|
"koa": "^2.13.4",
|
|
33
34
|
"koa-404-handler": "^0.1.0",
|
|
@@ -41,29 +42,29 @@
|
|
|
41
42
|
"koa-etag": "^4.0.0",
|
|
42
43
|
"koa-json": "^2.0.2",
|
|
43
44
|
"koa-no-trailing-slash": "^2.1.0",
|
|
44
|
-
"koa-simple-ratelimit": "^5.1.1",
|
|
45
45
|
"lodash": "^4.17.21",
|
|
46
|
+
"ms": "^2.1.3",
|
|
46
47
|
"multimatch": "5",
|
|
47
48
|
"request-received": "^0.0.3",
|
|
48
49
|
"response-time": "^2.3.2"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
51
|
-
"@commitlint/cli": "^
|
|
52
|
-
"@commitlint/config-conventional": "^
|
|
53
|
-
"ava": "^4.0
|
|
52
|
+
"@commitlint/cli": "^17.0.1",
|
|
53
|
+
"@commitlint/config-conventional": "^17.0.0",
|
|
54
|
+
"ava": "^4.2.0",
|
|
54
55
|
"codecov": "^3.8.3",
|
|
55
56
|
"cross-env": "^7.0.3",
|
|
56
|
-
"eslint": "^8.
|
|
57
|
+
"eslint": "^8.16.0",
|
|
57
58
|
"eslint-config-xo-lass": "^1.0.6",
|
|
58
59
|
"fixpack": "^4.0.0",
|
|
59
|
-
"husky": "^
|
|
60
|
-
"lint-staged": "12.
|
|
61
|
-
"mongoose": "^6.
|
|
60
|
+
"husky": "^8.0.1",
|
|
61
|
+
"lint-staged": "12.4.2",
|
|
62
|
+
"mongoose": "^6.3.4",
|
|
62
63
|
"nyc": "^15.1.0",
|
|
63
64
|
"remark-cli": "^10.0.1",
|
|
64
65
|
"remark-preset-github": "^4.0.1",
|
|
65
|
-
"supertest": "^6.2.
|
|
66
|
-
"xo": "^0.
|
|
66
|
+
"supertest": "^6.2.3",
|
|
67
|
+
"xo": "^0.49.0"
|
|
67
68
|
},
|
|
68
69
|
"engines": {
|
|
69
70
|
"node": ">=10.10.0"
|