Remove immer in favor of a simple function with similar result

This commit is contained in:
Mark Kaylor 2023-03-22 11:52:25 +01:00
parent 19ce046512
commit 03c811e6c6
3 changed files with 16 additions and 18 deletions

View File

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

View File

@ -3,17 +3,24 @@
const path = require('path');
const fs = require('fs-extra');
const _ = require('lodash/fp');
const { produce } = require('immer');
const { getAbsoluteServerUrl } = require('@strapi/utils');
const { builApiEndpointPath, buildComponentSchema } = require('./helpers');
const defaultOpenApiComponents = require('./utils/default-openapi-components');
const { getPluginsThatNeedDocumentation } = require('./utils/get-plugins-that-need-documentation');
const mutateDocumentation = (currentState, mutateStateCallback) => {
// Create a copy of the current state that is mutable
const draftState = _.cloneDeep(currentState);
// Pass the draft to the callback for mutation
mutateStateCallback(draftState);
// Return the mutated state as a new immutable state
return Object.freeze(draftState);
};
module.exports = ({ strapi }) => {
const config = strapi.config.get('plugin.documentation');
const pluginsThatNeedDocumentation = getPluginsThatNeedDocumentation(config);
const overrideService = strapi.plugin('documentation').service('override');
return {
@ -145,9 +152,9 @@ module.exports = ({ strapi }) => {
for (const api of apisThatNeedGeneratedDocumentation) {
const apiName = api.name;
// TODO: check if this is necessary
// TODO: confirm this can be removed
const apiDirPath = path.join(this.getApiDocumentationPath(api), version);
// TODO: check if this is necessary
// TODO: confirm this can be removed
const apiDocPath = path.join(apiDirPath, `${apiName}.json`);
const apiPath = builApiEndpointPath(api);
@ -156,7 +163,7 @@ module.exports = ({ strapi }) => {
continue;
}
// TODO: check if this is necessary
// TODO: confirm this can be removed
await fs.ensureFile(apiDocPath);
await fs.writeJson(apiDocPath, apiPath, { spaces: 2 });
@ -178,7 +185,7 @@ module.exports = ({ strapi }) => {
const serverUrl = getAbsoluteServerUrl(strapi.config);
const apiPath = strapi.config.get('api.rest.prefix');
const generatedDocumentation = produce(config, (draft) => {
const generatedDocumentation = mutateDocumentation(config, (draft) => {
// When no servers found set the default
if (draft.servers.length === 0) {
draft.servers = [
@ -229,11 +236,11 @@ module.exports = ({ strapi }) => {
}
});
// Get the documentation mutateDocumentation
const documentationCustomizer = config['x-strapi-config'].mutateDocumentation;
const userMutateCallback = config['x-strapi-config'].mutateDocumentation;
// Escape hatch, allow the user to provide a mutateDocumentation function that can alter any part of
// the generated documentation before it is written to the file system
const finalDocumentation = documentationCustomizer
? produce(generatedDocumentation, documentationCustomizer)
const finalDocumentation = userMutateCallback
? mutateDocumentation(generatedDocumentation, userMutateCallback)
: generatedDocumentation;
await fs.ensureFile(fullDocJsonPath);

View File

@ -7466,7 +7466,6 @@ __metadata:
formik: 2.2.9
fs-extra: 10.0.0
history: ^4.9.0
immer: ^9.0.19
koa-static: ^5.0.0
lodash: 4.17.21
msw: 1.0.1
@ -18794,13 +18793,6 @@ __metadata:
languageName: node
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":
version: 3.3.0
resolution: "import-fresh@npm:3.3.0"