From 6807423a1faf9bf32dc775d98ebe089c3e0b2dff Mon Sep 17 00:00:00 2001 From: zeshanziya Date: Wed, 11 Sep 2024 11:41:55 +0000 Subject: [PATCH] move types to common package Signed-off-by: zeshanziya --- plugins/platformsh-backend/package.json | 1 + .../src/PlatformshHelper.ts | 5 +- plugins/platformsh-backend/src/model.ts | 19 ------- .../platformsh-backend/src/service/router.ts | 2 +- plugins/platformsh-common/src/index.ts | 57 ++++++++++++++++++- plugins/platformsh/package.json | 1 + plugins/platformsh/src/api.ts | 12 ++-- .../Cards/EnvironmentsCard/ActionButtons.tsx | 2 +- .../EnvironmentsCard.test.tsx | 2 +- .../EnvironmentsCard/EnvironmentsCard.tsx | 2 +- .../ProjectDetailsCard.test.tsx | 4 +- .../ProjectDetailsCard/ProjectDetailsCard.tsx | 4 +- .../EntityTabComponent.test.tsx | 4 +- .../ProjectsComponent.test.tsx | 4 +- .../ProjectsComponent/ProjectsComponent.tsx | 6 +- plugins/platformsh/src/models.ts | 49 ---------------- 16 files changed, 82 insertions(+), 92 deletions(-) delete mode 100644 plugins/platformsh-backend/src/model.ts delete mode 100644 plugins/platformsh/src/models.ts diff --git a/plugins/platformsh-backend/package.json b/plugins/platformsh-backend/package.json index 1e1803f..164d0d0 100644 --- a/plugins/platformsh-backend/package.json +++ b/plugins/platformsh-backend/package.json @@ -26,6 +26,7 @@ "@backstage/backend-common": "^0.24.0", "@backstage/backend-defaults": "^0.4.2", "@backstage/backend-plugin-api": "^0.8.0", + "@internal/backstage-plugin-platformsh-common": "^0.1.0", "express": "^4.17.1", "express-promise-router": "^4.1.0", "node-fetch": "^2.6.7", diff --git a/plugins/platformsh-backend/src/PlatformshHelper.ts b/plugins/platformsh-backend/src/PlatformshHelper.ts index fa46ef7..bb285f4 100644 --- a/plugins/platformsh-backend/src/PlatformshHelper.ts +++ b/plugins/platformsh-backend/src/PlatformshHelper.ts @@ -5,7 +5,10 @@ import { import Client from 'platformsh-client'; import Activity from 'platformsh-client/types/model/Activity'; import Environment from 'platformsh-client/types/model/Environment'; -import { EnvironmentMethods, PlatformshEnvironment } from './model'; +import { + EnvironmentMethods, + PlatformshEnvironment, +} from '@internal/backstage-plugin-platformsh-common'; type PlatformshAccessToken = { access_token: string; diff --git a/plugins/platformsh-backend/src/model.ts b/plugins/platformsh-backend/src/model.ts deleted file mode 100644 index df9c82a..0000000 --- a/plugins/platformsh-backend/src/model.ts +++ /dev/null @@ -1,19 +0,0 @@ -export type EnvironmentMethods = - | 'pause' - | 'resume' - | 'activate' - | 'deactivate' - | 'delete'; - -export type PlatformshEnvironment = { - id: string; - name: string; - machine_name: string; - default_domain: string; - edge_hostname: string; - status: string; - type: string; - created_at: string; - updated_at: string; - parent: string; -}; diff --git a/plugins/platformsh-backend/src/service/router.ts b/plugins/platformsh-backend/src/service/router.ts index a95c32f..d193912 100644 --- a/plugins/platformsh-backend/src/service/router.ts +++ b/plugins/platformsh-backend/src/service/router.ts @@ -6,7 +6,7 @@ import { import express, { Request } from 'express'; import Router from 'express-promise-router'; import { PlatformshHelper } from '../PlatformshHelper'; -import { EnvironmentMethods } from '../model'; +import { EnvironmentMethods } from '@internal/backstage-plugin-platformsh-common'; export interface RouterOptions { logger: LoggerService; diff --git a/plugins/platformsh-common/src/index.ts b/plugins/platformsh-common/src/index.ts index b1dc02d..b8469ad 100644 --- a/plugins/platformsh-common/src/index.ts +++ b/plugins/platformsh-common/src/index.ts @@ -9,10 +9,63 @@ * In this package you might for example declare types that are common * between the frontend and backend plugin packages. */ -export type CommonType = { - field: string; +export type PlatformshProject = { + id: string; + status: string; + plan: string; + project_id: string; + project_title: string; + project_region_label: string; + project_ui: string; + size?: string; + environment?: { + count: number; + used: number; + }; + url?: string; }; +export type PlatformshEnvironment = { + id: string; + name: string; + machine_name: string; + default_domain: string; + edge_hostname: string; + status: string; + type: string; + created_at: string; + updated_at: string; + parent: string; +}; + +export type EnvironmentActionResponse = { + result: { + actionResult: { + valid: number; + message: string; + }; + }; +}; + +export type ListProjectsResponse = { + result: { projects: PlatformshProject[] }; +}; + +export type ProjectInfoResponse = { + result: { projectData: PlatformshProject }; +}; + +export type ProjectEnvironmentsResponse = { + result: { environments: PlatformshEnvironment[] }; +}; + +export type EnvironmentMethods = + | 'pause' + | 'resume' + | 'activate' + | 'deactivate' + | 'delete'; + /** * Or you might declare some common constants. */ diff --git a/plugins/platformsh/package.json b/plugins/platformsh/package.json index ccf28b2..cee620f 100644 --- a/plugins/platformsh/package.json +++ b/plugins/platformsh/package.json @@ -29,6 +29,7 @@ "@backstage/core-plugin-api": "^1.9.3", "@backstage/plugin-catalog-react": "^1.12.3", "@backstage/theme": "^0.5.6", + "@internal/backstage-plugin-platformsh-common": "^0.1.0", "@material-ui/core": "^4.9.13", "@material-ui/icons": "^4.9.1", "@material-ui/lab": "^4.0.0-alpha.61", diff --git a/plugins/platformsh/src/api.ts b/plugins/platformsh/src/api.ts index e19a4cd..be8bbac 100644 --- a/plugins/platformsh/src/api.ts +++ b/plugins/platformsh/src/api.ts @@ -7,14 +7,14 @@ import { EnvironmentActionResponse, ListProjectsResponse, PlatformshEnvironment, - PlatformShProject, + PlatformshProject, ProjectEnvironmentsResponse, ProjectInfoResponse, -} from './models'; +} from '@internal/backstage-plugin-platformsh-common'; export interface PlatformshApi { - listProjects(): Promise; - getProjectInfo(id: string): Promise; + listProjects(): Promise; + getProjectInfo(id: string): Promise; getProjectEnvironments(id: string): Promise; doEnvironmentAction( projectId: string, @@ -53,12 +53,12 @@ export class PlatformshClient implements PlatformshApi { return response.json(); } - async listProjects(): Promise { + async listProjects(): Promise { const data = await this.fetchApiData('/projects'); return data.result.projects; } - async getProjectInfo(id: string): Promise { + async getProjectInfo(id: string): Promise { const data = await this.fetchApiData(`/project/${id}`); return data.result.projectData; } diff --git a/plugins/platformsh/src/components/Cards/EnvironmentsCard/ActionButtons.tsx b/plugins/platformsh/src/components/Cards/EnvironmentsCard/ActionButtons.tsx index aa40f0d..a49016f 100644 --- a/plugins/platformsh/src/components/Cards/EnvironmentsCard/ActionButtons.tsx +++ b/plugins/platformsh/src/components/Cards/EnvironmentsCard/ActionButtons.tsx @@ -6,7 +6,7 @@ import { MenuItem, } from '@material-ui/core'; import MoreVertIcon from '@material-ui/icons/MoreVert'; -import { PlatformshEnvironment } from '../../../models'; +import { PlatformshEnvironment } from '@internal/backstage-plugin-platformsh-common'; import useAsyncFn from 'react-use/lib/useAsyncFn'; export const ActionButtons = ({ diff --git a/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.test.tsx b/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.test.tsx index dc5a418..fbf1fc7 100644 --- a/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.test.tsx +++ b/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.test.tsx @@ -9,7 +9,7 @@ import { TestApiProvider, } from '@backstage/test-utils'; import { platformshApiRef } from '../../../api'; -import { PlatformshEnvironment } from '../../../models'; +import { PlatformshEnvironment } from '@internal/backstage-plugin-platformsh-common'; describe('ProjectDetailsCard', () => { const server = setupServer(); diff --git a/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.tsx b/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.tsx index 42a89eb..be19beb 100644 --- a/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.tsx +++ b/plugins/platformsh/src/components/Cards/EnvironmentsCard/EnvironmentsCard.tsx @@ -5,7 +5,7 @@ import { Progress, ResponseErrorPanel, } from '@backstage/core-components'; -import { PlatformshEnvironment } from '../../../models'; +import { PlatformshEnvironment } from '@internal/backstage-plugin-platformsh-common'; import { alertApiRef, useApi } from '@backstage/core-plugin-api'; import { platformshApiRef } from '../../../api'; import { ActionButtons } from './ActionButtons'; diff --git a/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.test.tsx b/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.test.tsx index afa36b2..3ed1d54 100644 --- a/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.test.tsx +++ b/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.test.tsx @@ -9,7 +9,7 @@ import { TestApiProvider, } from '@backstage/test-utils'; import { platformshApiRef } from '../../../api'; -import { PlatformShProject } from '../../../models'; +import { PlatformshProject } from '@internal/backstage-plugin-platformsh-common'; describe('ProjectDetailsCard', () => { const server = setupServer(); @@ -37,7 +37,7 @@ describe('ProjectDetailsCard', () => { }); it('should render', async () => { - const project: PlatformShProject = { + const project: PlatformshProject = { id: 'proj-123', status: 'active', plan: 'standard', diff --git a/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.tsx b/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.tsx index 0b1eed8..d5333f6 100644 --- a/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.tsx +++ b/plugins/platformsh/src/components/Cards/ProjectDetailsCard/ProjectDetailsCard.tsx @@ -7,7 +7,7 @@ import { import { Typography, Box, makeStyles, Link } from '@material-ui/core'; import { platformshApiRef } from '../../../api'; import { useApi } from '@backstage/core-plugin-api'; -import { PlatformShProject } from '../../../models'; +import { PlatformshProject } from '@internal/backstage-plugin-platformsh-common'; import useAsync from 'react-use/lib/useAsync'; const useStyles = makeStyles(theme => ({ @@ -61,7 +61,7 @@ export const ProjectDetailsCard = ({ projectId }: { projectId: string }) => { const classes = useStyles(); const { value, loading, error } = - useAsync(async (): Promise => { + useAsync(async (): Promise => { return platformshApi.getProjectInfo(projectId); }, []); diff --git a/plugins/platformsh/src/components/EntityTabComponent/EntityTabComponent.test.tsx b/plugins/platformsh/src/components/EntityTabComponent/EntityTabComponent.test.tsx index 1f82c6a..46c492e 100644 --- a/plugins/platformsh/src/components/EntityTabComponent/EntityTabComponent.test.tsx +++ b/plugins/platformsh/src/components/EntityTabComponent/EntityTabComponent.test.tsx @@ -9,7 +9,7 @@ import { TestApiProvider, } from '@backstage/test-utils'; import { platformshApiRef } from '../../api'; -import { PlatformShProject } from '../../models'; +import { PlatformshProject } from '@internal/backstage-plugin-platformsh-common'; jest.mock('@backstage/plugin-catalog-react', () => ({ useEntity: () => { @@ -50,7 +50,7 @@ describe('EntityTabComponent', () => { }); it('should render', async () => { - const project: PlatformShProject = { + const project: PlatformshProject = { id: 'proj-123', status: 'active', plan: 'standard', diff --git a/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.test.tsx b/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.test.tsx index a61a439..852f890 100644 --- a/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.test.tsx +++ b/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.test.tsx @@ -3,7 +3,7 @@ import { render, screen } from '@testing-library/react'; import { ProjectsComponent } from './ProjectsComponent'; import { platformshApiRef } from '../../api'; import { TestApiProvider } from '@backstage/test-utils'; -import { PlatformShProject } from '../../models'; +import { PlatformshProject } from '@internal/backstage-plugin-platformsh-common'; describe('ProjectsComponent', () => { const platformshApi: jest.Mocked = { @@ -24,7 +24,7 @@ describe('ProjectsComponent', () => { }); it('renders the projects table', async () => { - const projects: PlatformShProject[] = [ + const projects: PlatformshProject[] = [ { id: 'a1b2c3d4', status: 'requested', diff --git a/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.tsx b/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.tsx index e08bf92..f9b98e0 100644 --- a/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.tsx +++ b/plugins/platformsh/src/components/ProjectsComponent/ProjectsComponent.tsx @@ -6,12 +6,12 @@ import { ResponseErrorPanel, } from '@backstage/core-components'; import useAsync from 'react-use/lib/useAsync'; -import { PlatformShProject } from '../../models'; +import { PlatformshProject } from '@internal/backstage-plugin-platformsh-common'; import { useApi } from '@backstage/core-plugin-api'; import { platformshApiRef } from '../../api'; type DenseTableProps = { - projects: PlatformShProject[]; + projects: PlatformshProject[]; }; export const DenseTable = ({ projects }: DenseTableProps) => { @@ -47,7 +47,7 @@ export const ProjectsComponent = () => { const platformshApi = useApi(platformshApiRef); const { value, loading, error } = useAsync(async (): Promise< - PlatformShProject[] + PlatformshProject[] > => { return platformshApi.listProjects(); }, []); diff --git a/plugins/platformsh/src/models.ts b/plugins/platformsh/src/models.ts deleted file mode 100644 index bcbbcb3..0000000 --- a/plugins/platformsh/src/models.ts +++ /dev/null @@ -1,49 +0,0 @@ -export type PlatformShProject = { - id: string; - status: string; - plan: string; - project_id: string; - project_title: string; - project_region_label: string; - project_ui: string; - size?: string; - environment?: { - count: number; - used: number; - }; - url?: string; -}; - -export type PlatformshEnvironment = { - id: string; - name: string; - machine_name: string; - default_domain: string; - edge_hostname: string; - status: string; - type: string; - created_at: string; - updated_at: string; - parent: string; -}; - -export type EnvironmentActionResponse = { - result: { - actionResult: { - valid: number; - message: string; - }; - }; -}; - -export type ListProjectsResponse = { - result: { projects: PlatformShProject[] }; -}; - -export type ProjectInfoResponse = { - result: { projectData: PlatformShProject }; -}; - -export type ProjectEnvironmentsResponse = { - result: { environments: PlatformshEnvironment[] }; -};