From 883782e8a7d42e2c36d95516ab7a9d219372af5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 21 Dec 2023 11:43:21 +0100 Subject: [PATCH] Fix a bug in `getLocationByRef` that led to invalid backend calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/large-poets-buy.md | 5 +++++ .changeset/lazy-teachers-walk.md | 5 +++++ docs/frontend-system/utility-apis/05-migrating.md | 2 +- packages/catalog-client/src/CatalogClient.ts | 10 +++++----- .../src/generated/apis/DefaultApi.client.ts | 7 ++++++- .../templates/typescript-backstage/api.mustache | 11 ++++++++--- 6 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 .changeset/large-poets-buy.md create mode 100644 .changeset/lazy-teachers-walk.md diff --git a/.changeset/large-poets-buy.md b/.changeset/large-poets-buy.md new file mode 100644 index 0000000000..f22ca7c6b8 --- /dev/null +++ b/.changeset/large-poets-buy.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-client': patch +--- + +Fix a bug in `getLocationByRef` that led to invalid backend calls diff --git a/.changeset/lazy-teachers-walk.md b/.changeset/lazy-teachers-walk.md new file mode 100644 index 0000000000..92f9a63d1e --- /dev/null +++ b/.changeset/lazy-teachers-walk.md @@ -0,0 +1,5 @@ +--- +'@backstage/repo-tools': patch +--- + +Updated the OpenAPI template to export the `TypedResponse` interface so that client code can leverage it diff --git a/docs/frontend-system/utility-apis/05-migrating.md b/docs/frontend-system/utility-apis/05-migrating.md index 6586618136..a81dc796ac 100644 --- a/docs/frontend-system/utility-apis/05-migrating.md +++ b/docs/frontend-system/utility-apis/05-migrating.md @@ -42,7 +42,7 @@ import { createApiRef } from '@backstage/frontend-plugin-api'; /** * Performs some work. - * @oublic + * @public */ export interface WorkApi { doWork(): Promise; diff --git a/packages/catalog-client/src/CatalogClient.ts b/packages/catalog-client/src/CatalogClient.ts index f97360d1d7..0d6bd6b926 100644 --- a/packages/catalog-client/src/CatalogClient.ts +++ b/packages/catalog-client/src/CatalogClient.ts @@ -43,7 +43,7 @@ import { QueryEntitiesResponse, } from './types/api'; import { isQueryEntitiesInitialRequest } from './utils'; -import { DefaultApiClient } from './generated'; +import { DefaultApiClient, TypedResponse } from './generated'; /** * A frontend and backend compatible client for communicating with the Backstage @@ -112,7 +112,7 @@ export class CatalogClient implements CatalogApi { } } - const entities: Entity[] = await this.requestRequired( + const entities = await this.requestRequired( await this.apiClient.getEntities( { query: { @@ -345,8 +345,8 @@ export class CatalogClient implements CatalogApi { locationRef: string, options?: CatalogRequestOptions, ): Promise { - const all: { data: Location }[] = await this.requestRequired( - await this.apiClient.getLocation({ path: { id: locationRef } }, options), + const all = await this.requestRequired( + await this.apiClient.getLocations({}, options), ); return all .map(r => r.data) @@ -418,7 +418,7 @@ export class CatalogClient implements CatalogApi { } } - private async requestRequired(response: Response): Promise { + private async requestRequired(response: TypedResponse): Promise { if (!response.ok) { throw await ResponseError.fromResponse(response); } diff --git a/packages/catalog-client/src/generated/apis/DefaultApi.client.ts b/packages/catalog-client/src/generated/apis/DefaultApi.client.ts index 260f7c9fc8..65b8699370 100644 --- a/packages/catalog-client/src/generated/apis/DefaultApi.client.ts +++ b/packages/catalog-client/src/generated/apis/DefaultApi.client.ts @@ -35,7 +35,12 @@ import { Location } from '../models/Location.model'; import { RefreshEntityRequest } from '../models/RefreshEntityRequest.model'; import { ValidateEntityRequest } from '../models/ValidateEntityRequest.model'; -type TypedResponse = Omit & { +/** + * Wraps the Response type to convey a type on the json call. + * + * @public + */ +export type TypedResponse = Omit & { json: () => Promise; }; diff --git a/packages/repo-tools/templates/typescript-backstage/api.mustache b/packages/repo-tools/templates/typescript-backstage/api.mustache index 1b8739ba57..8043d6e2f4 100644 --- a/packages/repo-tools/templates/typescript-backstage/api.mustache +++ b/packages/repo-tools/templates/typescript-backstage/api.mustache @@ -9,7 +9,12 @@ import * as parser from 'uri-template'; import { {{classname}} } from '{{filename}}.model{{importFileExtension}}'; {{/imports}} -type TypedResponse = Omit & { +/** + * Wraps the Response type to convey a type on the json call. + * + * @public + */ +export type TypedResponse = Omit & { json: () => Promise; }; @@ -84,9 +89,9 @@ export class {{classname}}Client { options?: RequestOptions ): Promise> { const baseUrl = await this.discoveryApi.getBaseUrl(pluginId); - + const uriTemplate = `{{{path}}}{{#hasQueryParams}}{?{{#queryParams}}{{baseName}}{{#isArray}}{{#isExplode}}*{{/isExplode}}{{/isArray}}{{^-last}},{{/-last}}{{/queryParams}}}{{/hasQueryParams}}`; - + const uri = parser.parse(uriTemplate).expand({ {{#pathParams}} {{baseName}}: request.path.{{paramName}},