Skip to content

Commit 8296176

Browse files
authored
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
1 parent 7583289 commit 8296176

File tree

7 files changed

+26
-43
lines changed

7 files changed

+26
-43
lines changed

docs/authentication/using-middleware.mdx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,25 @@ payload.init({
2525
secret: 'PAYLOAD_SECRET_KEY',
2626
mongoURL: 'mongodb://localhost/payload',
2727
express: app,
28-
});
29-
30-
const router = express.Router();
28+
onInit: async () => {
29+
const router = express.Router();
3130

32-
router.use(payload.authenticate); // highlight-line
31+
router.use(payload.authenticate); // highlight-line
3332

34-
router.get('/', (req, res) => {
35-
if (req.user) {
36-
return res.send(`Authenticated successfully as ${req.user.email}.`);
37-
}
33+
router.get('/', (req, res) => {
34+
if (req.user) {
35+
return res.send(`Authenticated successfully as ${req.user.email}.`);
36+
}
3837

39-
return res.send('Not authenticated');
40-
});
38+
return res.send('Not authenticated');
39+
});
4140

42-
app.use('/some-route-here', router);
41+
app.use('/some-route-here', router);
4342

44-
app.listen(3000, async () => {
45-
payload.logger.info(`listening on ${3000}...`);
43+
app.listen(3000, async () => {
44+
payload.logger.info(`listening on ${3000}...`);
45+
});
46+
},
4647
});
4748

4849
```

docs/getting-started/installation.mdx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ app.listen(3000, async () => {
6666
});
6767
```
6868

69-
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.
69+
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.
7070

7171
To initialize Payload, update your `server.js` file to reflect the following code:
7272

@@ -80,12 +80,13 @@ payload.init({
8080
secret: 'SECRET_KEY',
8181
mongoURL: 'mongodb://localhost/payload',
8282
express: app,
83+
onInit: () => {
84+
app.listen(3000, async () => {
85+
console.log('Express is now listening for incoming connections on port 3000.')
86+
});
87+
}
8388
})
8489

85-
app.listen(3000, async () => {
86-
console.log('Express is now listening for incoming connections on port 3000.')
87-
});
88-
8990
```
9091

9192
Here is a list of all properties available to pass through `payload.init`:

src/bin/generateGraphQLSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import Logger from '../utilities/logger';
55
import loadConfig from '../config/load';
66
import payload from '..';
77

8-
export function generateGraphQLSchema(): void {
8+
export async function generateGraphQLSchema(): Promise<void> {
99
const logger = Logger();
1010
const config = loadConfig();
1111

12-
payload.init({
12+
await payload.init({
1313
secret: '--unused--',
1414
mongoURL: false,
1515
local: true,

src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { Result as ResetPasswordResult } from './auth/operations/resetPassword';
4444
import { Result as LoginResult } from './auth/operations/login';
4545
import { Options as FindGlobalOptions } from './globals/operations/local/findOne';
4646
import { Options as UpdateGlobalOptions } from './globals/operations/local/update';
47-
import { initSync, initAsync } from './init';
47+
import { initAsync } from './init';
4848

4949
require('isomorphic-fetch');
5050

@@ -121,11 +121,7 @@ export class Payload {
121121
* @description Initializes Payload
122122
* @param options
123123
*/
124-
init(options: InitOptions): void {
125-
initSync(this, options);
126-
}
127-
128-
async initAsync(options: InitOptions): Promise<void> {
124+
async init(options: InitOptions): Promise<void> {
129125
await initAsync(this, options);
130126
}
131127

src/init.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,3 @@ export const initAsync = async (payload: Payload, options: InitOptions): Promise
161161
if (typeof options.onInit === 'function') await options.onInit(payload);
162162
if (typeof payload.config.onInit === 'function') await payload.config.onInit(payload);
163163
};
164-
165-
export const initSync = (payload: Payload, options: InitOptions): void => {
166-
payload.logger = Logger('payload', options.loggerOptions);
167-
payload.mongoURL = options.mongoURL;
168-
169-
if (payload.mongoURL) {
170-
mongoose.set('strictQuery', false);
171-
connectMongoose(payload.mongoURL, options.mongoOptions, payload.logger);
172-
}
173-
174-
init(payload, options);
175-
176-
if (typeof options.onInit === 'function') options.onInit(payload);
177-
if (typeof payload.config.onInit === 'function') payload.config.onInit(payload);
178-
};

test/devServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import payload from '../src';
55

66
const expressApp = express();
77
const init = async () => {
8-
await payload.initAsync({
8+
await payload.init({
99
secret: uuid(),
1010
mongoURL: process.env.MONGO_URL || 'mongodb://localhost/payload',
1111
express: expressApp,

test/helpers/configHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function initPayloadTest(options: Options): Promise<{ serverURL: st
4040
initOptions.express = express();
4141
}
4242

43-
await payload.initAsync(initOptions);
43+
await payload.init(initOptions);
4444

4545
if (initOptions.express) {
4646
initOptions.express.listen(port);

0 commit comments

Comments
 (0)