constructor args as method

This commit is contained in:
Niels Lyngsø
2024-03-03 22:03:43 +01:00
parent 7d89a6b5b7
commit 4e207ec6db
7 changed files with 28 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import { createExtensionElementWithApi } from '../functions/create-extension-element-with-api.function.js'; 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 { UmbApi } from '../models/api.interface.js';
import type { UmbExtensionRegistry } from '../registry/extension.registry.js'; import type { UmbExtensionRegistry } from '../registry/extension.registry.js';
import type { ManifestElementAndApi, ManifestCondition, ManifestWithDynamicConditions } from '../types/index.js'; import type { ManifestElementAndApi, ManifestCondition, ManifestWithDynamicConditions } from '../types/index.js';
@@ -26,7 +27,7 @@ export class UmbExtensionElementAndApiInitializer<
#defaultElement?: string; #defaultElement?: string;
#component?: ExtensionElementInterface; #component?: ExtensionElementInterface;
#api?: ExtensionApiInterface; #api?: ExtensionApiInterface;
#constructorArguments?: Array<unknown>; #constructorArguments?: Array<unknown> | UmbApiConstructorArgumentsMethodType<ManifestType>;
/** /**
* The component that is created for this extension. * The component that is created for this extension.
@@ -98,7 +99,7 @@ export class UmbExtensionElementAndApiInitializer<
host: UmbControllerHost, host: UmbControllerHost,
extensionRegistry: UmbExtensionRegistry<ManifestCondition>, extensionRegistry: UmbExtensionRegistry<ManifestCondition>,
alias: string, alias: string,
constructorArguments: Array<unknown> | undefined, constructorArguments: Array<unknown> | UmbApiConstructorArgumentsMethodType<ManifestType> | undefined,
onPermissionChanged: (isPermitted: boolean, controller: ControllerType) => void, onPermissionChanged: (isPermitted: boolean, controller: ControllerType) => void,
defaultElement?: string, defaultElement?: string,
) { ) {

View File

@@ -1,6 +1,7 @@
import type { ManifestBase } from '../types/index.js'; import type { ManifestBase } from '../types/index.js';
import type { UmbExtensionRegistry } from '../registry/extension.registry.js'; import type { UmbExtensionRegistry } from '../registry/extension.registry.js';
import type { SpecificManifestTypeOrManifestBase } from '../types/map.types.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 { UmbExtensionElementAndApiInitializer } from './extension-element-and-api-initializer.controller.js';
import { import {
type PermittedControllerType, type PermittedControllerType,
@@ -27,7 +28,7 @@ export class UmbExtensionsElementAndApiInitializer<
// //
#extensionRegistry; #extensionRegistry;
#defaultElement?: string; #defaultElement?: string;
#constructorArgs: Array<unknown> | undefined; #constructorArgs: Array<unknown> | UmbApiConstructorArgumentsMethodType<ManifestType> | undefined;
#elProps?: Record<string, unknown>; #elProps?: Record<string, unknown>;
#apiProps?: Record<string, unknown>; #apiProps?: Record<string, unknown>;

View File

@@ -1,15 +1,20 @@
import type { UmbApi } from '../models/api.interface.js'; import type { UmbApi } from '../models/api.interface.js';
import type { ManifestApi, ManifestElementAndApi } from '../types/base.types.js'; import type { ManifestApi, ManifestElementAndApi } from '../types/base.types.js';
import { loadManifestApi } from './load-manifest-api.function.js'; import { loadManifestApi } from './load-manifest-api.function.js';
import type { UmbApiConstructorArgumentsMethodType } from './types.js';
export async function createExtensionApi<ApiType extends UmbApi = UmbApi>( export async function createExtensionApi<ApiType extends UmbApi = UmbApi>(
manifest: ManifestApi<ApiType> | ManifestElementAndApi<any, ApiType>, manifest: ManifestApi<ApiType> | ManifestElementAndApi<any, ApiType>,
constructorArguments: Array<unknown> = [], constructorArgs:
| Array<unknown>
| UmbApiConstructorArgumentsMethodType<ManifestApi<ApiType> | ManifestElementAndApi<any, ApiType>> = [],
): Promise<ApiType | undefined> { ): Promise<ApiType | undefined> {
if (manifest.api) { if (manifest.api) {
const apiConstructor = await loadManifestApi<ApiType>(manifest.api); const apiConstructor = await loadManifestApi<ApiType>(manifest.api);
if (apiConstructor) { if (apiConstructor) {
return new apiConstructor(...constructorArguments); const additionalArgs =
(typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? [];
return new apiConstructor(...additionalArgs);
} else { } else {
console.error( 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`, `-- 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<ApiType extends UmbApi = UmbApi>(
if (manifest.js) { if (manifest.js) {
const apiConstructor2 = await loadManifestApi<ApiType>(manifest.js); const apiConstructor2 = await loadManifestApi<ApiType>(manifest.js);
if (apiConstructor2) { if (apiConstructor2) {
return new apiConstructor2(...constructorArguments); const additionalArgs =
(typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? [];
return new apiConstructor2(...additionalArgs);
} else { } else {
console.error( 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`, `-- 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`,

View File

@@ -3,6 +3,7 @@ import type { ManifestElementAndApi } from '../types/base.types.js';
import type { ClassConstructor } from '../types/utils.js'; import type { ClassConstructor } from '../types/utils.js';
import { loadManifestApi } from './load-manifest-api.function.js'; import { loadManifestApi } from './load-manifest-api.function.js';
import { loadManifestElement } from './load-manifest-element.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'; import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
export async function createExtensionElementWithApi< export async function createExtensionElementWithApi<
@@ -11,7 +12,7 @@ export async function createExtensionElementWithApi<
>( >(
manifest: ManifestElementAndApi<ElementType, ApiType>, manifest: ManifestElementAndApi<ElementType, ApiType>,
fallbackElement?: string, fallbackElement?: string,
constructorArgs?: unknown[], constructorArgs?: unknown[] | UmbApiConstructorArgumentsMethodType<ManifestElementAndApi<ElementType, ApiType>>,
): Promise<{ element?: ElementType; api?: ApiType }> { ): Promise<{ element?: ElementType; api?: ApiType }> {
const apiPropValue = manifest.api ?? manifest.js; const apiPropValue = manifest.api ?? manifest.js;
if (!apiPropValue) { if (!apiPropValue) {
@@ -53,7 +54,10 @@ export async function createExtensionElementWithApi<
} }
if (element && apiConstructor) { 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 object with element & api:
return { element, api }; return { element, api };
} }

View File

@@ -1,8 +1,9 @@
export * from './create-extension-api.function.js'; 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-with-api.function.js';
export * from './create-extension-element.function.js';
export * from './has-init-export.function.js'; export * from './has-init-export.function.js';
export * from './load-manifest-api.function.js'; export * from './load-manifest-api.function.js';
export * from './load-manifest-element.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-css.function.js';
export * from './load-manifest-plain-js.function.js';
export * from './types.js';

View File

@@ -0,0 +1 @@
export type UmbApiConstructorArgumentsMethodType<ManifestType> = (manifest: ManifestType) => unknown[];

View File

@@ -1,7 +1,9 @@
import type { UmbTreeItemContext, UmbTreeItemModelBase } from '../../index.js'; 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'; import type { ManifestElementAndApi } from '@umbraco-cms/backoffice/extension-api';
export interface ManifestTreeItem extends ManifestElementAndApi<HTMLElement, UmbTreeItemContext<UmbTreeItemModelBase>> { export interface ManifestTreeItem
extends ManifestElementAndApi<UmbControllerHostElement, UmbTreeItemContext<UmbTreeItemModelBase>> {
type: 'treeItem'; type: 'treeItem';
meta: MetaTreeItem; meta: MetaTreeItem;
} }