@backstage/frontend-plugin-api 0.0.0
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.
- package/CHANGELOG.md +9 -0
- package/README.md +9 -0
- package/dist/index.d.ts +181 -0
- package/dist/index.esm.js +148 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +47 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @backstage/frontend-plugin-api
|
|
2
|
+
|
|
3
|
+
**This package is EXPERIMENTAL, we recommend against using it for production deployments**
|
|
4
|
+
|
|
5
|
+
This package provides the core API used by Backstage frontend plugins. It implements the design outlined in [RFC: Frontend System Evolution](https://github.com/backstage/backstage/issues/18372).
|
|
6
|
+
|
|
7
|
+
## Documentation
|
|
8
|
+
|
|
9
|
+
- [Backstage Documentation](https://backstage.io/docs)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import React, { ComponentType, ReactNode } from 'react';
|
|
3
|
+
import { JsonObject } from '@backstage/types';
|
|
4
|
+
import { AnyApiFactory, RouteRef, AnyApiRef } from '@backstage/core-plugin-api';
|
|
5
|
+
import { z, ZodSchema, ZodTypeDef } from 'zod';
|
|
6
|
+
|
|
7
|
+
/** @public */
|
|
8
|
+
type ExtensionDataRef<TData, TConfig extends {
|
|
9
|
+
optional?: true;
|
|
10
|
+
} = {}> = {
|
|
11
|
+
id: string;
|
|
12
|
+
T: TData;
|
|
13
|
+
config: TConfig;
|
|
14
|
+
$$type: 'extension-data';
|
|
15
|
+
};
|
|
16
|
+
/** @public */
|
|
17
|
+
interface ConfigurableExtensionDataRef<TData, TConfig extends {
|
|
18
|
+
optional?: true;
|
|
19
|
+
} = {}> extends ExtensionDataRef<TData, TConfig> {
|
|
20
|
+
optional(): ConfigurableExtensionDataRef<TData, TData & {
|
|
21
|
+
optional: true;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
24
|
+
/** @public */
|
|
25
|
+
declare function createExtensionDataRef<TData>(id: string): ConfigurableExtensionDataRef<TData>;
|
|
26
|
+
|
|
27
|
+
/** @public */
|
|
28
|
+
declare const coreExtensionData: {
|
|
29
|
+
reactComponent: ConfigurableExtensionDataRef<ComponentType<{}>, {}>;
|
|
30
|
+
routePath: ConfigurableExtensionDataRef<string, {}>;
|
|
31
|
+
apiFactory: ConfigurableExtensionDataRef<AnyApiFactory, {}>;
|
|
32
|
+
routeRef: ConfigurableExtensionDataRef<RouteRef<any>, {}>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** @public */
|
|
36
|
+
type PortableSchema<TOutput> = {
|
|
37
|
+
parse: (input: unknown) => TOutput;
|
|
38
|
+
schema: JsonObject;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** @public */
|
|
42
|
+
declare function createSchemaFromZod<TOutput, TInput>(schemaCreator: (zImpl: typeof z) => ZodSchema<TOutput, ZodTypeDef, TInput>): PortableSchema<TOutput>;
|
|
43
|
+
|
|
44
|
+
/** @public */
|
|
45
|
+
type AnyExtensionDataMap = {
|
|
46
|
+
[name in string]: ExtensionDataRef<any, any>;
|
|
47
|
+
};
|
|
48
|
+
/** @public */
|
|
49
|
+
interface Extension<TConfig> {
|
|
50
|
+
$$type: 'extension';
|
|
51
|
+
id: string;
|
|
52
|
+
at: string;
|
|
53
|
+
disabled: boolean;
|
|
54
|
+
inputs: Record<string, {
|
|
55
|
+
extensionData: AnyExtensionDataMap;
|
|
56
|
+
}>;
|
|
57
|
+
output: AnyExtensionDataMap;
|
|
58
|
+
configSchema?: PortableSchema<TConfig>;
|
|
59
|
+
factory(options: {
|
|
60
|
+
source?: BackstagePlugin;
|
|
61
|
+
bind: ExtensionDataBind<AnyExtensionDataMap>;
|
|
62
|
+
config: TConfig;
|
|
63
|
+
inputs: Record<string, Array<Record<string, unknown>>>;
|
|
64
|
+
}): void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @public */
|
|
68
|
+
interface PluginOptions {
|
|
69
|
+
id: string;
|
|
70
|
+
extensions?: Extension<unknown>[];
|
|
71
|
+
}
|
|
72
|
+
/** @public */
|
|
73
|
+
interface BackstagePlugin {
|
|
74
|
+
$$type: 'backstage-plugin';
|
|
75
|
+
id: string;
|
|
76
|
+
extensions: Extension<unknown>[];
|
|
77
|
+
}
|
|
78
|
+
/** @public */
|
|
79
|
+
declare function createPlugin(options: PluginOptions): BackstagePlugin;
|
|
80
|
+
|
|
81
|
+
/** @public */
|
|
82
|
+
type ExtensionDataInputValues<TInputs extends {
|
|
83
|
+
[name in string]: {
|
|
84
|
+
extensionData: AnyExtensionDataMap;
|
|
85
|
+
};
|
|
86
|
+
}> = {
|
|
87
|
+
[InputName in keyof TInputs]: Array<{
|
|
88
|
+
[DataName in keyof TInputs[InputName]['extensionData'] as TInputs[InputName]['extensionData'][DataName]['config'] extends {
|
|
89
|
+
optional: true;
|
|
90
|
+
} ? never : DataName]: TInputs[InputName]['extensionData'][DataName]['T'];
|
|
91
|
+
} & {
|
|
92
|
+
[DataName in keyof TInputs[InputName]['extensionData'] as TInputs[InputName]['extensionData'][DataName]['config'] extends {
|
|
93
|
+
optional: true;
|
|
94
|
+
} ? DataName : never]?: TInputs[InputName]['extensionData'][DataName]['T'];
|
|
95
|
+
}>;
|
|
96
|
+
};
|
|
97
|
+
/** @public */
|
|
98
|
+
type ExtensionDataBind<TMap extends AnyExtensionDataMap> = (values: {
|
|
99
|
+
[DataName in keyof TMap as TMap[DataName]['config'] extends {
|
|
100
|
+
optional: true;
|
|
101
|
+
} ? never : DataName]: TMap[DataName]['T'];
|
|
102
|
+
} & {
|
|
103
|
+
[DataName in keyof TMap as TMap[DataName]['config'] extends {
|
|
104
|
+
optional: true;
|
|
105
|
+
} ? DataName : never]?: TMap[DataName]['T'];
|
|
106
|
+
}) => void;
|
|
107
|
+
/** @public */
|
|
108
|
+
interface CreateExtensionOptions<TOutput extends AnyExtensionDataMap, TInputs extends Record<string, {
|
|
109
|
+
extensionData: AnyExtensionDataMap;
|
|
110
|
+
}>, TConfig> {
|
|
111
|
+
id: string;
|
|
112
|
+
at: string;
|
|
113
|
+
disabled?: boolean;
|
|
114
|
+
inputs?: TInputs;
|
|
115
|
+
output: TOutput;
|
|
116
|
+
configSchema?: PortableSchema<TConfig>;
|
|
117
|
+
factory(options: {
|
|
118
|
+
source?: BackstagePlugin;
|
|
119
|
+
bind: ExtensionDataBind<TOutput>;
|
|
120
|
+
config: TConfig;
|
|
121
|
+
inputs: ExtensionDataInputValues<TInputs>;
|
|
122
|
+
}): void;
|
|
123
|
+
}
|
|
124
|
+
/** @public */
|
|
125
|
+
declare function createExtension<TOutput extends AnyExtensionDataMap, TInputs extends Record<string, {
|
|
126
|
+
extensionData: AnyExtensionDataMap;
|
|
127
|
+
}>, TConfig = never>(options: CreateExtensionOptions<TOutput, TInputs, TConfig>): Extension<TConfig>;
|
|
128
|
+
|
|
129
|
+
/** @public */
|
|
130
|
+
interface ExtensionBoundaryProps {
|
|
131
|
+
children: ReactNode;
|
|
132
|
+
source?: BackstagePlugin;
|
|
133
|
+
}
|
|
134
|
+
/** @public */
|
|
135
|
+
declare function ExtensionBoundary(props: ExtensionBoundaryProps): React.JSX.Element;
|
|
136
|
+
|
|
137
|
+
/** @public */
|
|
138
|
+
declare function createApiExtension<TConfig extends {}, TInputs extends Record<string, {
|
|
139
|
+
extensionData: AnyExtensionDataMap;
|
|
140
|
+
}>>(options: ({
|
|
141
|
+
api: AnyApiRef;
|
|
142
|
+
factory: (options: {
|
|
143
|
+
config: TConfig;
|
|
144
|
+
inputs: ExtensionDataInputValues<TInputs>;
|
|
145
|
+
}) => AnyApiFactory;
|
|
146
|
+
} | {
|
|
147
|
+
factory: AnyApiFactory;
|
|
148
|
+
}) & {
|
|
149
|
+
configSchema?: PortableSchema<TConfig>;
|
|
150
|
+
inputs?: TInputs;
|
|
151
|
+
}): Extension<TConfig>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Helper for creating extensions for a routable React page component.
|
|
155
|
+
*
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
declare function createPageExtension<TConfig extends {
|
|
159
|
+
path: string;
|
|
160
|
+
}, TInputs extends Record<string, {
|
|
161
|
+
extensionData: AnyExtensionDataMap;
|
|
162
|
+
}>>(options: ({
|
|
163
|
+
defaultPath: string;
|
|
164
|
+
} | {
|
|
165
|
+
configSchema: PortableSchema<TConfig>;
|
|
166
|
+
}) & {
|
|
167
|
+
id: string;
|
|
168
|
+
at?: string;
|
|
169
|
+
disabled?: boolean;
|
|
170
|
+
inputs?: TInputs;
|
|
171
|
+
routeRef?: RouteRef;
|
|
172
|
+
component: (props: {
|
|
173
|
+
config: TConfig;
|
|
174
|
+
inputs: ExtensionDataInputValues<TInputs>;
|
|
175
|
+
}) => Promise<JSX.Element>;
|
|
176
|
+
}): Extension<TConfig>;
|
|
177
|
+
|
|
178
|
+
/** @public */
|
|
179
|
+
declare function useRouteRef(routeRef: RouteRef<any>): () => string;
|
|
180
|
+
|
|
181
|
+
export { AnyExtensionDataMap, BackstagePlugin, ConfigurableExtensionDataRef, CreateExtensionOptions, Extension, ExtensionBoundary, ExtensionBoundaryProps, ExtensionDataBind, ExtensionDataInputValues, ExtensionDataRef, PluginOptions, PortableSchema, coreExtensionData, createApiExtension, createExtension, createExtensionDataRef, createPageExtension, createPlugin, createSchemaFromZod, useRouteRef };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import React, { useContext, useMemo } from 'react';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import zodToJsonSchema from 'zod-to-json-schema';
|
|
4
|
+
import { RoutingContext } from '@backstage/frontend-app-api/src/routing/RoutingContext';
|
|
5
|
+
import { useLocation } from 'react-router-dom';
|
|
6
|
+
|
|
7
|
+
function ExtensionBoundary(props) {
|
|
8
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, props.children);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function createExtensionDataRef(id) {
|
|
12
|
+
return {
|
|
13
|
+
id,
|
|
14
|
+
$$type: "extension-data",
|
|
15
|
+
config: {},
|
|
16
|
+
optional() {
|
|
17
|
+
return { ...this, config: { ...this.config, optional: true } };
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const coreExtensionData = {
|
|
23
|
+
reactComponent: createExtensionDataRef("core.reactComponent"),
|
|
24
|
+
routePath: createExtensionDataRef("core.routing.path"),
|
|
25
|
+
apiFactory: createExtensionDataRef("core.api.factory"),
|
|
26
|
+
routeRef: createExtensionDataRef("core.routing.ref")
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function createExtension(options) {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
return {
|
|
32
|
+
...options,
|
|
33
|
+
disabled: (_a = options.disabled) != null ? _a : false,
|
|
34
|
+
$$type: "extension",
|
|
35
|
+
inputs: (_b = options.inputs) != null ? _b : {},
|
|
36
|
+
factory({ bind, config, inputs }) {
|
|
37
|
+
return options.factory({
|
|
38
|
+
bind,
|
|
39
|
+
config,
|
|
40
|
+
inputs
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createPlugin(options) {
|
|
47
|
+
var _a;
|
|
48
|
+
return {
|
|
49
|
+
...options,
|
|
50
|
+
$$type: "backstage-plugin",
|
|
51
|
+
extensions: (_a = options.extensions) != null ? _a : []
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function createApiExtension(options) {
|
|
56
|
+
const { factory, configSchema, inputs: extensionInputs } = options;
|
|
57
|
+
const apiRef = "api" in options ? options.api : factory.api;
|
|
58
|
+
return createExtension({
|
|
59
|
+
id: `apis.${apiRef.id}`,
|
|
60
|
+
at: "core/apis",
|
|
61
|
+
inputs: extensionInputs,
|
|
62
|
+
configSchema,
|
|
63
|
+
output: {
|
|
64
|
+
api: coreExtensionData.apiFactory
|
|
65
|
+
},
|
|
66
|
+
factory({ bind, config, inputs }) {
|
|
67
|
+
if (typeof factory === "function") {
|
|
68
|
+
bind({ api: factory({ config, inputs }) });
|
|
69
|
+
} else {
|
|
70
|
+
bind({ api: factory });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function createSchemaFromZod(schemaCreator) {
|
|
77
|
+
const schema = schemaCreator(z);
|
|
78
|
+
return {
|
|
79
|
+
// TODO: Types allow z.array etc here but it will break stuff
|
|
80
|
+
parse: (input) => {
|
|
81
|
+
const result = schema.safeParse(input);
|
|
82
|
+
if (result.success) {
|
|
83
|
+
return result.data;
|
|
84
|
+
}
|
|
85
|
+
throw new Error(result.error.issues.map(formatIssue).join("; "));
|
|
86
|
+
},
|
|
87
|
+
// TODO: Verify why we are not compatible with the latest zodToJsonSchema.
|
|
88
|
+
schema: zodToJsonSchema(schema)
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function formatIssue(issue) {
|
|
92
|
+
if (issue.code === "invalid_union") {
|
|
93
|
+
return formatIssue(issue.unionErrors[0].issues[0]);
|
|
94
|
+
}
|
|
95
|
+
let message = issue.message;
|
|
96
|
+
if (message === "Required") {
|
|
97
|
+
message = `Missing required value`;
|
|
98
|
+
}
|
|
99
|
+
if (issue.path.length) {
|
|
100
|
+
message += ` at '${issue.path.join(".")}'`;
|
|
101
|
+
}
|
|
102
|
+
return message;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function createPageExtension(options) {
|
|
106
|
+
var _a;
|
|
107
|
+
const configSchema = "configSchema" in options ? options.configSchema : createSchemaFromZod(
|
|
108
|
+
(z) => z.object({ path: z.string().default(options.defaultPath) })
|
|
109
|
+
);
|
|
110
|
+
return createExtension({
|
|
111
|
+
id: options.id,
|
|
112
|
+
at: (_a = options.at) != null ? _a : "core.router/routes",
|
|
113
|
+
disabled: options.disabled,
|
|
114
|
+
output: {
|
|
115
|
+
component: coreExtensionData.reactComponent,
|
|
116
|
+
path: coreExtensionData.routePath,
|
|
117
|
+
routeRef: coreExtensionData.routeRef.optional()
|
|
118
|
+
},
|
|
119
|
+
inputs: options.inputs,
|
|
120
|
+
configSchema,
|
|
121
|
+
factory({ bind, config, inputs, source }) {
|
|
122
|
+
const LazyComponent = React.lazy(
|
|
123
|
+
() => options.component({ config, inputs }).then((element) => ({ default: () => element }))
|
|
124
|
+
);
|
|
125
|
+
bind({
|
|
126
|
+
path: config.path,
|
|
127
|
+
component: () => /* @__PURE__ */ React.createElement(ExtensionBoundary, { source }, /* @__PURE__ */ React.createElement(React.Suspense, { fallback: "..." }, /* @__PURE__ */ React.createElement(LazyComponent, null))),
|
|
128
|
+
routeRef: options.routeRef
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function useRouteRef(routeRef) {
|
|
135
|
+
const { pathname } = useLocation();
|
|
136
|
+
const resolver = useContext(RoutingContext);
|
|
137
|
+
const routeFunc = useMemo(
|
|
138
|
+
() => resolver && resolver.resolve(routeRef, { pathname }),
|
|
139
|
+
[resolver, routeRef, pathname]
|
|
140
|
+
);
|
|
141
|
+
if (!routeFunc) {
|
|
142
|
+
throw new Error(`Failed to resolve routeRef ${routeRef}`);
|
|
143
|
+
}
|
|
144
|
+
return routeFunc;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export { ExtensionBoundary, coreExtensionData, createApiExtension, createExtension, createExtensionDataRef, createPageExtension, createPlugin, createSchemaFromZod, useRouteRef };
|
|
148
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/components/ExtensionBoundary.tsx","../src/wiring/createExtensionDataRef.ts","../src/wiring/coreExtensionData.ts","../src/wiring/createExtension.ts","../src/wiring/createPlugin.ts","../src/extensions/createApiExtension.ts","../src/schema/createSchemaFromZod.ts","../src/extensions/createPageExtension.tsx","../src/routing/useRouteRef.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ReactNode } from 'react';\nimport { BackstagePlugin } from '../wiring';\n\n/** @public */\nexport interface ExtensionBoundaryProps {\n children: ReactNode;\n source?: BackstagePlugin;\n}\n\n/** @public */\nexport function ExtensionBoundary(props: ExtensionBoundaryProps) {\n return <>{props.children}</>;\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/** @public */\nexport type ExtensionDataRef<\n TData,\n TConfig extends { optional?: true } = {},\n> = {\n id: string;\n T: TData;\n config: TConfig;\n $$type: 'extension-data';\n};\n\n/** @public */\nexport interface ConfigurableExtensionDataRef<\n TData,\n TConfig extends { optional?: true } = {},\n> extends ExtensionDataRef<TData, TConfig> {\n optional(): ConfigurableExtensionDataRef<TData, TData & { optional: true }>;\n}\n\n// TODO: change to options object with ID.\n/** @public */\nexport function createExtensionDataRef<TData>(\n id: string,\n): ConfigurableExtensionDataRef<TData> {\n return {\n id,\n $$type: 'extension-data',\n config: {},\n optional() {\n return { ...this, config: { ...this.config, optional: true } };\n },\n } as ConfigurableExtensionDataRef<TData>;\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AnyApiFactory, RouteRef } from '@backstage/core-plugin-api';\nimport { ComponentType } from 'react';\nimport { createExtensionDataRef } from './createExtensionDataRef';\n\n/** @public */\nexport const coreExtensionData = {\n reactComponent: createExtensionDataRef<ComponentType>('core.reactComponent'),\n routePath: createExtensionDataRef<string>('core.routing.path'),\n apiFactory: createExtensionDataRef<AnyApiFactory>('core.api.factory'),\n routeRef: createExtensionDataRef<RouteRef>('core.routing.ref'),\n};\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PortableSchema } from '../schema';\nimport { BackstagePlugin } from './createPlugin';\nimport { AnyExtensionDataMap, Extension } from './types';\n\n/** @public */\nexport type ExtensionDataInputValues<\n TInputs extends { [name in string]: { extensionData: AnyExtensionDataMap } },\n> = {\n [InputName in keyof TInputs]: Array<\n {\n [DataName in keyof TInputs[InputName]['extensionData'] as TInputs[InputName]['extensionData'][DataName]['config'] extends {\n optional: true;\n }\n ? never\n : DataName]: TInputs[InputName]['extensionData'][DataName]['T'];\n } & {\n [DataName in keyof TInputs[InputName]['extensionData'] as TInputs[InputName]['extensionData'][DataName]['config'] extends {\n optional: true;\n }\n ? DataName\n : never]?: TInputs[InputName]['extensionData'][DataName]['T'];\n }\n >;\n};\n\n/** @public */\nexport type ExtensionDataBind<TMap extends AnyExtensionDataMap> = (\n values: {\n [DataName in keyof TMap as TMap[DataName]['config'] extends {\n optional: true;\n }\n ? never\n : DataName]: TMap[DataName]['T'];\n } & {\n [DataName in keyof TMap as TMap[DataName]['config'] extends {\n optional: true;\n }\n ? DataName\n : never]?: TMap[DataName]['T'];\n },\n) => void;\n\n/** @public */\nexport interface CreateExtensionOptions<\n TOutput extends AnyExtensionDataMap,\n TInputs extends Record<string, { extensionData: AnyExtensionDataMap }>,\n TConfig,\n> {\n id: string;\n at: string;\n disabled?: boolean;\n inputs?: TInputs;\n output: TOutput;\n configSchema?: PortableSchema<TConfig>;\n factory(options: {\n source?: BackstagePlugin;\n bind: ExtensionDataBind<TOutput>;\n config: TConfig;\n inputs: ExtensionDataInputValues<TInputs>;\n }): void;\n}\n\n/** @public */\nexport function createExtension<\n TOutput extends AnyExtensionDataMap,\n TInputs extends Record<string, { extensionData: AnyExtensionDataMap }>,\n TConfig = never,\n>(\n options: CreateExtensionOptions<TOutput, TInputs, TConfig>,\n): Extension<TConfig> {\n return {\n ...options,\n disabled: options.disabled ?? false,\n $$type: 'extension',\n inputs: options.inputs ?? {},\n factory({ bind, config, inputs }) {\n // TODO: Simplify this, but TS wouldn't infer the input type for some reason\n return options.factory({\n bind,\n config,\n inputs: inputs as ExtensionDataInputValues<TInputs>,\n });\n },\n };\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Extension } from './types';\n\n/** @public */\nexport interface PluginOptions {\n id: string;\n extensions?: Extension<unknown>[];\n}\n\n/** @public */\nexport interface BackstagePlugin {\n $$type: 'backstage-plugin';\n id: string;\n extensions: Extension<unknown>[];\n}\n\n/** @public */\nexport function createPlugin(options: PluginOptions): BackstagePlugin {\n return {\n ...options,\n $$type: 'backstage-plugin',\n extensions: options.extensions ?? [],\n };\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AnyApiFactory, AnyApiRef } from '@backstage/core-plugin-api';\nimport { PortableSchema } from '../schema';\nimport {\n AnyExtensionDataMap,\n ExtensionDataInputValues,\n createExtension,\n coreExtensionData,\n} from '../wiring';\n\n/** @public */\nexport function createApiExtension<\n TConfig extends {},\n TInputs extends Record<string, { extensionData: AnyExtensionDataMap }>,\n>(\n options: (\n | {\n api: AnyApiRef;\n factory: (options: {\n config: TConfig;\n inputs: ExtensionDataInputValues<TInputs>;\n }) => AnyApiFactory;\n }\n | {\n factory: AnyApiFactory;\n }\n ) & {\n configSchema?: PortableSchema<TConfig>;\n inputs?: TInputs;\n },\n) {\n const { factory, configSchema, inputs: extensionInputs } = options;\n\n const apiRef =\n 'api' in options ? options.api : (factory as { api: AnyApiRef }).api;\n\n return createExtension({\n id: `apis.${apiRef.id}`,\n at: 'core/apis',\n inputs: extensionInputs,\n configSchema,\n output: {\n api: coreExtensionData.apiFactory,\n },\n factory({ bind, config, inputs }) {\n if (typeof factory === 'function') {\n bind({ api: factory({ config, inputs }) });\n } else {\n bind({ api: factory });\n }\n },\n });\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { JsonObject } from '@backstage/types';\nimport { z, ZodSchema, ZodTypeDef } from 'zod';\nimport zodToJsonSchema from 'zod-to-json-schema';\nimport { PortableSchema } from './types';\n\n/** @public */\nexport function createSchemaFromZod<TOutput, TInput>(\n schemaCreator: (zImpl: typeof z) => ZodSchema<TOutput, ZodTypeDef, TInput>,\n): PortableSchema<TOutput> {\n const schema = schemaCreator(z);\n return {\n // TODO: Types allow z.array etc here but it will break stuff\n parse: input => {\n const result = schema.safeParse(input);\n if (result.success) {\n return result.data;\n }\n\n throw new Error(result.error.issues.map(formatIssue).join('; '));\n },\n // TODO: Verify why we are not compatible with the latest zodToJsonSchema.\n schema: zodToJsonSchema(schema) as JsonObject,\n };\n}\n\nfunction formatIssue(issue: z.ZodIssue): string {\n if (issue.code === 'invalid_union') {\n return formatIssue(issue.unionErrors[0].issues[0]);\n }\n let message = issue.message;\n if (message === 'Required') {\n message = `Missing required value`;\n }\n if (issue.path.length) {\n message += ` at '${issue.path.join('.')}'`;\n }\n return message;\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { RouteRef } from '@backstage/core-plugin-api';\nimport React from 'react';\nimport { ExtensionBoundary } from '../components';\nimport { createSchemaFromZod, PortableSchema } from '../schema';\nimport {\n AnyExtensionDataMap,\n coreExtensionData,\n createExtension,\n Extension,\n ExtensionDataInputValues,\n} from '../wiring';\n\n/**\n * Helper for creating extensions for a routable React page component.\n *\n * @public\n */\nexport function createPageExtension<\n TConfig extends { path: string },\n TInputs extends Record<string, { extensionData: AnyExtensionDataMap }>,\n>(\n options: (\n | {\n defaultPath: string;\n }\n | {\n configSchema: PortableSchema<TConfig>;\n }\n ) & {\n id: string;\n at?: string;\n disabled?: boolean;\n inputs?: TInputs;\n routeRef?: RouteRef;\n component: (props: {\n config: TConfig;\n inputs: ExtensionDataInputValues<TInputs>;\n }) => Promise<JSX.Element>;\n },\n): Extension<TConfig> {\n const configSchema =\n 'configSchema' in options\n ? options.configSchema\n : (createSchemaFromZod(z =>\n z.object({ path: z.string().default(options.defaultPath) }),\n ) as PortableSchema<TConfig>);\n\n return createExtension({\n id: options.id,\n at: options.at ?? 'core.router/routes',\n disabled: options.disabled,\n output: {\n component: coreExtensionData.reactComponent,\n path: coreExtensionData.routePath,\n routeRef: coreExtensionData.routeRef.optional(),\n },\n inputs: options.inputs,\n configSchema,\n factory({ bind, config, inputs, source }) {\n const LazyComponent = React.lazy(() =>\n options\n .component({ config, inputs })\n .then(element => ({ default: () => element })),\n );\n\n bind({\n path: config.path,\n component: () => (\n <ExtensionBoundary source={source}>\n <React.Suspense fallback=\"...\">\n <LazyComponent />\n </React.Suspense>\n </ExtensionBoundary>\n ),\n routeRef: options.routeRef,\n });\n },\n });\n}\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { RouteRef } from '@backstage/core-plugin-api';\n// eslint-disable-next-line @backstage/no-forbidden-package-imports\nimport { RoutingContext } from '@backstage/frontend-app-api/src/routing/RoutingContext';\nimport { useContext, useMemo } from 'react';\nimport { useLocation } from 'react-router-dom';\n\n/** @public */\nexport function useRouteRef(routeRef: RouteRef<any>): () => string {\n const { pathname } = useLocation();\n const resolver = useContext(RoutingContext);\n\n const routeFunc = useMemo(\n () => resolver && resolver.resolve(routeRef, { pathname }),\n [resolver, routeRef, pathname],\n );\n\n if (!routeFunc) {\n throw new Error(`Failed to resolve routeRef ${routeRef}`);\n }\n\n return routeFunc;\n}\n"],"names":[],"mappings":";;;;;;AA0BO,SAAS,kBAAkB,KAA+B,EAAA;AAC/D,EAAO,uBAAA,KAAA,CAAA,aAAA,CAAA,KAAA,CAAA,QAAA,EAAA,IAAA,EAAG,MAAM,QAAS,CAAA,CAAA;AAC3B;;ACSO,SAAS,uBACd,EACqC,EAAA;AACrC,EAAO,OAAA;AAAA,IACL,EAAA;AAAA,IACA,MAAQ,EAAA,gBAAA;AAAA,IACR,QAAQ,EAAC;AAAA,IACT,QAAW,GAAA;AACT,MAAO,OAAA,EAAE,GAAG,IAAA,EAAM,MAAQ,EAAA,EAAE,GAAG,IAAK,CAAA,MAAA,EAAQ,QAAU,EAAA,IAAA,EAAO,EAAA,CAAA;AAAA,KAC/D;AAAA,GACF,CAAA;AACF;;AC3BO,MAAM,iBAAoB,GAAA;AAAA,EAC/B,cAAA,EAAgB,uBAAsC,qBAAqB,CAAA;AAAA,EAC3E,SAAA,EAAW,uBAA+B,mBAAmB,CAAA;AAAA,EAC7D,UAAA,EAAY,uBAAsC,kBAAkB,CAAA;AAAA,EACpE,QAAA,EAAU,uBAAiC,kBAAkB,CAAA;AAC/D;;ACqDO,SAAS,gBAKd,OACoB,EAAA;AArFtB,EAAA,IAAA,EAAA,EAAA,EA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ACjD,CAAA;AAEA,MAAK,IAAA,CAAA;AAAA,QACH,MAAM,MAAO,CAAA,IAAA;AAAA,QACb,SAAW,EAAA,sBACR,KAAA,CAAA,aAAA,CAAA,iBAAA,EAAA,EAAkB,0BAChB,KAAA,CAAA,aAAA,CAAA,KAAA,CAAM,QAAN,EAAA,EAAe,QAAS,EAAA,KAAA,EAAA,kBACtB,KAAA,CAAA,aAAA,CAAA,aAAA,EAAA,IAAc,CACjB,CACF,CAAA;AAAA,QAEF,UAAU,OAAQ,CAAA,QAAA;AAAA,OACnB,CAAA,CAAA;AAAA,KACH;AAAA,GACD,CAAA,CAAA;AACH;;ACvEO,SAAS,YAAY,QAAuC,EAAA;AACjE,EAAM,MAAA,EAAE,QAAS,EAAA,GAAI,WAAY,EAAA,CAAA;AACjC,EAAM,MAAA,QAAA,GAAW,WAAW,cAAc,CAAA,CAAA;AAE1C,EAAA,MAAM,SAAY,GAAA,OAAA;AAAA,IAChB,MAAM,QAAY,IAAA,QAAA,CAAS,QAAQ,QAAU,EAAA,EAAE,UAAU,CAAA;AAAA,IACzD,CAAC,QAAU,EAAA,QAAA,EAAU,QAAQ,CAAA;AAAA,GAC/B,CAAA;AAEA,EAAA,IAAI,CAAC,SAAW,EAAA;AACd,IAAA,MAAM,IAAI,KAAA,CAAM,CAA8B,2BAAA,EAAA,QAAQ,CAAE,CAAA,CAAA,CAAA;AAAA,GAC1D;AAEA,EAAO,OAAA,SAAA,CAAA;AACT;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@backstage/frontend-plugin-api",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"main": "dist/index.esm.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public",
|
|
9
|
+
"main": "dist/index.esm.js",
|
|
10
|
+
"types": "dist/index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"backstage": {
|
|
13
|
+
"role": "web-library"
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"scripts": {
|
|
17
|
+
"start": "backstage-cli package start",
|
|
18
|
+
"build": "backstage-cli package build",
|
|
19
|
+
"lint": "backstage-cli package lint",
|
|
20
|
+
"test": "backstage-cli package test",
|
|
21
|
+
"clean": "backstage-cli package clean",
|
|
22
|
+
"prepack": "backstage-cli package prepack",
|
|
23
|
+
"postpack": "backstage-cli package postpack"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@backstage/cli": "^0.22.13-next.2",
|
|
27
|
+
"@backstage/frontend-app-api": "^0.0.0",
|
|
28
|
+
"@testing-library/jest-dom": "^5.10.1",
|
|
29
|
+
"@testing-library/react": "^12.1.3"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^16.13.1 || ^17.0.0",
|
|
36
|
+
"react-router-dom": "6.0.0-beta.0 || ^6.3.0"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@backstage/core-plugin-api": "^1.6.0-next.2",
|
|
40
|
+
"@backstage/types": "^1.1.0",
|
|
41
|
+
"@types/react": "^16.13.1 || ^17.0.0",
|
|
42
|
+
"lodash": "^4.17.21",
|
|
43
|
+
"zod": "^3.21.4",
|
|
44
|
+
"zod-to-json-schema": "^3.21.4"
|
|
45
|
+
},
|
|
46
|
+
"module": "./dist/index.esm.js"
|
|
47
|
+
}
|