Socket
Book a DemoSign in
Socket

@webiny/plugins

Package Overview
Dependencies
Maintainers
1
Versions
664
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@webiny/plugins - npm Package Compare versions

Comparing version
0.0.0-unstable.615a930a68
to
0.0.0-unstable.61c048f412
+9
AsyncPluginsContainer.d.ts
import type { PluginCollection } from "./types.js";
import { PluginsContainer } from "./types.js";
export declare class AsyncPluginsContainer {
private readonly plugins;
private pluginsContainer;
constructor(plugins: PluginCollection | PluginsContainer);
init(): Promise<PluginsContainer>;
private traverseAndLoadPlugins;
}
import { PluginsContainer } from "./types.js";
const isPluginLoader = value => {
return typeof value === "function";
};
export class AsyncPluginsContainer {
constructor(plugins) {
this.plugins = plugins instanceof PluginsContainer ? plugins.all() : plugins;
}
async init() {
if (this.pluginsContainer) {
return this.pluginsContainer;
}
const plugins = await this.traverseAndLoadPlugins(this.plugins);
this.pluginsContainer = new PluginsContainer(plugins);
return this.pluginsContainer;
}
async traverseAndLoadPlugins(plugins) {
if (!Array.isArray(plugins)) {
return [plugins];
}
return plugins.reduce((acc, item) => {
return acc.then(async plugins => {
if (isPluginLoader(item)) {
const lazyPlugins = await item();
return [...plugins, ...(await this.traverseAndLoadPlugins(lazyPlugins))];
}
return [...plugins, ...(await this.traverseAndLoadPlugins(item))];
});
}, Promise.resolve([]));
}
}
//# sourceMappingURL=AsyncPluginsContainer.js.map
{"version":3,"names":["PluginsContainer","isPluginLoader","value","AsyncPluginsContainer","constructor","plugins","all","init","pluginsContainer","traverseAndLoadPlugins","Array","isArray","reduce","acc","item","then","lazyPlugins","Promise","resolve"],"sources":["AsyncPluginsContainer.ts"],"sourcesContent":["import type { Plugin, PluginCollection, PluginFactory } from \"~/types.js\";\nimport { PluginsContainer } from \"~/types.js\";\n\nconst isPluginLoader = (value: unknown): value is PluginFactory => {\n return typeof value === \"function\";\n};\n\nexport class AsyncPluginsContainer {\n private readonly plugins: PluginCollection;\n private pluginsContainer: PluginsContainer | undefined;\n\n constructor(plugins: PluginCollection | PluginsContainer) {\n this.plugins = plugins instanceof PluginsContainer ? plugins.all() : plugins;\n }\n\n async init() {\n if (this.pluginsContainer) {\n return this.pluginsContainer;\n }\n\n const plugins = await this.traverseAndLoadPlugins(this.plugins);\n this.pluginsContainer = new PluginsContainer(plugins);\n\n return this.pluginsContainer;\n }\n\n private async traverseAndLoadPlugins(plugins: Plugin | PluginCollection): Promise<Plugin[]> {\n if (!Array.isArray(plugins)) {\n return [plugins];\n }\n\n return plugins.reduce<Promise<Plugin[]>>((acc, item) => {\n return acc.then(async plugins => {\n if (isPluginLoader(item)) {\n const lazyPlugins = await item();\n return [...plugins, ...(await this.traverseAndLoadPlugins(lazyPlugins))];\n }\n\n return [...plugins, ...(await this.traverseAndLoadPlugins(item))];\n });\n }, Promise.resolve([]));\n }\n}\n"],"mappings":"AACA,SAASA,gBAAgB;AAEzB,MAAMC,cAAc,GAAIC,KAAc,IAA6B;EAC/D,OAAO,OAAOA,KAAK,KAAK,UAAU;AACtC,CAAC;AAED,OAAO,MAAMC,qBAAqB,CAAC;EAI/BC,WAAWA,CAACC,OAA4C,EAAE;IACtD,IAAI,CAACA,OAAO,GAAGA,OAAO,YAAYL,gBAAgB,GAAGK,OAAO,CAACC,GAAG,CAAC,CAAC,GAAGD,OAAO;EAChF;EAEA,MAAME,IAAIA,CAAA,EAAG;IACT,IAAI,IAAI,CAACC,gBAAgB,EAAE;MACvB,OAAO,IAAI,CAACA,gBAAgB;IAChC;IAEA,MAAMH,OAAO,GAAG,MAAM,IAAI,CAACI,sBAAsB,CAAC,IAAI,CAACJ,OAAO,CAAC;IAC/D,IAAI,CAACG,gBAAgB,GAAG,IAAIR,gBAAgB,CAACK,OAAO,CAAC;IAErD,OAAO,IAAI,CAACG,gBAAgB;EAChC;EAEA,MAAcC,sBAAsBA,CAACJ,OAAkC,EAAqB;IACxF,IAAI,CAACK,KAAK,CAACC,OAAO,CAACN,OAAO,CAAC,EAAE;MACzB,OAAO,CAACA,OAAO,CAAC;IACpB;IAEA,OAAOA,OAAO,CAACO,MAAM,CAAoB,CAACC,GAAG,EAAEC,IAAI,KAAK;MACpD,OAAOD,GAAG,CAACE,IAAI,CAAC,MAAMV,OAAO,IAAI;QAC7B,IAAIJ,cAAc,CAACa,IAAI,CAAC,EAAE;UACtB,MAAME,WAAW,GAAG,MAAMF,IAAI,CAAC,CAAC;UAChC,OAAO,CAAC,GAAGT,OAAO,EAAE,IAAI,MAAM,IAAI,CAACI,sBAAsB,CAACO,WAAW,CAAC,CAAC,CAAC;QAC5E;QAEA,OAAO,CAAC,GAAGX,OAAO,EAAE,IAAI,MAAM,IAAI,CAACI,sBAAsB,CAACK,IAAI,CAAC,CAAC,CAAC;MACrE,CAAC,CAAC;IACN,CAAC,EAAEG,OAAO,CAACC,OAAO,CAAC,EAAE,CAAC,CAAC;EAC3B;AACJ","ignoreList":[]}
+4
-3

