Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/libs/resources/resource.controller.ts
Jacob Overgaard 796533ff11 Refactor libs into @umbraco-cms/backoffice/* (#608)
* merge libs rollup configs to one rollup

* move css from libs to src/core

* run rollup on cms build

* move test-utils to /utils folder

* move css to src/core

* mark @umbraco-cms/backoffice as external when building for CMS

* rename all models to include @umbraco-cms/backoffice in their path to allow us to publish as a single module

* rename all imports to @umbraco-cms/backoffice/*

* rename events to umb-events to avoid rollup error of protected module name(?)

* test that libs can build

* move css to src/core

* move umb-lit-element and modal elements to src/core

* move some modal interfaces back to libs/modal

* move the icon store into src/core since it is very localized to the backoffice

* comment out build:libs for now since Github runs out of memory

* rename to match tsconfig alias

* add package.json to libs

* only make libs for lib folders

* turn off emit for typescript since we are handling types for libs separately

* build libs locally

* add script to move libs to final destination with some transform

* move libs after build

* move package.json to dist folder first (so we can publish from there)

* remove inline comments

* ensure the outputDir exists

* Remove re-export of extensions-registry library from models library

* move to individual files to avoid circular imports

* check if outputDir exists before trying to create it

* write transforms first in dist file and then copy the file to outputDir

* ensure all umbraco types are external

* copy information from main package.json file
2023-03-21 11:41:06 +01:00

113 lines
3.2 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import {
UmbNotificationOptions,
UmbNotificationContext,
UMB_NOTIFICATION_CONTEXT_TOKEN,
} from '@umbraco-cms/backoffice/notification';
import { ApiError, CancelablePromise, ProblemDetailsModel } from '@umbraco-cms/backoffice/backend-api';
import { UmbController, UmbControllerHostInterface } from '@umbraco-cms/backoffice/controller';
import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
import type { DataSourceResponse } from '@umbraco-cms/backoffice/models';
export class UmbResourceController extends UmbController {
#promise: Promise<any>;
#notificationContext?: UmbNotificationContext;
constructor(host: UmbControllerHostInterface, promise: Promise<any>, alias?: string) {
super(host, alias);
this.#promise = promise;
new UmbContextConsumerController(host, UMB_NOTIFICATION_CONTEXT_TOKEN, (_instance) => {
this.#notificationContext = _instance;
});
}
hostConnected(): void {
// Do nothing
}
hostDisconnected(): void {
this.cancel();
}
/**
* Extract the ProblemDetailsModel object from an ApiError.
*
* This assumes that all ApiErrors contain a ProblemDetailsModel object in their body.
*/
static toProblemDetailsModel(error: unknown): ProblemDetailsModel | undefined {
if (error instanceof ApiError) {
const errorDetails = error.body as ProblemDetailsModel;
return errorDetails;
} else if (error instanceof Error) {
return {
title: error.name,
detail: error.message,
};
}
return undefined;
}
/**
* Base execute function with a try/catch block and return a tuple with the result and the error.
*/
static async tryExecute<T>(promise: Promise<T>): Promise<DataSourceResponse<T>> {
try {
return { data: await promise };
} catch (e) {
return { error: UmbResourceController.toProblemDetailsModel(e) };
}
}
/**
* Wrap the {execute} function in a try/catch block and return the result.
* If the executor function throws an error, then show the details in a notification.
*/
async tryExecuteAndNotify<T>(options?: UmbNotificationOptions): Promise<DataSourceResponse<T>> {
const { data, error } = await UmbResourceController.tryExecute<T>(this.#promise);
if (error) {
if (this.#notificationContext) {
this.#notificationContext?.peek('danger', {
data: {
headline: error.title ?? 'Server Error',
message: error.detail ?? 'Something went wrong',
},
...options,
});
} else {
console.group('UmbResourceController');
console.error(error);
console.groupEnd();
}
}
return { data, error };
}
/**
* Cancel all resources that are currently being executed by this controller if they are cancelable.
*
* This works by checking if the promise is a CancelablePromise and if so, it will call the cancel method.
*
* This is useful when the controller is being disconnected from the DOM.
*
* @see CancelablePromise
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortController
*/
cancel() {
if (this.#promise instanceof CancelablePromise) {
this.#promise.cancel();
}
}
destroy() {
super.destroy();
this.cancel();
}
}