frontend-plugin-api: remove deprecated extension creators

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-08-19 14:58:09 +02:00
parent f010b2cafc
commit 7fb8102c0c
19 changed files with 0 additions and 1615 deletions
@@ -1,92 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createApiExtension } from './createApiExtension';
import { createApiFactory, createApiRef } from '@backstage/core-plugin-api';
describe('createApiExtension', () => {
it('fills in the expected values for an existing factory', () => {
const api = createApiRef<{ foo: string }>({ id: 'test' });
const factory = createApiFactory({
api,
deps: {},
factory: () => ({ foo: 'bar' }),
});
expect(
createApiExtension({
factory,
}),
).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'api',
namespace: 'test',
attachTo: { id: 'app', input: 'apis' },
disabled: false,
configSchema: undefined,
inputs: {},
output: {
api: expect.objectContaining({
$$type: '@backstage/ExtensionDataRef',
id: 'core.api.factory',
config: {},
}),
},
factory: expect.any(Function),
toString: expect.any(Function),
override: expect.any(Function),
});
});
it('fills in the expected values for a ref and custom factory', () => {
const api = createApiRef<{ foo: string }>({ id: 'test' });
const factory = jest.fn(() => ({ foo: 'bar' }));
const extension = createApiExtension({
api,
inputs: {},
factory({ config: _config, inputs: _inputs }) {
return createApiFactory({
api,
deps: {},
factory,
});
},
});
// boo
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'api',
namespace: 'test',
attachTo: { id: 'app', input: 'apis' },
disabled: false,
configSchema: undefined,
inputs: {},
output: {
api: expect.objectContaining({
$$type: '@backstage/ExtensionDataRef',
id: 'core.api.factory',
config: {},
}),
},
factory: expect.any(Function),
toString: expect.any(Function),
override: expect.any(Function),
});
});
});
@@ -1,82 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AnyApiFactory, AnyApiRef } from '@backstage/core-plugin-api';
import { PortableSchema } from '../schema';
import { ResolvedExtensionInputs, createExtension } from '../wiring';
import { AnyExtensionInputMap } from '../wiring/createExtension';
import { Expand } from '../types';
import { ApiBlueprint } from '../blueprints/ApiBlueprint';
/**
* @public
* @deprecated Use {@link ApiBlueprint} instead.
*/
export function createApiExtension<
TConfig extends {},
TInputs extends AnyExtensionInputMap,
>(
options: (
| {
api: AnyApiRef;
factory: (options: {
config: TConfig;
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
}) => AnyApiFactory;
}
| {
factory: AnyApiFactory;
}
) & {
configSchema?: PortableSchema<TConfig>;
inputs?: TInputs;
},
) {
const { factory, configSchema, inputs: extensionInputs } = options;
const apiRef =
'api' in options ? options.api : (factory as { api: AnyApiRef }).api;
return createExtension({
kind: 'api',
// Since ApiRef IDs use a global namespace we use the namespace here in order to override
// potential plugin IDs and always end up with the format `api:<api-ref-id>`
namespace: apiRef.id,
attachTo: { id: 'app', input: 'apis' },
inputs: extensionInputs,
configSchema,
output: {
api: ApiBlueprint.dataRefs.factory,
},
factory({ config, inputs }) {
if (typeof factory === 'function') {
return { api: factory({ config, inputs }) };
}
return { api: factory };
},
});
}
/**
* @public
* @deprecated Use {@link ApiBlueprint} instead.
*/
export namespace createApiExtension {
/**
* @deprecated Use {@link ApiBlueprint} instead.
*/
export const factoryDataRef = ApiBlueprint.dataRefs.factory;
}
@@ -1,109 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createExtensionTester } from '@backstage/frontend-test-utils';
import { screen } from '@testing-library/react';
import React from 'react';
import { createSchemaFromZod } from '../schema/createSchemaFromZod';
import { coreExtensionData } from '../wiring/coreExtensionData';
import { createExtension } from '../wiring/createExtension';
import { createExtensionInput } from '../wiring/createExtensionInput';
import { createAppRootElementExtension } from './createAppRootElementExtension';
describe('createAppRootElementExtension', () => {
it('works with simple options and just an element', async () => {
const extension = createAppRootElementExtension({
element: <div>Hello</div>,
});
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'app-root-element',
attachTo: { id: 'app/root', input: 'elements' },
disabled: false,
inputs: {},
output: {
element: expect.anything(),
},
factory: expect.any(Function),
toString: expect.any(Function),
override: expect.any(Function),
});
createExtensionTester(extension).render();
await expect(screen.findByText('Hello')).resolves.toBeInTheDocument();
});
it('works with complex options and a callback', async () => {
const schema = createSchemaFromZod(z => z.object({ name: z.string() }));
const extension = createAppRootElementExtension({
namespace: 'ns',
name: 'test',
configSchema: schema,
attachTo: { id: 'other', input: 'slot' },
disabled: true,
inputs: {
children: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
element: ({ inputs, config }) => (
<div>
Hello, {config.name}, {inputs.children.length}
</div>
),
});
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'app-root-element',
namespace: 'ns',
name: 'test',
attachTo: { id: 'other', input: 'slot' },
configSchema: schema,
disabled: true,
inputs: {
children: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
output: {
element: expect.anything(),
},
factory: expect.any(Function),
toString: expect.any(Function),
override: expect.any(Function),
});
createExtensionTester(extension, { config: { name: 'Robin' } })
.add(
createExtension({
attachTo: { id: 'app-root-element:ns/test', input: 'children' },
output: { element: coreExtensionData.reactElement },
factory: () => ({ element: <div /> }),
}),
)
.render();
await expect(
screen.findByText('Hello, Robin, 1'),
).resolves.toBeInTheDocument();
});
});
@@ -1,72 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { JSX } from 'react';
import { PortableSchema } from '../schema/types';
import { Expand } from '../types';
import { coreExtensionData } from '../wiring/coreExtensionData';
import {
AnyExtensionInputMap,
ExtensionDefinition,
ResolvedExtensionInputs,
createExtension,
} from '../wiring/createExtension';
/**
* Creates an extension that renders a React element at the app root, outside of
* the app layout. This is useful for example for shared popups and similar.
*
* @public
* @deprecated Use {@link AppRootElementBlueprint} instead.
*/
export function createAppRootElementExtension<
TConfig extends {},
TInputs extends AnyExtensionInputMap,
>(options: {
namespace?: string;
name?: string;
attachTo?: { id: string; input: string };
configSchema?: PortableSchema<TConfig>;
disabled?: boolean;
inputs?: TInputs;
element:
| JSX.Element
| ((options: {
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
config: TConfig;
}) => JSX.Element);
}): ExtensionDefinition<TConfig> {
return createExtension({
kind: 'app-root-element',
namespace: options.namespace,
name: options.name,
attachTo: options.attachTo ?? { id: 'app/root', input: 'elements' },
configSchema: options.configSchema,
disabled: options.disabled,
inputs: options.inputs,
output: {
element: coreExtensionData.reactElement,
},
factory({ inputs, config }) {
return {
element:
typeof options.element === 'function'
? options.element({ inputs, config })
: options.element,
};
},
});
}
@@ -1,121 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createExtensionTester } from '@backstage/frontend-test-utils';
import { screen } from '@testing-library/react';
import React from 'react';
import { createSchemaFromZod } from '../schema/createSchemaFromZod';
import { coreExtensionData } from '../wiring/coreExtensionData';
import { createExtension } from '../wiring/createExtension';
import { createExtensionInput } from '../wiring/createExtensionInput';
import { createAppRootWrapperExtension } from './createAppRootWrapperExtension';
import { createPageExtension } from './createPageExtension';
describe('createAppRootWrapperExtension', () => {
it('works with simple options and no props', async () => {
const extension = createAppRootWrapperExtension({
Component: () => <div>Hello</div>,
});
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'app-root-wrapper',
attachTo: { id: 'app/root', input: 'wrappers' },
disabled: false,
inputs: {},
output: {
component: expect.anything(),
},
factory: expect.any(Function),
toString: expect.any(Function),
override: expect.any(Function),
});
createExtensionTester(
createPageExtension({
defaultPath: '/',
loader: async () => <div />,
}),
)
.add(extension)
.render();
await expect(screen.findByText('Hello')).resolves.toBeInTheDocument();
});
it('works with complex options and props', async () => {
const schema = createSchemaFromZod(z => z.object({ name: z.string() }));
const extension = createAppRootWrapperExtension({
namespace: 'ns',
name: 'test',
configSchema: schema,
disabled: true,
inputs: {
children: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
Component: ({ inputs, config, children }) => (
<div data-testid={`${config.name}-${inputs.children.length}`}>
{children}
</div>
),
});
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'app-root-wrapper',
namespace: 'ns',
name: 'test',
attachTo: { id: 'app/root', input: 'wrappers' },
configSchema: schema,
disabled: true,
inputs: {
children: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
output: {
component: expect.anything(),
},
factory: expect.any(Function),
override: expect.any(Function),
toString: expect.any(Function),
});
createExtensionTester(
createPageExtension({
defaultPath: '/',
loader: async () => <div>Hello</div>,
}),
)
.add(extension, { config: { name: 'Robin' } })
.add(
createExtension({
attachTo: { id: 'app-root-wrapper:ns/test', input: 'children' },
output: { element: coreExtensionData.reactElement },
factory: () => ({ element: <div /> }),
}),
)
.render();
await expect(screen.findByText('Hello')).resolves.toBeInTheDocument();
await expect(screen.findByTestId('Robin-1')).resolves.toBeInTheDocument();
});
});
@@ -1,88 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { ComponentType, PropsWithChildren } from 'react';
import { PortableSchema } from '../schema/types';
import {
AnyExtensionInputMap,
ExtensionDefinition,
ResolvedExtensionInputs,
createExtension,
} from '../wiring/createExtension';
import { Expand } from '../types';
import { AppRootWrapperBlueprint } from '../blueprints/AppRootWrapperBlueprint';
/**
* Creates an extension that renders a React wrapper at the app root, enclosing
* the app layout. This is useful for example for adding global React contexts
* and similar.
*
* @public
* @deprecated Use {@link AppRootWrapperBlueprint} instead.
*/
export function createAppRootWrapperExtension<
TConfig extends {},
TInputs extends AnyExtensionInputMap,
>(options: {
namespace?: string;
name?: string;
attachTo?: { id: string; input: string };
configSchema?: PortableSchema<TConfig>;
disabled?: boolean;
inputs?: TInputs;
Component: ComponentType<
PropsWithChildren<{
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
config: TConfig;
}>
>;
}): ExtensionDefinition<TConfig> {
return createExtension({
kind: 'app-root-wrapper',
namespace: options.namespace,
name: options.name,
attachTo: options.attachTo ?? { id: 'app/root', input: 'wrappers' },
configSchema: options.configSchema,
disabled: options.disabled,
inputs: options.inputs,
output: {
component: AppRootWrapperBlueprint.dataRefs.component,
},
factory({ inputs, config }) {
const Component = (props: PropsWithChildren<{}>) => {
return (
<options.Component inputs={inputs} config={config}>
{props.children}
</options.Component>
);
};
return {
component: Component,
};
},
});
}
/**
* @public
* @deprecated Use {@link AppRootWrapperBlueprint} instead.
*/
export namespace createAppRootWrapperExtension {
/**
* @deprecated Use {@link AppRootWrapperBlueprint} instead.
*/
export const componentDataRef = AppRootWrapperBlueprint.dataRefs.component;
}
@@ -1,69 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { IconComponent } from '@backstage/core-plugin-api';
import { createSchemaFromZod } from '../schema/createSchemaFromZod';
import { createExtension } from '../wiring';
import { RouteRef } from '../routing';
import { NavItemBlueprint } from '../blueprints/NavItemBlueprint';
/**
* Helper for creating extensions for a nav item.
*
* @public
* @deprecated Use {@link NavItemBlueprint} instead.
*/
export function createNavItemExtension(options: {
namespace?: string;
name?: string;
routeRef: RouteRef<undefined>;
title: string;
icon: IconComponent;
}) {
const { routeRef, title, icon, namespace, name } = options;
return createExtension({
namespace,
name,
kind: 'nav-item',
attachTo: { id: 'app/nav', input: 'items' },
configSchema: createSchemaFromZod(z =>
z.object({
title: z.string().default(title),
}),
),
output: {
navTarget: NavItemBlueprint.dataRefs.target,
},
factory: ({ config }) => ({
navTarget: {
title: config.title,
icon,
routeRef,
},
}),
});
}
/**
* @public
* @deprecated Use {@link NavItemBlueprint} instead.
*/
export namespace createNavItemExtension {
/**
* @deprecated Use {@link NavItemBlueprint} instead.
*/
export const targetDataRef = NavItemBlueprint.dataRefs.target;
}
@@ -1,48 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { createNavLogoExtension } from './createNavLogoExtension';
jest.mock('@backstage/core-plugin-api', () => ({
...jest.requireActual('@backstage/core-plugin-api'),
}));
describe('createNavLogoExtension', () => {
it('creates the extension properly', () => {
expect(
createNavLogoExtension({
name: 'test',
logoFull: <div>Logo Full</div>,
logoIcon: <div>Logo Icon</div>,
}),
).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'nav-logo',
name: 'test',
attachTo: { id: 'app/nav', input: 'logos' },
disabled: false,
inputs: {},
output: {
logos: expect.anything(),
},
factory: expect.any(Function),
override: expect.any(Function),
toString: expect.any(Function),
});
});
});
@@ -1,61 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createExtension } from '../wiring';
import { NavLogoBlueprint } from '../blueprints/NavLogoBlueprint';
/**
* Helper for creating extensions for a nav logos.
*
* @public
* @deprecated Use {@link NavLogoBlueprint} instead.
*/
export function createNavLogoExtension(options: {
name?: string;
namespace?: string;
logoIcon: JSX.Element;
logoFull: JSX.Element;
}) {
const { logoIcon, logoFull } = options;
return createExtension({
kind: 'nav-logo',
name: options?.name,
namespace: options?.namespace,
attachTo: { id: 'app/nav', input: 'logos' },
output: {
logos: NavLogoBlueprint.dataRefs.logoElements,
},
factory: () => {
return {
logos: {
logoIcon,
logoFull,
},
};
},
});
}
/**
* @public
* @deprecated Use {@link NavLogoBlueprint} instead.
*/
export namespace createNavLogoExtension {
/**
* @deprecated Use {@link NavLogoBlueprint} instead.
*/
export const logoElementsDataRef = NavLogoBlueprint.dataRefs.logoElements;
}
@@ -1,145 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { useAnalytics } from '@backstage/core-plugin-api';
import { waitFor } from '@testing-library/react';
import { PortableSchema } from '../schema';
import { coreExtensionData, createExtensionInput } from '../wiring';
import { createPageExtension } from './createPageExtension';
import { createExtensionTester } from '@backstage/frontend-test-utils';
jest.mock('@backstage/core-plugin-api', () => ({
...jest.requireActual('@backstage/core-plugin-api'),
useAnalytics: jest.fn(),
}));
describe('createPageExtension', () => {
it('creates the extension properly', () => {
const configSchema: PortableSchema<{ path: string }> = {
parse: jest.fn(),
schema: {} as any,
};
expect(
createPageExtension({
name: 'test',
configSchema,
loader: async () => <div />,
}),
).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
name: 'test',
kind: 'page',
attachTo: { id: 'app/routes', input: 'routes' },
configSchema: expect.anything(),
disabled: false,
inputs: {},
output: {
element: expect.anything(),
path: expect.anything(),
routeRef: expect.anything(),
},
factory: expect.any(Function),
toString: expect.any(Function),
override: expect.any(Function),
});
expect(
createPageExtension({
name: 'test',
attachTo: { id: 'other', input: 'place' },
disabled: true,
configSchema,
inputs: {
first: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
loader: async () => <div />,
}),
).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
name: 'test',
kind: 'page',
attachTo: { id: 'other', input: 'place' },
configSchema: expect.anything(),
override: expect.any(Function),
disabled: true,
inputs: {
first: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
output: {
element: expect.anything(),
path: expect.anything(),
routeRef: expect.anything(),
},
factory: expect.any(Function),
toString: expect.any(Function),
});
expect(
createPageExtension({
name: 'test',
defaultPath: '/here',
loader: async () => <div />,
}),
).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
name: 'test',
kind: 'page',
attachTo: { id: 'app/routes', input: 'routes' },
configSchema: expect.anything(),
disabled: false,
inputs: {},
output: {
element: expect.anything(),
path: expect.anything(),
routeRef: expect.anything(),
},
factory: expect.any(Function),
toString: expect.any(Function),
override: expect.any(Function),
});
});
it('capture page view event in analytics', async () => {
const captureEvent = jest.fn();
(useAnalytics as jest.Mock).mockReturnValue({
captureEvent,
});
createExtensionTester(
createPageExtension({
defaultPath: '/',
loader: async () => <div>Component</div>,
}),
).render();
await waitFor(() =>
expect(captureEvent).toHaveBeenCalledWith(
'_ROUTABLE-EXTENSION-RENDERED',
'',
),
);
});
});
@@ -1,98 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { lazy } from 'react';
import { ExtensionBoundary } from '../components';
import { createSchemaFromZod, PortableSchema } from '../schema';
import {
coreExtensionData,
createExtension,
ResolvedExtensionInputs,
AnyExtensionInputMap,
} from '../wiring';
import { RouteRef } from '../routing';
import { Expand } from '../types';
import { ExtensionDefinition } from '../wiring/createExtension';
/**
* Helper for creating extensions for a routable React page component.
*
* @public
* @deprecated Use {@link PageBlueprint} instead.
*/
export function createPageExtension<
TConfig extends { path: string },
TInputs extends AnyExtensionInputMap,
>(
options: (
| {
defaultPath: string;
}
| {
configSchema: PortableSchema<TConfig>;
}
) & {
namespace?: string;
name?: string;
attachTo?: { id: string; input: string };
disabled?: boolean;
inputs?: TInputs;
routeRef?: RouteRef;
loader: (options: {
config: TConfig;
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
}) => Promise<JSX.Element>;
},
): ExtensionDefinition<TConfig> {
const configSchema =
'configSchema' in options
? options.configSchema
: (createSchemaFromZod(z =>
z.object({ path: z.string().default(options.defaultPath) }),
) as PortableSchema<TConfig>);
return createExtension({
kind: 'page',
namespace: options.namespace,
name: options.name,
attachTo: options.attachTo ?? { id: 'app/routes', input: 'routes' },
configSchema,
inputs: options.inputs,
disabled: options.disabled,
output: {
element: coreExtensionData.reactElement,
path: coreExtensionData.routePath,
routeRef: coreExtensionData.routeRef.optional(),
},
factory({ config, inputs, node }) {
const ExtensionComponent = lazy(() =>
options
.loader({ config, inputs })
.then(element => ({ default: () => element })),
);
return {
path: config.path,
routeRef: options.routeRef,
element: (
<ExtensionBoundary node={node}>
<ExtensionComponent />
</ExtensionBoundary>
),
};
},
});
}
@@ -1,169 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createSpecializedApp } from '@backstage/frontend-app-api';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { MockConfigApi } from '@backstage/test-utils';
import { MemoryRouter } from 'react-router-dom';
import { createSchemaFromZod } from '../schema/createSchemaFromZod';
import { coreExtensionData } from '../wiring/coreExtensionData';
import { createExtension } from '../wiring/createExtension';
import { createExtensionInput } from '../wiring/createExtensionInput';
import { createExtensionOverrides } from '../wiring/createExtensionOverrides';
import { createPageExtension } from './createPageExtension';
import { createRouterExtension } from './createRouterExtension';
describe('createRouterExtension', () => {
it('works with simple options and no props', async () => {
const extension = createRouterExtension({
namespace: 'test',
Component: ({ children }) => (
<MemoryRouter>
<div data-testid="test-router">{children}</div>
</MemoryRouter>
),
});
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'app-router-component',
namespace: 'test',
attachTo: { id: 'app/root', input: 'router' },
disabled: false,
inputs: {},
output: {
component: expect.anything(),
},
factory: expect.any(Function),
override: expect.any(Function),
toString: expect.any(Function),
});
const app = createSpecializedApp({
features: [
createExtensionOverrides({
extensions: [
extension,
createPageExtension({
namespace: 'test',
defaultPath: '/',
loader: async () => <div data-testid="test-contents" />,
}),
],
}),
],
});
render(app.createRoot());
await expect(
screen.findByTestId('test-contents'),
).resolves.toBeInTheDocument();
await expect(
screen.findByTestId('test-router'),
).resolves.toBeInTheDocument();
});
it('works with complex options and props', async () => {
const schema = createSchemaFromZod(z => z.object({ name: z.string() }));
const extension = createRouterExtension({
namespace: 'test',
name: 'test',
configSchema: schema,
inputs: {
children: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
Component: ({ inputs, config, children }) => (
<MemoryRouter>
<div
data-testid={`test-router-${config.name}-${inputs.children.length}`}
>
{children}
</div>
</MemoryRouter>
),
});
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'app-router-component',
namespace: 'test',
name: 'test',
attachTo: { id: 'app/root', input: 'router' },
configSchema: schema,
disabled: false,
inputs: {
children: createExtensionInput({
element: coreExtensionData.reactElement,
}),
},
output: {
component: expect.anything(),
},
factory: expect.any(Function),
override: expect.any(Function),
toString: expect.any(Function),
});
const app = createSpecializedApp({
features: [
createExtensionOverrides({
extensions: [
extension,
createExtension({
namespace: 'test',
attachTo: {
id: 'app-router-component:test/test',
input: 'children',
},
output: { element: coreExtensionData.reactElement }, // doesn't matter
factory: () => ({ element: <div /> }),
}),
createPageExtension({
namespace: 'test',
defaultPath: '/',
loader: async () => <div data-testid="test-contents" />,
}),
],
}),
],
config: new MockConfigApi({
app: {
extensions: [
{
'app-router-component:test/test': { config: { name: 'Robin' } },
},
],
},
}),
});
render(app.createRoot());
await expect(
screen.findByTestId('test-contents'),
).resolves.toBeInTheDocument();
await expect(
screen.findByTestId('test-router-Robin-1'),
).resolves.toBeInTheDocument();
});
});
@@ -1,88 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { ComponentType, PropsWithChildren } from 'react';
import { PortableSchema } from '../schema/types';
import {
AnyExtensionInputMap,
ExtensionDefinition,
ResolvedExtensionInputs,
createExtension,
} from '../wiring/createExtension';
import { Expand } from '../types';
import { RouterBlueprint } from '../blueprints/RouterBlueprint';
/**
* Creates an extension that replaces the router implementation at the app root.
* This is useful to be able to for example replace the BrowserRouter with a
* MemoryRouter in tests, or to add additional props to a BrowserRouter.
*
* @public
* @deprecated Use {@link RouterBlueprint} instead.
*/
export function createRouterExtension<
TConfig extends {},
TInputs extends AnyExtensionInputMap,
>(options: {
namespace?: string;
name?: string;
attachTo?: { id: string; input: string };
configSchema?: PortableSchema<TConfig>;
disabled?: boolean;
inputs?: TInputs;
Component: ComponentType<
PropsWithChildren<{
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
config: TConfig;
}>
>;
}): ExtensionDefinition<TConfig> {
return createExtension({
kind: 'app-router-component',
namespace: options.namespace,
name: options.name,
attachTo: options.attachTo ?? { id: 'app/root', input: 'router' },
configSchema: options.configSchema,
disabled: options.disabled,
inputs: options.inputs,
output: {
component: RouterBlueprint.dataRefs.component,
},
factory({ inputs, config }) {
const Component = (props: PropsWithChildren<{}>) => {
return (
<options.Component inputs={inputs} config={config}>
{props.children}
</options.Component>
);
};
return {
component: Component,
};
},
});
}
/**
* @public
* @deprecated Use {@link RouterBlueprint} instead.
*/
export namespace createRouterExtension {
/**
* @deprecated Use {@link RouterBlueprint} instead.
*/
export const componentDataRef = RouterBlueprint.dataRefs.component;
}
@@ -1,47 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { createExtensionTester } from '@backstage/frontend-test-utils';
import { screen } from '@testing-library/react';
import { createSignInPageExtension } from './createSignInPageExtension';
import { coreExtensionData, createExtension } from '../wiring';
describe('createSignInPageExtension', () => {
it('renders a sign-in page', async () => {
const SignInPage = createSignInPageExtension({
name: 'test',
loader: async () => () => <div data-testid="sign-in-page" />,
});
createExtensionTester(
createExtension({
name: 'dummy',
attachTo: { id: 'ignored', input: 'ignored' },
output: {
element: coreExtensionData.reactElement,
},
factory: () => ({ element: <div /> }),
}),
)
.add(SignInPage)
.render();
await expect(
screen.findByTestId('sign-in-page'),
).resolves.toBeInTheDocument();
});
});
@@ -1,88 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { ComponentType, lazy } from 'react';
import { ExtensionBoundary } from '../components';
import { PortableSchema } from '../schema';
import {
createExtension,
ResolvedExtensionInputs,
AnyExtensionInputMap,
ExtensionDefinition,
} from '../wiring';
import { Expand } from '../types';
import { SignInPageProps } from '@backstage/core-plugin-api';
import { SignInPageBlueprint } from '../blueprints';
/**
*
* @public
* @deprecated Use {@link SignInPageBlueprint} instead.
*/
export function createSignInPageExtension<
TConfig extends {},
TInputs extends AnyExtensionInputMap,
>(options: {
namespace?: string;
name?: string;
attachTo?: { id: string; input: string };
configSchema?: PortableSchema<TConfig>;
disabled?: boolean;
inputs?: TInputs;
loader: (options: {
config: TConfig;
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
}) => Promise<ComponentType<SignInPageProps>>;
}): ExtensionDefinition<TConfig> {
return createExtension({
kind: 'sign-in-page',
namespace: options?.namespace,
name: options?.name,
attachTo: options.attachTo ?? { id: 'app/root', input: 'signInPage' },
configSchema: options.configSchema,
inputs: options.inputs,
disabled: options.disabled,
output: {
component: createSignInPageExtension.componentDataRef,
},
factory({ config, inputs, node }) {
const ExtensionComponent = lazy(() =>
options
.loader({ config, inputs })
.then(component => ({ default: component })),
);
return {
component: props => (
<ExtensionBoundary node={node} routable>
<ExtensionComponent {...props} />
</ExtensionBoundary>
),
};
},
});
}
/**
* @public
* @deprecated Use {@link SignInPageBlueprint} instead.
*/
export namespace createSignInPageExtension {
/**
* @deprecated Use {@link SignInPageBlueprint} instead.
*/
export const componentDataRef = SignInPageBlueprint.dataRefs.component;
}
@@ -1,44 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ThemeBlueprint } from '../blueprints/ThemeBlueprint';
import { createExtension } from '../wiring';
import { AppTheme } from '@backstage/core-plugin-api';
/**
* @public
* @deprecated Use {@link ThemeBlueprint} instead.
*/
export function createThemeExtension(theme: AppTheme) {
return createExtension({
kind: 'theme',
namespace: 'app',
name: theme.id,
attachTo: { id: 'app', input: 'themes' },
output: {
theme: ThemeBlueprint.dataRefs.theme,
},
factory: () => ({ theme }),
});
}
/**
* @public
* @deprecated Use {@link ThemeBlueprint} instead.
*/
export namespace createThemeExtension {
export const themeDataRef = ThemeBlueprint.dataRefs.theme;
}
@@ -1,137 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
createTranslationRef,
createTranslationMessages,
createTranslationResource,
} from '@backstage/core-plugin-api/alpha';
import { createTranslationExtension } from './createTranslationExtension';
const translationRef = createTranslationRef({
id: 'test',
messages: {
a: 'a',
b: 'b',
},
});
describe('createTranslationExtension', () => {
it('creates a translation message extension', () => {
const messages = createTranslationMessages({
ref: translationRef,
messages: {
a: 'A',
},
});
const extension = createTranslationExtension({ resource: messages });
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'translation',
namespace: 'test',
attachTo: { id: 'app', input: 'translations' },
disabled: false,
override: expect.any(Function),
inputs: {},
output: {
resource: createTranslationExtension.translationDataRef,
},
factory: expect.any(Function),
toString: expect.any(Function),
});
expect((extension as any).factory({} as any)).toEqual({
resource: messages,
});
});
it('creates a translation resource extension', () => {
const resource = createTranslationResource({
ref: translationRef,
translations: {
sv: () =>
Promise.resolve({
default: createTranslationMessages({
ref: translationRef,
messages: {
a: 'Ä',
b: 'Ö',
},
}),
}),
},
});
const extension = createTranslationExtension({ resource });
expect(extension).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'translation',
namespace: 'test',
attachTo: { id: 'app', input: 'translations' },
disabled: false,
override: expect.any(Function),
inputs: {},
output: {
resource: createTranslationExtension.translationDataRef,
},
factory: expect.any(Function),
toString: expect.any(Function),
});
expect((extension as any).factory({} as any)).toEqual({ resource });
});
it('creates a translation resource extension with a name', () => {
expect(
createTranslationExtension({
name: 'sv',
resource: createTranslationResource({
ref: translationRef,
translations: {
sv: () =>
Promise.resolve({
default: createTranslationMessages({
ref: translationRef,
messages: {
a: 'Ä',
b: 'Ö',
},
}),
}),
},
}),
}),
).toEqual({
$$type: '@backstage/ExtensionDefinition',
version: 'v1',
kind: 'translation',
namespace: 'test',
override: expect.any(Function),
name: 'sv',
attachTo: { id: 'app', input: 'translations' },
disabled: false,
inputs: {},
output: {
resource: createTranslationExtension.translationDataRef,
},
factory: expect.any(Function),
toString: expect.any(Function),
});
});
});
@@ -1,47 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TranslationBlueprint } from '../blueprints/TranslationBlueprint';
import { TranslationMessages, TranslationResource } from '../translation';
import { createExtension } from '../wiring';
/**
* @public
* @deprecated Use {@link TranslationBlueprint} instead.
*/
export function createTranslationExtension(options: {
name?: string;
resource: TranslationResource | TranslationMessages;
}) {
return createExtension({
kind: 'translation',
namespace: options.resource.id,
name: options.name,
attachTo: { id: 'app', input: 'translations' },
output: {
resource: TranslationBlueprint.dataRefs.translation,
},
factory: () => ({ resource: options.resource }),
});
}
/**
* @public
* @deprecated Use {@link TranslationBlueprint} instead.
*/
export namespace createTranslationExtension {
export const translationDataRef = TranslationBlueprint.dataRefs.translation;
}
@@ -14,14 +14,4 @@
* limitations under the License.
*/
export { createApiExtension } from './createApiExtension';
export { createAppRootElementExtension } from './createAppRootElementExtension';
export { createAppRootWrapperExtension } from './createAppRootWrapperExtension';
export { createRouterExtension } from './createRouterExtension';
export { createPageExtension } from './createPageExtension';
export { createNavItemExtension } from './createNavItemExtension';
export { createNavLogoExtension } from './createNavLogoExtension';
export { createSignInPageExtension } from './createSignInPageExtension';
export { createThemeExtension } from './createThemeExtension';
export { createComponentExtension } from './createComponentExtension';
export { createTranslationExtension } from './createTranslationExtension';