feat: base files for static preview (#21555)

* feat: base files for static preview

* Update packages/core/content-manager/server/src/preview/routes/index.ts

Co-authored-by: Rémi de Juvigny <8087692+remidej@users.noreply.github.com>

* chore: comment controllers type

---------

Co-authored-by: Rémi de Juvigny <8087692+remidej@users.noreply.github.com>
This commit is contained in:
Marc Roig 2024-10-03 10:02:19 +02:00 committed by GitHub
parent 959c5589d0
commit 475ef78c75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 102 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import relations from './relations';
import singleTypes from './single-types'; import singleTypes from './single-types';
import uid from './uid'; import uid from './uid';
import history from '../history'; import history from '../history';
import preview from '../preview';
export default { export default {
'collection-types': collectionTypes, 'collection-types': collectionTypes,
@ -16,4 +17,5 @@ export default {
'single-types': singleTypes, 'single-types': singleTypes,
uid, uid,
...(history.controllers ? history.controllers : {}), ...(history.controllers ? history.controllers : {}),
...(preview.controllers ? preview.controllers : {}),
}; };

View File

@ -0,0 +1,10 @@
import type { Plugin } from '@strapi/types';
import { createPreviewController } from './preview';
export const controllers = {
preview: createPreviewController,
/**
* Casting is needed because the types aren't aware that Strapi supports
* passing a controller factory as the value, instead of a controller object directly
*/
} as unknown as Plugin.LoadedPlugin['controllers'];

View File

@ -0,0 +1,16 @@
import type { Core } from '@strapi/types';
const createPreviewController = () => {
return {
async getPreviewURL(ctx) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const request = ctx.request;
return {
data: { url: '' },
};
},
} satisfies Core.Controller;
};
export { createPreviewController };

View File

@ -1,21 +1,38 @@
import type { Plugin } from '@strapi/types'; import type { Plugin } from '@strapi/types';
import { FEATURE_ID } from './constants'; import { FEATURE_ID } from './constants';
import { routes } from './routes';
import { controllers } from './controllers';
import { services } from './services';
/** /**
* Check once if the feature is enabled before loading it, * Check once if the feature is enabled before loading it,
* so that we can assume it is enabled in the other files. * so that we can assume it is enabled in the other files.
*/ */
const getFeature = (): Partial<Plugin.LoadedPlugin> => { const getFeature = (): Partial<Plugin.LoadedPlugin> => {
// TODO: Add license registry check when it's available
if (!strapi.features.future.isEnabled(FEATURE_ID)) { if (!strapi.features.future.isEnabled(FEATURE_ID)) {
return {}; return {};
} }
// TODO: Add license registry check when it's available
// if (!strapi.ee.features.isEnabled('cms-content-preview')) {
// return {};
// }
return { return {
bootstrap() { bootstrap() {
// eslint-disable-next-line no-console -- TODO remove when we have real functionality // eslint-disable-next-line no-console -- TODO remove when we have real functionality
console.log('Bootstrapping preview server'); console.log('Bootstrapping preview server');
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- TODO: Remove when implemented
const config = strapi.config.get('admin.preview');
// TODO: Validation
// TODO: Disable feature if config is not valid
}, },
routes,
controllers,
services,
}; };
}; };

View File

@ -0,0 +1,10 @@
import type { Plugin } from '@strapi/types';
import { previewRouter } from './preview';
/**
* The routes will be merged with the other Content Manager routers,
* so we need to avoid conficts in the router name, and to prefix the path for each route.
*/
export const routes = {
preview: previewRouter,
} satisfies Plugin.LoadedPlugin['routes'];

View File

@ -0,0 +1,20 @@
import type { Plugin } from '@strapi/types';
const info = { pluginName: 'content-manager', type: 'admin' };
const previewRouter: Plugin.LoadedPlugin['routes'][string] = {
type: 'admin',
routes: [
{
method: 'GET',
info,
path: '/preview/url/:contentType',
handler: 'preview.getPreviewURL',
config: {
policies: ['admin::isAuthenticatedAdmin'],
},
},
],
};
export { previewRouter };

View File

@ -0,0 +1,6 @@
import type { Plugin } from '@strapi/types';
import { createPreviewService } from './preview';
export const services = {
preview: createPreviewService,
} satisfies Plugin.LoadedPlugin['services'];

View File

@ -0,0 +1,6 @@
/**
* Responsible of routing an entry to a preview URL.
*/
const createPreviewService = () => {};
export { createPreviewService };

View File

@ -0,0 +1,10 @@
import type { Core } from '@strapi/types';
type PreviewServices = typeof import('./services').services;
function getService<T extends keyof PreviewServices>(strapi: Core.Strapi, name: T) {
// Cast is needed because the return type of strapi.service is too vague
return strapi.service(`plugin::content-manager.${name}`) as ReturnType<PreviewServices[T]>;
}
export { getService };

View File

@ -1,7 +1,9 @@
import admin from './admin'; import admin from './admin';
import history from '../history'; import history from '../history';
import preview from '../preview';
export default { export default {
admin, admin,
...(history.routes ? history.routes : {}), ...(history.routes ? history.routes : {}),
...(preview.routes ? preview.routes : {}),
}; };

View File

@ -8,6 +8,7 @@ import permission from './permission';
import populateBuilder from './populate-builder'; import populateBuilder from './populate-builder';
import uid from './uid'; import uid from './uid';
import history from '../history'; import history from '../history';
import preview from '../preview';
import documentMetadata from './document-metadata'; import documentMetadata from './document-metadata';
import documentManager from './document-manager'; import documentManager from './document-manager';
@ -24,4 +25,5 @@ export default {
'populate-builder': populateBuilder, 'populate-builder': populateBuilder,
uid, uid,
...(history.services ? history.services : {}), ...(history.services ? history.services : {}),
...(preview.services ? preview.services : {}),
}; };