@@ -16,6 +16,45 @@ function createFeatureDisabledError(feature: TwurpleFeatureName, decoratorName:
1616 )
1717}
1818
19+ function createMissingConfigError ( feature : TwurpleFeatureName ) : Error {
20+ return new Error (
21+ `Twurple feature "${ feature } " is enabled but "options.config" is missing.` ,
22+ )
23+ }
24+
25+ function createMissingAuthProviderError ( feature : TwurpleFeatureName ) : Error {
26+ return new Error (
27+ `Twurple feature "${ feature } " is enabled but "options.config.authProvider" is missing.` ,
28+ )
29+ }
30+
31+ function getErrorMessage ( error : unknown ) : string {
32+ if ( error instanceof Error ) {
33+ return error . message
34+ }
35+ return String ( error )
36+ }
37+
38+ function ensureTwurpleConfig (
39+ feature : TwurpleFeatureName ,
40+ options : TwurpleModuleOptions ,
41+ ) {
42+ const config = options ?. config
43+ if ( ! config ) {
44+ throw createMissingConfigError ( feature )
45+ }
46+ if ( ! config . authProvider ) {
47+ throw createMissingAuthProviderError ( feature )
48+ }
49+ return config
50+ }
51+
52+ function createConnectionInitError ( feature : TwurpleFeatureName , error : unknown ) : Error {
53+ return new Error (
54+ `Failed to initialize Twurple "${ feature } " connection: ${ getErrorMessage ( error ) } ` ,
55+ )
56+ }
57+
1958function createDisabledFeatureProxy < T > ( feature : TwurpleFeatureName , decoratorName : string ) : T {
2059 const throwFeatureDisabled = ( ) : never => {
2160 throw createFeatureDisabledError ( feature , decoratorName )
@@ -85,39 +124,41 @@ export async function createTwurpleApiConnection(options: TwurpleModuleOptions)
85124 if ( ! options ?. features ?. api ) {
86125 return createDisabledFeatureProxy ( 'api' , 'InjectTwurpleApi' )
87126 }
88- const { config } = options
89- Logger . verbose ( 'Initialize TwurpleApi connection singletion...' , 'TwurpleModule' )
90- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
91- // @ts -ignore
92- const { ApiClient } = await import ( '@twurple/api' )
93- return new ApiClient ( { authProvider : config ?. authProvider , ...config ?. features ?. api } )
127+ const config = ensureTwurpleConfig ( 'api' , options )
128+ Logger . verbose ( 'Initialize TwurpleApi connection singleton...' , 'TwurpleModule' )
129+ try {
130+ const { ApiClient } = await import ( '@twurple/api' )
131+ return new ApiClient ( { authProvider : config . authProvider , ...config . features ?. api } )
132+ } catch ( error ) {
133+ throw createConnectionInitError ( 'api' , error )
134+ }
94135}
95136
96137export async function createTwurpleChatConnection ( options : TwurpleModuleOptions ) {
97138 if ( ! options ?. features ?. chat ) {
98139 return createDisabledFeatureProxy ( 'chat' , 'InjectTwurpleChat' )
99140 }
100- const { config } = options
101- Logger . verbose ( 'Initialize TwurpleChat connection singletion...' , 'TwurpleModule' )
102- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
103- // @ts -ignore
104- const { ChatClient } = await import ( '@twurple/chat' )
105- return new ChatClient ( { authProvider : config ?. authProvider , ...config ?. features ?. chat } )
141+ const config = ensureTwurpleConfig ( 'chat' , options )
142+ Logger . verbose ( 'Initialize TwurpleChat connection singleton...' , 'TwurpleModule' )
143+ try {
144+ const { ChatClient } = await import ( '@twurple/chat' )
145+ return new ChatClient ( { authProvider : config . authProvider , ...config . features ?. chat } )
146+ } catch ( error ) {
147+ throw createConnectionInitError ( 'chat' , error )
148+ }
106149}
107150
108151
109152export async function createTwurplePubsubConnection ( options : TwurpleModuleOptions ) {
110153 if ( ! options ?. features ?. pubsub ) {
111154 return createDisabledFeatureProxy ( 'pubsub' , 'InjectTwurplePubsub' )
112155 }
113- const { config } = options
114- Logger . verbose ( 'Initialize TwurplePubsub connection singletion...' , 'TwurpleModule' )
115- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
116- // @ts -ignore
117- const { PubSubClient } = await import ( '@twurple/pubsub' )
156+ const config = ensureTwurpleConfig ( 'pubsub' , options )
157+ Logger . verbose ( 'Initialize TwurplePubsub connection singleton...' , 'TwurpleModule' )
118158 try {
119- return new PubSubClient ( { authProvider : config ?. authProvider , ...config ?. features ?. pubsub } )
120- } catch ( e ) {
121- console . error ( 'e' , e )
159+ const { PubSubClient } = await import ( '@twurple/pubsub' )
160+ return new PubSubClient ( { authProvider : config . authProvider , ...config . features ?. pubsub } )
161+ } catch ( error ) {
162+ throw createConnectionInitError ( 'pubsub' , error )
122163 }
123164}
0 commit comments