@nestjs/common 12.0.0-alpha.0 → 12.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/common",
3
- "version": "12.0.0-alpha.0",
3
+ "version": "12.0.0-alpha.1",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@common)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://nestjs.com",
@@ -19,3 +19,11 @@ export declare function loadPackageSync(packageName: string, context: string, lo
19
19
  * before calling this function (typically during `init()` or `compile()`).
20
20
  */
21
21
  export declare function loadPackageCached(packageName: string): any;
22
+ /**
23
+ * Attempts to load and cache a package. Returns the loaded module on success
24
+ * or `null` if the package is not installed.
25
+ *
26
+ * Unlike {@link loadPackage}, this function does **not** terminate the process
27
+ * when the package is missing, making it suitable for optional dependencies.
28
+ */
29
+ export declare function tryLoadPackage(packageName: string, loaderFn?: Function): Promise<any>;
@@ -1,6 +1,6 @@
1
1
  import { createRequire } from 'module';
2
2
  import { Logger } from '../services/logger.service.js';
3
- const MISSING_REQUIRED_DEPENDENCY = (name, reason) => `The "${name}" package is missing. Please, make sure to install it to take advantage of ${reason}.`;
3
+ const MISSING_REQUIRED_DEPENDENCY = (name, reason) => `The "${name}" package is missing. Please, make sure to install it to use ${reason}.`;
4
4
  const logger = new Logger('PackageLoader');
5
5
  /**
6
6
  * Cache of already-loaded packages keyed by package name.
@@ -68,3 +68,24 @@ export function loadPackageCached(packageName) {
68
68
  }
69
69
  return cached;
70
70
  }
71
+ /**
72
+ * Attempts to load and cache a package. Returns the loaded module on success
73
+ * or `null` if the package is not installed.
74
+ *
75
+ * Unlike {@link loadPackage}, this function does **not** terminate the process
76
+ * when the package is missing, making it suitable for optional dependencies.
77
+ */
78
+ export async function tryLoadPackage(packageName, loaderFn) {
79
+ const cached = packageCache.get(packageName);
80
+ if (cached) {
81
+ return cached;
82
+ }
83
+ try {
84
+ const pkg = loaderFn ? await loaderFn() : await import(packageName);
85
+ packageCache.set(packageName, pkg);
86
+ return pkg;
87
+ }
88
+ catch {
89
+ return null;
90
+ }
91
+ }