@@ -12,6 +12,7 @@ import type {
1212import type {
1313 AccessToken ,
1414 BaseUrl ,
15+ DrupalClientAuth ,
1516 DrupalClientAuthAccessToken ,
1617 DrupalClientAuthClientIdSecret ,
1718 DrupalClientAuthUsernamePassword ,
@@ -53,25 +54,28 @@ const DEFAULT_HEADERS = {
5354}
5455
5556function isBasicAuth (
56- auth : DrupalClientOptions [ "auth" ]
57+ auth : DrupalClientAuth
5758) : auth is DrupalClientAuthUsernamePassword {
5859 return (
59- ( auth as DrupalClientAuthUsernamePassword ) ?. username !== undefined ||
60+ ( auth as DrupalClientAuthUsernamePassword ) ?. username !== undefined &&
6061 ( auth as DrupalClientAuthUsernamePassword ) ?. password !== undefined
6162 )
6263}
6364
6465function isAccessTokenAuth (
65- auth : DrupalClientOptions [ "auth" ]
66+ auth : DrupalClientAuth
6667) : auth is DrupalClientAuthAccessToken {
67- return ( auth as DrupalClientAuthAccessToken ) ?. access_token !== undefined
68+ return (
69+ ( auth as DrupalClientAuthAccessToken ) ?. access_token !== undefined &&
70+ ( auth as DrupalClientAuthAccessToken ) ?. token_type !== undefined
71+ )
6872}
6973
7074function isClientIdSecretAuth (
71- auth : DrupalClient [ "auth" ]
75+ auth : DrupalClientAuth
7276) : auth is DrupalClientAuthClientIdSecret {
7377 return (
74- ( auth as DrupalClientAuthClientIdSecret ) ?. clientId !== undefined ||
78+ ( auth as DrupalClientAuthClientIdSecret ) ?. clientId !== undefined &&
7579 ( auth as DrupalClientAuthClientIdSecret ) ?. clientSecret !== undefined
7680 )
7781}
@@ -177,31 +181,47 @@ export class DrupalClient {
177181
178182 set auth ( auth : DrupalClientOptions [ "auth" ] ) {
179183 if ( typeof auth === "object" ) {
180- if ( isBasicAuth ( auth ) ) {
181- if ( ! auth . username || ! auth . password ) {
184+ const checkUsernamePassword = auth as DrupalClientAuthUsernamePassword
185+ const checkAccessToken = auth as DrupalClientAuthAccessToken
186+ const checkClientIdSecret = auth as DrupalClientAuthClientIdSecret
187+
188+ if (
189+ checkUsernamePassword . username !== undefined ||
190+ checkUsernamePassword . password !== undefined
191+ ) {
192+ if (
193+ ! checkUsernamePassword . username ||
194+ ! checkUsernamePassword . password
195+ ) {
182196 throw new Error (
183197 `'username' and 'password' are required for auth. See https://next-drupal.org/docs/client/auth`
184198 )
185199 }
186- } else if ( isAccessTokenAuth ( auth ) ) {
187- if ( ! auth . access_token || ! auth . token_type ) {
200+ } else if (
201+ checkAccessToken . access_token !== undefined ||
202+ checkAccessToken . token_type !== undefined
203+ ) {
204+ if ( ! checkAccessToken . access_token || ! checkAccessToken . token_type ) {
188205 throw new Error (
189206 `'access_token' and 'token_type' are required for auth. See https://next-drupal.org/docs/client/auth`
190207 )
191208 }
192- } else if ( ! auth . clientId || ! auth . clientSecret ) {
209+ } else if (
210+ ! checkClientIdSecret . clientId ||
211+ ! checkClientIdSecret . clientSecret
212+ ) {
193213 throw new Error (
194214 `'clientId' and 'clientSecret' are required for auth. See https://next-drupal.org/docs/client/auth`
195215 )
196216 }
197217
198- auth = {
199- url : DEFAULT_AUTH_URL ,
218+ this . _auth = {
219+ ... ( isClientIdSecretAuth ( auth ) ? { url : DEFAULT_AUTH_URL } : { } ) ,
200220 ...auth ,
201221 }
222+ } else {
223+ this . _auth = auth
202224 }
203-
204- this . _auth = auth
205225 }
206226
207227 set headers ( value : DrupalClientOptions [ "headers" ] ) {
@@ -1356,26 +1376,25 @@ export class DrupalClient {
13561376 return this . accessToken
13571377 }
13581378
1359- if ( ! opts ?. clientId || ! opts ?. clientSecret ) {
1360- if ( typeof this . _auth === "undefined" ) {
1361- throw new Error (
1362- "auth is not configured. See https://next-drupal.org/docs/client/auth"
1363- )
1379+ let auth : DrupalClientAuthClientIdSecret
1380+ if ( isClientIdSecretAuth ( opts ) ) {
1381+ auth = {
1382+ url : DEFAULT_AUTH_URL ,
1383+ ... opts ,
13641384 }
1365- }
1366-
1367- if (
1368- ! isClientIdSecretAuth ( this . _auth ) ||
1369- ( opts && ! isClientIdSecretAuth ( opts ) )
1370- ) {
1385+ } else if ( isClientIdSecretAuth ( this . _auth ) ) {
1386+ auth = this . _auth
1387+ } else if ( typeof this . _auth === "undefined" ) {
1388+ throw new Error (
1389+ "auth is not configured. See https://next-drupal.org/docs/client/auth"
1390+ )
1391+ } else {
13711392 throw new Error (
13721393 `'clientId' and 'clientSecret' required. See https://next-drupal.org/docs/client/auth`
13731394 )
13741395 }
13751396
1376- const clientId = opts ?. clientId || this . _auth . clientId
1377- const clientSecret = opts ?. clientSecret || this . _auth . clientSecret
1378- const url = this . buildUrl ( opts ?. url || this . _auth . url )
1397+ const url = this . buildUrl ( auth . url )
13791398
13801399 if (
13811400 this . accessTokenScope === opts ?. scope &&
@@ -1388,7 +1407,9 @@ export class DrupalClient {
13881407
13891408 this . debug ( `Fetching new access token.` )
13901409
1391- const basic = Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( "base64" )
1410+ const basic = Buffer . from ( `${ auth . clientId } :${ auth . clientSecret } ` ) . toString (
1411+ "base64"
1412+ )
13921413
13931414 let body = `grant_type=client_credentials`
13941415
0 commit comments