From cbfc6747011329f7210e79ebe228f44ed8607321 Mon Sep 17 00:00:00 2001 From: Gautam Singh <5769869+gautamsi@users.noreply.github.com> Date: Fri, 22 May 2020 10:38:26 +0530 Subject: [PATCH] List plugin improvement (#3011) --- .changeset/gentle-deers-knock.md | 7 +++++ docs/api/create-list.md | 3 +- packages/keystone/lib/Keystone/index.js | 42 ++++++++++++++----------- 3 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 .changeset/gentle-deers-knock.md diff --git a/.changeset/gentle-deers-knock.md b/.changeset/gentle-deers-knock.md new file mode 100644 index 00000000000..0bf26cd6edc --- /dev/null +++ b/.changeset/gentle-deers-knock.md @@ -0,0 +1,7 @@ +--- +'@keystonejs/keystone': minor +--- + +Added additional parameters to list plugins which expose `{ listKey, keystone }` to plugins. This helps plugin know name of list and keystone instance. Existing plugins are not affected by this change. + +New plugin signature: `(config, { listKey, keystone }) => config` \ No newline at end of file diff --git a/docs/api/create-list.md b/docs/api/create-list.md index 54107340ff9..75e41c11901 100644 --- a/docs/api/create-list.md +++ b/docs/api/create-list.md @@ -286,7 +286,8 @@ Changes the path in the Admin UI. Updating `plural` and `singular` values will n ### `plugins` -An array of functions that modify config values. Plugin functions receive a config object and can modify or extend this. They should return a valid list config. +An array of functions that modify config values. Plugin functions receive `(config, { listKey, keystone })`, where `config` is the a list config object, `listKey` is the name of the list, and `keystone` is the keystone object. They should return a valid list config. Plugin functions are executed in the order provided in the list, with the output config of one being passed as input to the next. The output of the final plugin is used to construct the `List` instance. + ```javascript const setupUserList = ({ fields, ...config }) => { diff --git a/packages/keystone/lib/Keystone/index.js b/packages/keystone/lib/Keystone/index.js index 4bb6e224728..7084285cb12 100644 --- a/packages/keystone/lib/Keystone/index.js +++ b/packages/keystone/lib/Keystone/index.js @@ -14,7 +14,6 @@ const { flatten, unique, filterValues, - compose, } = require('@keystonejs/utils'); const { validateFieldAccessControl, @@ -306,23 +305,30 @@ module.exports = class Keystone { throw new Error(`Invalid list name "${key}". List names cannot start with an underscore.`); } - const list = new List(key, compose(config.plugins || [])(config), { - getListByKey, - queryHelper: this._buildQueryHelper.bind(this), - adapter: adapters[adapterName], - defaultAccess: this.defaultAccess, - registerType: type => this.registeredTypes.add(type), - isAuxList, - createAuxList: (auxKey, auxConfig) => { - if (isAuxList) { - throw new Error( - `Aux list "${key}" shouldn't be creating more aux lists ("${auxKey}"). Something's probably not right here.` - ); - } - return this.createList(auxKey, auxConfig, { isAuxList: true }); - }, - schemaNames: this._schemaNames, - }); + // composePlugins([f, g, h])(o, e) = h(g(f(o, e), e), e) + const composePlugins = fns => (o, e) => fns.reduce((acc, fn) => fn(acc, e), o); + + const list = new List( + key, + composePlugins(config.plugins || [])(config, { listKey: key, keystone: this }), + { + getListByKey, + queryHelper: this._buildQueryHelper.bind(this), + adapter: adapters[adapterName], + defaultAccess: this.defaultAccess, + registerType: type => this.registeredTypes.add(type), + isAuxList, + createAuxList: (auxKey, auxConfig) => { + if (isAuxList) { + throw new Error( + `Aux list "${key}" shouldn't be creating more aux lists ("${auxKey}"). Something's probably not right here.` + ); + } + return this.createList(auxKey, auxConfig, { isAuxList: true }); + }, + schemaNames: this._schemaNames, + } + ); this.lists[key] = list; this.listsArray.push(list); this._listCRUDProvider.lists.push(list);