From 781ac9ac25ad1a392fa3b20e1a51ad86856d3ccc Mon Sep 17 00:00:00 2001 From: Juan Picado Date: Sat, 18 Feb 2023 15:42:19 +0100 Subject: [PATCH] docs: add docs for theme and filter plugins (#3619) * docs: add docs for theme and filter plugins * typos * typos * Create strong-socks-type.md * Update plugins.md * typos --- .changeset/strong-socks-type.md | 7 + packages/plugins/ui-theme/index.js | 3 + packages/ui-components/package.json | 3 +- packages/web/src/middleware.ts | 8 +- website/docs/dev-plugins.md | 66 ------- website/docs/plugin-auth.md | 2 +- website/docs/plugin-filter.md | 42 ++++ website/docs/plugin-middleware.md | 2 +- website/docs/plugin-storage.md | 2 +- website/docs/plugin-theme.md | 182 ++++++++++++++++++ website/docs/plugins.md | 102 +++------- website/sidebars.js | 24 ++- .../versioned_docs/version-5.x/dev-plugins.md | 66 ------- .../versioned_docs/version-5.x/plugin-auth.md | 2 +- .../version-5.x/plugin-filter.md | 42 ++++ .../version-5.x/plugin-middleware.md | 2 +- .../version-5.x/plugin-storage.md | 2 +- .../version-5.x/plugin-theme.md | 182 ++++++++++++++++++ website/versioned_docs/version-5.x/plugins.md | 98 ++-------- .../version-5.x-sidebars.json | 24 ++- 20 files changed, 548 insertions(+), 313 deletions(-) create mode 100644 .changeset/strong-socks-type.md delete mode 100644 website/docs/dev-plugins.md create mode 100644 website/docs/plugin-filter.md create mode 100644 website/docs/plugin-theme.md delete mode 100644 website/versioned_docs/version-5.x/dev-plugins.md create mode 100644 website/versioned_docs/version-5.x/plugin-filter.md create mode 100644 website/versioned_docs/version-5.x/plugin-theme.md diff --git a/.changeset/strong-socks-type.md b/.changeset/strong-socks-type.md new file mode 100644 index 000000000..2895502a6 --- /dev/null +++ b/.changeset/strong-socks-type.md @@ -0,0 +1,7 @@ +--- +'@verdaccio/ui-theme': major +'@verdaccio/ui-components': major +'@verdaccio/web': major +--- + +fix package configuration issues diff --git a/packages/plugins/ui-theme/index.js b/packages/plugins/ui-theme/index.js index 90704aad9..2acc01a87 100644 --- a/packages/plugins/ui-theme/index.js +++ b/packages/plugins/ui-theme/index.js @@ -2,8 +2,11 @@ const path = require('path'); module.exports = () => { return { + // location of the static files, webpack output staticPath: path.join(__dirname, 'static'), + // webpack manifest json file manifest: require('./static/manifest.json'), + // main manifest files to be loaded manifestFiles: { js: ['runtime.js', 'vendors.js', 'main.js'], }, diff --git a/packages/ui-components/package.json b/packages/ui-components/package.json index 048ca5e22..28014f213 100644 --- a/packages/ui-components/package.json +++ b/packages/ui-components/package.json @@ -8,7 +8,8 @@ "main": "./build/index.js", "types": "build/index.d.ts", "files": [ - "./build" + "./build", + "./src" ], "scripts": { "test": "cross-env TZ=UTC jest --config jest/jest.config.js", diff --git a/packages/web/src/middleware.ts b/packages/web/src/middleware.ts index 554e3c333..2d0fb2c95 100644 --- a/packages/web/src/middleware.ts +++ b/packages/web/src/middleware.ts @@ -14,6 +14,12 @@ export async function loadTheme(config: any) { { config, logger }, // TODO: add types { staticPath: string; manifest: unknown; manifestFiles: unknown } function (plugin: any) { + /** + * + - `staticPath`: is the same data returned in Verdaccio 5. + - `manifest`: A webpack manifest object. + - `manifestFiles`: A object with one property `js` and the array (order matters) of the manifest id to be loaded in the template dynamically. + */ return plugin.staticPath && plugin.manifest && plugin.manifestFiles; }, config?.serverSettings?.pluginPrefix ?? 'verdaccio-theme' @@ -27,7 +33,7 @@ export async function loadTheme(config: any) { } export default async (config, auth, storage) => { - const pluginOptions = (await loadTheme(config)) || require('@verdaccio/ui-theme')(); + const pluginOptions = (await loadTheme(config)) || require('@verdaccio/ui-theme')(config.web); // eslint-disable-next-line new-cap const router = express.Router(); diff --git a/website/docs/dev-plugins.md b/website/docs/dev-plugins.md deleted file mode 100644 index 1b288cbba..000000000 --- a/website/docs/dev-plugins.md +++ /dev/null @@ -1,66 +0,0 @@ ---- -id: dev-plugins -title: 'Developing Plugins' ---- - -There are many ways to extend `verdaccio`, the kind of plugins supported are: - -- [Authentication](plugin-auth.md) -- [Middleware](plugin-middleware.md) -- [Storage](plugin-storage.md) -- Theme -- Filter plugins - -> We recommend developing plugins using our [TypeScript type definitions](https://github.com/verdaccio/monorepo/tree/9.x/core/types). - -# Other plugins - -The following plugins are valid and in process of incubation. - -## Theme Plugin {#theme-plugin} - -The plugin must return a function that returns a **string**. The string should be the absolute location of the root of your user interface. - -### API {#api} - -```javascript -const path = require('path'); - -module.exports = (...arguments) => { - return path.join(__dirname, 'static'); -}; -``` - -It is imporant that the name of the plugin **must start with `verdaccio-theme-` prefix**. - -### Theme Example {#theme-example} - -- [@verdaccio/ui-theme](https://github.com/verdaccio/ui): The default Verdaccio theme based in React.js. - -## Filter Plugin {#filter-plugin} - -Since [`4.1.0`](https://github.com/verdaccio/verdaccio/pull/1313) - -Filter plugins were introduced due a [request](https://github.com/verdaccio/verdaccio/issues/818) in order -to be able to filter metadata from uplinks. - -More [info in the PR](https://github.com/verdaccio/verdaccio/pull/1161). - -```yaml -filters: - storage-filter-blackwhitelist: - filter_file: /path/to/file -``` - -### API {#api-1} - -The method `filter_metadata` will allow you to filter metadata that comes from any uplink, it is `Promise` based -and has to return the same metadata modified. - -> Do not remove properties from the metadata, try to do not mutate rather return a new object. - -``` -interface IPluginStorageFilter extends IPlugin { - filter_metadata(packageInfo: Package): Promise; -} -``` diff --git a/website/docs/plugin-auth.md b/website/docs/plugin-auth.md index 704fd5d81..d56180baa 100644 --- a/website/docs/plugin-auth.md +++ b/website/docs/plugin-auth.md @@ -3,7 +3,7 @@ id: plugin-auth title: 'Authentication Plugin' --- -## What's an Authentication Plugin? {#whats-an-authentication-plugin} +## What's an authentication plugin? {#whats-an-authentication-plugin} Is a sort plugin that allows to handle who access or publish to a specific package. By default the `htpasswd` is built-in, but can easily be replaced by your own. diff --git a/website/docs/plugin-filter.md b/website/docs/plugin-filter.md new file mode 100644 index 000000000..d33104a1b --- /dev/null +++ b/website/docs/plugin-filter.md @@ -0,0 +1,42 @@ +--- +id: plugin-filter +title: 'Filter Plugin' +--- + +## What's a filter plugin? {#whats-a-filter-plugin} + +:::caution + +Filter plugins are **experimental** and requires more users feedback + +:::caution + +### When to use a filter plugin? {#when-to-use} + +If you need to mutate the metadata for different reasons this is a way to do it, all manifest request are intercepted, but the tarballs, user, profile or tokens requests are not included. A good example to review is the [verdaccio-plugin-secfilter](https://github.com/Ansile/verdaccio-plugin-secfilter). + +### Plugin structure {#build-structure} + +The plugin only has one async method named `filter_metadata` that reference of the manifest and must return a copy (or modified object but not recommended) of the metadata. + +```ts +export default class VerdaccioMiddlewarePlugin implements IPluginStorageFilter { + async filter_metadata(metadata: Readonly): Promise { + // modify the metadata + const newMetadata = { ...metadata, ...{ name: 'fooName' } }; + return newMetadata; + } +} +``` + +### Configuration + +Just add `filters` to the `config.yaml` file and your own plugin options. + +```yaml +filters: + storage-filter-blackwhitelist: + filter_file: /path/to/file +``` + +More [info in the PR](https://github.com/verdaccio/verdaccio/pull/1161). diff --git a/website/docs/plugin-middleware.md b/website/docs/plugin-middleware.md index 6b25392ac..5de5a2404 100644 --- a/website/docs/plugin-middleware.md +++ b/website/docs/plugin-middleware.md @@ -3,7 +3,7 @@ id: plugin-middleware title: 'Middleware Plugin' --- -## What's an Middleware Plugin? {#whats-an-middleware-plugin} +## What's a Middleware Plugin? {#whats-a-middleware-plugin} Middleware plugins have the capability to modify the API (web and cli) layer, either adding new endpoints or intercepting requests. diff --git a/website/docs/plugin-storage.md b/website/docs/plugin-storage.md index f98320dbe..d7abb2bf2 100644 --- a/website/docs/plugin-storage.md +++ b/website/docs/plugin-storage.md @@ -3,7 +3,7 @@ id: plugin-storage title: 'Storage Plugin' --- -## What's an Storage Plugin? {#whats-an-storage-plugin} +## What's a Storage Plugin? {#whats-a-storage-plugin} Verdaccio by default uses a file system storage plugin [local-storage](https://github.com/verdaccio/verdaccio/tree/master/packages/plugins/local-storage). The default storage can be easily replaced, either using a community plugin or creating one by your own. diff --git a/website/docs/plugin-theme.md b/website/docs/plugin-theme.md new file mode 100644 index 000000000..d6d4b61f4 --- /dev/null +++ b/website/docs/plugin-theme.md @@ -0,0 +1,182 @@ +--- +id: plugin-theme +title: 'Theme Plugin' +--- + +## What's a theme plugin? {#whats-a-theme-plugin} + +Verdaccio uses by default a [custom UI](https://www.npmjs.com/package/@verdaccio/ui-theme) that provides a good set of feature to visualize the packages, but might be case your team needs some custom extra features and here is where a custom theme is an option. The plugin store static assets that will be loaded in the client side when the page is being rendered. + +### How a theme plugin load phase works? + +```mermaid +stateDiagram-v2 + state if_loads <> + state if_load_fails <> + + + start : Verdaccio start + Yes: Loading custom plugin + No: Custom plugin not found + Yes_loads: Plugin loads successfully + No_loads: Plugin fails on load + load_default: Load default theme (@verdaccio/theme-ui) + Crash: Verdaccio stops + + [*] --> start + start --> if_loads + if_loads --> No: false + if_loads --> Yes : true + Yes --> if_load_fails + No --> load_default + + if_load_fails --> No_loads: false + if_load_fails --> Yes_loads : true + No_loads --> Crash +``` + +### How the assets of the theme loads? {#loads} + +:::caution + +By default the application loads on `http://localhost:4873`, but in cases where a resverse proxy with custom domain are involved the assets are loaded based on the property `__VERDACCIO_BASENAME_UI_OPTIONS.base` and `__VERDACCIO_BASENAME_UI_OPTIONS.basename`, thus only one domain configuration can be used. + +:::caution + +The theme loads only in the client side, the application renders HTML with `