@@ -77,10 +77,10 @@ function isClientIdSecretAuth(
77
77
export class DrupalClient {
78
78
baseUrl : BaseUrl
79
79
80
- debug : DrupalClientOptions [ "debug" ]
81
-
82
80
frontPage : DrupalClientOptions [ "frontPage" ]
83
81
82
+ private isDebugEnabled : DrupalClientOptions [ "debug" ]
83
+
84
84
private serializer : DrupalClientOptions [ "serializer" ]
85
85
86
86
private cache : DrupalClientOptions [ "cache" ]
@@ -148,7 +148,7 @@ export class DrupalClient {
148
148
this . apiPrefix = apiPrefix
149
149
this . serializer = serializer
150
150
this . frontPage = frontPage
151
- this . debug = debug
151
+ this . isDebugEnabled = ! ! debug
152
152
this . useDefaultResourceTypeEntry = useDefaultResourceTypeEntry
153
153
this . fetcher = fetcher
154
154
this . auth = auth
@@ -166,7 +166,7 @@ export class DrupalClient {
166
166
this . throwJsonApiErrors = false
167
167
}
168
168
169
- this . _debug ( "Debug mode is on." )
169
+ this . debug ( "Debug mode is on." )
170
170
}
171
171
172
172
set apiPrefix ( apiPrefix : DrupalClientOptions [ "apiPrefix" ] ) {
@@ -228,7 +228,7 @@ export class DrupalClient {
228
228
// Using the auth set on the client.
229
229
// TODO: Abstract this to a re-usable.
230
230
if ( init ?. withAuth ) {
231
- this . _debug ( `Using authenticated request.` )
231
+ this . debug ( `Using authenticated request.` )
232
232
233
233
if ( init . withAuth === true ) {
234
234
if ( typeof this . _auth === "undefined" ) {
@@ -240,15 +240,15 @@ export class DrupalClient {
240
240
// By default, if withAuth is set to true, we use the auth configured
241
241
// in the client constructor.
242
242
if ( typeof this . _auth === "function" ) {
243
- this . _debug ( `Using custom auth callback.` )
243
+ this . debug ( `Using custom auth callback.` )
244
244
245
245
init [ "headers" ] [ "Authorization" ] = this . _auth ( )
246
246
} else if ( typeof this . _auth === "string" ) {
247
- this . _debug ( `Using custom authorization header.` )
247
+ this . debug ( `Using custom authorization header.` )
248
248
249
249
init [ "headers" ] [ "Authorization" ] = this . _auth
250
250
} else if ( typeof this . _auth === "object" ) {
251
- this . _debug ( `Using custom auth credentials.` )
251
+ this . debug ( `Using custom auth credentials.` )
252
252
253
253
if ( isBasicAuth ( this . _auth ) ) {
254
254
const basic = Buffer . from (
@@ -258,7 +258,7 @@ export class DrupalClient {
258
258
init [ "headers" ] [ "Authorization" ] = `Basic ${ basic } `
259
259
} else if ( isClientIdSecretAuth ( this . _auth ) ) {
260
260
// Use the built-in client_credentials grant.
261
- this . _debug ( `Using default auth (client_credentials).` )
261
+ this . debug ( `Using default auth (client_credentials).` )
262
262
263
263
// Fetch an access token and add it to the request.
264
264
// Access token can be fetched from cache or using a custom auth method.
@@ -272,15 +272,15 @@ export class DrupalClient {
272
272
}
273
273
}
274
274
} else if ( typeof init . withAuth === "string" ) {
275
- this . _debug ( `Using custom authorization header.` )
275
+ this . debug ( `Using custom authorization header.` )
276
276
277
277
init [ "headers" ] [ "Authorization" ] = init . withAuth
278
278
} else if ( typeof init . withAuth === "function" ) {
279
- this . _debug ( `Using custom authorization callback.` )
279
+ this . debug ( `Using custom authorization callback.` )
280
280
281
281
init [ "headers" ] [ "Authorization" ] = init . withAuth ( )
282
282
} else if ( isBasicAuth ( init . withAuth ) ) {
283
- this . _debug ( `Using basic authorization header` )
283
+ this . debug ( `Using basic authorization header. ` )
284
284
285
285
const basic = Buffer . from (
286
286
`${ init . withAuth . username } :${ init . withAuth . password } `
@@ -301,12 +301,12 @@ export class DrupalClient {
301
301
}
302
302
303
303
if ( this . fetcher ) {
304
- this . _debug ( `Using custom fetcher. ` )
304
+ this . debug ( `Using custom fetcher, fetching: ${ input } ` )
305
305
306
306
return await this . fetcher ( input , init )
307
307
}
308
308
309
- this . _debug ( `Using default fetch (polyfilled by Next.js). ` )
309
+ this . debug ( `Using default fetch, fetching: ${ input } ` )
310
310
311
311
return await fetch ( input , init )
312
312
}
@@ -329,8 +329,7 @@ export class DrupalClient {
329
329
330
330
const url = this . buildUrl ( apiPath , options ?. params )
331
331
332
- this . _debug ( `Creating resource of type ${ type } .` )
333
- this . _debug ( url . toString ( ) )
332
+ this . debug ( `Creating resource of type ${ type } .` )
334
333
335
334
// Add type to body.
336
335
body . data . type = type
@@ -373,8 +372,7 @@ export class DrupalClient {
373
372
options ?. params
374
373
)
375
374
376
- this . _debug ( `Creating file resource for media of type ${ type } .` )
377
- this . _debug ( url . toString ( ) )
375
+ this . debug ( `Creating file resource for media of type ${ type } .` )
378
376
379
377
const response = await this . fetch ( url . toString ( ) , {
380
378
method : "POST" ,
@@ -415,8 +413,7 @@ export class DrupalClient {
415
413
416
414
const url = this . buildUrl ( `${ apiPath } /${ uuid } ` , options ?. params )
417
415
418
- this . _debug ( `Updating resource of type ${ type } with id ${ uuid } .` )
419
- this . _debug ( url . toString ( ) )
416
+ this . debug ( `Updating resource of type ${ type } with id ${ uuid } .` )
420
417
421
418
// Update body.
422
419
body . data . type = type
@@ -455,8 +452,7 @@ export class DrupalClient {
455
452
456
453
const url = this . buildUrl ( `${ apiPath } /${ uuid } ` , options ?. params )
457
454
458
- this . _debug ( `Deleting resource of type ${ type } with id ${ uuid } .` )
459
- this . _debug ( url . toString ( ) )
455
+ this . debug ( `Deleting resource of type ${ type } with id ${ uuid } .` )
460
456
461
457
const response = await this . fetch ( url . toString ( ) , {
462
458
method : "DELETE" ,
@@ -489,7 +485,7 @@ export class DrupalClient {
489
485
const cached = ( await this . cache . get ( options . cacheKey ) ) as string
490
486
491
487
if ( cached ) {
492
- this . _debug ( `Returning cached resource ${ type } with id ${ uuid } ` )
488
+ this . debug ( `Returning cached resource ${ type } with id ${ uuid } . ` )
493
489
494
490
const json = JSON . parse ( cached )
495
491
@@ -504,8 +500,7 @@ export class DrupalClient {
504
500
505
501
const url = this . buildUrl ( `${ apiPath } /${ uuid } ` , options ?. params )
506
502
507
- this . _debug ( `Fetching resource ${ type } with id ${ uuid } .` )
508
- this . _debug ( url . toString ( ) )
503
+ this . debug ( `Fetching resource ${ type } with id ${ uuid } .` )
509
504
510
505
const response = await this . fetch ( url . toString ( ) , {
511
506
withAuth : options . withAuth ,
@@ -687,6 +682,8 @@ export class DrupalClient {
687
682
_format : "json" ,
688
683
} )
689
684
685
+ this . debug ( `Fetching resource by path, ${ path } .` )
686
+
690
687
const response = await this . fetch ( url . toString ( ) , {
691
688
method : "POST" ,
692
689
credentials : "include" ,
@@ -739,8 +736,7 @@ export class DrupalClient {
739
736
...options ?. params ,
740
737
} )
741
738
742
- this . _debug ( `Fetching resource collection of type ${ type } ` )
743
- this . _debug ( url . toString ( ) )
739
+ this . debug ( `Fetching resource collection of type ${ type } .` )
744
740
745
741
const response = await this . fetch ( url . toString ( ) , {
746
742
withAuth : options . withAuth ,
@@ -910,6 +906,8 @@ export class DrupalClient {
910
906
path,
911
907
} )
912
908
909
+ this . debug ( `Fetching translated path, ${ path } .` )
910
+
913
911
const response = await this . fetch ( url . toString ( ) , {
914
912
withAuth : options . withAuth ,
915
913
} )
@@ -990,6 +988,8 @@ export class DrupalClient {
990
988
)
991
989
992
990
try {
991
+ this . debug ( `Fetching JSON:API index.` )
992
+
993
993
const response = await this . fetch ( url . toString ( ) , {
994
994
// As per https://www.drupal.org/node/2984034 /jsonapi is public.
995
995
withAuth : false ,
@@ -1123,7 +1123,7 @@ export class DrupalClient {
1123
1123
const cached = ( await this . cache . get ( options . cacheKey ) ) as string
1124
1124
1125
1125
if ( cached ) {
1126
- this . _debug ( `Returning cached menu items for ${ name } ` )
1126
+ this . debug ( `Returning cached menu items for ${ name } . ` )
1127
1127
return JSON . parse ( cached )
1128
1128
}
1129
1129
}
@@ -1138,8 +1138,7 @@ export class DrupalClient {
1138
1138
options . params
1139
1139
)
1140
1140
1141
- this . _debug ( `Fetching menu items for ${ name } .` )
1142
- this . _debug ( url . toString ( ) )
1141
+ this . debug ( `Fetching menu items for ${ name } .` )
1143
1142
1144
1143
const response = await this . fetch ( url . toString ( ) , {
1145
1144
withAuth : options . withAuth ,
@@ -1212,6 +1211,8 @@ export class DrupalClient {
1212
1211
options . params
1213
1212
)
1214
1213
1214
+ this . debug ( `Fetching view, ${ viewId } .${ displayId } .` )
1215
+
1215
1216
const response = await this . fetch ( url . toString ( ) , {
1216
1217
withAuth : options . withAuth ,
1217
1218
} )
@@ -1252,6 +1253,8 @@ export class DrupalClient {
1252
1253
options . params
1253
1254
)
1254
1255
1256
+ this . debug ( `Fetching search index, ${ name } .` )
1257
+
1255
1258
const response = await this . fetch ( url . toString ( ) , {
1256
1259
withAuth : options . withAuth ,
1257
1260
} )
@@ -1330,11 +1333,11 @@ export class DrupalClient {
1330
1333
this . _token &&
1331
1334
Date . now ( ) < this . tokenExpiresOn
1332
1335
) {
1333
- this . _debug ( `Using existing access token.` )
1336
+ this . debug ( `Using existing access token.` )
1334
1337
return this . _token
1335
1338
}
1336
1339
1337
- this . _debug ( `Fetching new access token.` )
1340
+ this . debug ( `Fetching new access token.` )
1338
1341
1339
1342
const basic = Buffer . from ( `${ clientId } :${ clientSecret } ` ) . toString ( "base64" )
1340
1343
@@ -1343,7 +1346,7 @@ export class DrupalClient {
1343
1346
if ( opts ?. scope ) {
1344
1347
body = `${ body } &scope=${ opts . scope } `
1345
1348
1346
- this . _debug ( `Using scope: ${ opts . scope } ` )
1349
+ this . debug ( `Using scope: ${ opts . scope } ` )
1347
1350
}
1348
1351
1349
1352
const response = await this . fetch ( url . toString ( ) , {
@@ -1362,8 +1365,6 @@ export class DrupalClient {
1362
1365
1363
1366
const result : AccessToken = await response . json ( )
1364
1367
1365
- this . _debug ( result )
1366
-
1367
1368
this . token = result
1368
1369
1369
1370
this . accessTokenScope = opts ?. scope
@@ -1411,8 +1412,8 @@ export class DrupalClient {
1411
1412
return message
1412
1413
}
1413
1414
1414
- private _debug ( message ) {
1415
- ! ! this . debug && this . logger . debug ( message )
1415
+ debug ( message ) {
1416
+ this . isDebugEnabled && this . logger . debug ( message )
1416
1417
}
1417
1418
1418
1419
// Error handling.
0 commit comments