@fastify/error 3.1.0 → 3.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/error",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "devDependencies": {
30
30
  "standard": "^17.0.0",
31
31
  "tap": "^16.0.0",
32
- "tsd": "^0.24.1"
32
+ "tsd": "^0.25.0"
33
33
  },
34
34
  "tsd": {
35
35
  "compilerOptions": {
package/types/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- type CreateError = (code: string, message: string, statusCode?: number, Base?: Error) => createError.FastifyErrorConstructor
1
+ declare function createError<C extends string, SC extends number>(code: C, message: string, statusCode: SC, Base?: Error): createError.FastifyErrorConstructor<{ code: C; statusCode: SC }>;
2
+ declare function createError<C extends string>(code: C, message: string, statusCode?: number, Base?: Error): createError.FastifyErrorConstructor<{ code: C; }>;
3
+
4
+ type CreateError = typeof createError;
2
5
 
3
6
  declare namespace createError {
4
7
  export interface FastifyError extends Error {
@@ -7,15 +10,14 @@ declare namespace createError {
7
10
  statusCode?: number;
8
11
  }
9
12
 
10
- export interface FastifyErrorConstructor {
11
- new(a?: any, b?: any, c?: any): FastifyError;
12
- (a?: any, b?: any, c?: any): FastifyError;
13
- readonly prototype: FastifyError;
13
+ export interface FastifyErrorConstructor<E extends { code: string; statusCode?: number } = { code: string; statusCode?: number }> {
14
+ new (a?: any, b?: any, c?: any): FastifyError & E;
15
+ (a?: any, b?: any, c?: any): FastifyError & E;
16
+ readonly prototype: FastifyError & E;
14
17
  }
15
18
 
16
19
  export const createError: CreateError
17
20
  export { createError as default }
18
21
  }
19
22
 
20
- declare function createError(...params: Parameters<CreateError>): ReturnType<CreateError>
21
23
  export = createError
@@ -2,9 +2,17 @@ import createError, { FastifyError, FastifyErrorConstructor } from '..'
2
2
  import { expectType } from 'tsd'
3
3
 
4
4
  const CustomError = createError('ERROR_CODE', 'message')
5
- expectType<FastifyErrorConstructor>(CustomError)
5
+ expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomError)
6
6
  const err = new CustomError()
7
- expectType<FastifyError>(err)
8
- expectType<string>(err.code)
7
+ expectType<FastifyError & { code: 'ERROR_CODE' }>(err)
8
+ expectType<'ERROR_CODE'>(err.code)
9
9
  expectType<string>(err.message)
10
- expectType<number>(err.statusCode!)
10
+ expectType<number>(err.statusCode!)
11
+
12
+ const CustomTypedError = createError('OTHER_CODE', 'message', 400)
13
+ expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>>(CustomTypedError)
14
+ const typed = new CustomTypedError()
15
+ expectType<FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }>(typed)
16
+ expectType<'OTHER_CODE'>(typed.code)
17
+ expectType<string>(typed.message)
18
+ expectType<400>(typed.statusCode)