@ladjs/api 10.0.6 → 11.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.
- package/index.js +10 -39
- package/package.json +4 -6
package/index.js
CHANGED
|
@@ -20,8 +20,6 @@ const etag = require('koa-etag');
|
|
|
20
20
|
const json = require('koa-json');
|
|
21
21
|
const koa404Handler = require('koa-404-handler');
|
|
22
22
|
const koaConnect = require('koa-connect');
|
|
23
|
-
const ms = require('ms');
|
|
24
|
-
const multimatch = require('multimatch');
|
|
25
23
|
const ratelimit = require('@ladjs/koa-simple-ratelimit');
|
|
26
24
|
const removeTrailingSlashes = require('koa-no-trailing-slash');
|
|
27
25
|
const requestId = require('express-request-id');
|
|
@@ -30,14 +28,11 @@ const responseTime = require('response-time');
|
|
|
30
28
|
const sharedConfig = require('@ladjs/shared-config');
|
|
31
29
|
const { boolean } = require('boolean');
|
|
32
30
|
|
|
33
|
-
const RATE_LIMIT_EXCEEDED = 'Rate limit exceeded, retry in %s.';
|
|
34
|
-
|
|
35
31
|
class API {
|
|
36
32
|
// eslint-disable-next-line complexity
|
|
37
33
|
constructor(config, Users) {
|
|
38
34
|
this.config = {
|
|
39
35
|
...sharedConfig('API'),
|
|
40
|
-
rateLimitIgnoredGlobs: [],
|
|
41
36
|
...config
|
|
42
37
|
};
|
|
43
38
|
|
|
@@ -106,42 +101,21 @@ class API {
|
|
|
106
101
|
app.use(this.logger.middleware);
|
|
107
102
|
|
|
108
103
|
// Allow before hooks to get setup
|
|
109
|
-
if (_.isFunction(this.config.hookBeforeSetup))
|
|
104
|
+
if (_.isFunction(this.config.hookBeforeSetup))
|
|
110
105
|
this.config.hookBeforeSetup(app);
|
|
111
|
-
}
|
|
112
106
|
|
|
113
107
|
// Basic auth
|
|
114
|
-
if (this.config.auth)
|
|
115
|
-
app.use(auth(this.config.auth));
|
|
116
|
-
}
|
|
108
|
+
if (this.config.auth) app.use(auth(this.config.auth));
|
|
117
109
|
|
|
118
110
|
// Rate limiting
|
|
119
|
-
if (this.client && this.config.rateLimit)
|
|
120
|
-
app.use(
|
|
121
|
-
|
|
122
|
-
if (
|
|
123
|
-
Array.isArray(this.config.rateLimitIgnoredGlobs) &&
|
|
124
|
-
this.config.rateLimitIgnoredGlobs.length > 0
|
|
125
|
-
) {
|
|
126
|
-
const match = multimatch(ctx.path, this.config.rateLimitIgnoredGlobs);
|
|
127
|
-
if (Array.isArray(match) && match.length > 0) {
|
|
128
|
-
return next();
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return ratelimit({
|
|
111
|
+
if (this.client && this.config.rateLimit)
|
|
112
|
+
app.use(
|
|
113
|
+
ratelimit({
|
|
133
114
|
...this.config.rateLimit,
|
|
134
115
|
db: this.client,
|
|
135
|
-
logger: this.logger
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
typeof ctx.request.t === 'function' ? ctx.request.t : util.format;
|
|
139
|
-
// NOTE: ms does not support i18n localization
|
|
140
|
-
return fn(RATE_LIMIT_EXCEEDED, ms(exp, { long: true }));
|
|
141
|
-
}
|
|
142
|
-
})(ctx, next);
|
|
143
|
-
});
|
|
144
|
-
}
|
|
116
|
+
logger: this.logger
|
|
117
|
+
})
|
|
118
|
+
);
|
|
145
119
|
|
|
146
120
|
// Remove trailing slashes
|
|
147
121
|
app.use(removeTrailingSlashes());
|
|
@@ -172,9 +146,7 @@ class API {
|
|
|
172
146
|
app.use(json());
|
|
173
147
|
|
|
174
148
|
// Passport
|
|
175
|
-
if (this.passport)
|
|
176
|
-
app.use(this.passport.initialize());
|
|
177
|
-
}
|
|
149
|
+
if (this.passport) app.use(this.passport.initialize());
|
|
178
150
|
|
|
179
151
|
// Store the user's last ip address in the background
|
|
180
152
|
if (this.config.storeIPAddress) {
|
|
@@ -189,9 +161,8 @@ class API {
|
|
|
189
161
|
app.use(koa404Handler);
|
|
190
162
|
|
|
191
163
|
// Allow before hooks to get setup
|
|
192
|
-
if (_.isFunction(this.config.hookBeforeRoutes))
|
|
164
|
+
if (_.isFunction(this.config.hookBeforeRoutes))
|
|
193
165
|
this.config.hookBeforeRoutes(app);
|
|
194
|
-
}
|
|
195
166
|
|
|
196
167
|
// Mount the app's defined and nested routes
|
|
197
168
|
if (this.config.routes) {
|
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": "11.0.0",
|
|
5
5
|
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/ladjs/api/issues",
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@koa/router": "^10.1.1",
|
|
15
15
|
"@ladjs/i18n": "^7.2.6",
|
|
16
|
-
"@ladjs/koa-simple-ratelimit": "^
|
|
16
|
+
"@ladjs/koa-simple-ratelimit": "^4.0.1",
|
|
17
17
|
"@ladjs/passport": "^5.0.2",
|
|
18
18
|
"@ladjs/redis": "^1.0.7",
|
|
19
|
-
"@ladjs/shared-config": "^
|
|
19
|
+
"@ladjs/shared-config": "^8.0.0",
|
|
20
20
|
"@ladjs/store-ip-address": "^0.0.7",
|
|
21
21
|
"boolean": "^3.2.0",
|
|
22
22
|
"cabin": "^9.1.2",
|
|
@@ -35,8 +35,6 @@
|
|
|
35
35
|
"koa-json": "^2.0.2",
|
|
36
36
|
"koa-no-trailing-slash": "^2.1.0",
|
|
37
37
|
"lodash": "^4.17.21",
|
|
38
|
-
"ms": "^2.1.3",
|
|
39
|
-
"multimatch": "5",
|
|
40
38
|
"request-received": "^0.0.3",
|
|
41
39
|
"response-time": "^2.3.2"
|
|
42
40
|
},
|
|
@@ -45,7 +43,7 @@
|
|
|
45
43
|
"@commitlint/config-conventional": "^17.0.3",
|
|
46
44
|
"ava": "^4.3.0",
|
|
47
45
|
"cross-env": "^7.0.3",
|
|
48
|
-
"eslint": "^8.
|
|
46
|
+
"eslint": "^8.19.0",
|
|
49
47
|
"eslint-config-xo-lass": "^2.0.1",
|
|
50
48
|
"fixpack": "^4.0.0",
|
|
51
49
|
"husky": "^8.0.1",
|