1
+ import type { Items , Resolver , Source , ResolveCallback } from "@aedart/contracts/config" ;
2
+ import { isPromise , isCallable } from "@aedart/support/reflections" ;
3
+ import { getErrorMessage } from "@aedart/support/exceptions" ;
4
+ import ResolveError from "../exceptions/ResolveError" ;
5
+ import UnsupportedSourceError from "../exceptions/UnsupportedSourceError" ;
6
+
7
+ /**
8
+ * Default Configuration Resolver
9
+ *
10
+ * @see {Resolver}
11
+ */
12
+ export default class DefaultResolver implements Resolver
13
+ {
14
+ /**
15
+ * Resolves configuration items from the given source
16
+ *
17
+ * @param {Source } source
18
+ *
19
+ * @returns {Promise<Items> }
20
+ *
21
+ * @throws {ResolveException }
22
+ */
23
+ public async resolve ( source : Source ) : Promise < Items >
24
+ {
25
+ if ( isPromise ( source ) ) {
26
+ return this . resolveItems ( source as Promise < Items > ) ;
27
+ }
28
+
29
+ if ( typeof source === 'object' ) {
30
+ return this . resolveItems ( new Promise < Items > ( ( resolve ) => {
31
+ resolve ( source as Items ) ;
32
+ } ) ) ;
33
+ }
34
+
35
+ if ( isCallable ( source ) ) {
36
+ return this . resolveItems ( ( source as ResolveCallback ) ( ) ) ;
37
+ }
38
+
39
+ throw new UnsupportedSourceError ( 'Unable to resolve configuration items from source' , { cause : { source : source } } ) ;
40
+ }
41
+
42
+ /**
43
+ * Resolves configuration items from the given promise
44
+ *
45
+ * @param {Promise<Items> } promise
46
+ *
47
+ * @returns {Promise<Items> }
48
+ *
49
+ * @protected
50
+ */
51
+ protected async resolveItems ( promise : Promise < Items > ) : Promise < Items >
52
+ {
53
+ try {
54
+ return await promise ;
55
+ } catch ( e ) {
56
+ const reason : string = getErrorMessage ( e ) ;
57
+ throw new ResolveError ( `Unable to resolve configuration items: ${ reason } ` , { cause : { source : promise } } ) ;
58
+ }
59
+ }
60
+ }
0 commit comments