@poppinss/utils 6.1.0-0 → 6.2.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
@@ -771,19 +771,18 @@ class ResourceNotFound extends Exception {
771
771
  throw new ResourceNotFound()
772
772
  ```
773
773
 
774
- Following is an example of creating a dynamic message with interpolation.
774
+ #### Anonymous error classes
775
+ You can also create an anonymous exception class using the `createError` method. The return value is a class
776
+ constructor that accepts an array of values to use for interpolation.
775
777
 
776
- ```ts
777
- import { Exception } from '@poppinss/utils'
778
- import string from '@poppinss/utils/string'
778
+ The interpolation of error message is performed using the `util.format` message.
779
779
 
780
- class ResourceNotFound extends Exception {
781
- static code = 'E_RESOURCE_NOT_FOUND'
782
- static status = 404
783
- static message = 'Unable to find resource with id {{ id }}'
784
- }
780
+ ```ts
781
+ import { createError } from '@poppinss/utils'
782
+ const E_RESOURCE_NOT_FOUND = createError('Unable to find resource with id %d', 'E_RESOURCE_NOT_FOUND')
785
783
 
786
- throw new ResourceNotFound(interpolate(ResourceNotFound.message, { id: 1 }))
784
+ const id = 1
785
+ throw new E_RESOURCE_NOT_FOUND([id])
787
786
  ```
788
787
 
789
788
  #### flatten
package/build/index.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  export { base64 } from './src/base64.js';
3
3
  export { compose } from './src/compose.js';
4
4
  export { defineStaticProperty } from './src/define_static_property.js';
5
- export { Exception } from './src/exception.js';
5
+ export { Exception, createError } from './src/exception.js';
6
6
  export { flatten } from './src/flatten.js';
7
7
  export { fsImportAll } from './src/fs_import_all.js';
8
8
  export { fsReadAll } from './src/fs_read_all.js';
package/build/index.js CHANGED
@@ -3,7 +3,7 @@ import { dirname as pathDirname } from 'node:path';
3
3
  export { base64 } from './src/base64.js';
4
4
  export { compose } from './src/compose.js';
5
5
  export { defineStaticProperty } from './src/define_static_property.js';
6
- export { Exception } from './src/exception.js';
6
+ export { Exception, createError } from './src/exception.js';
7
7
  export { flatten } from './src/flatten.js';
8
8
  export { fsImportAll } from './src/fs_import_all.js';
9
9
  export { fsReadAll } from './src/fs_read_all.js';
@@ -14,3 +14,8 @@ export declare class Exception extends Error {
14
14
  get [Symbol.toStringTag](): string;
15
15
  toString(): string;
16
16
  }
17
+ export declare function createError<T extends any[] = never>(message: string, code: string, status?: number): typeof Exception & T extends never ? {
18
+ new (args?: any, options?: ErrorOptions): Exception;
19
+ } : {
20
+ new (args: T, options?: ErrorOptions): Exception;
21
+ };
@@ -1,3 +1,4 @@
1
+ import { format } from 'node:util';
1
2
  export class Exception extends Error {
2
3
  name;
3
4
  status;
@@ -27,3 +28,13 @@ export class Exception extends Error {
27
28
  return `${this.name}: ${this.message}`;
28
29
  }
29
30
  }
31
+ export function createError(message, code, status) {
32
+ return class extends Exception {
33
+ static message = message;
34
+ static code = code;
35
+ static status = status;
36
+ constructor(args, options) {
37
+ super(format(message, ...(args || [])), options);
38
+ }
39
+ };
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@poppinss/utils",
3
- "version": "6.1.0-0",
3
+ "version": "6.2.0-0",
4
4
  "description": "Handy utilities for repetitive work",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
@@ -60,25 +60,25 @@
60
60
  "@japa/run-failed-tests": "^1.1.0",
61
61
  "@japa/runner": "^2.2.2",
62
62
  "@japa/spec-reporter": "^1.3.2",
63
- "@swc/core": "^1.3.20",
63
+ "@swc/core": "^1.3.23",
64
64
  "@types/fs-extra": "^9.0.13",
65
- "@types/node": "^18.11.7",
65
+ "@types/node": "^18.11.15",
66
66
  "c8": "^7.12.0",
67
67
  "del-cli": "^5.0.0",
68
- "eslint": "^8.28.0",
68
+ "eslint": "^8.29.0",
69
69
  "eslint-config-prettier": "^8.5.0",
70
70
  "eslint-plugin-adonis": "^3.0.3",
71
71
  "eslint-plugin-prettier": "^4.2.1",
72
- "fs-extra": "^11.0.0",
72
+ "fs-extra": "^11.1.0",
73
73
  "github-label-sync": "^2.2.0",
74
74
  "husky": "^8.0.1",
75
75
  "lodash": "^4.17.21",
76
76
  "lodash-cli": "^4.17.5",
77
77
  "move-file-cli": "^3.0.0",
78
78
  "np": "^7.6.2",
79
- "prettier": "^2.8.0",
79
+ "prettier": "^2.8.1",
80
80
  "ts-node": "^10.9.1",
81
- "typescript": "^4.9.3"
81
+ "typescript": "^4.9.4"
82
82
  },
83
83
  "dependencies": {
84
84
  "@lukeed/ms": "^2.0.0",
@@ -89,7 +89,7 @@
89
89
  "flattie": "^1.1.0",
90
90
  "pluralize": "^8.0.0",
91
91
  "safe-stable-stringify": "^2.4.1",
92
- "secure-json-parse": "^2.5.0",
92
+ "secure-json-parse": "^2.6.0",
93
93
  "slash": "^5.0.0",
94
94
  "slugify": "^1.6.5",
95
95
  "truncatise": "^0.0.8"