Skip to content

Commit

Permalink
Merge pull request #941 from umbraco/feature/decouple-resource-contro…
Browse files Browse the repository at this point in the history
…ller
  • Loading branch information
nielslyngsoe authored Oct 26, 2023
2 parents 817c54c + ca6fd35 commit 6d42601
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/shared/resources/apiTypeValidators.function.ts
Original file line number Diff line number Diff line change
@@ -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<T>(promise: unknown): promise is CancelablePromise<T> {
return (promise as CancelablePromise<T>).cancel !== undefined;
}
8 changes: 4 additions & 4 deletions src/shared/resources/resource.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 };
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 6d42601

Please sign in to comment.