mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
Fix some legacy code and cleanup old code
This commit is contained in:
parent
63f592a586
commit
20ae9ce9e5
@ -14,6 +14,13 @@ module.exports = () => ({
|
||||
},
|
||||
},
|
||||
},
|
||||
documentation: {
|
||||
config: {
|
||||
info: {
|
||||
version: '2.0.0',
|
||||
},
|
||||
},
|
||||
},
|
||||
myplugin: {
|
||||
enabled: true,
|
||||
resolve: `./src/plugins/myplugin`, // From the root of the project
|
||||
|
||||
@ -41,7 +41,6 @@ module.exports = (dir, initialConfig = {}) => {
|
||||
|
||||
const rootConfig = {
|
||||
launchedAt: Date.now(),
|
||||
appPath: dir,
|
||||
paths: CONFIG_PATHS,
|
||||
serveAdminPanel,
|
||||
autoReload,
|
||||
|
||||
@ -34,8 +34,10 @@ describe('Session middleware', () => {
|
||||
},
|
||||
use: jest.fn(),
|
||||
},
|
||||
dirs: {
|
||||
root: __dirname,
|
||||
},
|
||||
config: {
|
||||
appPath: __dirname,
|
||||
database: {
|
||||
connections: {
|
||||
mysql: {},
|
||||
|
||||
@ -9,7 +9,7 @@ const session = require('koa-session');
|
||||
*/
|
||||
module.exports = strapi => {
|
||||
const requireStore = store => {
|
||||
return require(path.resolve(strapi.config.appPath, 'node_modules', 'koa-' + store));
|
||||
return require(path.resolve(strapi.dirs.root, 'node_modules', 'koa-' + store));
|
||||
};
|
||||
|
||||
const defineStore = session => {
|
||||
|
||||
@ -1,26 +0,0 @@
|
||||
import styled from 'styled-components';
|
||||
|
||||
const Wrapper = styled.div`
|
||||
margin-bottom: 28px;
|
||||
background: #ffffff;
|
||||
padding: 22px 28px 18px;
|
||||
border-radius: 2px;
|
||||
box-shadow: 0 2px 4px #e3e9f3;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
`;
|
||||
|
||||
const Title = styled.div`
|
||||
padding-top: 0px;
|
||||
line-height: 18px;
|
||||
> span {
|
||||
font-weight: 600;
|
||||
color: #333740;
|
||||
font-size: 18px;
|
||||
}
|
||||
> p {
|
||||
color: #787e8f;
|
||||
font-size: 13px;
|
||||
}
|
||||
`;
|
||||
|
||||
export { Wrapper, Title };
|
||||
@ -1,39 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Block
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import { Wrapper, Title } from './components';
|
||||
|
||||
const renderMsg = msg => <p>{msg}</p>;
|
||||
|
||||
const Block = ({ children, description, style, title }) => (
|
||||
<div className="col-md-12">
|
||||
<Wrapper style={style}>
|
||||
<Title>
|
||||
<FormattedMessage id={title} />
|
||||
<FormattedMessage id={description}>{renderMsg}</FormattedMessage>
|
||||
</Title>
|
||||
{children}
|
||||
</Wrapper>
|
||||
</div>
|
||||
);
|
||||
|
||||
Block.defaultProps = {
|
||||
children: null,
|
||||
description: 'app.utils.defaultMessage',
|
||||
style: {},
|
||||
title: 'app.utils.defaultMessage',
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
children: PropTypes.any,
|
||||
description: PropTypes.string,
|
||||
style: PropTypes.object,
|
||||
title: PropTypes.string,
|
||||
};
|
||||
|
||||
export default Block;
|
||||
@ -1,36 +0,0 @@
|
||||
import React from 'react';
|
||||
import { auth, InputsIndex as Input, useNotification } from '@strapi/helper-plugin';
|
||||
import { CopyToClipboard } from 'react-copy-to-clipboard';
|
||||
import getTrad from '../../utils/getTrad';
|
||||
|
||||
const Copy = () => {
|
||||
const toggleNotification = useNotification();
|
||||
|
||||
const handleCopy = () => {
|
||||
toggleNotification({
|
||||
type: 'info',
|
||||
message: { id: getTrad('containers.HomePage.copied') },
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<CopyToClipboard text={auth.getToken()} onCopy={handleCopy}>
|
||||
<div className="row" style={{ zIndex: '99' }}>
|
||||
<Input
|
||||
style={{ zIndex: '9', cursor: 'pointer' }}
|
||||
inputStyle={{ cursor: 'pointer' }}
|
||||
name="jwtToken"
|
||||
value={auth.getToken()}
|
||||
type="string"
|
||||
onChange={() => {}}
|
||||
label={{ id: getTrad('containers.HomePage.form.jwtToken') }}
|
||||
inputDescription={{
|
||||
id: getTrad('containers.HomePage.form.jwtToken.description'),
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</CopyToClipboard>
|
||||
);
|
||||
};
|
||||
|
||||
export default Copy;
|
||||
@ -1,72 +0,0 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { useIntl } from 'react-intl';
|
||||
import { Header as BaseHeader } from '@buffetjs/custom';
|
||||
import { Button } from '@buffetjs/core';
|
||||
import { CheckPermissions } from '@strapi/helper-plugin';
|
||||
import openWithNewTab from '../../utils/openWithNewTab';
|
||||
import pluginPermissions from '../../permissions';
|
||||
import getTrad from '../../utils/getTrad';
|
||||
|
||||
const Header = ({ currentDocVersion, docPrefixURL }) => {
|
||||
const { formatMessage } = useIntl();
|
||||
const headerActions = [
|
||||
{
|
||||
color: 'none',
|
||||
label: formatMessage({
|
||||
id: getTrad('containers.HomePage.Button.open'),
|
||||
defaultMessage: 'Open the documentation',
|
||||
}),
|
||||
className: 'buttonOutline',
|
||||
onClick: () => {
|
||||
const slash = docPrefixURL.startsWith('/') ? '' : '/';
|
||||
|
||||
return openWithNewTab(`${slash}${docPrefixURL}/v${currentDocVersion}`);
|
||||
},
|
||||
type: 'button',
|
||||
key: 'button-open',
|
||||
Component: props => (
|
||||
<CheckPermissions permissions={pluginPermissions.open}>
|
||||
<Button {...props} />
|
||||
</CheckPermissions>
|
||||
),
|
||||
},
|
||||
{
|
||||
label: formatMessage({
|
||||
id: getTrad('containers.HomePage.Button.update'),
|
||||
defaultMessage: 'Update',
|
||||
}),
|
||||
color: 'success',
|
||||
type: 'submit',
|
||||
key: 'button-submit',
|
||||
Component: props => (
|
||||
<CheckPermissions permissions={pluginPermissions.update}>
|
||||
<Button {...props} />
|
||||
</CheckPermissions>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<BaseHeader
|
||||
actions={headerActions}
|
||||
content={formatMessage({
|
||||
id: getTrad('containers.HomePage.PluginHeader.description'),
|
||||
defaultMessage: 'Configure the documentation plugin',
|
||||
})}
|
||||
title={{
|
||||
label: formatMessage({
|
||||
id: getTrad('containers.HomePage.PluginHeader.title'),
|
||||
defaultMessage: 'Documentation - Settings',
|
||||
}),
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
Header.propTypes = {
|
||||
currentDocVersion: PropTypes.string.isRequired,
|
||||
docPrefixURL: PropTypes.string.isRequired,
|
||||
};
|
||||
|
||||
export default Header;
|
||||
@ -1,67 +0,0 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import { CheckPermissions } from '@strapi/helper-plugin';
|
||||
import pluginPermissions from '../../permissions';
|
||||
import openWithNewTab from '../../utils/openWithNewTab';
|
||||
import { StyledButton } from './components';
|
||||
|
||||
const ButtonContainer = ({ currentDocVersion, isHeader, onClick, onClickDelete, version }) => {
|
||||
if (isHeader) {
|
||||
return <div />;
|
||||
}
|
||||
|
||||
const handleClickGenerate = e => {
|
||||
e.preventDefault();
|
||||
onClick(version);
|
||||
};
|
||||
|
||||
const handleDelete = e => {
|
||||
e.preventDefault();
|
||||
onClickDelete(version);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<StyledButton
|
||||
type="openDocumentation"
|
||||
onClick={e => {
|
||||
e.preventDefault();
|
||||
openWithNewTab(`/documentation/v${version}`);
|
||||
}}
|
||||
>
|
||||
<FormattedMessage id="documentation.components.Row.open" />
|
||||
</StyledButton>
|
||||
<CheckPermissions permissions={pluginPermissions.regenerate}>
|
||||
<StyledButton
|
||||
// FIXME when new DS ready
|
||||
type="generateDocumentation"
|
||||
onClick={handleClickGenerate}
|
||||
>
|
||||
<FormattedMessage id="documentation.components.Row.regenerate" />
|
||||
</StyledButton>
|
||||
</CheckPermissions>
|
||||
<CheckPermissions permissions={pluginPermissions.update}>
|
||||
<StyledButton type={version === currentDocVersion ? '' : 'trash'} onClick={handleDelete} />
|
||||
</CheckPermissions>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
ButtonContainer.defaultProps = {
|
||||
currentDocVersion: '1.0.0',
|
||||
isHeader: false,
|
||||
onClick: () => {},
|
||||
onClickDelete: () => {},
|
||||
version: '',
|
||||
};
|
||||
|
||||
ButtonContainer.propTypes = {
|
||||
currentDocVersion: PropTypes.string,
|
||||
isHeader: PropTypes.bool,
|
||||
onClick: PropTypes.func,
|
||||
onClickDelete: PropTypes.func,
|
||||
version: PropTypes.string,
|
||||
};
|
||||
|
||||
export default ButtonContainer;
|
||||
@ -1,83 +0,0 @@
|
||||
import styled, { css } from 'styled-components';
|
||||
import { Button } from '@strapi/helper-plugin';
|
||||
|
||||
const Wrapper = styled.div`
|
||||
height: 54px;
|
||||
display: flex;
|
||||
line-height: 53px;
|
||||
margin: 0 28px 0 36px;
|
||||
justify-content: space-between;
|
||||
border-bottom: 1px solid rgba(14, 22, 34, 0.04);
|
||||
font-size: 13px;
|
||||
color: #333740;
|
||||
> div:first-child {
|
||||
flex: 0 0 70px;
|
||||
font-weight: 500;
|
||||
}
|
||||
> div:nth-child(2) {
|
||||
flex: 0 0 160px;
|
||||
text-align: left;
|
||||
font-weight: 500 !important;
|
||||
}
|
||||
> div:last-child {
|
||||
flex: 0 0 400px;
|
||||
align-self: center;
|
||||
text-align: right;
|
||||
}
|
||||
-webkit-font-smoothing: antialiased;
|
||||
`;
|
||||
|
||||
const StyledButton = styled(Button)`
|
||||
height: 26px;
|
||||
margin: 0;
|
||||
padding: 0 15px;
|
||||
line-height: initial;
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
${({ type }) => {
|
||||
if (type === 'openDocumentation') {
|
||||
return css`
|
||||
margin-right: 10px;
|
||||
border: 1px solid #dfe0e1;
|
||||
&:before {
|
||||
margin-right: 10px;
|
||||
content: '\f08e';
|
||||
font-family: 'FontAwesome';
|
||||
font-size: 10px;
|
||||
}
|
||||
`;
|
||||
}
|
||||
if (type === 'generateDocumentation') {
|
||||
return css`
|
||||
background: #e6f0fb;
|
||||
border: 1px solid #aed4fb;
|
||||
color: #007eff;
|
||||
&:before {
|
||||
margin-right: 10px;
|
||||
content: '\f021';
|
||||
font-family: 'FontAwesome';
|
||||
font-size: 10px;
|
||||
}
|
||||
`;
|
||||
}
|
||||
if (type === 'trash') {
|
||||
return css`
|
||||
margin-left: 25px;
|
||||
font-weight: 400;
|
||||
&:before {
|
||||
margin-right: 10px;
|
||||
content: '\f2ed';
|
||||
font-family: 'FontAwesome';
|
||||
font-size: 12 px;
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
return css`
|
||||
margin-left: 45px;
|
||||
font-weight: 400;
|
||||
`;
|
||||
}}
|
||||
`;
|
||||
|
||||
export { Wrapper, StyledButton };
|
||||
@ -1,51 +0,0 @@
|
||||
/**
|
||||
* Row
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import PropTypes from 'prop-types';
|
||||
import ButtonContainer from './ButtonContainer';
|
||||
import { Wrapper } from './components';
|
||||
|
||||
function Row({ currentDocVersion, data, isHeader, onClickDelete, onUpdateDoc }) {
|
||||
const { version, generatedDate } = data;
|
||||
|
||||
return (
|
||||
<Wrapper>
|
||||
<div>{version}</div>
|
||||
<div>
|
||||
{isHeader ? (
|
||||
<FormattedMessage id="documentation.components.Row.generatedDate" />
|
||||
) : (
|
||||
<span>{generatedDate}</span>
|
||||
)}
|
||||
</div>
|
||||
<ButtonContainer
|
||||
currentDocVersion={currentDocVersion}
|
||||
isHeader={isHeader}
|
||||
version={version}
|
||||
onClickDelete={onClickDelete}
|
||||
onClick={onUpdateDoc}
|
||||
/>
|
||||
</Wrapper>
|
||||
);
|
||||
}
|
||||
|
||||
Row.defaultProps = {
|
||||
currentDocVersion: '1.0.0',
|
||||
data: {},
|
||||
isHeader: false,
|
||||
onClickDelete: () => {},
|
||||
onUpdateDoc: () => {},
|
||||
};
|
||||
|
||||
Row.propTypes = {
|
||||
currentDocVersion: PropTypes.string,
|
||||
data: PropTypes.object,
|
||||
isHeader: PropTypes.bool,
|
||||
onClickDelete: PropTypes.func,
|
||||
onUpdateDoc: PropTypes.func,
|
||||
};
|
||||
|
||||
export default Row;
|
||||
@ -1,59 +0,0 @@
|
||||
import styled from 'styled-components';
|
||||
|
||||
const ContainerFluid = styled.div`
|
||||
padding: 18px 30px;
|
||||
> div:first-child {
|
||||
max-height: 33px;
|
||||
}
|
||||
|
||||
.buttonOutline {
|
||||
height: 30px;
|
||||
padding: 0 15px;
|
||||
border: 1px solid #dfe0e1;
|
||||
font-weight: 500;
|
||||
font-size: 13px;
|
||||
&:before {
|
||||
margin-right: 10px;
|
||||
content: '\f08e';
|
||||
font-family: 'FontAwesome';
|
||||
font-size: 10px;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const StyledRow = styled.div`
|
||||
padding-top: 11px;
|
||||
> div:last-child {
|
||||
> div {
|
||||
padding-bottom: 0 !important;
|
||||
}
|
||||
}
|
||||
> div:first-child {
|
||||
> div {
|
||||
margin-bottom: 22px !important;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const VersionWrapper = styled.div`
|
||||
margin-top: -9px;
|
||||
margin-left: -28px;
|
||||
margin-right: -28px;
|
||||
> div:first-child {
|
||||
height: 18px;
|
||||
line-height: 16px;
|
||||
border-bottom: 0;
|
||||
|
||||
> div:nth-child(2) {
|
||||
font-weight: 600;
|
||||
color: #9ea7b8;
|
||||
}
|
||||
}
|
||||
> div:last-child {
|
||||
> div {
|
||||
border-bottom: none;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export { ContainerFluid, StyledRow, VersionWrapper };
|
||||
@ -1,173 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* HomePage
|
||||
*
|
||||
*/
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import flatten from 'lodash/flatten';
|
||||
import {
|
||||
PopUpWarning,
|
||||
LoadingIndicatorPage,
|
||||
CheckPermissions,
|
||||
SizedInput,
|
||||
getYupInnerErrors,
|
||||
} from '@strapi/helper-plugin';
|
||||
import pluginPermissions from '../../permissions';
|
||||
import getTrad from '../../utils/getTrad';
|
||||
import Block from '../../components/Block';
|
||||
import Copy from '../../components/Copy';
|
||||
import Header from '../../components/Header';
|
||||
import Row from '../../components/Row';
|
||||
import { ContainerFluid, StyledRow, VersionWrapper } from './components';
|
||||
import useHomePage from './useHomePage';
|
||||
import schema from './utils/schema';
|
||||
|
||||
const HomePage = () => {
|
||||
const [versionToDelete, setVersionToDelete] = useState(null);
|
||||
|
||||
const [{ formErrors, modifiedData }, setState] = useState({
|
||||
formErrors: null,
|
||||
modifiedData: {
|
||||
restrictedAccess: true,
|
||||
password: '',
|
||||
},
|
||||
});
|
||||
|
||||
const { data, isLoading, deleteMutation, submitMutation, regenerateDocMutation } = useHomePage();
|
||||
|
||||
useEffect(() => {
|
||||
if (data?.form) {
|
||||
const initialData = flatten(data.form).reduce((acc, current) => {
|
||||
acc[current.name] = current.value;
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
setState({ formErrors: null, modifiedData: initialData });
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
const handleChange = ({ target: { name, value } }) => {
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
modifiedData: {
|
||||
...prev.modifiedData,
|
||||
[name]: value,
|
||||
},
|
||||
}));
|
||||
};
|
||||
|
||||
const handleDeleteDoc = version => {
|
||||
setVersionToDelete(version);
|
||||
};
|
||||
|
||||
const handleConfirmDeleteDoc = () => {
|
||||
deleteMutation.mutate({ prefix: data.prefix, version: versionToDelete });
|
||||
toggleModal();
|
||||
};
|
||||
|
||||
const handleSubmit = async e => {
|
||||
e.preventDefault();
|
||||
|
||||
try {
|
||||
await schema.validate(modifiedData, { abortEarly: false });
|
||||
|
||||
setState(prev => ({ ...prev, formErrors: null }));
|
||||
|
||||
submitMutation.mutate({ body: modifiedData, prefix: data.prefix });
|
||||
} catch (err) {
|
||||
const errors = getYupInnerErrors(err);
|
||||
|
||||
setState(prev => ({ ...prev, formErrors: errors }));
|
||||
}
|
||||
};
|
||||
|
||||
const handleUpdateDoc = version => {
|
||||
regenerateDocMutation.mutate({ version, prefix: data.prefix });
|
||||
};
|
||||
|
||||
const toggleModal = () => {
|
||||
setVersionToDelete(null);
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return <LoadingIndicatorPage />;
|
||||
}
|
||||
|
||||
// FIXME
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<ContainerFluid className="container-fluid">
|
||||
<PopUpWarning
|
||||
isOpen={versionToDelete !== null}
|
||||
toggleModal={toggleModal}
|
||||
content={{
|
||||
title: 'components.popUpWarning.title',
|
||||
message: getTrad('containers.HomePage.PopUpWarning.message'),
|
||||
cancel: 'app.components.Button.cancel',
|
||||
confirm: getTrad('containers.HomePage.PopUpWarning.confirm'),
|
||||
}}
|
||||
popUpWarningType="danger"
|
||||
onConfirm={handleConfirmDeleteDoc}
|
||||
/>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<Header currentDocVersion={data.currentVersion} docPrefixURL={data.prefix} />
|
||||
<StyledRow className="row">
|
||||
<Block>
|
||||
<Copy />
|
||||
</Block>
|
||||
<CheckPermissions permissions={pluginPermissions.update}>
|
||||
<Block>
|
||||
<div className="row">
|
||||
<SizedInput
|
||||
description={getTrad(
|
||||
'containers.HomePage.form.restrictedAccess.inputDescription'
|
||||
)}
|
||||
label={getTrad('containers.HomePage.form.restrictedAccess')}
|
||||
name="restrictedAccess"
|
||||
onChange={handleChange}
|
||||
size={{ xs: 6 }}
|
||||
type="bool"
|
||||
value={modifiedData.restrictedAccess}
|
||||
/>
|
||||
{modifiedData.restrictedAccess && (
|
||||
<SizedInput
|
||||
description={getTrad('containers.HomePage.form.password.inputDescription')}
|
||||
label={getTrad('containers.HomePage.form.password')}
|
||||
error={formErrors?.password}
|
||||
name="password"
|
||||
onChange={handleChange}
|
||||
size={{ xs: 6 }}
|
||||
type="password"
|
||||
value={modifiedData.password}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</Block>
|
||||
</CheckPermissions>
|
||||
<Block title={getTrad('containers.HomePage.Block.title')}>
|
||||
<VersionWrapper>
|
||||
<Row isHeader />
|
||||
{data.docVersions.map(doc => {
|
||||
return (
|
||||
<Row
|
||||
key={doc.generatedDate}
|
||||
data={doc}
|
||||
currentDocVersion={data.currentVersion}
|
||||
onClickDelete={handleDeleteDoc}
|
||||
onUpdateDoc={handleUpdateDoc}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</VersionWrapper>
|
||||
</Block>
|
||||
</StyledRow>
|
||||
</form>
|
||||
</ContainerFluid>
|
||||
);
|
||||
};
|
||||
|
||||
export default HomePage;
|
||||
@ -1,56 +0,0 @@
|
||||
import { useQuery, useMutation, useQueryClient } from 'react-query';
|
||||
import { useNotification } from '@strapi/helper-plugin';
|
||||
import { fetchData, deleteDoc, regenerateDoc, submit } from './utils/api';
|
||||
import getTrad from '../../utils/getTrad';
|
||||
|
||||
const useHomePage = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const toggleNotification = useNotification();
|
||||
const { isLoading, data } = useQuery('get-documentation', () => fetchData(toggleNotification));
|
||||
|
||||
const handleError = err => {
|
||||
toggleNotification({
|
||||
type: 'warning',
|
||||
message: err.response.payload.message,
|
||||
});
|
||||
};
|
||||
|
||||
const deleteMutation = useMutation(deleteDoc, {
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries('get-documentation');
|
||||
toggleNotification({
|
||||
type: 'info',
|
||||
message: { id: getTrad('notification.delete.success') },
|
||||
});
|
||||
},
|
||||
onError: handleError,
|
||||
});
|
||||
|
||||
const submitMutation = useMutation(submit, {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('get-documentation');
|
||||
|
||||
toggleNotification({
|
||||
type: 'success',
|
||||
message: { id: getTrad('notification.update.success') },
|
||||
});
|
||||
},
|
||||
onError: handleError,
|
||||
});
|
||||
|
||||
const regenerateDocMutation = useMutation(regenerateDoc, {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('get-documentation');
|
||||
|
||||
toggleNotification({
|
||||
type: 'info',
|
||||
message: { id: getTrad('notification.generate.success') },
|
||||
});
|
||||
},
|
||||
onError: handleError,
|
||||
});
|
||||
|
||||
return { data, isLoading, deleteMutation, submitMutation, regenerateDocMutation };
|
||||
};
|
||||
|
||||
export default useHomePage;
|
||||
@ -1,30 +0,0 @@
|
||||
import { request } from '@strapi/helper-plugin';
|
||||
import pluginId from '../../../pluginId';
|
||||
|
||||
const deleteDoc = ({ prefix, version }) => {
|
||||
return request(`${prefix}/deleteDoc/${version}`, { method: 'DELETE' });
|
||||
};
|
||||
|
||||
const fetchData = async toggleNotification => {
|
||||
try {
|
||||
const data = await request(`/${pluginId}/getInfos`, { method: 'GET' });
|
||||
|
||||
return data;
|
||||
} catch (err) {
|
||||
toggleNotification({
|
||||
type: 'warning',
|
||||
message: { id: 'notification.error' },
|
||||
});
|
||||
|
||||
// FIXME
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const regenerateDoc = ({ prefix, version }) => {
|
||||
return request(`${prefix}/regenerateDoc`, { method: 'POST', body: { version } });
|
||||
};
|
||||
|
||||
const submit = ({ prefix, body }) => request(`${prefix}/updateSettings`, { method: 'PUT', body });
|
||||
|
||||
export { deleteDoc, fetchData, regenerateDoc, submit };
|
||||
@ -1,11 +0,0 @@
|
||||
import { translatedErrors } from '@strapi/helper-plugin';
|
||||
import * as yup from 'yup';
|
||||
|
||||
const schema = yup.object().shape({
|
||||
restrictedAccess: yup.boolean(),
|
||||
password: yup.string().when('restrictedAccess', (value, initSchema) => {
|
||||
return value ? initSchema.required(translatedErrors.required) : initSchema;
|
||||
}),
|
||||
});
|
||||
|
||||
export default schema;
|
||||
@ -20,7 +20,6 @@
|
||||
"koa-session": "6.2.0",
|
||||
"koa-static": "^5.0.0",
|
||||
"lodash": "4.17.21",
|
||||
"moment": "^2.29.1",
|
||||
"path-to-regexp": "6.2.0",
|
||||
"pluralize": "8.0.0",
|
||||
"react": "^17.0.2",
|
||||
|
||||
@ -43,9 +43,9 @@ module.exports = async ({ strapi }) => {
|
||||
name: 'documentation',
|
||||
});
|
||||
|
||||
const restrictedAccess = await pluginStore.get({ key: 'config' });
|
||||
const config = await pluginStore.get({ key: 'config' });
|
||||
|
||||
if (!restrictedAccess) {
|
||||
if (!config) {
|
||||
pluginStore.set({ key: 'config', value: { restrictedAccess: false } });
|
||||
}
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ module.exports = {
|
||||
path: '/documentation',
|
||||
showGeneratedFiles: true,
|
||||
generateDefaultResponse: true,
|
||||
plugins: ['email', 'upload', 'users-permissions'],
|
||||
plugins: ['email', 'upload'],
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
|
||||
@ -49,9 +49,7 @@ module.exports = {
|
||||
.getDocumentationVersion();
|
||||
|
||||
const openAPISpecsPath = path.join(
|
||||
strapi.config.appPath,
|
||||
'src',
|
||||
'extensions',
|
||||
strapi.dirs.extensions,
|
||||
'documentation',
|
||||
'documentation',
|
||||
version,
|
||||
@ -71,9 +69,7 @@ module.exports = {
|
||||
|
||||
try {
|
||||
const layoutPath = path.resolve(
|
||||
strapi.config.appPath,
|
||||
'src',
|
||||
'extensions',
|
||||
strapi.dirs.extensions,
|
||||
'documentation',
|
||||
'public',
|
||||
'index.html'
|
||||
@ -85,13 +81,7 @@ module.exports = {
|
||||
ctx.url = path.basename(`${ctx.url}/index.html`);
|
||||
|
||||
try {
|
||||
const staticFolder = path.resolve(
|
||||
strapi.config.appPath,
|
||||
'src',
|
||||
'extensions',
|
||||
'documentation',
|
||||
'public'
|
||||
);
|
||||
const staticFolder = path.resolve(strapi.dirs.extensions, 'documentation', 'public');
|
||||
return koaStatic(staticFolder)(ctx, next);
|
||||
} catch (e) {
|
||||
strapi.log.error(e);
|
||||
@ -126,9 +116,7 @@ module.exports = {
|
||||
|
||||
try {
|
||||
const layoutPath = path.resolve(
|
||||
strapi.config.appPath,
|
||||
'src',
|
||||
'extensions',
|
||||
strapi.dirs.extensions,
|
||||
'documentation',
|
||||
'public',
|
||||
'login.html'
|
||||
@ -139,13 +127,7 @@ module.exports = {
|
||||
ctx.url = path.basename(`${ctx.url}/login.html`);
|
||||
|
||||
try {
|
||||
const staticFolder = path.resolve(
|
||||
strapi.config.appPath,
|
||||
'src',
|
||||
'extensions',
|
||||
'documentation',
|
||||
'public'
|
||||
);
|
||||
const staticFolder = path.resolve(strapi.dirs.extensions, 'documentation', 'public');
|
||||
return koaStatic(staticFolder)(ctx, next);
|
||||
} catch (e) {
|
||||
strapi.log.error(e);
|
||||
@ -187,82 +169,48 @@ module.exports = {
|
||||
},
|
||||
|
||||
async regenerateDoc(ctx) {
|
||||
const service = strapi.plugin('documentation').service('documentation');
|
||||
const { version } = ctx.request.body;
|
||||
|
||||
const service = strapi.service('plugin::documentation.documentation');
|
||||
|
||||
const documentationVersions = service.getDocumentationVersions().map(el => el.version);
|
||||
|
||||
const {
|
||||
request: {
|
||||
body: { version },
|
||||
admin,
|
||||
},
|
||||
} = ctx;
|
||||
|
||||
if (_.isEmpty(version)) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
admin ? 'documentation.error.noVersion' : 'Please provide a version.'
|
||||
);
|
||||
return ctx.badRequest('Please provide a version.');
|
||||
}
|
||||
|
||||
if (!documentationVersions.includes(version)) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
admin
|
||||
? 'documentation.error.regenerateDoc.versionMissing'
|
||||
: 'The version you are trying to generate does not exist.'
|
||||
);
|
||||
return ctx.badRequest('The version you are trying to generate does not exist.');
|
||||
}
|
||||
|
||||
try {
|
||||
strapi.reload.isWatching = false;
|
||||
const fullDoc = service.generateFullDoc(version);
|
||||
const documentationPath = service.getMergedDocumentationPath(version);
|
||||
// Write the file
|
||||
fs.writeFileSync(
|
||||
path.resolve(documentationPath, 'full_documentation.json'),
|
||||
JSON.stringify(fullDoc, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
await service.generateFullDoc(version);
|
||||
ctx.send({ ok: true });
|
||||
} catch (err) {
|
||||
ctx.badRequest(null, admin ? 'documentation.error.regenerateDoc' : 'An error occured');
|
||||
} finally {
|
||||
strapi.reload.isWatching = true;
|
||||
}
|
||||
},
|
||||
|
||||
async deleteDoc(ctx) {
|
||||
strapi.reload.isWatching = false;
|
||||
const service = strapi.plugin('documentation').service('documentation');
|
||||
const { version } = ctx.params;
|
||||
|
||||
const service = strapi.service('plugin::documentation.documentation');
|
||||
|
||||
const documentationVersions = service.getDocumentationVersions().map(el => el.version);
|
||||
|
||||
const {
|
||||
params: { version },
|
||||
request: { admin },
|
||||
} = ctx;
|
||||
|
||||
if (_.isEmpty(version)) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
admin ? 'documentation.error.noVersion' : 'Please provide a version.'
|
||||
);
|
||||
return ctx.badRequest('Please provide a version.');
|
||||
}
|
||||
|
||||
if (!documentationVersions.includes(version)) {
|
||||
return ctx.badRequest(
|
||||
null,
|
||||
admin
|
||||
? 'documentation.error.deleteDoc.versionMissing'
|
||||
: 'The version you are trying to delete does not exist.'
|
||||
);
|
||||
return ctx.badRequest('The version you are trying to delete does not exist.');
|
||||
}
|
||||
|
||||
try {
|
||||
strapi.reload.isWatching = false;
|
||||
await service.deleteDocumentation(version);
|
||||
ctx.send({ ok: true });
|
||||
} catch (err) {
|
||||
ctx.badRequest(null, admin ? 'notification.error' : err.message);
|
||||
} finally {
|
||||
strapi.reload.isWatching = true;
|
||||
}
|
||||
@ -271,8 +219,6 @@ module.exports = {
|
||||
async updateSettings(ctx) {
|
||||
const { restrictedAccess, password } = ctx.request.body;
|
||||
|
||||
console.log(ctx.request.body);
|
||||
|
||||
const pluginStore = strapi.store({ type: 'plugin', name: 'documentation' });
|
||||
|
||||
const config = {
|
||||
|
||||
@ -1,39 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const _ = require('lodash');
|
||||
const moment = require('moment');
|
||||
|
||||
const { builApiEndpointPath } = require('../utils/builders');
|
||||
const defaultConfig = require('../config/default-config');
|
||||
|
||||
module.exports = () => {
|
||||
const docPlugin = strapi.plugin('documentation');
|
||||
module.exports = ({ strapi }) => {
|
||||
const config = strapi.config.get('plugin.documentation');
|
||||
|
||||
return {
|
||||
getMergedDocumentationPath(version = this.getDocumentationVersion()) {
|
||||
return path.join(
|
||||
strapi.config.appPath,
|
||||
'src',
|
||||
'extensions',
|
||||
'documentation',
|
||||
'documentation',
|
||||
version
|
||||
);
|
||||
},
|
||||
|
||||
getDocumentationVersion() {
|
||||
return docPlugin.config('info.version');
|
||||
return _.get(config, 'info.version');
|
||||
},
|
||||
|
||||
getFullDocumentationPath() {
|
||||
return path.join(
|
||||
strapi.config.appPath,
|
||||
'src',
|
||||
'extensions',
|
||||
'documentation',
|
||||
'documentation'
|
||||
);
|
||||
return path.join(strapi.dirs.extensions, 'documentation', 'documentation');
|
||||
},
|
||||
|
||||
getDocumentationVersions() {
|
||||
@ -83,10 +66,10 @@ module.exports = () => {
|
||||
*/
|
||||
getApiDocumentationPath(api) {
|
||||
if (api.getter === 'plugin') {
|
||||
return path.join(strapi.config.appPath, 'src', 'extensions', api.name, 'documentation');
|
||||
return path.join(strapi.dirs.extensions, api.name, 'documentation');
|
||||
}
|
||||
|
||||
return path.join(strapi.config.appPath, 'src', 'api', api.name, 'documentation');
|
||||
return path.join(strapi.dirs.api, api.name, 'documentation');
|
||||
},
|
||||
|
||||
async deleteDocumentation(version) {
|
||||
@ -99,7 +82,7 @@ module.exports = () => {
|
||||
},
|
||||
|
||||
getPluginAndApiInfo() {
|
||||
const plugins = docPlugin.config('x-strapi-config.plugins');
|
||||
const plugins = _.get(config, 'x-strapi-config.plugins');
|
||||
const pluginsToDocument = plugins.map(plugin => {
|
||||
return {
|
||||
name: plugin,
|
||||
@ -122,16 +105,13 @@ module.exports = () => {
|
||||
/**
|
||||
* @description - Creates the Swagger json files
|
||||
*/
|
||||
async generateFullDoc() {
|
||||
async generateFullDoc(version = this.getDocumentationVersion()) {
|
||||
let paths = {};
|
||||
|
||||
const apis = this.getPluginAndApiInfo();
|
||||
for (const api of apis) {
|
||||
const apiName = api.name;
|
||||
const apiDirPath = path.join(
|
||||
this.getApiDocumentationPath(api),
|
||||
this.getDocumentationVersion()
|
||||
);
|
||||
const apiDirPath = path.join(this.getApiDocumentationPath(api), version);
|
||||
|
||||
const apiDocPath = path.join(apiDirPath, `${apiName}.json`);
|
||||
|
||||
@ -144,14 +124,14 @@ module.exports = () => {
|
||||
|
||||
const fullDocJsonPath = path.join(
|
||||
this.getFullDocumentationPath(),
|
||||
this.getDocumentationVersion(),
|
||||
version,
|
||||
'full_documentation.json'
|
||||
);
|
||||
|
||||
const settings = _.cloneDeep(defaultConfig);
|
||||
|
||||
_.set(settings, ['info', 'x-generation-date'], moment().format('L LTS'));
|
||||
_.set(settings, ['info', 'version'], this.getDocumentationVersion());
|
||||
_.set(settings, ['info', 'x-generation-date'], new Date().toISOString());
|
||||
_.set(settings, ['info', 'version'], version);
|
||||
|
||||
await fs.ensureFile(fullDocJsonPath);
|
||||
await fs.writeJson(fullDocJsonPath, { ...settings, paths }, { spaces: 2 });
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user