@@ -4,19 +4,37 @@ import { FailureConverter } from '../converter/failure-converter';
4
4
import { errorCode , hasOwnProperty , isRecord } from '../type-helpers' ;
5
5
import { ValueError } from '../errors' ;
6
6
7
- const isValidPayloadConverter = ( converter : unknown ) : converter is PayloadConverter =>
8
- typeof converter === 'object' &&
9
- converter !== null &&
10
- [ 'toPayload' , 'fromPayload' ] . every ( ( method ) => typeof ( converter as Record < string , unknown > ) [ method ] === 'function' ) ;
7
+ const isValidPayloadConverter = ( converter : unknown , path : string ) : asserts converter is PayloadConverter => {
8
+ const isValid =
9
+ typeof converter === 'object' &&
10
+ converter !== null &&
11
+ [ 'toPayload' , 'fromPayload' ] . every (
12
+ ( method ) => typeof ( converter as Record < string , unknown > ) [ method ] === 'function'
13
+ ) ;
14
+ if ( ! isValid ) {
15
+ throw new ValueError ( `payloadConverter export at ${ path } must be an object with toPayload and fromPayload methods` ) ;
16
+ }
17
+ } ;
11
18
12
- const isValidFailureConverter = ( converter : unknown ) : converter is FailureConverter =>
13
- typeof converter === 'object' &&
14
- converter !== null &&
15
- [ 'errorToFailure' , 'failureToError' ] . every (
16
- ( method ) => typeof ( converter as Record < string , unknown > ) [ method ] === 'function'
17
- ) ;
19
+ const isValidFailureConverter = ( converter : unknown , path : string ) : asserts converter is FailureConverter => {
20
+ const isValid =
21
+ typeof converter === 'object' &&
22
+ converter !== null &&
23
+ [ 'errorToFailure' , 'failureToError' ] . every (
24
+ ( method ) => typeof ( converter as Record < string , unknown > ) [ method ] === 'function'
25
+ ) ;
26
+ if ( ! isValid ) {
27
+ throw new ValueError (
28
+ `failureConverter export at ${ path } must be an object with errorToFailure and failureToError methods`
29
+ ) ;
30
+ }
31
+ } ;
18
32
19
- function requireConverter < T > ( path : string , type : string , validator : ( converter : unknown ) => converter is T ) : T {
33
+ function requireConverter < T > (
34
+ path : string ,
35
+ type : string ,
36
+ validator : ( converter : unknown , path : string ) => asserts converter is T
37
+ ) : T {
20
38
let module ;
21
39
try {
22
40
module = require ( path ) ; // eslint-disable-line @typescript-eslint/no-var-requires
@@ -29,15 +47,10 @@ function requireConverter<T>(path: string, type: string, validator: (converter:
29
47
30
48
if ( isRecord ( module ) && hasOwnProperty ( module , type ) ) {
31
49
const converter = module [ type ] ;
32
- if ( validator ( converter ) ) {
33
- return converter ;
34
- } else {
35
- throw new ValueError (
36
- `payloadConverter export at ${ path } must be an object with toPayload and fromPayload methods`
37
- ) ;
38
- }
50
+ validator ( converter , path ) ;
51
+ return converter ;
39
52
} else {
40
- throw new ValueError ( `Module ${ path } does not have a \`payloadConverter \` named export` ) ;
53
+ throw new ValueError ( `Module ${ path } does not have a \`${ type } \` named export` ) ;
41
54
}
42
55
}
43
56
0 commit comments