From 02437b0bfbde2bdff81e912eed5fd073f7b7c1ab Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Tue, 24 Oct 2023 09:59:16 +0200 Subject: [PATCH 1/2] add validator functions for backend-api types --- .../shared/resources/apiTypeValidators.function.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/Umbraco.Web.UI.Client/src/shared/resources/apiTypeValidators.function.ts diff --git a/src/Umbraco.Web.UI.Client/src/shared/resources/apiTypeValidators.function.ts b/src/Umbraco.Web.UI.Client/src/shared/resources/apiTypeValidators.function.ts new file mode 100644 index 0000000000..091120886e --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/shared/resources/apiTypeValidators.function.ts @@ -0,0 +1,13 @@ +import type { ApiError, CancelError, CancelablePromise } from '@umbraco-cms/backoffice/backend-api'; + +export function isApiError(error: unknown): error is ApiError { + return (error as ApiError).name === 'ApiError'; +} + +export function isCancelError(error: unknown): error is CancelError { + return (error as CancelError).name === 'CancelError'; +} + +export function isCancelablePromise(promise: unknown): promise is CancelablePromise { + return (promise as CancelablePromise).cancel !== undefined; +} From ed02e343c6adfb17bbb5c20d36ff3300e2c24480 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Tue, 24 Oct 2023 10:07:05 +0200 Subject: [PATCH 2/2] ensure that UmbResourceController only uses "like types" mimicking the generated models from backend-api to ensure that extensions can provide their own implementations --- .../src/shared/resources/resource.controller.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/shared/resources/resource.controller.ts b/src/Umbraco.Web.UI.Client/src/shared/resources/resource.controller.ts index fe986945b6..0afe664c6d 100644 --- a/src/Umbraco.Web.UI.Client/src/shared/resources/resource.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/shared/resources/resource.controller.ts @@ -1,10 +1,10 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { isApiError, isCancelError, isCancelablePromise } from './apiTypeValidators.function.js'; import { UmbNotificationContext, UMB_NOTIFICATION_CONTEXT_TOKEN, UmbNotificationOptions, } from '@umbraco-cms/backoffice/notification'; -import { ApiError, CancelError, CancelablePromise } from '@umbraco-cms/backoffice/backend-api'; import { UmbBaseController, UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api'; import type { DataSourceResponse } from '@umbraco-cms/backoffice/repository'; @@ -39,7 +39,7 @@ export class UmbResourceController extends UmbBaseController { try { return { data: await promise }; } catch (error) { - if (error instanceof ApiError || error instanceof CancelError) { + if (isApiError(error) || isCancelError(error)) { return { error }; } @@ -61,7 +61,7 @@ export class UmbResourceController extends UmbBaseController { * If the error is not a recognizable system error (i.e. a HttpError), then we will show a notification * with the error details using the default notification options. */ - if (error instanceof CancelError) { + if (isCancelError(error)) { // Cancelled - do nothing return {}; } else { @@ -125,7 +125,7 @@ export class UmbResourceController extends UmbBaseController { * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortController */ cancel() { - if (this.#promise instanceof CancelablePromise) { + if (isCancelablePromise(this.#promise)) { this.#promise.cancel(); } }