@poppinss/utils 6.4.0-0 → 6.5.0-0

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/README.md CHANGED
@@ -937,6 +937,16 @@ await Promise.all(
937
937
  )
938
938
  ```
939
939
 
940
+ #### importDefault
941
+ A helper function that assert a lazy import function output to have a `default export`, otherwise raises an exception.
942
+
943
+ We use dynamic default exports a lot in AdonisJS apps, so extracting the check to a helper function.
944
+
945
+ ```ts
946
+ import { importDefault } from '@poppinss/utils'
947
+ const defaultVal = await importDefault(() => import('./some_module.js'))
948
+ ```
949
+
940
950
  #### naturalSort
941
951
 
942
952
  A sorting function to use natural sort for ordering an array.
package/build/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  export { base64 } from './src/base64.js';
3
3
  export { compose } from './src/compose.js';
4
+ export { importDefault } from './src/import_default.js';
4
5
  export { defineStaticProperty } from './src/define_static_property.js';
5
6
  export { Exception, createError } from './src/exception.js';
6
7
  export { flatten } from './src/flatten.js';
package/build/index.js CHANGED
@@ -2,6 +2,7 @@ import { fileURLToPath } from 'node:url';
2
2
  import { dirname as pathDirname } from 'node:path';
3
3
  export { base64 } from './src/base64.js';
4
4
  export { compose } from './src/compose.js';
5
+ export { importDefault } from './src/import_default.js';
5
6
  export { defineStaticProperty } from './src/define_static_property.js';
6
7
  export { Exception, createError } from './src/exception.js';
7
8
  export { flatten } from './src/flatten.js';
@@ -0,0 +1,3 @@
1
+ export declare function importDefault<T extends object>(importFn: () => Promise<T>, filePath?: string): Promise<T extends {
2
+ default: infer A;
3
+ } ? A : never>;
@@ -0,0 +1,15 @@
1
+ import { RuntimeException } from './exceptions/runtime_exception.js';
2
+ export async function importDefault(importFn, filePath) {
3
+ const moduleExports = await importFn();
4
+ if (!('default' in moduleExports)) {
5
+ const errorMessage = filePath
6
+ ? `Missing "export default" in module "${filePath}"`
7
+ : `Missing "export default" from lazy import "${importFn}"`;
8
+ throw new RuntimeException(errorMessage, {
9
+ cause: {
10
+ source: importFn,
11
+ },
12
+ });
13
+ }
14
+ return moduleExports.default;
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@poppinss/utils",
3
- "version": "6.4.0-0",
3
+ "version": "6.5.0-0",
4
4
  "description": "Handy utilities for repetitive work",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
@@ -61,7 +61,7 @@
61
61
  "@japa/run-failed-tests": "^1.1.0",
62
62
  "@japa/runner": "^2.2.2",
63
63
  "@japa/spec-reporter": "^1.3.2",
64
- "@swc/core": "^1.3.27",
64
+ "@swc/core": "^1.3.29",
65
65
  "@types/fs-extra": "^11.0.1",
66
66
  "@types/node": "^18.11.18",
67
67
  "c8": "^7.12.0",