Back

Version 7

2/24/2024
import { type AccessDeep, accessDeep } from "https://esm.town/v/postpostscript/accessDeep";

export async function importModule<ModuleType>(moduleName: string) {
const url = moduleName[0] === "@"
? `https://esm.town/v/${moduleName.slice(1)}`
: moduleName;
return import(url) as Promise<ModuleType>;
}

export async function call<ModuleType, const TMethod extends string | string[]>(
moduleName: string,
methodName: TMethod,
args: any[] = [],
): Promise<
AccessDeep<ModuleType, TMethod> extends (...args: any[]) => infer T ? T
: never
> {
const module = await importModule<ModuleType>(moduleName);
const method = accessDeep(module, methodName);
if (!(method instanceof Function)) {
const key = methodName instanceof Array
? methodName.join(".")
: methodName;
throw new Error(`\`call\` value \`${key}\` is not callable`);
}
return method(...args);
}
Updated: March 2, 2024