From 2a46e3701e291ac499997ecae9bc2d6fbf81290d Mon Sep 17 00:00:00 2001 From: Taras Date: Thu, 8 Sep 2022 08:42:07 -0400 Subject: [PATCH] Integrated feedback from @freben Signed-off-by: Taras --- .changeset/large-books-deny.md | 2 +- packages/catalog-client/api-report.md | 12 ++++++++---- packages/catalog-client/src/CatalogClient.ts | 9 ++++++--- packages/catalog-client/src/types/api.ts | 7 +++---- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/.changeset/large-books-deny.md b/.changeset/large-books-deny.md index 51d2ce38c0..c88d391639 100644 --- a/.changeset/large-books-deny.md +++ b/.changeset/large-books-deny.md @@ -2,4 +2,4 @@ '@backstage/catalog-client': minor --- -Adding validateEntity method that calls /validate-entity endpoint +Adding `validateEntity` method that calls `/validate-entity` endpoint. diff --git a/packages/catalog-client/api-report.md b/packages/catalog-client/api-report.md index 1d66b469a9..0ad77ce39c 100644 --- a/packages/catalog-client/api-report.md +++ b/packages/catalog-client/api-report.md @@ -209,8 +209,12 @@ type Location_2 = { export { Location_2 as Location }; // @public -export type ValidateEntityResponse = { - valid: boolean; - errors?: SerializedError[]; -}; +export type ValidateEntityResponse = + | { + valid: true; + } + | { + valid: false; + errors: SerializedError[]; + }; ``` diff --git a/packages/catalog-client/src/CatalogClient.ts b/packages/catalog-client/src/CatalogClient.ts index bbc9b0b62d..f2b4186bfb 100644 --- a/packages/catalog-client/src/CatalogClient.ts +++ b/packages/catalog-client/src/CatalogClient.ts @@ -21,7 +21,7 @@ import { stringifyEntityRef, stringifyLocationRef, } from '@backstage/catalog-model'; -import { ResponseError, SerializedError } from '@backstage/errors'; +import { ResponseError } from '@backstage/errors'; import crossFetch from 'cross-fetch'; import { CATALOG_FILTER_EXISTS, @@ -377,11 +377,14 @@ export class CatalogClient implements CatalogApi { if (response.ok) { return { valid: true, - errors: undefined, }; } - const { errors } = (await response.json()) as { errors: SerializedError[] }; + if (response.status !== 400) { + throw await ResponseError.fromResponse(response); + } + + const { errors = [] } = await response.json(); return { valid: false, diff --git a/packages/catalog-client/src/types/api.ts b/packages/catalog-client/src/types/api.ts index d66e2dfaf9..f483c3454c 100644 --- a/packages/catalog-client/src/types/api.ts +++ b/packages/catalog-client/src/types/api.ts @@ -275,10 +275,9 @@ export type AddLocationResponse = { * * @public */ -export type ValidateEntityResponse = { - valid: boolean; - errors?: SerializedError[]; -}; +export type ValidateEntityResponse = + | { valid: true } + | { valid: false; errors: SerializedError[] }; /** * A client for interacting with the Backstage software catalog through its API.