@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
36 lines (35 loc) • 1.75 kB
TypeScript
/**
* Dynamically import an optional dependency, converting a missing package into
* an actionable error.
*
* NeuroLink declares 42 `optionalDependencies`, and each loader had grown its
* own copy of the same try/catch: check `err.code`, substring-match the package
* name, rethrow a friendlier Error. The copies had drifted — some said
* `pnpm add`, one said `npm install`, and the phrasing varied per call site.
*
* Only missing-module failures are intercepted. A package that is installed but
* throws while evaluating (syntax error, ESM/CJS interop, a broken transitive
* dep) rethrows unchanged — turning those into "please install X" would send
* the caller after a package they already have. The same applies to specifiers
* that are not installable packages at all (relative paths, file/data URLs):
* they rethrow untouched rather than producing an unrunnable install command.
*
* @param pkg - Module specifier. The install hint is only produced for bare
* package names; anything else rethrows the loader's own error.
* @param feature - Human-readable capability name, used to open the message.
* @returns The imported module namespace, typed as `T` (`unknown` if the
* caller does not supply a type argument, since the shape cannot be known
* for a dynamic specifier).
* @throws The loader's original error, or an `Error` naming the package and
* the `pnpm add` command when a bare package is genuinely absent. The
* original failure is always preserved on `cause`.
*
* @example
* ```ts
* const ExcelJS = await tryImport<typeof import("exceljs")>(
* "exceljs",
* "Excel file processing",
* );
* ```
*/
export declare function tryImport<T = unknown>(pkg: string, feature: string): Promise<T>;