@ladjs/api 7.1.0 → 8.0.0

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 +12 -24
  2. package/package.json +13 -13
package/index.js CHANGED
@@ -29,7 +29,7 @@ const { boolean } = require('boolean');
29
29
  const { ratelimit } = require('koa-simple-ratelimit');
30
30
 
31
31
  class API {
32
- constructor(config) {
32
+ constructor(config, client) {
33
33
  this.config = {
34
34
  ...sharedConfig('API'),
35
35
  rateLimitIgnoredGlobs: [],
@@ -41,9 +41,17 @@ class API {
41
41
  ...this.config.cabin
42
42
  });
43
43
 
44
+ // initialize redis
45
+ this.client = client
46
+ ? client
47
+ : new Redis(this.config.redis, cabin, this.config.redisMonitor);
48
+
44
49
  // initialize the app
45
50
  const app = new Koa();
46
51
 
52
+ // allow middleware to access redis client
53
+ app.context.client = this.client;
54
+
47
55
  // listen for error and log events emitted by app
48
56
  app.on('error', (err, ctx) => {
49
57
  const level = err.status && err.status < 500 ? 'warn' : 'error';
@@ -52,16 +60,6 @@ class API {
52
60
  });
53
61
  app.on('log', cabin.log);
54
62
 
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
63
  // override koa's undocumented error handler
66
64
  app.context.onerror = errorHandler(false, cabin);
67
65
 
@@ -111,19 +109,11 @@ class API {
111
109
 
112
110
  return ratelimit({
113
111
  ...this.config.rateLimit,
114
- db: client
112
+ db: this.client
115
113
  })(ctx, next);
116
114
  });
117
115
  }
118
116
 
119
- if (this.config.rateLimit)
120
- app.use(
121
- ratelimit({
122
- ...this.config.rateLimit,
123
- db: client
124
- })
125
- );
126
-
127
117
  // remove trailing slashes
128
118
  app.use(removeTrailingSlashes());
129
119
 
@@ -177,15 +167,13 @@ class API {
177
167
  }
178
168
 
179
169
  // start server on either http or https
180
- const server =
170
+ this.server =
181
171
  this.config.protocol === 'https'
182
172
  ? https.createServer(this.config.ssl, app.callback())
183
173
  : http.createServer(app.callback());
184
174
 
185
- // expose app, server, client
175
+ // expose the app
186
176
  this.app = app;
187
- this.server = server;
188
- this.client = client;
189
177
 
190
178
  // bind listen/close to this
191
179
  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.0",
4
+ "version": "8.0.0",
5
5
  "author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
6
6
  "ava": {
7
7
  "failFast": true,
@@ -21,13 +21,13 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "@koa/router": "^10.1.1",
24
- "@ladjs/i18n": "^7.2.4",
24
+ "@ladjs/i18n": "^7.2.6",
25
25
  "@ladjs/redis": "^1.0.7",
26
26
  "@ladjs/shared-config": "^6.0.0",
27
27
  "@ladjs/store-ip-address": "^0.0.7",
28
- "boolean": "^3.1.4",
29
- "cabin": "^9.1.0",
30
- "express-request-id": "^1.4.1",
28
+ "boolean": "^3.2.0",
29
+ "cabin": "^9.1.2",
30
+ "express-request-id": "1.4.1",
31
31
  "kcors": "^2.2.2",
32
32
  "koa": "^2.13.4",
33
33
  "koa-404-handler": "^0.1.0",
@@ -48,22 +48,22 @@
48
48
  "response-time": "^2.3.2"
49
49
  },
50
50
  "devDependencies": {
51
- "@commitlint/cli": "^16.0.2",
52
- "@commitlint/config-conventional": "^16.0.0",
53
- "ava": "^4.0.1",
51
+ "@commitlint/cli": "^16.2.4",
52
+ "@commitlint/config-conventional": "^16.2.4",
53
+ "ava": "^4.2.0",
54
54
  "codecov": "^3.8.3",
55
55
  "cross-env": "^7.0.3",
56
- "eslint": "^8.6.0",
56
+ "eslint": "^8.14.0",
57
57
  "eslint-config-xo-lass": "^1.0.6",
58
58
  "fixpack": "^4.0.0",
59
59
  "husky": "^7.0.4",
60
- "lint-staged": "12.1.7",
61
- "mongoose": "^6.1.6",
60
+ "lint-staged": "12.4.1",
61
+ "mongoose": "^6.3.2",
62
62
  "nyc": "^15.1.0",
63
63
  "remark-cli": "^10.0.1",
64
64
  "remark-preset-github": "^4.0.1",
65
- "supertest": "^6.2.1",
66
- "xo": "^0.47.0"
65
+ "supertest": "^6.2.3",
66
+ "xo": "^0.48.0"
67
67
  },
68
68
  "engines": {
69
69
  "node": ">=10.10.0"