@ladjs/api 8.0.2 → 9.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.
- package/index.js +44 -30
- package/package.json +10 -9
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const util = require('util');
|
|
|
5
5
|
|
|
6
6
|
const Cabin = require('cabin');
|
|
7
7
|
const I18N = require('@ladjs/i18n');
|
|
8
|
+
const Passport = require('@ladjs/passport');
|
|
8
9
|
const Koa = require('koa');
|
|
9
10
|
const Redis = require('@ladjs/redis');
|
|
10
11
|
const StoreIPAddress = require('@ladjs/store-ip-address');
|
|
@@ -32,45 +33,58 @@ const { boolean } = require('boolean');
|
|
|
32
33
|
const RATE_LIMIT_EXCEEDED = `Rate limit exceeded, retry in %s.`;
|
|
33
34
|
|
|
34
35
|
class API {
|
|
35
|
-
|
|
36
|
+
// eslint-disable-next-line complexity
|
|
37
|
+
constructor(config, Users) {
|
|
36
38
|
this.config = {
|
|
37
39
|
...sharedConfig('API'),
|
|
38
40
|
rateLimitIgnoredGlobs: [],
|
|
39
41
|
...config
|
|
40
42
|
};
|
|
41
43
|
|
|
42
|
-
const cabin = new Cabin({
|
|
43
|
-
logger: this.config.logger,
|
|
44
|
-
...this.config.cabin
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// initialize redis
|
|
48
|
-
this.client = client
|
|
49
|
-
? client
|
|
50
|
-
: new Redis(this.config.redis, cabin, this.config.redisMonitor);
|
|
51
|
-
|
|
52
44
|
// initialize the app
|
|
53
45
|
const app = new Koa();
|
|
54
46
|
|
|
55
|
-
//
|
|
47
|
+
// only trust proxy if enabled
|
|
48
|
+
app.proxy = boolean(process.env.TRUST_PROXY);
|
|
49
|
+
|
|
50
|
+
// specify that this is our api (used by error handler)
|
|
51
|
+
app.context.api = true;
|
|
52
|
+
|
|
53
|
+
// initialize cabin
|
|
54
|
+
this.logger = _.isPlainObject(this.config.logger)
|
|
55
|
+
? new Cabin(this.config.logger)
|
|
56
|
+
: this.config.logger instanceof Cabin
|
|
57
|
+
? this.config.logger
|
|
58
|
+
: new Cabin({
|
|
59
|
+
logger: this.config.logger ? this.config.logger : console
|
|
60
|
+
});
|
|
61
|
+
app.context.logger = this.logger;
|
|
62
|
+
|
|
63
|
+
// initialize redis
|
|
64
|
+
this.client =
|
|
65
|
+
this.config.redis === false
|
|
66
|
+
? false
|
|
67
|
+
: _.isPlainObject(this.config.redis)
|
|
68
|
+
? new Redis(this.config.redis, this.logger, this.config.redisMonitor)
|
|
69
|
+
: this.config.redis;
|
|
56
70
|
app.context.client = this.client;
|
|
57
71
|
|
|
58
|
-
//
|
|
72
|
+
// expose passport
|
|
73
|
+
this.passport =
|
|
74
|
+
this.config.passport === false
|
|
75
|
+
? false
|
|
76
|
+
: _.isPlainObject(this.config.passport)
|
|
77
|
+
? new Passport(this.config.passport, Users)
|
|
78
|
+
: this.config.passport;
|
|
79
|
+
app.context.passport = this.passport;
|
|
80
|
+
|
|
81
|
+
// listen for errors emitted by app
|
|
59
82
|
app.on('error', (err, ctx) => {
|
|
60
|
-
|
|
61
|
-
if (ctx.logger) ctx.logger[level](err);
|
|
62
|
-
else cabin[level](err);
|
|
83
|
+
ctx.logger[err.status && err.status < 500 ? 'warn' : 'error'](err);
|
|
63
84
|
});
|
|
64
|
-
app.on('log', cabin.log);
|
|
65
85
|
|
|
66
86
|
// override koa's undocumented error handler
|
|
67
|
-
app.context.onerror = errorHandler(false
|
|
68
|
-
|
|
69
|
-
// only trust proxy if enabled
|
|
70
|
-
app.proxy = boolean(process.env.TRUST_PROXY);
|
|
71
|
-
|
|
72
|
-
// specify that this is our api (used by error handler)
|
|
73
|
-
app.context.api = true;
|
|
87
|
+
app.context.onerror = errorHandler(false);
|
|
74
88
|
|
|
75
89
|
// adds request received hrtime and date symbols to request object
|
|
76
90
|
// (which is used by Cabin internally to add `request.timestamp` to logs
|
|
@@ -89,7 +103,7 @@ class API {
|
|
|
89
103
|
app.use(koaConnect(requestId()));
|
|
90
104
|
|
|
91
105
|
// use the cabin middleware (adds request-based logging and helpers)
|
|
92
|
-
app.use(
|
|
106
|
+
app.use(this.logger.middleware);
|
|
93
107
|
|
|
94
108
|
// allow before hooks to get setup
|
|
95
109
|
if (_.isFunction(this.config.hookBeforeSetup))
|
|
@@ -99,7 +113,7 @@ class API {
|
|
|
99
113
|
if (this.config.auth) app.use(auth(this.config.auth));
|
|
100
114
|
|
|
101
115
|
// rate limiting
|
|
102
|
-
if (this.config.rateLimit) {
|
|
116
|
+
if (this.client && this.config.rateLimit) {
|
|
103
117
|
app.use((ctx, next) => {
|
|
104
118
|
// check against ignored/whitelisted paths
|
|
105
119
|
if (
|
|
@@ -113,7 +127,7 @@ class API {
|
|
|
113
127
|
return ratelimit({
|
|
114
128
|
...this.config.rateLimit,
|
|
115
129
|
db: this.client,
|
|
116
|
-
logger:
|
|
130
|
+
logger: this.logger,
|
|
117
131
|
errorMessage(exp) {
|
|
118
132
|
const fn =
|
|
119
133
|
typeof ctx.request.t === 'function' ? ctx.request.t : util.format;
|
|
@@ -131,7 +145,7 @@ class API {
|
|
|
131
145
|
if (this.config.i18n) {
|
|
132
146
|
const i18n = this.config.i18n.config
|
|
133
147
|
? this.config.i18n
|
|
134
|
-
: new I18N({ ...this.config.i18n, logger:
|
|
148
|
+
: new I18N({ ...this.config.i18n, logger: this.logger });
|
|
135
149
|
app.use(i18n.middleware);
|
|
136
150
|
}
|
|
137
151
|
|
|
@@ -151,12 +165,12 @@ class API {
|
|
|
151
165
|
app.use(json());
|
|
152
166
|
|
|
153
167
|
// passport
|
|
154
|
-
if (this.
|
|
168
|
+
if (this.passport) app.use(this.passport.initialize());
|
|
155
169
|
|
|
156
170
|
// store the user's last ip address in the background
|
|
157
171
|
if (this.config.storeIPAddress) {
|
|
158
172
|
const storeIPAddress = new StoreIPAddress({
|
|
159
|
-
logger:
|
|
173
|
+
logger: this.logger,
|
|
160
174
|
...this.config.storeIPAddress
|
|
161
175
|
});
|
|
162
176
|
app.use(storeIPAddress.middleware);
|
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": "9.0.2",
|
|
5
5
|
"author": "Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)",
|
|
6
6
|
"ava": {
|
|
7
7
|
"failFast": true,
|
|
@@ -23,8 +23,9 @@
|
|
|
23
23
|
"@koa/router": "^10.1.1",
|
|
24
24
|
"@ladjs/i18n": "^7.2.6",
|
|
25
25
|
"@ladjs/koa-simple-ratelimit": "^3.0.0",
|
|
26
|
+
"@ladjs/passport": "^5.0.0",
|
|
26
27
|
"@ladjs/redis": "^1.0.7",
|
|
27
|
-
"@ladjs/shared-config": "^7.0.
|
|
28
|
+
"@ladjs/shared-config": "^7.0.3",
|
|
28
29
|
"@ladjs/store-ip-address": "^0.0.7",
|
|
29
30
|
"boolean": "^3.2.0",
|
|
30
31
|
"cabin": "^9.1.2",
|
|
@@ -33,7 +34,7 @@
|
|
|
33
34
|
"koa": "^2.13.4",
|
|
34
35
|
"koa-404-handler": "^0.1.0",
|
|
35
36
|
"koa-basic-auth": "^4.0.0",
|
|
36
|
-
"koa-better-error-handler": "^
|
|
37
|
+
"koa-better-error-handler": "^8.0.1",
|
|
37
38
|
"koa-better-timeout": "^0.0.6",
|
|
38
39
|
"koa-bodyparser": "^4.3.0",
|
|
39
40
|
"koa-compress": "^5.1.0",
|
|
@@ -49,20 +50,20 @@
|
|
|
49
50
|
"response-time": "^2.3.2"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
|
-
"@commitlint/cli": "^17.0.
|
|
53
|
-
"@commitlint/config-conventional": "^17.0.
|
|
54
|
-
"ava": "^4.
|
|
53
|
+
"@commitlint/cli": "^17.0.2",
|
|
54
|
+
"@commitlint/config-conventional": "^17.0.2",
|
|
55
|
+
"ava": "^4.3.0",
|
|
55
56
|
"codecov": "^3.8.3",
|
|
56
57
|
"cross-env": "^7.0.3",
|
|
57
58
|
"eslint": "^8.16.0",
|
|
58
59
|
"eslint-config-xo-lass": "^1.0.6",
|
|
59
60
|
"fixpack": "^4.0.0",
|
|
60
61
|
"husky": "^8.0.1",
|
|
61
|
-
"lint-staged": "
|
|
62
|
-
"mongoose": "^6.3.
|
|
62
|
+
"lint-staged": "13.0.0",
|
|
63
|
+
"mongoose": "^6.3.5",
|
|
63
64
|
"nyc": "^15.1.0",
|
|
64
65
|
"remark-cli": "^10.0.1",
|
|
65
|
-
"remark-preset-github": "^4.0.
|
|
66
|
+
"remark-preset-github": "^4.0.2",
|
|
66
67
|
"supertest": "^6.2.3",
|
|
67
68
|
"xo": "^0.49.0"
|
|
68
69
|
},
|