All files / src/utils try-import.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                                  30x 30x   2x          
import { StackOneError } from './error-stackone';
 
/**
 * Dynamically import an optional dependency with a friendly error message
 *
 * @param moduleName - The name of the module to import
 * @param installHint - Installation instructions shown in error message
 * @returns The imported module
 * @throws StackOneError if the module is not installed
 *
 * @example
 * ```ts
 * const ai = await tryImport('ai', 'npm install ai');
 * const { jsonSchema } = ai;
 * ```
 */
export async function tryImport<T>(moduleName: string, installHint: string): Promise<T> {
	try {
		return await import(moduleName);
	} catch {
		throw new StackOneError(
			`${moduleName} is not installed. Please install it with: ${installHint}`,
		);
	}
}