Reduce use of lodash and respect fp immutability

This commit is contained in:
Mark Kaylor 2023-03-20 17:56:09 +01:00
parent 02a6ea5cf5
commit 179f3eb65d
7 changed files with 67 additions and 42 deletions

View File

@ -19,6 +19,9 @@ module.exports = () => ({
info: { info: {
version: '2.0.0', version: '2.0.0',
}, },
'x-strapi-config': {
plugins: ['upload'],
},
}, },
}, },
myplugin: { myplugin: {

View File

@ -33,6 +33,7 @@
"cheerio": "^1.0.0-rc.12", "cheerio": "^1.0.0-rc.12",
"formik": "2.2.9", "formik": "2.2.9",
"fs-extra": "10.0.0", "fs-extra": "10.0.0",
"immer": "^9.0.19",
"koa-static": "^5.0.0", "koa-static": "^5.0.0",
"lodash": "4.17.21", "lodash": "4.17.21",
"path-to-regexp": "6.2.1", "path-to-regexp": "6.2.1",

View File

@ -16,6 +16,7 @@ module.exports = {
name: 'Apache 2.0', name: 'Apache 2.0',
url: 'https://www.apache.org/licenses/LICENSE-2.0.html', url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
}, },
'x-strapi-generation-date': new Date().toISOString(),
}, },
'x-strapi-config': { 'x-strapi-config': {
path: '/documentation', path: '/documentation',

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const _ = require('lodash'); const _ = require('lodash/fp');
const buildComponentSchema = require('../helpers/build-component-schema'); const buildComponentSchema = require('../helpers/build-component-schema');
const strapi = { const strapi = {

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const _ = require('lodash/fp');
const fse = require('fs-extra'); const fse = require('fs-extra');
const SwaggerParser = require('@apidevtools/swagger-parser'); const SwaggerParser = require('@apidevtools/swagger-parser');
const { api, plugins, components, contentTypes } = require('../__mocks__/mock-strapi-data'); const { api, plugins, components, contentTypes } = require('../__mocks__/mock-strapi-data');
@ -71,7 +72,8 @@ describe('Documentation service', () => {
const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1]; const lastMockCall = fse.writeJson.mock.calls[fse.writeJson.mock.calls.length - 1];
const mockFinalDoc = lastMockCall[1]; const mockFinalDoc = lastMockCall[1];
const validatePromise = SwaggerParser.validate(mockFinalDoc); // The final documenation is read only, clone deep for this test
const validatePromise = SwaggerParser.validate(_.cloneDeep(mockFinalDoc));
await expect(validatePromise).resolves.not.toThrow(); await expect(validatePromise).resolves.not.toThrow();
}); });

View File

@ -2,9 +2,9 @@
const path = require('path'); const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const _ = require('lodash'); const _ = require('lodash/fp');
const { produce } = require('immer');
const { getAbsoluteServerUrl } = require('@strapi/utils'); const { getAbsoluteServerUrl } = require('@strapi/utils');
const { builApiEndpointPath, buildComponentSchema } = require('./helpers'); const { builApiEndpointPath, buildComponentSchema } = require('./helpers');
const defaultOpenApiComponents = require('./utils/default-openapi-components'); const defaultOpenApiComponents = require('./utils/default-openapi-components');
@ -32,7 +32,7 @@ module.exports = ({ strapi }) => {
}, },
getDocumentationVersion() { getDocumentationVersion() {
return _.get(config, 'info.version'); return _.get('info.version', config);
}, },
getFullDocumentationPath() { getFullDocumentationPath() {
@ -58,7 +58,8 @@ module.exports = ({ strapi }) => {
path.resolve(this.getFullDocumentationPath(), version, 'full_documentation.json') path.resolve(this.getFullDocumentationPath(), version, 'full_documentation.json')
) )
); );
const generatedDate = _.get(doc, ['info', 'x-generation-date'], null);
const generatedDate = _.get(['info', 'x-generation-date'], doc);
return { version, generatedDate, url: '' }; return { version, generatedDate, url: '' };
} catch (err) { } catch (err) {
@ -142,6 +143,7 @@ module.exports = ({ strapi }) => {
); );
for (const api of apisThatNeedGeneratedDocumentation) { for (const api of apisThatNeedGeneratedDocumentation) {
const apiName = api.name; const apiName = api.name;
// TODO: check if this is necessary // TODO: check if this is necessary
const apiDirPath = path.join(this.getApiDocumentationPath(api), version); const apiDirPath = path.join(this.getApiDocumentationPath(api), version);
// TODO: check if this is necessary // TODO: check if this is necessary
@ -176,47 +178,55 @@ module.exports = ({ strapi }) => {
// Set config defaults // Set config defaults
const serverUrl = getAbsoluteServerUrl(strapi.config); const serverUrl = getAbsoluteServerUrl(strapi.config);
const apiPath = strapi.config.get('api.rest.prefix'); const apiPath = strapi.config.get('api.rest.prefix');
const generatedDocumentation = produce(config, (draft) => {
// When no servers found set the default // When no servers found set the default
if (config.servers.length === 0) { if (draft.servers.length === 0) {
_.set(config, 'servers', [ draft.servers = [
{ {
url: `${serverUrl}${apiPath}`, url: `${serverUrl}${apiPath}`,
description: 'Development server', description: 'Development server',
}, },
]); ];
} }
_.set(config, ['info', 'x-generation-date'], new Date().toISOString());
_.set(config, ['x-strapi-config', 'plugins'], pluginsThatNeedDocumentation); draft['x-strapi-config'].plugins = pluginsThatNeedDocumentation;
// Get the documentation customizer
const documentationCustomizer = _.get(config, ['x-strapi-config', 'customizer']);
// Delete it from the config so it doesn't end up in the spec // Delete it from the config so it doesn't end up in the spec
_.unset(config, ['x-strapi-config', 'customizer']); delete draft['x-strapi-config'].customizer;
// Prepare the document to be written with default config and generated paths
const generatedDocumentation = { ...config, paths }; // Set the generated paths
// Add the default components to the document to be written draft.paths = paths;
_.set(generatedDocumentation, 'components', defaultOpenApiComponents);
// Merge the generated component schemas with the defaults // Merge the generated component schemas with the defaults
_.merge(generatedDocumentation.components, { schemas }); draft.components = _.merge(defaultOpenApiComponents, { schemas });
// Apply the the registered overrides
overrideService.registeredOverrides.forEach((doc) => { overrideService.registeredOverrides.forEach((doc) => {
if (doc.tags) {
// Merge override tags with the generated tags // Merge override tags with the generated tags
generatedDocumentation.tags = generatedDocumentation.tags || []; draft.tags = draft.tags || [];
generatedDocumentation.tags.push(...(doc.tags || [])); draft.tags.push(...doc.tags);
}
if (doc.paths) {
// Merge override paths with the generated paths // Merge override paths with the generated paths
// The override will add a new path or replace the value of an existing path // The override will add a new path or replace the value of an existing path
_.assign(generatedDocumentation.paths, doc.paths); draft.paths = { ...draft.paths, ...doc.paths };
// Add components }
_.forEach(doc.components || {}, (val, key) => {
generatedDocumentation.components[key] = generatedDocumentation.components[key] || {}; if (doc.components) {
Object.entries(doc.components).forEach(([key, val]) => {
draft.components[key] = draft.components[key] || {};
// Merge override components with the generated components, // Merge override components with the generated components,
// The override will add a new component or replace the value of an existing component // The override will add a new component or replace the value of an existing component
_.assign(generatedDocumentation.components[key], val); draft.components[key] = { ...draft.components[key], ...val };
});
}
}); });
}); });
// Get the documentation customizer
const documentationCustomizer = config['x-strapi-config'].customizer;
// Escape hatch, allow the user to provide a customizer function that can manipulate // Escape hatch, allow the user to provide a customizer function that can manipulate
// the generated documentation before it is written to the file system // the generated documentation before it is written to the file system
const finalDocumentation = documentationCustomizer const finalDocumentation = documentationCustomizer
? documentationCustomizer(_.cloneDeep(generatedDocumentation)) ? produce(generatedDocumentation, documentationCustomizer)
: generatedDocumentation; : generatedDocumentation;
await fs.ensureFile(fullDocJsonPath); await fs.ensureFile(fullDocJsonPath);

View File

@ -7466,6 +7466,7 @@ __metadata:
formik: 2.2.9 formik: 2.2.9
fs-extra: 10.0.0 fs-extra: 10.0.0
history: ^4.9.0 history: ^4.9.0
immer: ^9.0.19
koa-static: ^5.0.0 koa-static: ^5.0.0
lodash: 4.17.21 lodash: 4.17.21
msw: 1.0.1 msw: 1.0.1
@ -18793,6 +18794,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"immer@npm:^9.0.19":
version: 9.0.21
resolution: "immer@npm:9.0.21"
checksum: 70e3c274165995352f6936695f0ef4723c52c92c92dd0e9afdfe008175af39fa28e76aafb3a2ca9d57d1fb8f796efc4dd1e1cc36f18d33fa5b74f3dfb0375432
languageName: node
linkType: hard
"import-fresh@npm:^3.0.0, import-fresh@npm:^3.1.0, import-fresh@npm:^3.2.1": "import-fresh@npm:^3.0.0, import-fresh@npm:^3.1.0, import-fresh@npm:^3.2.1":
version: 3.3.0 version: 3.3.0
resolution: "import-fresh@npm:3.3.0" resolution: "import-fresh@npm:3.3.0"