@ladjs/api 7.1.2 → 8.0.2

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.
Files changed (2) hide show
  1. package/index.js +23 -17
  2. package/package.json +11 -10
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
- const { ratelimit } = require('koa-simple-ratelimit');
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,7 +112,14 @@ 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
  }
@@ -169,15 +177,13 @@ class API {
169
177
  }
170
178
 
171
179
  // start server on either http or https
172
- const server =
180
+ this.server =
173
181
  this.config.protocol === 'https'
174
182
  ? https.createServer(this.config.ssl, app.callback())
175
183
  : http.createServer(app.callback());
176
184
 
177
- // expose app, server, client
185
+ // expose the app
178
186
  this.app = app;
179
- this.server = server;
180
- this.client = client;
181
187
 
182
188
  // bind listen/close to this
183
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": "7.1.2",
4
+ "version": "8.0.2",
5
5
  "author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
6
6
  "ava": {
7
7
  "failFast": true,
@@ -22,8 +22,9 @@
22
22
  "dependencies": {
23
23
  "@koa/router": "^10.1.1",
24
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.0",
27
+ "@ladjs/shared-config": "^7.0.0",
27
28
  "@ladjs/store-ip-address": "^0.0.7",
28
29
  "boolean": "^3.2.0",
29
30
  "cabin": "^9.1.2",
@@ -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": "^16.2.4",
52
- "@commitlint/config-conventional": "^16.2.4",
52
+ "@commitlint/cli": "^17.0.1",
53
+ "@commitlint/config-conventional": "^17.0.0",
53
54
  "ava": "^4.2.0",
54
55
  "codecov": "^3.8.3",
55
56
  "cross-env": "^7.0.3",
56
- "eslint": "^8.14.0",
57
+ "eslint": "^8.16.0",
57
58
  "eslint-config-xo-lass": "^1.0.6",
58
59
  "fixpack": "^4.0.0",
59
- "husky": "^7.0.4",
60
- "lint-staged": "12.4.1",
61
- "mongoose": "^6.3.2",
60
+ "husky": "^8.0.1",
61
+ "lint-staged": "12.4.3",
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
66
  "supertest": "^6.2.3",
66
- "xo": "^0.48.0"
67
+ "xo": "^0.49.0"
67
68
  },
68
69
  "engines": {
69
70
  "node": ">=10.10.0"