From 82961767e3643985ced8b41be64c22cb7154b80e Mon Sep 17 00:00:00 2001 From: Christian Schurr Date: Mon, 9 Jan 2023 17:24:22 +0100 Subject: [PATCH] feat!: enforces payload.init is async * Run connectMongoose before starting payload init * - reverted changes - added deprecated to init - docs: changed all payload.init to payload.initAsync - changed all internal init calls * forgotten inits in docs * reverted back - removed init and renamed initAsync to init --- docs/authentication/using-middleware.mdx | 27 ++++++++++++------------ docs/getting-started/installation.mdx | 11 +++++----- src/bin/generateGraphQLSchema.ts | 4 ++-- src/index.ts | 8 ++----- src/init.ts | 15 ------------- test/devServer.ts | 2 +- test/helpers/configHelpers.ts | 2 +- 7 files changed, 26 insertions(+), 43 deletions(-) diff --git a/docs/authentication/using-middleware.mdx b/docs/authentication/using-middleware.mdx index e26b924e04..607d493faf 100644 --- a/docs/authentication/using-middleware.mdx +++ b/docs/authentication/using-middleware.mdx @@ -25,24 +25,25 @@ payload.init({ secret: 'PAYLOAD_SECRET_KEY', mongoURL: 'mongodb://localhost/payload', express: app, -}); + onInit: async () => { + const router = express.Router(); -const router = express.Router(); + router.use(payload.authenticate); // highlight-line -router.use(payload.authenticate); // highlight-line + router.get('/', (req, res) => { + if (req.user) { + return res.send(`Authenticated successfully as ${req.user.email}.`); + } -router.get('/', (req, res) => { - if (req.user) { - return res.send(`Authenticated successfully as ${req.user.email}.`); - } + return res.send('Not authenticated'); + }); - return res.send('Not authenticated'); -}); + app.use('/some-route-here', router); -app.use('/some-route-here', router); - -app.listen(3000, async () => { - payload.logger.info(`listening on ${3000}...`); + 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 cc2dcecac2..a0a8499fc3 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#init) 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) docs. To initialize Payload, update your `server.js` file to reflect the following code: @@ -80,12 +80,13 @@ 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 1f58ed3eff..5c4ae8a5f0 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 function generateGraphQLSchema(): void { +export async function generateGraphQLSchema(): Promise { const logger = Logger(); const config = loadConfig(); - payload.init({ + await payload.init({ secret: '--unused--', mongoURL: false, local: true, diff --git a/src/index.ts b/src/index.ts index 319a5e9fc5..a0bd676449 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 { initSync, initAsync } from './init'; +import { initAsync } from './init'; require('isomorphic-fetch'); @@ -121,11 +121,7 @@ export class Payload { * @description Initializes Payload * @param options */ - init(options: InitOptions): void { - initSync(this, options); - } - - async initAsync(options: InitOptions): Promise { + async init(options: InitOptions): Promise { await initAsync(this, options); } diff --git a/src/init.ts b/src/init.ts index fb1f2e61e5..04cf2b1ede 100644 --- a/src/init.ts +++ b/src/init.ts @@ -161,18 +161,3 @@ export const initAsync = async (payload: Payload, options: InitOptions): Promise 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 9d452d9a1a..49138309ba 100644 --- a/test/devServer.ts +++ b/test/devServer.ts @@ -5,7 +5,7 @@ import payload from '../src'; const expressApp = express(); const init = async () => { - await payload.initAsync({ + await payload.init({ 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 3144a8921e..bd1d30e5cc 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.initAsync(initOptions); + await payload.init(initOptions); if (initOptions.express) { initOptions.express.listen(port);