From e89b554ae0af20136e11742c1ea04df20df390ba Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 4 Sep 2023 14:38:18 +0200 Subject: [PATCH] frontend-plugin-api: refactor to remove root modules Signed-off-by: Patrik Oldsberg --- packages/frontend-plugin-api/api-report.md | 3 ++ .../src/extensions/createApiExtension.ts | 8 +-- .../extensions/createPageExtension.test.tsx | 4 +- .../src/extensions/createPageExtension.tsx | 4 +- packages/frontend-plugin-api/src/index.ts | 15 +----- .../{ => schema}/createSchemaFromZod.test.ts | 0 .../src/{ => schema}/createSchemaFromZod.ts | 7 +-- .../frontend-plugin-api/src/schema/index.ts | 18 +++++++ .../frontend-plugin-api/src/schema/types.ts | 23 +++++++++ .../src/wiring/coreExtensionData.ts | 27 ++++++++++ .../{types.ts => wiring/createExtension.ts} | 49 ++----------------- .../src/wiring/createExtensionDataRef.ts | 28 +++++++++++ .../src/wiring/createPlugin.test.ts | 10 ++-- .../src/wiring/createPlugin.ts | 2 +- .../frontend-plugin-api/src/wiring/index.ts | 12 +++++ .../frontend-plugin-api/src/wiring/types.ts | 40 +++++++++++++++ 16 files changed, 169 insertions(+), 81 deletions(-) rename packages/frontend-plugin-api/src/{ => schema}/createSchemaFromZod.test.ts (100%) rename packages/frontend-plugin-api/src/{ => schema}/createSchemaFromZod.ts (93%) create mode 100644 packages/frontend-plugin-api/src/schema/index.ts create mode 100644 packages/frontend-plugin-api/src/schema/types.ts create mode 100644 packages/frontend-plugin-api/src/wiring/coreExtensionData.ts rename packages/frontend-plugin-api/src/{types.ts => wiring/createExtension.ts} (55%) create mode 100644 packages/frontend-plugin-api/src/wiring/createExtensionDataRef.ts create mode 100644 packages/frontend-plugin-api/src/wiring/types.ts diff --git a/packages/frontend-plugin-api/api-report.md b/packages/frontend-plugin-api/api-report.md index a27875bcf8..e83d963348 100644 --- a/packages/frontend-plugin-api/api-report.md +++ b/packages/frontend-plugin-api/api-report.md @@ -80,6 +80,9 @@ export function createExtension< TConfig = never, >(options: CreateExtensionOptions): Extension; +// @public (undocumented) +export function createExtensionDataRef(id: string): ExtensionDataRef; + // @public (undocumented) export interface CreateExtensionOptions< TData extends AnyExtensionDataMap, diff --git a/packages/frontend-plugin-api/src/extensions/createApiExtension.ts b/packages/frontend-plugin-api/src/extensions/createApiExtension.ts index 5dab38aec8..d59a7ab2c3 100644 --- a/packages/frontend-plugin-api/src/extensions/createApiExtension.ts +++ b/packages/frontend-plugin-api/src/extensions/createApiExtension.ts @@ -15,13 +15,13 @@ */ import { AnyApiFactory, AnyApiRef } from '@backstage/core-plugin-api'; -import { PortableSchema } from '../createSchemaFromZod'; +import { PortableSchema } from '../schema'; import { AnyExtensionDataMap, - coreExtensionData, - createExtension, ExtensionDataValue, -} from '../types'; + createExtension, + coreExtensionData, +} from '../wiring'; /** @public */ export function createApiExtension< diff --git a/packages/frontend-plugin-api/src/extensions/createPageExtension.test.tsx b/packages/frontend-plugin-api/src/extensions/createPageExtension.test.tsx index f1caaf7fdf..e5939d24ea 100644 --- a/packages/frontend-plugin-api/src/extensions/createPageExtension.test.tsx +++ b/packages/frontend-plugin-api/src/extensions/createPageExtension.test.tsx @@ -15,8 +15,8 @@ */ import React from 'react'; -import { PortableSchema } from '../createSchemaFromZod'; -import { coreExtensionData } from '../types'; +import { PortableSchema } from '../schema'; +import { coreExtensionData } from '../wiring'; import { createPageExtension } from './createPageExtension'; describe('createPageExtension', () => { diff --git a/packages/frontend-plugin-api/src/extensions/createPageExtension.tsx b/packages/frontend-plugin-api/src/extensions/createPageExtension.tsx index fb2d930844..e136f1bc2a 100644 --- a/packages/frontend-plugin-api/src/extensions/createPageExtension.tsx +++ b/packages/frontend-plugin-api/src/extensions/createPageExtension.tsx @@ -17,14 +17,14 @@ import { RouteRef } from '@backstage/core-plugin-api'; import React from 'react'; import { ExtensionBoundary } from '../components'; -import { createSchemaFromZod, PortableSchema } from '../createSchemaFromZod'; +import { createSchemaFromZod, PortableSchema } from '../schema'; import { AnyExtensionDataMap, coreExtensionData, createExtension, Extension, ExtensionDataValue, -} from '../types'; +} from '../wiring'; /** * Helper for creating extensions for a routable React page component. diff --git a/packages/frontend-plugin-api/src/index.ts b/packages/frontend-plugin-api/src/index.ts index 16f226c483..4e1a383899 100644 --- a/packages/frontend-plugin-api/src/index.ts +++ b/packages/frontend-plugin-api/src/index.ts @@ -20,21 +20,8 @@ * @packageDocumentation */ -export { - createSchemaFromZod, - type PortableSchema, -} from './createSchemaFromZod'; export * from './components'; export * from './extensions'; -export { - coreExtensionData, - createExtension, - type AnyExtensionDataMap, - type CreateExtensionOptions, - type Extension, - type ExtensionDataBind, - type ExtensionDataRef, - type ExtensionDataValue, -} from './types'; +export * from './schema'; export * from './wiring'; export * from './routing'; diff --git a/packages/frontend-plugin-api/src/createSchemaFromZod.test.ts b/packages/frontend-plugin-api/src/schema/createSchemaFromZod.test.ts similarity index 100% rename from packages/frontend-plugin-api/src/createSchemaFromZod.test.ts rename to packages/frontend-plugin-api/src/schema/createSchemaFromZod.test.ts diff --git a/packages/frontend-plugin-api/src/createSchemaFromZod.ts b/packages/frontend-plugin-api/src/schema/createSchemaFromZod.ts similarity index 93% rename from packages/frontend-plugin-api/src/createSchemaFromZod.ts rename to packages/frontend-plugin-api/src/schema/createSchemaFromZod.ts index 447cea8bbd..bfaf57f522 100644 --- a/packages/frontend-plugin-api/src/createSchemaFromZod.ts +++ b/packages/frontend-plugin-api/src/schema/createSchemaFromZod.ts @@ -17,12 +17,7 @@ import { JsonObject } from '@backstage/types'; import { z, ZodSchema, ZodTypeDef } from 'zod'; import zodToJsonSchema from 'zod-to-json-schema'; - -/** @public */ -export type PortableSchema = { - parse: (input: unknown) => TOutput; - schema: JsonObject; -}; +import { PortableSchema } from './types'; /** @public */ export function createSchemaFromZod( diff --git a/packages/frontend-plugin-api/src/schema/index.ts b/packages/frontend-plugin-api/src/schema/index.ts new file mode 100644 index 0000000000..7f21c4e07d --- /dev/null +++ b/packages/frontend-plugin-api/src/schema/index.ts @@ -0,0 +1,18 @@ +/* + * 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. + */ + +export { createSchemaFromZod } from './createSchemaFromZod'; +export { type PortableSchema } from './types'; diff --git a/packages/frontend-plugin-api/src/schema/types.ts b/packages/frontend-plugin-api/src/schema/types.ts new file mode 100644 index 0000000000..9636d3b896 --- /dev/null +++ b/packages/frontend-plugin-api/src/schema/types.ts @@ -0,0 +1,23 @@ +/* + * 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 { JsonObject } from '@backstage/types'; + +/** @public */ +export type PortableSchema = { + parse: (input: unknown) => TOutput; + schema: JsonObject; +}; diff --git a/packages/frontend-plugin-api/src/wiring/coreExtensionData.ts b/packages/frontend-plugin-api/src/wiring/coreExtensionData.ts new file mode 100644 index 0000000000..1f4e4370c9 --- /dev/null +++ b/packages/frontend-plugin-api/src/wiring/coreExtensionData.ts @@ -0,0 +1,27 @@ +/* + * 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, RouteRef } from '@backstage/core-plugin-api'; +import { ComponentType } from 'react'; +import { createExtensionDataRef } from './createExtensionDataRef'; + +/** @public */ +export const coreExtensionData = { + reactComponent: createExtensionDataRef('core.reactComponent'), + routePath: createExtensionDataRef('core.routing.path'), + apiFactory: createExtensionDataRef('core.api.factory'), + routeRef: createExtensionDataRef('core.routing.ref'), +}; diff --git a/packages/frontend-plugin-api/src/types.ts b/packages/frontend-plugin-api/src/wiring/createExtension.ts similarity index 55% rename from packages/frontend-plugin-api/src/types.ts rename to packages/frontend-plugin-api/src/wiring/createExtension.ts index 940e821af5..e7f673cd2f 100644 --- a/packages/frontend-plugin-api/src/types.ts +++ b/packages/frontend-plugin-api/src/wiring/createExtension.ts @@ -14,35 +14,9 @@ * limitations under the License. */ -import { AnyApiFactory } from '@backstage/core-plugin-api'; -import { RouteRef } from '@backstage/core-plugin-api'; -import { ComponentType } from 'react'; -import { PortableSchema } from './createSchemaFromZod'; -import { BackstagePlugin } from './wiring'; - -/** @public */ -export type ExtensionDataRef = { - id: string; - T: T; - $$type: 'extension-data'; -}; - -/** @public */ -// TODO: change to options object with ID. -export function createExtensionDataRef(id: string): ExtensionDataRef { - return { id, $$type: 'extension-data' } as ExtensionDataRef; -} - -/** @public */ -export const coreExtensionData = { - reactComponent: createExtensionDataRef('core.reactComponent'), - routePath: createExtensionDataRef('core.routing.path'), - apiFactory: createExtensionDataRef('core.api.factory'), - routeRef: createExtensionDataRef('core.routing.ref'), -}; - -/** @public */ -export type AnyExtensionDataMap = Record>; +import { PortableSchema } from '../schema'; +import { BackstagePlugin } from './createPlugin'; +import { AnyExtensionDataMap, Extension } from './types'; /** @public */ export type ExtensionDataBind = { @@ -78,23 +52,6 @@ export interface CreateExtensionOptions< }): void; } -/** @public */ -export interface Extension { - $$type: 'extension'; - id: string; - at: string; - disabled: boolean; - inputs: Record; - output: AnyExtensionDataMap; - configSchema?: PortableSchema; - factory(options: { - source?: BackstagePlugin; - bind: ExtensionDataBind; - config: TConfig; - inputs: Record>>; - }): void; -} - /** @public */ export function createExtension< TData extends AnyExtensionDataMap, diff --git a/packages/frontend-plugin-api/src/wiring/createExtensionDataRef.ts b/packages/frontend-plugin-api/src/wiring/createExtensionDataRef.ts new file mode 100644 index 0000000000..10a931a65d --- /dev/null +++ b/packages/frontend-plugin-api/src/wiring/createExtensionDataRef.ts @@ -0,0 +1,28 @@ +/* + * 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. + */ + +/** @public */ +export type ExtensionDataRef = { + id: string; + T: T; + $$type: 'extension-data'; +}; + +/** @public */ +// TODO: change to options object with ID. +export function createExtensionDataRef(id: string): ExtensionDataRef { + return { id, $$type: 'extension-data' } as ExtensionDataRef; +} diff --git a/packages/frontend-plugin-api/src/wiring/createPlugin.test.ts b/packages/frontend-plugin-api/src/wiring/createPlugin.test.ts index 36389d7e63..7ebcb62ceb 100644 --- a/packages/frontend-plugin-api/src/wiring/createPlugin.test.ts +++ b/packages/frontend-plugin-api/src/wiring/createPlugin.test.ts @@ -17,14 +17,12 @@ import React from 'react'; import { createApp } from '@backstage/frontend-app-api'; import { render, screen } from '@testing-library/react'; -import { createSchemaFromZod } from '../createSchemaFromZod'; -import { - createExtension, - coreExtensionData, - createExtensionDataRef, -} from '../types'; +import { createSchemaFromZod } from '../schema/createSchemaFromZod'; import { createPlugin, BackstagePlugin } from './createPlugin'; import { JsonObject } from '@backstage/types'; +import { createExtension } from './createExtension'; +import { createExtensionDataRef } from './createExtensionDataRef'; +import { coreExtensionData } from './coreExtensionData'; const nameExtensionDataRef = createExtensionDataRef('name'); diff --git a/packages/frontend-plugin-api/src/wiring/createPlugin.ts b/packages/frontend-plugin-api/src/wiring/createPlugin.ts index 3e15fb0b19..7f7e44ff03 100644 --- a/packages/frontend-plugin-api/src/wiring/createPlugin.ts +++ b/packages/frontend-plugin-api/src/wiring/createPlugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Extension } from '../types'; +import { Extension } from './types'; /** @public */ export interface PluginOptions { diff --git a/packages/frontend-plugin-api/src/wiring/index.ts b/packages/frontend-plugin-api/src/wiring/index.ts index d96ba09971..1a243eb2e3 100644 --- a/packages/frontend-plugin-api/src/wiring/index.ts +++ b/packages/frontend-plugin-api/src/wiring/index.ts @@ -14,8 +14,20 @@ * limitations under the License. */ +export { coreExtensionData } from './coreExtensionData'; +export { + createExtension, + type CreateExtensionOptions, + type ExtensionDataBind, + type ExtensionDataValue, +} from './createExtension'; +export { + createExtensionDataRef, + type ExtensionDataRef, +} from './createExtensionDataRef'; export { createPlugin, type BackstagePlugin, type PluginOptions, } from './createPlugin'; +export type { AnyExtensionDataMap, Extension } from './types'; diff --git a/packages/frontend-plugin-api/src/wiring/types.ts b/packages/frontend-plugin-api/src/wiring/types.ts new file mode 100644 index 0000000000..3779a5c676 --- /dev/null +++ b/packages/frontend-plugin-api/src/wiring/types.ts @@ -0,0 +1,40 @@ +/* + * 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 { PortableSchema } from '../schema'; +import { ExtensionDataBind } from './createExtension'; +import { ExtensionDataRef } from './createExtensionDataRef'; +import { BackstagePlugin } from './createPlugin'; + +/** @public */ +export type AnyExtensionDataMap = Record>; + +/** @public */ +export interface Extension { + $$type: 'extension'; + id: string; + at: string; + disabled: boolean; + inputs: Record; + output: AnyExtensionDataMap; + configSchema?: PortableSchema; + factory(options: { + source?: BackstagePlugin; + bind: ExtensionDataBind; + config: TConfig; + inputs: Record>>; + }): void; +}