@perses-dev/plugin-system 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PluginBoundary = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const components_1 = require("@perses-dev/components");
6
+ const PluginLoadingBoundary_1 = require("./PluginLoadingBoundary");
7
+ /**
8
+ * Combination ErrorBoundary and PluginLoadingBoundary.
9
+ */
10
+ function PluginBoundary(props) {
11
+ const { ErrorFallbackComponent, loadingFallback, children } = props;
12
+ return ((0, jsx_runtime_1.jsx)(components_1.ErrorBoundary, { FallbackComponent: ErrorFallbackComponent, children: (0, jsx_runtime_1.jsx)(PluginLoadingBoundary_1.PluginLoadingBoundary, { fallback: loadingFallback, children: children }) }));
13
+ }
14
+ exports.PluginBoundary = PluginBoundary;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PluginLoader = void 0;
16
+ const react_query_1 = require("react-query");
17
+ const PluginRegistry_1 = require("../PluginRegistry");
18
+ /**
19
+ * Uses the PluginRegistry to load the specified plugin and throws any errors
20
+ * encountered while loading.
21
+ */
22
+ function PluginLoader(props) {
23
+ const { pluginType, kind } = props;
24
+ // Load the plugin and throw any loading/not found errors
25
+ const { loadPlugin } = (0, PluginRegistry_1.usePluginRegistry)();
26
+ const { error } = (0, react_query_1.useQuery)(`PluginLoader:${pluginType}_${kind}`, () => {
27
+ // The enabled option below should prevent this from being run until loadPlugin is available, but check here to
28
+ // make Typescript happy
29
+ if (loadPlugin === undefined) {
30
+ throw new Error('PluginRegistry loadPlugin not available');
31
+ }
32
+ return loadPlugin(pluginType, kind);
33
+ }, { enabled: loadPlugin !== undefined });
34
+ if (error !== undefined && error !== null) {
35
+ throw error;
36
+ }
37
+ return null;
38
+ }
39
+ exports.PluginLoader = PluginLoader;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePlugin = exports.usePluginLoadingBoundary = exports.PluginLoadingBoundary = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const use_immer_1 = require("use-immer");
7
+ const model_1 = require("../../model");
8
+ const PluginRegistry_1 = require("../PluginRegistry");
9
+ const PluginLoader_1 = require("./PluginLoader");
10
+ /**
11
+ * Plugin dependencies are registered here by children. This takes care of loading
12
+ * those dependencies on demand and acts similar to React's Suspense in that it
13
+ * will show a loading fallback while those plugins are being loaded.
14
+ */
15
+ function PluginLoadingBoundary(props) {
16
+ const { fallback, children } = props;
17
+ const { plugins } = (0, PluginRegistry_1.usePluginRegistry)();
18
+ // Keep track of all plugin dependencies registered by child components
19
+ const [dependencies, setDependencies] = (0, use_immer_1.useImmer)(() => {
20
+ const deps = {};
21
+ for (const pluginType of model_1.ALL_PLUGIN_TYPES) {
22
+ deps[pluginType] = {};
23
+ }
24
+ return deps;
25
+ });
26
+ // When children are rendered, they can register a dependency here to tell us
27
+ // what plugins they need so we can load them if necessary
28
+ const registerPluginDependency = (0, react_1.useCallback)((pluginType, kind) => {
29
+ setDependencies((draft) => {
30
+ draft[pluginType][kind] = true;
31
+ });
32
+ }, [setDependencies]);
33
+ const context = (0, react_1.useMemo)(() => ({ registerPluginDependency }), [registerPluginDependency]);
34
+ const { loaders, fragmentKey } = (0, react_1.useMemo)(() => {
35
+ let fragmentKey = '';
36
+ const loaders = [];
37
+ for (const pluginType of model_1.ALL_PLUGIN_TYPES) {
38
+ // Build an overall key for the dependencies by combining pluginType and
39
+ // kind of all dependencies
40
+ fragmentKey += `_${pluginType}:`;
41
+ const kinds = dependencies[pluginType];
42
+ for (const kind in kinds) {
43
+ fragmentKey += kind;
44
+ // Is this plugin already loaded?
45
+ const plugin = plugins[pluginType][kind];
46
+ if (plugin !== undefined)
47
+ continue;
48
+ // Nope, add it to the loaders list
49
+ loaders.push((0, jsx_runtime_1.jsx)(PluginLoader_1.PluginLoader, { pluginType: pluginType, kind: kind }, `${pluginType}:${kind}`));
50
+ }
51
+ }
52
+ return { loaders, fragmentKey };
53
+ }, [plugins, dependencies]);
54
+ // All dependencies are loaded once we don't have any loaders to render
55
+ const isLoaded = loaders.length === 0;
56
+ return ((0, jsx_runtime_1.jsxs)(PluginLoadingBoundaryContext.Provider, { value: context, children: [loaders, isLoaded && (0, jsx_runtime_1.jsx)(react_1.Fragment, { children: children }, fragmentKey), isLoaded === false && fallback] }));
57
+ }
58
+ exports.PluginLoadingBoundary = PluginLoadingBoundary;
59
+ const PluginLoadingBoundaryContext = (0, react_1.createContext)(undefined);
60
+ /**
61
+ * Gets the PluginLoadingBoundary context and throws if the Provider is missing.
62
+ */
63
+ function usePluginLoadingBoundary() {
64
+ const ctx = (0, react_1.useContext)(PluginLoadingBoundaryContext);
65
+ if (ctx === undefined) {
66
+ throw new Error(`PluginLoadingBoundary context not found. Did you forget a Provider?`);
67
+ }
68
+ return ctx;
69
+ }
70
+ exports.usePluginLoadingBoundary = usePluginLoadingBoundary;
71
+ /**
72
+ * Generic usePlugin that will register the dependency with the nearest LoadingBoundary and get the plugin if it's
73
+ * already loaded
74
+ */
75
+ function usePlugin(pluginType, definition) {
76
+ // Tell the loading boundary about the dependency
77
+ const { registerPluginDependency } = usePluginLoadingBoundary();
78
+ (0, react_1.useEffect)(() => {
79
+ registerPluginDependency(pluginType, definition.kind);
80
+ }, [pluginType, definition.kind, registerPluginDependency]);
81
+ // Get the plugin, which could be undefined if it hasn't loaded yet
82
+ const { plugins } = (0, PluginRegistry_1.usePluginRegistry)();
83
+ return plugins[pluginType][definition.kind];
84
+ }
85
+ exports.usePlugin = usePlugin;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./PluginLoadingBoundary"), exports);
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePluginRegistry = exports.PluginRegistry = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2021 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ const react_query_1 = require("react-query");
19
+ const registry_state_1 = require("./registry-state");
20
+ /**
21
+ * PluginRegistryContext provider that keeps track of all available plugins and
22
+ * their implementations once they've been loaded.
23
+ */
24
+ function PluginRegistry(props) {
25
+ const { children, getInstalledPlugins, importPluginModule } = props;
26
+ const installedPlugins = (0, react_query_1.useQuery)('installed-plugins', getInstalledPlugins);
27
+ const { loadablePlugins, plugins, register } = (0, registry_state_1.useRegistryState)(installedPlugins.data);
28
+ const loadPlugin = (0, react_1.useCallback)(async (pluginType, kind) => {
29
+ // Is it already loaded?
30
+ const plugin = plugins[pluginType][kind];
31
+ if (plugin !== undefined)
32
+ return;
33
+ // Is it a valid plugin we know about?
34
+ const resource = loadablePlugins[pluginType][kind];
35
+ if (resource === undefined) {
36
+ throw new Error(`No ${pluginType} plugin is available for kind ${kind}`);
37
+ }
38
+ // Load and register the resource
39
+ const pluginModule = await importPluginModule(resource);
40
+ register(pluginModule);
41
+ }, [plugins, loadablePlugins, importPluginModule, register]);
42
+ const registry = (0, react_1.useMemo)(() => ({ plugins, loadPlugin: installedPlugins.isLoading ? undefined : loadPlugin }), [installedPlugins.isLoading, plugins, loadPlugin]);
43
+ return (0, jsx_runtime_1.jsx)(PluginRegistryContext.Provider, { value: registry, children: children });
44
+ }
45
+ exports.PluginRegistry = PluginRegistry;
46
+ const PluginRegistryContext = (0, react_1.createContext)(undefined);
47
+ /**
48
+ * Gets the PluginRegistryContext, throwing if the provider is missing.
49
+ */
50
+ function usePluginRegistry() {
51
+ const ctx = (0, react_1.useContext)(PluginRegistryContext);
52
+ if (ctx === undefined) {
53
+ throw new Error('No PluginRegistry context found. Did you forget a Provider?');
54
+ }
55
+ return ctx;
56
+ }
57
+ exports.usePluginRegistry = usePluginRegistry;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidPluginDefinitionError = exports.createGraphQueryPlugin = exports.createPanelPlugin = exports.createVariablePlugin = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2021 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ /**
19
+ * Take a Variable plugin and wrap it so it works with AnyVariableDefinition,
20
+ * doing runtime checking of the definition before delegating to the plugin.
21
+ */
22
+ function createVariablePlugin(config) {
23
+ // Create runtime validation function
24
+ const useRuntimeValidation = createValidationHook(config);
25
+ // Wrap hook with validation (TODO: Can this wrapper become generic for all
26
+ // plugin hooks?)
27
+ function useVariableOptions(definition) {
28
+ const { isValid, errorRef } = useRuntimeValidation();
29
+ if (isValid(definition)) {
30
+ return config.plugin.useVariableOptions(definition);
31
+ }
32
+ throw errorRef.current;
33
+ }
34
+ return {
35
+ useVariableOptions,
36
+ };
37
+ }
38
+ exports.createVariablePlugin = createVariablePlugin;
39
+ /**
40
+ * Take a Panel plugin and wraps it so it works with AnyPanelDefinition, doing
41
+ * runtime checking of the definition before delegating to the plugin.
42
+ */
43
+ function createPanelPlugin(config) {
44
+ const useRuntimeValidation = createValidationHook(config);
45
+ // Wrap PanelComponent from config with validation (TODO: Can this wrapper
46
+ // become generic for all Plugin components?)
47
+ function PanelComponent(props) {
48
+ const { definition, ...others } = props;
49
+ const { isValid, errorRef } = useRuntimeValidation();
50
+ if (isValid(definition)) {
51
+ const { PanelComponent } = config.plugin;
52
+ return (0, jsx_runtime_1.jsx)(PanelComponent, { definition: definition, ...others });
53
+ }
54
+ throw errorRef.current;
55
+ }
56
+ return {
57
+ PanelComponent,
58
+ };
59
+ }
60
+ exports.createPanelPlugin = createPanelPlugin;
61
+ /**
62
+ * Take a GraphQuery plugin and wrap it so it works with AnyChartQueryDefinition,
63
+ * doing runtime validation of the definition before delegating to the plugin.
64
+ */
65
+ function createGraphQueryPlugin(config) {
66
+ // Create runtime validation function
67
+ const useRuntimeValidation = createValidationHook(config);
68
+ // Wrap hook with validation (TODO: Can this wrapper become generic for all
69
+ // plugin hooks?)
70
+ function useGraphQuery(definition) {
71
+ const { isValid, errorRef } = useRuntimeValidation();
72
+ if (isValid(definition)) {
73
+ return config.plugin.useGraphQuery(definition);
74
+ }
75
+ throw errorRef.current;
76
+ }
77
+ return {
78
+ useGraphQuery,
79
+ };
80
+ }
81
+ exports.createGraphQueryPlugin = createGraphQueryPlugin;
82
+ // Create a hook for doing runtime validation of a plugin definition, given the
83
+ // plugin's config
84
+ function createValidationHook(config) {
85
+ const useRuntimeValidation = () => {
86
+ // Ref for storing any validation errors as a side-effect of calling isValid
87
+ const errorRef = (0, react_1.useRef)(undefined);
88
+ // Type guard that validates the generic runtime plugin definition data
89
+ // is correct for Kind/Options
90
+ const isValid = (definition) => {
91
+ var _a, _b;
92
+ // If they don't give us a validate function in the plugin config, not
93
+ // much we can do so just assume we're OK
94
+ const validateErrors = (_b = (_a = config.validate) === null || _a === void 0 ? void 0 : _a.call(config, definition)) !== null && _b !== void 0 ? _b : [];
95
+ if (validateErrors.length === 0)
96
+ return true;
97
+ errorRef.current = new InvalidPluginDefinitionError(config.pluginType, config.kind, validateErrors);
98
+ return false;
99
+ };
100
+ return {
101
+ isValid,
102
+ errorRef,
103
+ };
104
+ };
105
+ return useRuntimeValidation;
106
+ }
107
+ /**
108
+ * Thrown when ConfigData fails the runtime validation check for a plugin.
109
+ */
110
+ class InvalidPluginDefinitionError extends Error {
111
+ constructor(pluginType, kind, validateErrors) {
112
+ super(`Invalid ${pluginType} plugin definition for kind ${kind}`);
113
+ this.pluginType = pluginType;
114
+ this.kind = kind;
115
+ this.validateErrors = validateErrors;
116
+ }
117
+ }
118
+ exports.InvalidPluginDefinitionError = InvalidPluginDefinitionError;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./PluginRegistry"), exports);
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useRegistryState = void 0;
16
+ const react_1 = require("react");
17
+ const use_immer_1 = require("use-immer");
18
+ const model_1 = require("../../model");
19
+ const create_plugin_1 = require("./create-plugin");
20
+ /**
21
+ * Hook for setting up plugin registry state. Returns the state, plus a function
22
+ * for registering plugins with that state.
23
+ */
24
+ function useRegistryState(installedPlugins) {
25
+ // Go through all installed plugins and bundled plugins and build an index of
26
+ // those resources by type and kind
27
+ const loadablePlugins = (0, react_1.useMemo)(() => {
28
+ const loadableProps = {};
29
+ for (const pluginType of model_1.ALL_PLUGIN_TYPES) {
30
+ loadableProps[pluginType] = {};
31
+ }
32
+ // If no plugins installed or waiting on that data, nothing else to do
33
+ if (installedPlugins === undefined)
34
+ return loadableProps;
35
+ const addToLoadable = (resource) => {
36
+ const supportedKinds = resource.spec.supported_kinds;
37
+ for (const kind in supportedKinds) {
38
+ const pluginType = supportedKinds[kind];
39
+ if (pluginType === undefined)
40
+ continue;
41
+ const map = loadableProps[pluginType];
42
+ if (map[kind] !== undefined) {
43
+ console.warn(`Got multiple ${pluginType} plugin definitions for kind ${kind}`);
44
+ continue;
45
+ }
46
+ map[kind] = resource;
47
+ }
48
+ };
49
+ for (const resource of installedPlugins) {
50
+ addToLoadable(resource);
51
+ }
52
+ return loadableProps;
53
+ }, [installedPlugins]);
54
+ const [plugins, setPlugins] = (0, use_immer_1.useImmer)(() => {
55
+ const loadedPlugins = {};
56
+ for (const pluginType of model_1.ALL_PLUGIN_TYPES) {
57
+ loadedPlugins[pluginType] = {};
58
+ }
59
+ return loadedPlugins;
60
+ });
61
+ // Create the register callback to pass to the module's setup function
62
+ const registerPlugin = (0, react_1.useCallback)((config) => {
63
+ switch (config.pluginType) {
64
+ case 'Variable':
65
+ setPlugins((draft) => {
66
+ draft.Variable[config.kind] = (0, create_plugin_1.createVariablePlugin)(config);
67
+ });
68
+ return;
69
+ case 'Panel':
70
+ setPlugins((draft) => {
71
+ draft.Panel[config.kind] = (0, create_plugin_1.createPanelPlugin)(config);
72
+ });
73
+ return;
74
+ case 'GraphQuery':
75
+ setPlugins((draft) => {
76
+ draft.GraphQuery[config.kind] = (0, create_plugin_1.createGraphQueryPlugin)(config);
77
+ });
78
+ return;
79
+ default:
80
+ const exhaustive = config;
81
+ throw new Error(`Unhandled plugin config: ${exhaustive}`);
82
+ }
83
+ }, [setPlugins]);
84
+ const registeredModules = (0, react_1.useRef)(new Set());
85
+ const register = (0, react_1.useCallback)((pluginModule) => {
86
+ // De-dupe register calls in case multiple plugin loading boundaries
87
+ // are waiting for the same module in parallel
88
+ if (registeredModules.current.has(pluginModule)) {
89
+ return;
90
+ }
91
+ // Call the setup function and remember it's been registered
92
+ pluginModule.setup(registerPlugin);
93
+ registeredModules.current.add(pluginModule);
94
+ }, [registerPlugin]);
95
+ return { loadablePlugins, plugins, register };
96
+ }
97
+ exports.useRegistryState = useRegistryState;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./PluginBoundary"), exports);
30
+ __exportStar(require("./PluginLoadingBoundary"), exports);
31
+ __exportStar(require("./PluginRegistry"), exports);
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./components"), exports);
30
+ __exportStar(require("./model"), exports);
31
+ __exportStar(require("./runtime"), exports);
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useGraphQuery = void 0;
16
+ const PluginLoadingBoundary_1 = require("../components/PluginLoadingBoundary");
17
+ /**
18
+ * Use a Graph Query's results from a graph query plugin at runtime.
19
+ */
20
+ const useGraphQuery = (definition) => {
21
+ const plugin = (0, PluginLoadingBoundary_1.usePlugin)('GraphQuery', definition);
22
+ if (plugin === undefined) {
23
+ // Provide default values while the plugin is being loaded
24
+ return { loading: true };
25
+ }
26
+ return plugin.useGraphQuery(definition);
27
+ };
28
+ exports.useGraphQuery = useGraphQuery;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./graph-queries"), exports);
30
+ __exportStar(require("./panels"), exports);
31
+ __exportStar(require("./plugins"), exports);
32
+ __exportStar(require("./variables"), exports);
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PanelComponent = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const PluginLoadingBoundary_1 = require("../components/PluginLoadingBoundary");
6
+ /**
7
+ * Renders a PanelComponent from a panel plugin at runtime.
8
+ */
9
+ const PanelComponent = (props) => {
10
+ const plugin = (0, PluginLoadingBoundary_1.usePlugin)('Panel', props.definition);
11
+ if (plugin === undefined) {
12
+ return null;
13
+ }
14
+ const { PanelComponent: PluginComponent } = plugin;
15
+ return (0, jsx_runtime_1.jsx)(PluginComponent, { ...props });
16
+ };
17
+ exports.PanelComponent = PanelComponent;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.ALL_PLUGIN_TYPES = void 0;
16
+ /**
17
+ * All supported plugin type values as an array for use at runtime.
18
+ */
19
+ exports.ALL_PLUGIN_TYPES = ['Variable', 'Panel', 'GraphQuery'];
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useVariableOptions = void 0;
16
+ const PluginLoadingBoundary_1 = require("../components/PluginLoadingBoundary");
17
+ /**
18
+ * Use the variable options from a variable plugin at runtime.
19
+ */
20
+ const useVariableOptions = (definition) => {
21
+ const plugin = (0, PluginLoadingBoundary_1.usePlugin)('Variable', definition);
22
+ if (plugin === undefined) {
23
+ // Provide default values while the plugin is being loaded
24
+ return { data: [], loading: true };
25
+ }
26
+ return plugin.useVariableOptions(definition);
27
+ };
28
+ exports.useVariableOptions = useVariableOptions;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useDatasources = exports.DatasourcesContext = void 0;
16
+ const react_1 = require("react");
17
+ exports.DatasourcesContext = (0, react_1.createContext)(undefined);
18
+ /**
19
+ * Gets the Datasources at runtime.
20
+ */
21
+ function useDatasources() {
22
+ const ctx = (0, react_1.useContext)(exports.DatasourcesContext);
23
+ if (ctx === undefined) {
24
+ throw new Error('No DatasourcesContext found. Did you forget a Provider?');
25
+ }
26
+ return ctx;
27
+ }
28
+ exports.useDatasources = useDatasources;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./datasources"), exports);
30
+ __exportStar(require("./template-variables"), exports);
31
+ __exportStar(require("./time-range"), exports);
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useTemplateVariables = exports.TemplateVariablesContext = void 0;
16
+ const react_1 = require("react");
17
+ exports.TemplateVariablesContext = (0, react_1.createContext)(undefined);
18
+ /**
19
+ * Gets TemplateVariables at runtime.
20
+ */
21
+ function useTemplateVariables() {
22
+ const ctx = (0, react_1.useContext)(exports.TemplateVariablesContext);
23
+ if (ctx === undefined) {
24
+ throw new Error('No TemplateVariablesContext found. Did you forget a Provider?');
25
+ }
26
+ return ctx;
27
+ }
28
+ exports.useTemplateVariables = useTemplateVariables;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // Copyright 2021 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useTimeRange = exports.TimeRangeContext = void 0;
16
+ const react_1 = require("react");
17
+ exports.TimeRangeContext = (0, react_1.createContext)(undefined);
18
+ /**
19
+ * Gets the current TimeRange at runtime.
20
+ */
21
+ function useTimeRange() {
22
+ const ctx = (0, react_1.useContext)(exports.TimeRangeContext);
23
+ if (ctx === undefined) {
24
+ throw new Error('No TimeRangeContext found. Did you forget a Provider?');
25
+ }
26
+ return ctx;
27
+ }
28
+ exports.useTimeRange = useTimeRange;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perses-dev/plugin-system",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "The plugin feature in Pereses",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/perses/perses/blob/main/README.md",
@@ -12,17 +12,19 @@
12
12
  "url": "https://github.com/perses/perses/issues"
13
13
  },
14
14
  "module": "dist/index.js",
15
+ "main": "dist/cjs/index.js",
15
16
  "types": "dist/index.d.ts",
16
17
  "scripts": {
17
18
  "clean": "rimraf dist/",
18
- "build": "tsc",
19
+ "build": "tsc --build",
20
+ "build:cjs": "tsc --project ./tsconfig.cjs.json",
19
21
  "test": "echo 'no test to run' && exit 0",
20
22
  "lint": "eslint src --ext .ts,.tsx",
21
23
  "lint:fix": "eslint --fix src --ext .ts,.tsx"
22
24
  },
23
25
  "dependencies": {
24
- "@perses-dev/components": "^0.3.0",
25
- "@perses-dev/core": "^0.3.0",
26
+ "@perses-dev/components": "^0.3.1",
27
+ "@perses-dev/core": "^0.3.1",
26
28
  "immer": "^9.0.12",
27
29
  "use-immer": "^0.6.0"
28
30
  },