From 45272d0b1dc68e6ae8dbc4cfda790b3a50cf1b25 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Fri, 26 Mar 2021 16:38:51 +1100 Subject: [PATCH] Remove keystone prepare (#5226) * Remove legacy keystone.prepare() method * Add changeset --- .changeset/funny-timers-drop.md | 5 + packages/keystone/README.md | 13 --- packages/keystone/lib/Keystone/index.js | 47 -------- packages/keystone/package.json | 5 +- packages/keystone/tests/Keystone.test.js | 132 ----------------------- yarn.lock | 10 -- 6 files changed, 6 insertions(+), 206 deletions(-) create mode 100644 .changeset/funny-timers-drop.md diff --git a/.changeset/funny-timers-drop.md b/.changeset/funny-timers-drop.md new file mode 100644 index 00000000000..1c404e396b9 --- /dev/null +++ b/.changeset/funny-timers-drop.md @@ -0,0 +1,5 @@ +--- +'@keystone-next/keystone-legacy': major +--- + +Removed legacy method `keystone.prepare()`. diff --git a/packages/keystone/README.md b/packages/keystone/README.md index c2fab32a2aa..03af1d18697 100644 --- a/packages/keystone/README.md +++ b/packages/keystone/README.md @@ -170,19 +170,6 @@ For more information about the first four arguments, please see the [Apollo docs - The `access` argument for `types`, `queries`, and `mutations` are all either boolean values which are used at schema generation time to include or exclude the item from the schema, or a function which must return boolean. - See the [Access control API](https://www.keystonejs.com/api/access-control#custom-schema-access-control) docs for more details. -### `prepare(config)` - -Manually prepare middlewares. Returns a promise representing the processed middlewares. They are available as an array through the `middlewares` property of the returned object. - -#### Usage - -```javascript -const { middlewares } = await keystone.prepare({ - apps, - dev: process.env.NODE_ENV !== 'production', -}); -``` - #### Config | Option | Type | default | Description | diff --git a/packages/keystone/lib/Keystone/index.js b/packages/keystone/lib/Keystone/index.js index 2d44d7221bb..cfcb7bf25c0 100644 --- a/packages/keystone/lib/Keystone/index.js +++ b/packages/keystone/lib/Keystone/index.js @@ -1,8 +1,5 @@ const { ApolloServer, gql } = require('apollo-server-express'); -const flattenDeep = require('lodash.flattendeep'); const memoize = require('micro-memoize'); -const falsey = require('falsey'); -const createCorsMiddleware = require('cors'); const { execute } = require('graphql'); const { GraphQLUpload } = require('graphql-upload'); const { objMerge, flatten, unique, filterValues } = require('@keystone-next/utils-legacy'); @@ -453,48 +450,4 @@ module.exports = class Keystone { o => Object.entries(o).length > 0 ); } - - async _prepareMiddlewares({ dev, apps, distDir, pinoOptions, cors }) { - return flattenDeep([ - // Used by other middlewares such as authentication strategies. Important - // to be first so the methods added to `req` are available further down - // the request pipeline. - // TODO: set up a session test rig (maybe by wrapping an in-memory store) - falsey(process.env.DISABLE_LOGGING) && require('express-pino-logger')(pinoOptions), - cors && createCorsMiddleware(cors), - ...(await Promise.all( - [ - // Inject any field middlewares (eg; WYSIWIG's static assets) - // We do this first to avoid it conflicting with any catch-all routes the - // user may have specified - ...this.registeredTypes, - ...flattenDeep( - Object.values(this.auth).map(authStrategies => Object.values(authStrategies)) - ), - ...apps, - ] - .filter(({ prepareMiddleware } = {}) => !!prepareMiddleware) - .map(app => app.prepareMiddleware({ keystone: this, dev, distDir: distDir || 'dist' })) - )), - ]).filter(middleware => !!middleware); - } - - async prepare({ - dev = false, - apps = [], - distDir, - pinoOptions, - cors = { origin: true, credentials: true }, - } = {}) { - this.createApolloServer({ schemaName: 'internal' }); - const middlewares = await this._prepareMiddlewares({ dev, apps, distDir, pinoOptions, cors }); - // These function can't be called after prepare(), so make them throw an error from now on. - ['createList'].forEach(f => { - this[f] = () => { - throw new Error(`keystone.${f} must be called before keystone.prepare()`); - }; - }); - - return { middlewares }; - } }; diff --git a/packages/keystone/package.json b/packages/keystone/package.json index b1a5ea2768b..a958b26cb3f 100644 --- a/packages/keystone/package.json +++ b/packages/keystone/package.json @@ -12,7 +12,6 @@ "@keystone-next/utils-legacy": "^7.0.0", "apollo-errors": "^1.9.0", "apollo-server-express": "^2.21.2", - "cors": "^2.8.5", "cuid": "^2.1.8", "ensure-error": "^3.0.1", "express": "^4.17.1", @@ -21,7 +20,6 @@ "graphql": "^15.5.0", "graphql-type-json": "^0.3.2", "graphql-upload": "^11.0.0", - "lodash.flattendeep": "^4.4.0", "micro-memoize": "^4.0.9", "p-waterfall": "^2.1.1", "pino": "^6.11.2", @@ -31,8 +29,7 @@ }, "devDependencies": { "@keystone-next/fields-legacy": "^23.1.0", - "@keystone-next/test-utils-legacy": "*", - "p-is-promise": "^3.0.0" + "@keystone-next/test-utils-legacy": "*" }, "repository": "https://github.com/keystonejs/keystone/tree/master/packages/keystone" } diff --git a/packages/keystone/tests/Keystone.test.js b/packages/keystone/tests/Keystone.test.js index f6c556df97c..a36f58a88b6 100644 --- a/packages/keystone/tests/Keystone.test.js +++ b/packages/keystone/tests/Keystone.test.js @@ -1,4 +1,3 @@ -const isPromise = require('p-is-promise'); const Keystone = require('../lib/Keystone'); const { List } = require('../lib/ListTypes'); class MockFieldAdapter {} @@ -147,134 +146,3 @@ describe('Keystone.createList()', () => { expect(keystone.lists['Comment'].fields.length).toEqual(3); // id, heading, content }); }); - -describe('keystone.prepare()', () => { - test('returns a Promise', () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - - // NOTE: We add a `.catch()` to silence the "unhandled promise rejection" - // warning - expect(isPromise(keystone.prepare().catch(() => {}))).toBeTruthy(); - }); - - test('returns the middlewares array', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - const { middlewares } = await keystone.prepare(); - - expect(middlewares).toBeInstanceOf(Array); - }); - - test('handles apps:undefined', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - const { middlewares } = await keystone.prepare({ apps: undefined }); - - expect(middlewares).toBeInstanceOf(Array); - }); - - test('handles apps:[]', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - const { middlewares } = await keystone.prepare({ apps: [] }); - - expect(middlewares).toBeInstanceOf(Array); - }); - - test('Handles apps without a `prepareMiddleware`', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - // For less-brittle tests, we grab the list of middlewares when prepare is - // given no apps, then compare it with the one that did. - const { middlewares: defaultMiddlewares } = await keystone.prepare(); - const { middlewares } = await keystone.prepare({ apps: [{ foo: 'bar' }] }); - - expect(middlewares).toBeInstanceOf(Array); - expect(middlewares).toHaveLength(defaultMiddlewares.length); - }); - - test('filters out null middleware results', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - // For less-brittle tests, we grab the list of middlewares when prepare is - // given no apps, then compare it with the one that did. - const { middlewares: defaultMiddlewares } = await keystone.prepare(); - const { middlewares } = await keystone.prepare({ apps: [{ prepareMiddleware: () => {} }] }); - - expect(middlewares).toBeInstanceOf(Array); - expect(middlewares).toHaveLength(defaultMiddlewares.length); - }); - - test('filters out empty middleware arrays', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - // For less-brittle tests, we grab the list of middlewares when prepare is - // given no apps, then compare it with the one that did. - const { middlewares: defaultMiddlewares } = await keystone.prepare(); - const { middlewares } = await keystone.prepare({ apps: [{ prepareMiddleware: () => [] }] }); - - expect(middlewares).toBeInstanceOf(Array); - expect(middlewares).toHaveLength(defaultMiddlewares.length); - }); - - test('returns middlewares', async () => { - const config = { - adapter: new MockAdapter(), - }; - const middleware = jest.fn(() => {}); - const keystone = new Keystone(config); - const { middlewares } = await keystone.prepare({ - apps: [{ prepareMiddleware: () => middleware }], - }); - - expect(middlewares).toBeInstanceOf(Array); - expect(middlewares).toEqual(expect.arrayContaining([middleware])); - }); - - test('flattens deeply nested middlewares', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - const fn0 = jest.fn(() => {}); - const fn1 = jest.fn(() => {}); - const fn2 = jest.fn(() => {}); - const { middlewares } = await keystone.prepare({ - apps: [{ prepareMiddleware: () => [[fn0, fn1], fn2] }], - }); - - expect(middlewares).toBeInstanceOf(Array); - expect(middlewares).toEqual(expect.arrayContaining([fn0, fn1, fn2])); - }); - - test('should create `internal` GraphQL schema instance', async () => { - const config = { - adapter: new MockAdapter(), - }; - const keystone = new Keystone(config); - - // Prepare middlewares - await keystone.prepare(); - - expect(keystone._schemas['internal']).not.toBe(null); - }); - - test('orders field middlewares before app middlewares', async () => {}); - - test('calls prepareMiddleware with correct params', async () => {}); -}); diff --git a/yarn.lock b/yarn.lock index d44d128eb13..b00558674dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9142,11 +9142,6 @@ lodash.flatten@^4.4.0: resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f" integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8= -lodash.flattendeep@^4.4.0: - version "4.4.0" - resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2" - integrity sha1-+wMJF/hqMTTlvJvsDWngAT3f7bI= - lodash.groupby@^4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash.groupby/-/lodash.groupby-4.6.0.tgz#0b08a1dcf68397c397855c3239783832df7403d1" @@ -10476,11 +10471,6 @@ p-finally@^2.0.0: resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-2.0.1.tgz#bd6fcaa9c559a096b680806f4d657b3f0f240561" integrity sha512-vpm09aKwq6H9phqRQzecoDpD8TmVyGw70qmWlyq5onxY7tqyTTFVvxMykxQSQKILBSFlbXpypIw2T1Ml7+DDtw== -p-is-promise@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-3.0.0.tgz#58e78c7dfe2e163cf2a04ff869e7c1dba64a5971" - integrity sha512-Wo8VsW4IRQSKVXsJCn7TomUaVtyfjVDn3nUP7kE967BQk0CwFpdbZs0X0uk5sW9mkBa9eNM7hCMaG93WUAwxYQ== - p-lazy@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/p-lazy/-/p-lazy-3.1.0.tgz#4b1e40482b7ee87853abbcf31824ff64e1816d61"