-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove plugnplay fetch CMS meta (#30)
There is no pluginization for fetching CMS meta. It was very confusing. BREAKING CHANGE: all plugins are now ignored.
- Loading branch information
Showing
15 changed files
with
129 additions
and
552 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
src/helpers/cmsMeta/plugins/__snapshots__/CmsMetaLoader.test.js.snap
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
src/helpers/cmsMeta/plugins/pluginType/CmsMetaPluginTypeLoader.js
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
src/helpers/cmsMeta/plugins/pluginType/CmsMetaPluginTypeLoader.test.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,55 @@ | ||
// @flow | ||
|
||
import type { JsonRpcResponseItem } from '../../flow/types/jsonrpc'; | ||
import type { PluginInstance } from 'plugnplay'; | ||
import type { ObjectLiteral } from '../../flow/types/common'; | ||
|
||
const config = require('config'); | ||
const { PluginManager } = require('plugnplay'); | ||
const _ = require('lodash'); | ||
const logger = require('pino')(); | ||
|
||
const cmsHost = config.get('cms.host'); | ||
|
||
const jsonrpc = require('./jsonrpc')(cmsHost); | ||
const openApiPathToRegExp = require('./openApiPathToRegExp'); | ||
|
||
/** | ||
* Connects to the CMS to get some important bootstrap information. | ||
* | ||
* @return {Promise<JsonRpcResponse>} | ||
* The data from the CMS used to initialize the node proxy. | ||
*/ | ||
module.exports = (): Promise<Array<[ObjectLiteral, JsonRpcResponseItem]>> => { | ||
let requestor; | ||
let pluginManager; | ||
// Initialize the JSON RPC requestor. | ||
return jsonrpc | ||
module.exports = (): Promise<[ObjectLiteral, JsonRpcResponseItem]> => | ||
jsonrpc | ||
.init() | ||
.then(reqr => { | ||
requestor = reqr; | ||
// Instantiate a plugin manager to discover all possible | ||
// cms-meta-plugin-type plugins. | ||
pluginManager = new PluginManager({ | ||
discovery: { | ||
rootPath: | ||
'./{node_modules/@contentacms/**/lib,lib}/helpers/cmsMeta/plugins', | ||
.then(requestor => | ||
requestor.execute([ | ||
{ | ||
jsonrpc: '2.0', | ||
method: 'jsonapi.metadata', | ||
id: 'req-jsonapi.metadata', | ||
}, | ||
]) | ||
) | ||
.then(res => { | ||
const response = [].concat(res).pop(); | ||
// Contenta CMS will send the paths as the Open API specification, we need | ||
// them to match incoming requests so we transform them into regular | ||
// expressions. | ||
const paths = openApiPathToRegExp( | ||
Object.keys(_.get(response, 'result.openApi.paths', {})) | ||
); | ||
return { | ||
jsonrpc: '2.0', | ||
id: 'req-jsonapi.metadata', | ||
result: { | ||
basePath: _.get(response, 'result.openApi.basePath', ''), | ||
paths: JSON.stringify(paths), | ||
}, | ||
}); | ||
return pluginManager.instantiate('cms-meta-plugin'); | ||
// Instantiate all the CMS Meta Fetchers and fetch the data. | ||
}; | ||
}) | ||
.catch(error => { | ||
// If a particular fetcher returns an error, log it then swallow. | ||
logger.error(error); | ||
return error; | ||
}) | ||
.then(pluginType => | ||
// Execute fetch() for all the plugins of type cms-meta-plugin. | ||
Promise.all( | ||
pluginType.exports.plugins.map(descriptor => | ||
pluginManager | ||
.instantiate(descriptor.id, { requestor }) | ||
.then((plugin: PluginInstance) => | ||
Promise.all([ | ||
plugin.descriptor.resultMap, | ||
plugin.exports | ||
.fetch() | ||
.then(res => { | ||
// Contenta CMS will send the paths as the Open API | ||
// specification, we need them to match incoming requests | ||
// so we transform them into regular expressions. | ||
const paths = openApiPathToRegExp( | ||
Object.keys(res.result.openApi.paths) | ||
); | ||
return { | ||
result: { | ||
basePath: res.result.openApi.basePath, | ||
paths: JSON.stringify(paths), | ||
}, | ||
}; | ||
}) | ||
.catch(error => { | ||
// If a particular fetcher returns an error, log it then swallow. | ||
logger.error(error); | ||
return error; | ||
}), | ||
]) | ||
) | ||
) | ||
) | ||
); | ||
}; | ||
.then(res => [{ jsonApiPrefix: 'basePath', jsonApiPaths: 'paths' }, res]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters