1- import express , { Express , Response , Router } from 'express' ;
1+ import { Express , Router } from 'express' ;
22import pino from 'pino' ;
3- import crypto from 'crypto' ;
43import { GraphQLError , GraphQLFormattedError , GraphQLSchema } from 'graphql' ;
54import {
65 TypeWithID ,
@@ -15,31 +14,13 @@ import {
1514import { TypeWithVersion } from './versions/types' ;
1615import { PaginatedDocs } from './mongoose/types' ;
1716
18- import Logger from './utilities/logger' ;
19- import loadConfig from './config/load' ;
20- import authenticate , { PayloadAuthenticate } from './express/middleware/authenticate' ;
21- import connectMongoose from './mongoose/connect' ;
22- import expressMiddleware from './express/middleware' ;
23- import initAdmin from './express/admin' ;
24- import initAuth from './auth/init' ;
25- import access from './auth/requestHandlers/access' ;
26- import initCollections from './collections/init' ;
27- import initPreferences from './preferences/init' ;
28- import initGlobals from './globals/init' ;
17+ import { PayloadAuthenticate } from './express/middleware/authenticate' ;
2918import { Globals , TypeWithID as GlobalTypeWithID } from './globals/config/types' ;
30- import initGraphQLPlayground from './graphql/initPlayground' ;
31- import initStatic from './express/static' ;
32- import registerSchema from './graphql/registerSchema' ;
33- import graphQLHandler from './graphql/graphQLHandler' ;
34- import buildEmail from './email/build' ;
35- import identifyAPI from './express/middleware/identifyAPI' ;
36- import errorHandler , { ErrorHandler } from './express/middleware/errorHandler' ;
19+ import { ErrorHandler } from './express/middleware/errorHandler' ;
3720import localOperations from './collections/operations/local' ;
3821import localGlobalOperations from './globals/operations/local' ;
3922import { encrypt , decrypt } from './auth/crypto' ;
4023import { BuildEmailResult , Message } from './email/types' ;
41- import { PayloadRequest } from './express/types' ;
42- import sendEmail from './email/sendEmail' ;
4324import { Preferences } from './preferences/types' ;
4425
4526import { Options as CreateOptions } from './collections/operations/local/create' ;
@@ -63,7 +44,7 @@ import { Result as ResetPasswordResult } from './auth/operations/resetPassword';
6344import { Result as LoginResult } from './auth/operations/login' ;
6445import { Options as FindGlobalOptions } from './globals/operations/local/findOne' ;
6546import { Options as UpdateGlobalOptions } from './globals/operations/local/update' ;
66- import { serverInit as serverInitTelemetry } from './utilities/telemetry/events/serverInit ' ;
47+ import { init } from './init ' ;
6748
6849require ( 'isomorphic-fetch' ) ;
6950
@@ -75,19 +56,19 @@ export class Payload {
7556
7657 collections : {
7758 [ slug : string ] : Collection ;
78- } = { } ;
59+ } = { }
7960
8061 versions : {
8162 [ slug : string ] : CollectionModel ;
82- } = { } ;
63+ } = { }
8364
8465 preferences : Preferences ;
8566
8667 globals : Globals ;
8768
8869 logger : pino . Logger ;
8970
90- express : Express ;
71+ express : Express
9172
9273 router : Router ;
9374
@@ -101,7 +82,7 @@ export class Payload {
10182
10283 mongoURL : string | false ;
10384
104- mongoMemoryServer : any ;
85+ mongoMemoryServer : any
10586
10687 local : boolean ;
10788
@@ -140,97 +121,12 @@ export class Payload {
140121 * @description Initializes Payload
141122 * @param options
142123 */
143- async init ( options : InitOptions ) : Promise < void > {
144- this . logger = Logger ( 'payload' , options . loggerOptions ) ;
145- this . logger . info ( 'Starting Payload...' ) ;
146- if ( ! options . secret ) {
147- throw new Error (
148- 'Error: missing secret key. A secret key is needed to secure Payload.' ,
149- ) ;
150- }
151-
152- if ( options . mongoURL !== false && typeof options . mongoURL !== 'string' ) {
153- throw new Error ( 'Error: missing MongoDB connection URL.' ) ;
154- }
155-
156- this . emailOptions = { ...( options . email ) } ;
157- this . secret = crypto
158- . createHash ( 'sha256' )
159- . update ( options . secret )
160- . digest ( 'hex' )
161- . slice ( 0 , 32 ) ;
162-
163- this . mongoURL = options . mongoURL ;
164- this . local = options . local ;
165-
166- this . config = loadConfig ( this . logger ) ;
167-
168- // Connect to database
169- if ( this . mongoURL ) {
170- this . mongoMemoryServer = await connectMongoose ( this . mongoURL , options . mongoOptions , this . logger ) ;
171- }
172-
173- // If not initializing locally, scaffold router
174- if ( ! this . local ) {
175- this . router = express . Router ( ) ;
176- this . router . use ( ...expressMiddleware ( this ) ) ;
177- initAuth ( this ) ;
178- }
179-
180- // Configure email service
181- this . email = buildEmail ( this . emailOptions , this . logger ) ;
182- this . sendEmail = sendEmail . bind ( this ) ;
183-
184- // Initialize collections & globals
185- initCollections ( this ) ;
186- initGlobals ( this ) ;
187-
188- if ( ! this . config . graphQL . disable ) {
189- registerSchema ( this ) ;
190- }
191- // If not initializing locally, set up HTTP routing
192- if ( ! this . local ) {
193- options . express . use ( ( req : PayloadRequest , res , next ) => {
194- req . payload = this ;
195- next ( ) ;
196- } ) ;
197-
198- this . express = options . express ;
199-
200- if ( this . config . rateLimit . trustProxy ) {
201- this . express . set ( 'trust proxy' , 1 ) ;
202- }
203-
204- initAdmin ( this ) ;
205- initPreferences ( this ) ;
206-
207- this . router . get ( '/access' , access ) ;
208-
209- if ( ! this . config . graphQL . disable ) {
210- this . router . use (
211- this . config . routes . graphQL ,
212- identifyAPI ( 'GraphQL' ) ,
213- ( req : PayloadRequest , res : Response ) => graphQLHandler ( req , res ) ( req , res ) ,
214- ) ;
215- initGraphQLPlayground ( this ) ;
216- }
217-
218- // Bind router to API
219- this . express . use ( this . config . routes . api , this . router ) ;
220-
221- // Enable static routes for all collections permitting upload
222- initStatic ( this ) ;
223-
224- this . errorHandler = errorHandler ( this . config , this . logger ) ;
225- this . router . use ( this . errorHandler ) ;
226-
227- this . authenticate = authenticate ( this . config ) ;
228- }
229-
230- if ( typeof options . onInit === 'function' ) await options . onInit ( this ) ;
231- if ( typeof this . config . onInit === 'function' ) await this . config . onInit ( this ) ;
232-
233- serverInitTelemetry ( this ) ;
124+ init ( options : InitOptions ) : void {
125+ init ( this , options ) ;
126+ }
127+
128+ async initAsync ( options : InitOptions ) : Promise < void > {
129+ await init ( this , options ) ;
234130 }
235131
236132 getAdminURL = ( ) : string => `${ this . config . serverURL } ${ this . config . routes . admin } ` ;
@@ -245,7 +141,7 @@ export class Payload {
245141 create = async < T = any > ( options : CreateOptions < T > ) : Promise < T > => {
246142 const { create } = localOperations ;
247143 return create ( this , options ) ;
248- } ;
144+ }
249145
250146 /**
251147 * @description Find documents with criteria
@@ -255,17 +151,17 @@ export class Payload {
255151 find = async < T extends TypeWithID = any > ( options : FindOptions ) : Promise < PaginatedDocs < T > > => {
256152 const { find } = localOperations ;
257153 return find ( this , options ) ;
258- } ;
154+ }
259155
260156 findGlobal = async < T extends GlobalTypeWithID = any > ( options : FindGlobalOptions ) : Promise < T > => {
261157 const { findOne } = localGlobalOperations ;
262158 return findOne ( this , options ) ;
263- } ;
159+ }
264160
265161 updateGlobal = async < T extends GlobalTypeWithID = any > ( options : UpdateGlobalOptions ) : Promise < T > => {
266162 const { update } = localGlobalOperations ;
267163 return update ( this , options ) ;
268- } ;
164+ }
269165
270166 /**
271167 * @description Find global versions with criteria
@@ -275,7 +171,7 @@ export class Payload {
275171 findGlobalVersions = async < T extends TypeWithVersion < T > = any > ( options : FindGlobalVersionsOptions ) : Promise < PaginatedDocs < T > > => {
276172 const { findVersions } = localGlobalOperations ;
277173 return findVersions < T > ( this , options ) ;
278- } ;
174+ }
279175
280176 /**
281177 * @description Find global version by ID
@@ -285,7 +181,7 @@ export class Payload {
285181 findGlobalVersionByID = async < T extends TypeWithVersion < T > = any > ( options : FindGlobalVersionByIDOptions ) : Promise < T > => {
286182 const { findVersionByID } = localGlobalOperations ;
287183 return findVersionByID ( this , options ) ;
288- } ;
184+ }
289185
290186 /**
291187 * @description Restore global version by ID
@@ -295,7 +191,7 @@ export class Payload {
295191 restoreGlobalVersion = async < T extends TypeWithVersion < T > = any > ( options : RestoreGlobalVersionOptions ) : Promise < T > => {
296192 const { restoreVersion } = localGlobalOperations ;
297193 return restoreVersion ( this , options ) ;
298- } ;
194+ }
299195
300196 /**
301197 * @description Find document by ID
@@ -305,7 +201,7 @@ export class Payload {
305201 findByID = async < T extends TypeWithID = any > ( options : FindByIDOptions ) : Promise < T > => {
306202 const { findByID } = localOperations ;
307203 return findByID < T > ( this , options ) ;
308- } ;
204+ }
309205
310206 /**
311207 * @description Update document
@@ -315,12 +211,12 @@ export class Payload {
315211 update = async < T = any > ( options : UpdateOptions < T > ) : Promise < T > => {
316212 const { update } = localOperations ;
317213 return update < T > ( this , options ) ;
318- } ;
214+ }
319215
320216 delete = async < T extends TypeWithID = any > ( options : DeleteOptions ) : Promise < T > => {
321217 const { localDelete } = localOperations ;
322218 return localDelete < T > ( this , options ) ;
323- } ;
219+ }
324220
325221 /**
326222 * @description Find versions with criteria
@@ -330,7 +226,7 @@ export class Payload {
330226 findVersions = async < T extends TypeWithVersion < T > = any > ( options : FindVersionsOptions ) : Promise < PaginatedDocs < T > > => {
331227 const { findVersions } = localOperations ;
332228 return findVersions < T > ( this , options ) ;
333- } ;
229+ }
334230
335231 /**
336232 * @description Find version by ID
@@ -340,7 +236,7 @@ export class Payload {
340236 findVersionByID = async < T extends TypeWithVersion < T > = any > ( options : FindVersionByIDOptions ) : Promise < T > => {
341237 const { findVersionByID } = localOperations ;
342238 return findVersionByID ( this , options ) ;
343- } ;
239+ }
344240
345241 /**
346242 * @description Restore version by ID
@@ -350,32 +246,32 @@ export class Payload {
350246 restoreVersion = async < T extends TypeWithVersion < T > = any > ( options : RestoreVersionOptions ) : Promise < T > => {
351247 const { restoreVersion } = localOperations ;
352248 return restoreVersion ( this , options ) ;
353- } ;
249+ }
354250
355251 login = async < T extends TypeWithID = any > ( options : LoginOptions ) : Promise < LoginResult & { user : T } > => {
356252 const { login } = localOperations . auth ;
357253 return login ( this , options ) ;
358- } ;
254+ }
359255
360256 forgotPassword = async ( options : ForgotPasswordOptions ) : Promise < ForgotPasswordResult > => {
361257 const { forgotPassword } = localOperations . auth ;
362258 return forgotPassword ( this , options ) ;
363- } ;
259+ }
364260
365261 resetPassword = async ( options : ResetPasswordOptions ) : Promise < ResetPasswordResult > => {
366262 const { resetPassword } = localOperations . auth ;
367263 return resetPassword ( this , options ) ;
368- } ;
264+ }
369265
370266 unlock = async ( options : UnlockOptions ) : Promise < boolean > => {
371267 const { unlock } = localOperations . auth ;
372268 return unlock ( this , options ) ;
373- } ;
269+ }
374270
375271 verifyEmail = async ( options : VerifyEmailOptions ) : Promise < boolean > => {
376272 const { verifyEmail } = localOperations . auth ;
377273 return verifyEmail ( this , options ) ;
378- } ;
274+ }
379275}
380276
381277const payload = new Payload ( ) ;
0 commit comments