From 4e207ec6db3dc97e7aa6bae13c38f78e51f46124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Sun, 3 Mar 2024 22:03:43 +0100 Subject: [PATCH] constructor args as method --- ...ension-element-and-api-initializer.controller.ts | 5 +++-- ...nsions-element-and-api-initializer.controller.ts | 3 ++- .../functions/create-extension-api.function.ts | 13 ++++++++++--- .../create-extension-element-with-api.function.ts | 8 ++++++-- .../src/libs/extension-api/functions/index.ts | 5 +++-- .../src/libs/extension-api/functions/types.ts | 1 + .../extension-registry/models/tree-item.model.ts | 4 +++- 7 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/types.ts diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extension-element-and-api-initializer.controller.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extension-element-and-api-initializer.controller.ts index 37d4389b72..8a15cacba6 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extension-element-and-api-initializer.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extension-element-and-api-initializer.controller.ts @@ -1,4 +1,5 @@ import { createExtensionElementWithApi } from '../functions/create-extension-element-with-api.function.js'; +import type { UmbApiConstructorArgumentsMethodType } from '../index.js'; import type { UmbApi } from '../models/api.interface.js'; import type { UmbExtensionRegistry } from '../registry/extension.registry.js'; import type { ManifestElementAndApi, ManifestCondition, ManifestWithDynamicConditions } from '../types/index.js'; @@ -26,7 +27,7 @@ export class UmbExtensionElementAndApiInitializer< #defaultElement?: string; #component?: ExtensionElementInterface; #api?: ExtensionApiInterface; - #constructorArguments?: Array; + #constructorArguments?: Array | UmbApiConstructorArgumentsMethodType; /** * The component that is created for this extension. @@ -98,7 +99,7 @@ export class UmbExtensionElementAndApiInitializer< host: UmbControllerHost, extensionRegistry: UmbExtensionRegistry, alias: string, - constructorArguments: Array | undefined, + constructorArguments: Array | UmbApiConstructorArgumentsMethodType | undefined, onPermissionChanged: (isPermitted: boolean, controller: ControllerType) => void, defaultElement?: string, ) { diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extensions-element-and-api-initializer.controller.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extensions-element-and-api-initializer.controller.ts index 94631c2dcc..683d39348a 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extensions-element-and-api-initializer.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/controller/extensions-element-and-api-initializer.controller.ts @@ -1,6 +1,7 @@ import type { ManifestBase } from '../types/index.js'; import type { UmbExtensionRegistry } from '../registry/extension.registry.js'; import type { SpecificManifestTypeOrManifestBase } from '../types/map.types.js'; +import type { UmbApiConstructorArgumentsMethodType } from '../index.js'; import { UmbExtensionElementAndApiInitializer } from './extension-element-and-api-initializer.controller.js'; import { type PermittedControllerType, @@ -27,7 +28,7 @@ export class UmbExtensionsElementAndApiInitializer< // #extensionRegistry; #defaultElement?: string; - #constructorArgs: Array | undefined; + #constructorArgs: Array | UmbApiConstructorArgumentsMethodType | undefined; #elProps?: Record; #apiProps?: Record; diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-api.function.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-api.function.ts index 2c2264efaf..2a95855aa0 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-api.function.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-api.function.ts @@ -1,15 +1,20 @@ import type { UmbApi } from '../models/api.interface.js'; import type { ManifestApi, ManifestElementAndApi } from '../types/base.types.js'; import { loadManifestApi } from './load-manifest-api.function.js'; +import type { UmbApiConstructorArgumentsMethodType } from './types.js'; export async function createExtensionApi( manifest: ManifestApi | ManifestElementAndApi, - constructorArguments: Array = [], + constructorArgs: + | Array + | UmbApiConstructorArgumentsMethodType | ManifestElementAndApi> = [], ): Promise { if (manifest.api) { const apiConstructor = await loadManifestApi(manifest.api); if (apiConstructor) { - return new apiConstructor(...constructorArguments); + const additionalArgs = + (typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? []; + return new apiConstructor(...additionalArgs); } else { console.error( `-- Extension of alias "${manifest.alias}" did not succeed instantiate a API class via the extension manifest property 'api', using either a 'api' or 'default' export`, @@ -21,7 +26,9 @@ export async function createExtensionApi( if (manifest.js) { const apiConstructor2 = await loadManifestApi(manifest.js); if (apiConstructor2) { - return new apiConstructor2(...constructorArguments); + const additionalArgs = + (typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? []; + return new apiConstructor2(...additionalArgs); } else { console.error( `-- Extension of alias "${manifest.alias}" did not succeed instantiate a API class via the extension manifest property 'js', using either a 'api' or 'default' export`, diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-element-with-api.function.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-element-with-api.function.ts index 903234f7b9..1532c63089 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-element-with-api.function.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/create-extension-element-with-api.function.ts @@ -3,6 +3,7 @@ import type { ManifestElementAndApi } from '../types/base.types.js'; import type { ClassConstructor } from '../types/utils.js'; import { loadManifestApi } from './load-manifest-api.function.js'; import { loadManifestElement } from './load-manifest-element.function.js'; +import type { UmbApiConstructorArgumentsMethodType } from './types.js'; import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api'; export async function createExtensionElementWithApi< @@ -11,7 +12,7 @@ export async function createExtensionElementWithApi< >( manifest: ManifestElementAndApi, fallbackElement?: string, - constructorArgs?: unknown[], + constructorArgs?: unknown[] | UmbApiConstructorArgumentsMethodType>, ): Promise<{ element?: ElementType; api?: ApiType }> { const apiPropValue = manifest.api ?? manifest.js; if (!apiPropValue) { @@ -53,7 +54,10 @@ export async function createExtensionElementWithApi< } if (element && apiConstructor) { - const api = new apiConstructor(element, ...(constructorArgs ?? [])); + // If constructorArgs is a function, call it with the manifest to get the arguments: + const additionalArgs = (typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? []; + + const api = new apiConstructor(element, ...additionalArgs); // Return object with element & api: return { element, api }; } diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/index.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/index.ts index 8b67026452..cf2758ba9f 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/index.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/index.ts @@ -1,8 +1,9 @@ export * from './create-extension-api.function.js'; -export * from './create-extension-element.function.js'; export * from './create-extension-element-with-api.function.js'; +export * from './create-extension-element.function.js'; export * from './has-init-export.function.js'; export * from './load-manifest-api.function.js'; export * from './load-manifest-element.function.js'; -export * from './load-manifest-plain-js.function.js'; export * from './load-manifest-plain-css.function.js'; +export * from './load-manifest-plain-js.function.js'; +export * from './types.js'; diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/types.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/types.ts new file mode 100644 index 0000000000..040415e602 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/functions/types.ts @@ -0,0 +1 @@ +export type UmbApiConstructorArgumentsMethodType = (manifest: ManifestType) => unknown[]; diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/models/tree-item.model.ts b/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/models/tree-item.model.ts index aff81bc5d9..908ca29034 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/models/tree-item.model.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/models/tree-item.model.ts @@ -1,7 +1,9 @@ import type { UmbTreeItemContext, UmbTreeItemModelBase } from '../../index.js'; +import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api'; import type { ManifestElementAndApi } from '@umbraco-cms/backoffice/extension-api'; -export interface ManifestTreeItem extends ManifestElementAndApi> { +export interface ManifestTreeItem + extends ManifestElementAndApi> { type: 'treeItem'; meta: MetaTreeItem; }