@@ -2,58 +2,54 @@ import { type Callback } from '../../apis/index.ts'
22import { AuthenticationError } from '../../errors.ts'
33import { type SASLCredentialProvider } from '../../network/connection.ts'
44
5- export function getCredential (
5+ export function getCredential < T > (
66 label : string ,
7- credentialOrProvider : string | SASLCredentialProvider ,
8- callback : Callback < string >
7+ credentialOrProvider : T | SASLCredentialProvider < T > ,
8+ callback : Callback < T >
99) : void {
10- if ( typeof credentialOrProvider === 'string ' ) {
11- callback ( null , credentialOrProvider )
10+ if ( typeof credentialOrProvider === 'undefined ' ) {
11+ callback ( new AuthenticationError ( `The ${ label } should be a value or a function.` ) , undefined as unknown as T )
1212 return
1313 } else if ( typeof credentialOrProvider !== 'function' ) {
14- callback ( new AuthenticationError ( `The ${ label } should be a string or a function.` ) , undefined as unknown as string )
14+ callback ( null , credentialOrProvider )
1515 return
1616 }
1717
1818 try {
19- const credential = credentialOrProvider ( )
19+ const credential = ( credentialOrProvider as SASLCredentialProvider < T > ) ( )
2020
21- if ( typeof credential === 'string' ) {
22- callback ( null , credential )
23- return
24- } else if ( typeof ( credential as Promise < string > ) ?. then !== 'function' ) {
21+ if ( credential == null ) {
2522 callback (
26- new AuthenticationError ( `The ${ label } provider should return a string or a promise that resolves to a string .` ) ,
27- undefined as unknown as string
23+ new AuthenticationError ( `The ${ label } provider should return a string or a promise that resolves to a value .` ) ,
24+ undefined as unknown as T
2825 )
29-
26+ return
27+ } else if ( typeof ( credential as Promise < string > ) ?. then !== 'function' ) {
28+ callback ( null , credential as T )
3029 return
3130 }
3231
33- credential
34- . then ( token => {
35- if ( typeof token ! == 'string' ) {
32+ ; ( credential as Promise < T > )
33+ . then ( ( result : T ) => {
34+ if ( result == null ) {
3635 process . nextTick (
3736 callback ,
38- new AuthenticationError ( `The ${ label } provider should resolve to a string .` ) ,
37+ new AuthenticationError ( `The ${ label } provider should resolve to a value .` ) ,
3938 undefined as unknown as string
4039 )
4140
4241 return
4342 }
4443
45- process . nextTick ( callback , null , token )
44+ process . nextTick ( callback , null , result )
4645 } )
47- . catch ( error => {
48- process . nextTick (
49- callback ,
50- new AuthenticationError ( `The ${ label } provider threw an error.` , { cause : error as Error } )
51- )
46+ . catch ( ( error : Error ) => {
47+ process . nextTick ( callback , new AuthenticationError ( `The ${ label } provider threw an error.` , { cause : error } ) )
5248 } )
5349 } catch ( error ) {
5450 callback (
5551 new AuthenticationError ( `The ${ label } provider threw an error.` , { cause : error as Error } ) ,
56- undefined as unknown as string
52+ undefined as unknown as T
5753 )
5854 }
5955}
0 commit comments