Files
ThreadNet-Web/packages/element-web-module-api/src/api.ts
T
2025-01-29 08:35:40 +00:00

72 lines
2.1 KiB
TypeScript

/*
Copyright 2025 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { LegacyModuleApiExtension } from "./legacy-modules";
import { LegacyCustomisationsApiExtension } from "./legacy-customisations";
export interface Module {
load(): Promise<void>;
}
const moduleSignature: Record<keyof Module, Type> = {
load: "function",
};
export interface ModuleFactory {
readonly moduleApiVersion: string;
new (api: Api): Module;
readonly prototype: Module;
}
const moduleFactorySignature: Record<keyof ModuleFactory, Type> = {
moduleApiVersion: "string",
prototype: "object",
};
export interface ModuleExport {
default: ModuleFactory;
}
const moduleExportSignature: Record<keyof ModuleExport, Type> = {
default: "object",
};
type Type = "function" | "string" | "number" | "boolean" | "object";
function isInterface<T>(obj: unknown, keys: Record<keyof T, Type>): obj is T {
if (obj === null || typeof obj !== "object") return false;
for (const key in keys) {
if (typeof (obj as Record<keyof T, unknown>)[key] !== keys[key]) return false;
}
return true;
}
export function isModule(module: unknown): module is ModuleExport {
return (
isInterface(module, moduleExportSignature) &&
isInterface(module.default, moduleFactorySignature) &&
isInterface(module.default.prototype, moduleSignature)
);
}
export interface Config {
// The branding name of the application
brand: string;
// Other config options are available but not specified in the types as that would make it difficult to change for element-web
// they are accessible at runtime all the same, see list at https://github.com/element-hq/element-web/blob/develop/docs/config.md
}
export interface ConfigApi {
get(): Config;
get<K extends keyof Config>(key: K): Config[K];
get<K extends keyof Config = never>(key?: K): Config | Config[K];
}
export interface Api extends LegacyModuleApiExtension, LegacyCustomisationsApiExtension {
config: ConfigApi;
}