@@ -5,6 +5,7 @@ const util = require('util');
55
66const Cabin = require ( 'cabin' ) ;
77const I18N = require ( '@ladjs/i18n' ) ;
8+ const Passport = require ( '@ladjs/passport' ) ;
89const Koa = require ( 'koa' ) ;
910const Redis = require ( '@ladjs/redis' ) ;
1011const StoreIPAddress = require ( '@ladjs/store-ip-address' ) ;
@@ -32,39 +33,52 @@ const { boolean } = require('boolean');
3233const RATE_LIMIT_EXCEEDED = `Rate limit exceeded, retry in %s.` ;
3334
3435class API {
35- constructor ( config , client ) {
36+ // eslint-disable-next-line complexity
37+ constructor ( config , Users ) {
3638 this . config = {
3739 ...sharedConfig ( 'API' ) ,
3840 rateLimitIgnoredGlobs : [ ] ,
3941 ...config
4042 } ;
4143
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-
5244 // initialize the app
5345 const app = new Koa ( ) ;
5446
55- // allow middleware to access redis client
47+ // initialize cabin
48+ this . logger = _ . isPlainObject ( this . config . logger )
49+ ? new Cabin ( this . config . logger )
50+ : this . config . logger instanceof Cabin
51+ ? this . config . logger
52+ : new Cabin ( {
53+ logger : this . config . logger ? this . config . logger : console
54+ } ) ;
55+ app . context . logger = this . logger ;
56+
57+ // initialize redis
58+ this . client =
59+ this . config . redis === false
60+ ? false
61+ : _ . isPlainObject ( this . config . redis )
62+ ? new Redis ( this . config . redis , this . logger , this . config . redisMonitor )
63+ : this . config . redis ;
5664 app . context . client = this . client ;
5765
58- // listen for error and log events emitted by app
66+ // expose passport
67+ this . passport =
68+ this . config . passport === false
69+ ? false
70+ : _ . isPlainObject ( this . config . passport )
71+ ? new Passport ( this . config . passport , Users )
72+ : this . config . passport ;
73+ app . context . passport = this . passport ;
74+
75+ // listen for errors emitted by app
5976 app . on ( 'error' , ( err , ctx ) => {
60- const level = err . status && err . status < 500 ? 'warn' : 'error' ;
61- if ( ctx . logger ) ctx . logger [ level ] ( err ) ;
62- else cabin [ level ] ( err ) ;
77+ ctx . logger [ err . status && err . status < 500 ? 'warn' : 'error' ] ( err ) ;
6378 } ) ;
64- app . on ( 'log' , cabin . log ) ;
6579
6680 // override koa's undocumented error handler
67- app . context . onerror = errorHandler ( false , cabin ) ;
81+ app . context . onerror = errorHandler ( false , this . logger ) ;
6882
6983 // only trust proxy if enabled
7084 app . proxy = boolean ( process . env . TRUST_PROXY ) ;
@@ -89,7 +103,7 @@ class API {
89103 app . use ( koaConnect ( requestId ( ) ) ) ;
90104
91105 // use the cabin middleware (adds request-based logging and helpers)
92- app . use ( cabin . middleware ) ;
106+ app . use ( this . logger . middleware ) ;
93107
94108 // allow before hooks to get setup
95109 if ( _ . isFunction ( this . config . hookBeforeSetup ) )
@@ -99,7 +113,7 @@ class API {
99113 if ( this . config . auth ) app . use ( auth ( this . config . auth ) ) ;
100114
101115 // rate limiting
102- if ( this . config . rateLimit ) {
116+ if ( this . client && this . config . rateLimit ) {
103117 app . use ( ( ctx , next ) => {
104118 // check against ignored/whitelisted paths
105119 if (
@@ -113,7 +127,7 @@ class API {
113127 return ratelimit ( {
114128 ...this . config . rateLimit ,
115129 db : this . client ,
116- logger : cabin ,
130+ logger : this . logger ,
117131 errorMessage ( exp ) {
118132 const fn =
119133 typeof ctx . request . t === 'function' ? ctx . request . t : util . format ;
@@ -131,7 +145,7 @@ class API {
131145 if ( this . config . i18n ) {
132146 const i18n = this . config . i18n . config
133147 ? this . config . i18n
134- : new I18N ( { ...this . config . i18n , logger : cabin } ) ;
148+ : new I18N ( { ...this . config . i18n , logger : this . logger } ) ;
135149 app . use ( i18n . middleware ) ;
136150 }
137151
@@ -151,12 +165,12 @@ class API {
151165 app . use ( json ( ) ) ;
152166
153167 // passport
154- if ( this . config . passport ) app . use ( this . config . passport . initialize ( ) ) ;
168+ if ( this . passport ) app . use ( this . passport . initialize ( ) ) ;
155169
156170 // store the user's last ip address in the background
157171 if ( this . config . storeIPAddress ) {
158172 const storeIPAddress = new StoreIPAddress ( {
159- logger : cabin ,
173+ logger : this . logger ,
160174 ...this . config . storeIPAddress
161175 } ) ;
162176 app . use ( storeIPAddress . middleware ) ;
0 commit comments