@@ -1,4 +0,5 @@

import { PluginsContainer } from "./PluginsContainer";
import { Plugin } from "./Plugin";
import { PluginsContainer } from "./PluginsContainer.js";
import { AsyncPluginsContainer } from "./AsyncPluginsContainer.js";
import { Plugin } from "./Plugin.js";
declare const plugins: PluginsContainer;
export { Plugin, PluginsContainer, plugins };
export { Plugin, PluginsContainer, plugins, AsyncPluginsContainer };

@@ -1,25 +0,7 @@

"use strict";
import { PluginsContainer } from "./PluginsContainer.js";
import { AsyncPluginsContainer } from "./AsyncPluginsContainer.js";
import { Plugin } from "./Plugin.js";
const plugins = new PluginsContainer();
export { Plugin, PluginsContainer, plugins, AsyncPluginsContainer };
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Plugin", {
enumerable: true,
get: function () {
return _Plugin.Plugin;
}
});
Object.defineProperty(exports, "PluginsContainer", {
enumerable: true,
get: function () {
return _PluginsContainer.PluginsContainer;
}
});
exports.plugins = void 0;
var _PluginsContainer = require("./PluginsContainer");
var _Plugin = require("./Plugin");
const plugins = new _PluginsContainer.PluginsContainer();
exports.plugins = plugins;
//# sourceMappingURL=index.js.map
{
"name": "@webiny/plugins",
"version": "0.0.0-unstable.615a930a68",
"version": "0.0.0-unstable.61c048f412",
"type": "module",
"main": "index.js",

@@ -16,14 +17,10 @@ "repository": {

"dependencies": {
"@babel/runtime": "7.19.0",
"uniqid": "5.4.0"
},
"devDependencies": {
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.3",
"@types/uniqid": "^5.3.2",
"@webiny/cli": "^0.0.0-unstable.615a930a68",
"@webiny/project-utils": "^0.0.0-unstable.615a930a68",
"rimraf": "^3.0.2",
"ttypescript": "^1.5.13",
"typescript": "4.7.4"
"@types/uniqid": "5.3.4",
"@webiny/build-tools": "0.0.0-unstable.61c048f412",
"rimraf": "6.1.2",
"typescript": "5.9.3",
"vitest": "3.2.4"
},

@@ -34,7 +31,3 @@ "publishConfig": {

},
"scripts": {
"build": "yarn webiny run build",
"watch": "yarn webiny run watch"
},
"gitHead": "615a930a68d692c832e4f100405eeb3f5a8874af"
"gitHead": "61c048f412d6b4aa70c1d105aab21e3fa69730f3"
}

@@ -1,16 +0,3 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Plugin = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
class Plugin {
export class Plugin {
constructor() {
(0, _defineProperty2.default)(this, "name", void 0);
if (!this.constructor.type) {

@@ -20,10 +7,7 @@ throw Error(`Missing "type" definition in "${this.constructor.name}"!`);

}
get type() {
return this.constructor.type;
}
}
exports.Plugin = Plugin;
(0, _defineProperty2.default)(Plugin, "type", void 0);
//# sourceMappingURL=Plugin.js.map

@@ -1,2 +0,5 @@

import { Plugin, PluginCollection } from "./types";
import type { Plugin, PluginCollection } from "./types.js";
export type WithName<T extends Plugin> = T & {
name: string;
};
export declare class PluginsContainer {

@@ -6,6 +9,8 @@ private plugins;

constructor(...args: PluginCollection);
byName<T extends Plugin>(name: T["name"]): T | null;
byType<T extends Plugin>(type: T["type"]): T[];
atLeastOneByType<T extends Plugin>(type: T["type"]): T[];
oneByType<T extends Plugin>(type: T["type"]): T;
byName<T extends Plugin>(name: T["name"]): WithName<T> | null;
byType<T extends Plugin>(type: T["type"]): WithName<T>[];
atLeastOneByType<T extends Plugin>(type: T["type"]): WithName<T>[];
oneByType<T extends Plugin>(type: T["type"]): WithName<T>;
merge(input: PluginsContainer | PluginCollection): void;
mergeByType(container: PluginsContainer, type: string): void;
all<T extends Plugin>(): T[];

@@ -12,0 +17,0 @@ register(...args: any): void;

@@ -1,30 +0,14 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PluginsContainer = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _uniqid = _interopRequireDefault(require("uniqid"));
import uniqid from "uniqid";
const isOptionsObject = item => item && !Array.isArray(item) && !item.type && !item.name;
const normalizeArgs = args => {
let options = {}; // Check if last item in the plugins array is actually an options object.
let options = {};
// Check if last item in the plugins array is actually an options object.
if (isOptionsObject(args[args.length - 1])) {
[options] = args.splice(-1, 1);
}
return [args, options];
};
const assign = (plugins, options, target) => {
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i];
for (const plugin of plugins) {
if (Array.isArray(plugin)) {

@@ -34,10 +18,18 @@ assign(plugin, options, target);

}
if (typeof plugin !== "object") {
throw new Error(`Could not register plugin. Expected an object, but got ${typeof plugin}.`);
}
if (!plugin.type) {
let name = "";
if (plugin.name) {
name = ` "${plugin.name}"`;
}
throw new Error(`Could not register plugin${name}. Missing "type" definition.`);
}
let name = plugin._name || plugin.name;
if (!name) {
plugin.name = name = (0, _uniqid.default)(plugin.type + "-");
} // If skip existing was set to true, and a plugin with the same name was already registered, skip registration.
plugin.name = name = uniqid(plugin.type + "-");
}
// If skip existing was set to true, and a plugin with the same name was already registered, skip registration.
if (!options.skipExisting || !target[name]) {

@@ -49,10 +41,8 @@ target[name] = plugin;

};
class PluginsContainer {
export class PluginsContainer {
plugins = {};
_byTypeCache = {};
constructor(...args) {
(0, _defineProperty2.default)(this, "plugins", {});
(0, _defineProperty2.default)(this, "_byTypeCache", {});
this.register(...args);
}
byName(name) {

@@ -65,7 +55,4 @@ if (!name) {

*/
return this.plugins[name];
}
byType(type) {

@@ -75,3 +62,2 @@ if (this._byTypeCache[type]) {

}
const plugins = this.findByType(type);

@@ -81,27 +67,29 @@ this._byTypeCache[type] = plugins;

}
atLeastOneByType(type) {
const list = this.byType(type);
if (list.length === 0) {
throw new Error(`There are no plugins by type "${type}".`);
}
return list;
}
oneByType(type) {
const list = this.atLeastOneByType(type);
if (list.length > 1) {
throw new Error(`There is a requirement for plugin of type "${type}" to be only one registered.`);
}
return list[0];
}
merge(input) {
if (input instanceof PluginsContainer) {
this.register(...input.all());
return;
}
this.register(input);
}
mergeByType(container, type) {
this.register(...container.byType(type));
}
all() {
return Object.values(this.plugins);
}
register(...args) {

@@ -113,3 +101,2 @@ // reset the cache when adding new plugins

}
unregister(name) {

@@ -120,9 +107,7 @@ // reset the cache when removing a plugin

}
findByType(type) {
return Object.values(this.plugins).filter(pl => pl.type === type);
}
}
exports.PluginsContainer = PluginsContainer;
//# sourceMappingURL=PluginsContainer.js.map
# @webiny/plugins
[![](https://img.shields.io/npm/dw/@webiny/plugins.svg)](https://www.npmjs.com/package/@webiny/plugins)
[![](https://img.shields.io/npm/v/@webiny/plugins.svg)](https://www.npmjs.com/package/@webiny/plugins)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
A simple registry that stores all plugins in a shared object.
The only requirement for a plugin is to have a `name` and a `type` properties.
The rest is entirely up to you.
> [!NOTE]
> This package is part of the [Webiny](https://www.webiny.com) monorepo.
> It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
There is nothing spectacular going on under the hood, just a simple
object for storing references and a few utility functions.
📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
For more information, please visit [the official docs](https://docs.webiny.com/docs/developer-tutorials/plugins-crash-course).
## Install
```
npm install --save @webiny/plugins
```
---
Or if you prefer yarn:
```
yarn add @webiny/plugins
```
## Usage
### Adding a plugin
```
import { plugins } from "@webiny/plugins";
// Add a plugin
plugins.register({
name: "my-plugin",
type: "say-hi",
salute: () => "Hi!"
});
plugins.register({
name: "my-second-plugin",
type: "say-hi",
salute: () => "Yo!"
});
```
### Getting plugins by type
```
// anywhere in your app
import { plugins } from "@webiny/plugins";
const pluginList = plugins.byType("say-hi");
pluginList.forEach(plugin => {
// Call "salute" function
plugin.salute();
});
```
### Getting a single plugin by name
```
// anywhere in your app
import { plugins } from "@webiny/plugins";
const plugin = plugins.byName("my-plugin");
// Call "salute" function
plugin.salute();
```
### Removing a plugin
```
// anywhere in your app
import { plugins } from "@webiny/plugins";
plugins.unregister("my-plugin");
```
_This README file is automatically generated during the publish process._

@@ -1,3 +0,3 @@

export { PluginsContainer } from "./PluginsContainer";
export declare type Plugin<T = Record<string, any>> = {
export { PluginsContainer } from "./PluginsContainer.js";
export type Plugin<T = Record<string, any>> = {
type: string;

@@ -8,2 +8,3 @@ name?: string;

} & T;
export declare type PluginCollection = (Plugin | PluginCollection)[];
export type PluginCollection = (Plugin | PluginFactory | PluginCollection)[];
export type PluginFactory = () => Promise<Plugin | PluginCollection>;

@@ -1,13 +0,3 @@

"use strict";
export { PluginsContainer } from "./PluginsContainer.js";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "PluginsContainer", {
enumerable: true,
get: function () {
return _PluginsContainer.PluginsContainer;
}
});
var _PluginsContainer = require("./PluginsContainer");
//# sourceMappingURL=types.js.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet