diff --git a/docs/authentication/using-middleware.mdx b/docs/authentication/using-middleware.mdx index 607d493fa..e26b924e0 100644 --- a/docs/authentication/using-middleware.mdx +++ b/docs/authentication/using-middleware.mdx @@ -25,25 +25,24 @@ payload.init({ secret: 'PAYLOAD_SECRET_KEY', mongoURL: 'mongodb://localhost/payload', express: app, - onInit: async () => { - const router = express.Router(); +}); - router.use(payload.authenticate); // highlight-line +const router = express.Router(); - router.get('/', (req, res) => { - if (req.user) { - return res.send(`Authenticated successfully as ${req.user.email}.`); - } +router.use(payload.authenticate); // highlight-line - return res.send('Not authenticated'); - }); +router.get('/', (req, res) => { + if (req.user) { + return res.send(`Authenticated successfully as ${req.user.email}.`); + } - app.use('/some-route-here', router); + return res.send('Not authenticated'); +}); - app.listen(3000, async () => { - payload.logger.info(`listening on ${3000}...`); - }); - }, +app.use('/some-route-here', router); + +app.listen(3000, async () => { + payload.logger.info(`listening on ${3000}...`); }); ``` diff --git a/docs/getting-started/installation.mdx b/docs/getting-started/installation.mdx index a0a8499fc..cc2dcecac 100644 --- a/docs/getting-started/installation.mdx +++ b/docs/getting-started/installation.mdx @@ -66,7 +66,7 @@ app.listen(3000, async () => { }); ``` -This server doesn't do anything just yet. But, after you have this in place, we can initialize Payload via its `init()` method, which accepts a small set of arguments to tell it how to operate. For a full list of `init` arguments, please consult the [main configuration](/docs/configuration/overview) docs. +This server doesn't do anything just yet. But, after you have this in place, we can initialize Payload via its `init()` method, which accepts a small set of arguments to tell it how to operate. For a full list of `init` arguments, please consult the [main configuration](/docs/configuration/overview#init) docs. To initialize Payload, update your `server.js` file to reflect the following code: @@ -80,13 +80,12 @@ payload.init({ secret: 'SECRET_KEY', mongoURL: 'mongodb://localhost/payload', express: app, - onInit: () => { - app.listen(3000, async () => { - console.log('Express is now listening for incoming connections on port 3000.') - }); - } }) +app.listen(3000, async () => { + console.log('Express is now listening for incoming connections on port 3000.') +}); + ``` Here is a list of all properties available to pass through `payload.init`: diff --git a/src/bin/generateGraphQLSchema.ts b/src/bin/generateGraphQLSchema.ts index 5c4ae8a5f..1f58ed3ef 100644 --- a/src/bin/generateGraphQLSchema.ts +++ b/src/bin/generateGraphQLSchema.ts @@ -5,11 +5,11 @@ import Logger from '../utilities/logger'; import loadConfig from '../config/load'; import payload from '..'; -export async function generateGraphQLSchema(): Promise { +export function generateGraphQLSchema(): void { const logger = Logger(); const config = loadConfig(); - await payload.init({ + payload.init({ secret: '--unused--', mongoURL: false, local: true, diff --git a/src/index.ts b/src/index.ts index 1d31742b0..319a5e9fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,7 @@ import { Result as ResetPasswordResult } from './auth/operations/resetPassword'; import { Result as LoginResult } from './auth/operations/login'; import { Options as FindGlobalOptions } from './globals/operations/local/findOne'; import { Options as UpdateGlobalOptions } from './globals/operations/local/update'; -import { initPayload } from './init'; +import { initSync, initAsync } from './init'; require('isomorphic-fetch'); @@ -121,8 +121,12 @@ export class Payload { * @description Initializes Payload * @param options */ - async init(options: InitOptions): Promise { - await initPayload(this, options); + init(options: InitOptions): void { + initSync(this, options); + } + + async initAsync(options: InitOptions): Promise { + await initAsync(this, options); } getAdminURL = (): string => `${this.config.serverURL}${this.config.routes.admin}`; diff --git a/src/init.ts b/src/init.ts index 5f87c76d2..fb1f2e61e 100644 --- a/src/init.ts +++ b/src/init.ts @@ -33,15 +33,7 @@ import mountEndpoints from './express/mountEndpoints'; import PreferencesModel from './preferences/model'; import findConfig from './config/find'; -export const initPayload = async (payload: Payload, options: InitOptions): Promise => { - payload.logger = Logger('payload', options.loggerOptions); - payload.mongoURL = options.mongoURL; - - if (payload.mongoURL) { - mongoose.set('strictQuery', false); - payload.mongoMemoryServer = await connectMongoose(payload.mongoURL, options.mongoOptions, payload.logger); - } - +export const init = (payload: Payload, options: InitOptions): void => { payload.logger.info('Starting Payload...'); if (!options.secret) { throw new Error( @@ -153,7 +145,34 @@ export const initPayload = async (payload: Payload, options: InitOptions): Promi } serverInitTelemetry(payload); +}; + +export const initAsync = async (payload: Payload, options: InitOptions): Promise => { + payload.logger = Logger('payload', options.loggerOptions); + payload.mongoURL = options.mongoURL; + + if (payload.mongoURL) { + mongoose.set('strictQuery', false); + payload.mongoMemoryServer = await connectMongoose(payload.mongoURL, options.mongoOptions, payload.logger); + } + + init(payload, options); if (typeof options.onInit === 'function') await options.onInit(payload); if (typeof payload.config.onInit === 'function') await payload.config.onInit(payload); }; + +export const initSync = (payload: Payload, options: InitOptions): void => { + payload.logger = Logger('payload', options.loggerOptions); + payload.mongoURL = options.mongoURL; + + if (payload.mongoURL) { + mongoose.set('strictQuery', false); + connectMongoose(payload.mongoURL, options.mongoOptions, payload.logger); + } + + init(payload, options); + + if (typeof options.onInit === 'function') options.onInit(payload); + if (typeof payload.config.onInit === 'function') payload.config.onInit(payload); +}; diff --git a/test/devServer.ts b/test/devServer.ts index 49138309b..02c9b8778 100644 --- a/test/devServer.ts +++ b/test/devServer.ts @@ -4,8 +4,9 @@ import { v4 as uuid } from 'uuid'; import payload from '../src'; const expressApp = express(); + const init = async () => { - await payload.init({ + await payload.initAsync({ secret: uuid(), mongoURL: process.env.MONGO_URL || 'mongodb://localhost/payload', express: expressApp, diff --git a/test/helpers/configHelpers.ts b/test/helpers/configHelpers.ts index bd1d30e5c..3144a8921 100644 --- a/test/helpers/configHelpers.ts +++ b/test/helpers/configHelpers.ts @@ -40,7 +40,7 @@ export async function initPayloadTest(options: Options): Promise<{ serverURL: st initOptions.express = express(); } - await payload.init(initOptions); + await payload.initAsync(initOptions); if (initOptions.express) { initOptions.express.listen(port);