212 lines
5.8 KiB
JavaScript
Raw Normal View History

'use strict';
const path = require('path');
2021-09-02 11:25:24 +02:00
const fs = require('fs-extra');
const _ = require('lodash');
const { getAbsoluteServerUrl } = require('@strapi/utils');
2022-03-17 16:54:37 +01:00
const defaultPluginConfig = require('../config/default-plugin-config');
const { builApiEndpointPath, buildComponentSchema } = require('./helpers');
module.exports = ({ strapi }) => {
const config = strapi.config.get('plugin.documentation');
2022-06-01 23:12:16 +02:00
const registeredDocs = [];
2021-09-02 11:25:24 +02:00
return {
2022-06-01 23:12:16 +02:00
registerDoc(doc) {
2022-09-05 15:18:24 +02:00
let registeredDoc = doc;
// parseYaml
if (typeof doc === 'string') {
2022-09-05 15:18:24 +02:00
registeredDoc = require('yaml').parse(registeredDoc);
}
// receive an object we can register it directly
2022-09-05 15:18:24 +02:00
registeredDocs.push(registeredDoc);
2022-06-01 23:12:16 +02:00
},
2021-09-02 11:25:24 +02:00
getDocumentationVersion() {
return _.get(config, 'info.version');
2021-09-02 11:25:24 +02:00
},
2021-09-02 11:25:24 +02:00
getFullDocumentationPath() {
2022-03-14 17:54:35 +01:00
return path.join(strapi.dirs.app.extensions, 'documentation', 'documentation');
2021-09-02 11:25:24 +02:00
},
2022-02-10 15:55:29 +01:00
getCustomDocumentationPath() {
2022-03-14 17:54:35 +01:00
// ??
return path.join(strapi.dirs.app.extensions, 'documentation', 'config', 'settings.json');
2022-02-10 15:55:29 +01:00
},
2021-09-02 11:25:24 +02:00
getDocumentationVersions() {
return fs
.readdirSync(this.getFullDocumentationPath())
2022-08-08 23:33:39 +02:00
.map((version) => {
try {
2021-09-02 11:25:24 +02:00
const doc = JSON.parse(
fs.readFileSync(
path.resolve(this.getFullDocumentationPath(), version, 'full_documentation.json')
)
);
2021-09-02 11:25:24 +02:00
const generatedDate = _.get(doc, ['info', 'x-generation-date'], null);
2021-09-02 11:25:24 +02:00
return { version, generatedDate, url: '' };
} catch (err) {
2021-09-02 11:25:24 +02:00
return null;
}
2021-09-02 11:25:24 +02:00
})
2022-08-08 23:33:39 +02:00
.filter((x) => x);
2021-09-02 11:25:24 +02:00
},
/**
* Returns settings stored in core-store
*/
async getDocumentationAccess() {
const { restrictedAccess } = await strapi
2021-09-02 11:25:24 +02:00
.store({
environment: '',
type: 'plugin',
name: 'documentation',
key: 'config',
})
.get();
return { restrictedAccess };
2021-09-02 11:25:24 +02:00
},
/**
* @description - Gets the path for an api or plugin
*
* @param {object} api
* @property {string} api.name - Name of the api
* @property {string} api.getter - api | plugin
*
* @returns path to the api | plugin
*/
getApiDocumentationPath(api) {
if (api.getter === 'plugin') {
2022-03-14 17:54:35 +01:00
return path.join(strapi.dirs.app.extensions, api.name, 'documentation');
2021-09-02 11:25:24 +02:00
}
2022-03-14 17:54:35 +01:00
return path.join(strapi.dirs.app.api, api.name, 'documentation');
2021-09-02 11:25:24 +02:00
},
async deleteDocumentation(version) {
const apis = this.getPluginAndApiInfo();
for (const api of apis) {
await fs.remove(path.join(this.getApiDocumentationPath(api), version));
}
await fs.remove(path.join(this.getFullDocumentationPath(), version));
},
getPluginAndApiInfo() {
const plugins = _.get(config, 'x-strapi-config.plugins');
2023-03-14 11:10:55 +01:00
2022-08-08 23:33:39 +02:00
const pluginsToDocument = plugins.map((plugin) => {
2021-09-02 11:25:24 +02:00
return {
name: plugin,
getter: 'plugin',
ctNames: Object.keys(strapi.plugin(plugin).contentTypes),
};
});
2022-08-08 23:33:39 +02:00
const apisToDocument = Object.keys(strapi.api).map((api) => {
2021-09-02 11:25:24 +02:00
return {
name: api,
getter: 'api',
ctNames: Object.keys(strapi.api[api].contentTypes),
};
});
2021-09-02 11:25:24 +02:00
return [...apisToDocument, ...pluginsToDocument];
},
2022-03-17 16:54:37 +01:00
async getCustomConfig() {
2022-02-10 15:55:29 +01:00
const customConfigPath = this.getCustomDocumentationPath();
const pathExists = await fs.pathExists(customConfigPath);
if (pathExists) {
return fs.readJson(customConfigPath);
}
return {};
},
2021-09-02 11:25:24 +02:00
/**
* @description - Creates the Swagger json files
*/
async generateFullDoc(version = this.getDocumentationVersion()) {
2021-09-02 11:25:24 +02:00
let paths = {};
2022-03-17 16:54:37 +01:00
let schemas = {};
2021-09-02 11:25:24 +02:00
const apis = this.getPluginAndApiInfo();
for (const api of apis) {
const apiName = api.name;
const apiDirPath = path.join(this.getApiDocumentationPath(api), version);
2021-09-02 11:25:24 +02:00
const apiDocPath = path.join(apiDirPath, `${apiName}.json`);
2022-03-17 16:54:37 +01:00
const apiPath = builApiEndpointPath(api);
if (!apiPath) {
continue;
}
await fs.ensureFile(apiDocPath);
2022-03-17 16:54:37 +01:00
await fs.writeJson(apiDocPath, apiPath, { spaces: 2 });
2022-03-17 16:54:37 +01:00
const componentSchema = buildComponentSchema(api);
schemas = {
...schemas,
...componentSchema,
};
paths = { ...paths, ...apiPath };
2021-09-02 11:25:24 +02:00
}
2021-09-02 11:25:24 +02:00
const fullDocJsonPath = path.join(
this.getFullDocumentationPath(),
version,
2021-09-02 11:25:24 +02:00
'full_documentation.json'
);
2022-03-17 16:54:37 +01:00
const defaultConfig = _.cloneDeep(defaultPluginConfig);
2021-10-05 14:46:25 -04:00
const serverUrl = getAbsoluteServerUrl(strapi.config);
const apiPath = strapi.config.get('api.rest.prefix');
2022-03-17 16:54:37 +01:00
_.set(defaultConfig, 'servers', [
{
url: `${serverUrl}${apiPath}`,
description: 'Development server',
},
]);
2022-03-17 16:54:37 +01:00
_.set(defaultConfig, ['info', 'x-generation-date'], new Date().toISOString());
_.set(defaultConfig, ['info', 'version'], version);
_.merge(defaultConfig.components, { schemas });
2022-03-17 16:54:37 +01:00
const customConfig = await this.getCustomConfig();
const config = _.merge(defaultConfig, customConfig);
2021-10-05 14:46:25 -04:00
2022-06-01 23:12:16 +02:00
const finalDoc = { ...config, paths };
2022-08-08 23:33:39 +02:00
registeredDocs.forEach((doc) => {
// Add tags
finalDoc.tags = finalDoc.tags || [];
finalDoc.tags.push(...(doc.tags || []));
// Add Paths
2022-06-01 23:12:16 +02:00
_.assign(finalDoc.paths, doc.paths);
// Add components
_.forEach(doc.components || {}, (val, key) => {
finalDoc.components[key] = finalDoc.components[key] || {};
_.assign(finalDoc.components[key], val);
});
2022-06-01 23:12:16 +02:00
});
2021-09-02 11:25:24 +02:00
await fs.ensureFile(fullDocJsonPath);
2022-06-01 23:12:16 +02:00
await fs.writeJson(fullDocJsonPath, finalDoc, { spaces: 2 });
2021-09-02 11:25:24 +02:00
},
};
};