Skip to content

Commit e0fc4bd

Browse files
committed
feat(next-drupal)!: add public debug() method to DrupalClient
Fixes #668 BREAKING CHANGE: DrupalClient previously had a debug property that indicated if debugging was enabled. This property has been removed and replaced with a debug() method that accepts a string and logs it as a debug message if debugging is enabled.
1 parent 61f723b commit e0fc4bd

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

packages/next-drupal/src/client.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ function isClientIdSecretAuth(
7777
export class DrupalClient {
7878
baseUrl: BaseUrl
7979

80-
debug: DrupalClientOptions["debug"]
81-
8280
frontPage: DrupalClientOptions["frontPage"]
8381

82+
private isDebugEnabled: DrupalClientOptions["debug"]
83+
8484
private serializer: DrupalClientOptions["serializer"]
8585

8686
private cache: DrupalClientOptions["cache"]
@@ -148,7 +148,7 @@ export class DrupalClient {
148148
this.apiPrefix = apiPrefix
149149
this.serializer = serializer
150150
this.frontPage = frontPage
151-
this.debug = debug
151+
this.isDebugEnabled = !!debug
152152
this.useDefaultResourceTypeEntry = useDefaultResourceTypeEntry
153153
this.fetcher = fetcher
154154
this.auth = auth
@@ -166,7 +166,7 @@ export class DrupalClient {
166166
this.throwJsonApiErrors = false
167167
}
168168

169-
this._debug("Debug mode is on.")
169+
this.debug("Debug mode is on.")
170170
}
171171

172172
set apiPrefix(apiPrefix: DrupalClientOptions["apiPrefix"]) {
@@ -228,7 +228,7 @@ export class DrupalClient {
228228
// Using the auth set on the client.
229229
// TODO: Abstract this to a re-usable.
230230
if (init?.withAuth) {
231-
this._debug(`Using authenticated request.`)
231+
this.debug(`Using authenticated request.`)
232232

233233
if (init.withAuth === true) {
234234
if (typeof this._auth === "undefined") {
@@ -240,15 +240,15 @@ export class DrupalClient {
240240
// By default, if withAuth is set to true, we use the auth configured
241241
// in the client constructor.
242242
if (typeof this._auth === "function") {
243-
this._debug(`Using custom auth callback.`)
243+
this.debug(`Using custom auth callback.`)
244244

245245
init["headers"]["Authorization"] = this._auth()
246246
} else if (typeof this._auth === "string") {
247-
this._debug(`Using custom authorization header.`)
247+
this.debug(`Using custom authorization header.`)
248248

249249
init["headers"]["Authorization"] = this._auth
250250
} else if (typeof this._auth === "object") {
251-
this._debug(`Using custom auth credentials.`)
251+
this.debug(`Using custom auth credentials.`)
252252

253253
if (isBasicAuth(this._auth)) {
254254
const basic = Buffer.from(
@@ -258,7 +258,7 @@ export class DrupalClient {
258258
init["headers"]["Authorization"] = `Basic ${basic}`
259259
} else if (isClientIdSecretAuth(this._auth)) {
260260
// Use the built-in client_credentials grant.
261-
this._debug(`Using default auth (client_credentials).`)
261+
this.debug(`Using default auth (client_credentials).`)
262262

263263
// Fetch an access token and add it to the request.
264264
// Access token can be fetched from cache or using a custom auth method.
@@ -272,15 +272,15 @@ export class DrupalClient {
272272
}
273273
}
274274
} else if (typeof init.withAuth === "string") {
275-
this._debug(`Using custom authorization header.`)
275+
this.debug(`Using custom authorization header.`)
276276

277277
init["headers"]["Authorization"] = init.withAuth
278278
} else if (typeof init.withAuth === "function") {
279-
this._debug(`Using custom authorization callback.`)
279+
this.debug(`Using custom authorization callback.`)
280280

281281
init["headers"]["Authorization"] = init.withAuth()
282282
} else if (isBasicAuth(init.withAuth)) {
283-
this._debug(`Using basic authorization header`)
283+
this.debug(`Using basic authorization header.`)
284284

285285
const basic = Buffer.from(
286286
`${init.withAuth.username}:${init.withAuth.password}`
@@ -301,12 +301,12 @@ export class DrupalClient {
301301
}
302302

303303
if (this.fetcher) {
304-
this._debug(`Using custom fetcher.`)
304+
this.debug(`Using custom fetcher, fetching: ${input}`)
305305

306306
return await this.fetcher(input, init)
307307
}
308308

309-
this._debug(`Using default fetch (polyfilled by Next.js).`)
309+
this.debug(`Using default fetch, fetching: ${input}`)
310310

311311
return await fetch(input, init)
312312
}
@@ -329,8 +329,7 @@ export class DrupalClient {
329329

330330
const url = this.buildUrl(apiPath, options?.params)
331331

332-
this._debug(`Creating resource of type ${type}.`)
333-
this._debug(url.toString())
332+
this.debug(`Creating resource of type ${type}.`)
334333

335334
// Add type to body.
336335
body.data.type = type
@@ -373,8 +372,7 @@ export class DrupalClient {
373372
options?.params
374373
)
375374

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}.`)
378376

379377
const response = await this.fetch(url.toString(), {
380378
method: "POST",
@@ -415,8 +413,7 @@ export class DrupalClient {
415413

416414
const url = this.buildUrl(`${apiPath}/${uuid}`, options?.params)
417415

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}.`)
420417

421418
// Update body.
422419
body.data.type = type
@@ -455,8 +452,7 @@ export class DrupalClient {
455452

456453
const url = this.buildUrl(`${apiPath}/${uuid}`, options?.params)
457454

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}.`)
460456

461457
const response = await this.fetch(url.toString(), {
462458
method: "DELETE",
@@ -489,7 +485,7 @@ export class DrupalClient {
489485
const cached = (await this.cache.get(options.cacheKey)) as string
490486

491487
if (cached) {
492-
this._debug(`Returning cached resource ${type} with id ${uuid}`)
488+
this.debug(`Returning cached resource ${type} with id ${uuid}.`)
493489

494490
const json = JSON.parse(cached)
495491

@@ -504,8 +500,7 @@ export class DrupalClient {
504500

505501
const url = this.buildUrl(`${apiPath}/${uuid}`, options?.params)
506502

507-
this._debug(`Fetching resource ${type} with id ${uuid}.`)
508-
this._debug(url.toString())
503+
this.debug(`Fetching resource ${type} with id ${uuid}.`)
509504

510505
const response = await this.fetch(url.toString(), {
511506
withAuth: options.withAuth,
@@ -687,6 +682,8 @@ export class DrupalClient {
687682
_format: "json",
688683
})
689684

685+
this.debug(`Fetching resource by path, ${path}.`)
686+
690687
const response = await this.fetch(url.toString(), {
691688
method: "POST",
692689
credentials: "include",
@@ -739,8 +736,7 @@ export class DrupalClient {
739736
...options?.params,
740737
})
741738

742-
this._debug(`Fetching resource collection of type ${type}`)
743-
this._debug(url.toString())
739+
this.debug(`Fetching resource collection of type ${type}.`)
744740

745741
const response = await this.fetch(url.toString(), {
746742
withAuth: options.withAuth,
@@ -910,6 +906,8 @@ export class DrupalClient {
910906
path,
911907
})
912908

909+
this.debug(`Fetching translated path, ${path}.`)
910+
913911
const response = await this.fetch(url.toString(), {
914912
withAuth: options.withAuth,
915913
})
@@ -990,6 +988,8 @@ export class DrupalClient {
990988
)
991989

992990
try {
991+
this.debug(`Fetching JSON:API index.`)
992+
993993
const response = await this.fetch(url.toString(), {
994994
// As per https://www.drupal.org/node/2984034 /jsonapi is public.
995995
withAuth: false,
@@ -1123,7 +1123,7 @@ export class DrupalClient {
11231123
const cached = (await this.cache.get(options.cacheKey)) as string
11241124

11251125
if (cached) {
1126-
this._debug(`Returning cached menu items for ${name}`)
1126+
this.debug(`Returning cached menu items for ${name}.`)
11271127
return JSON.parse(cached)
11281128
}
11291129
}
@@ -1138,8 +1138,7 @@ export class DrupalClient {
11381138
options.params
11391139
)
11401140

1141-
this._debug(`Fetching menu items for ${name}.`)
1142-
this._debug(url.toString())
1141+
this.debug(`Fetching menu items for ${name}.`)
11431142

11441143
const response = await this.fetch(url.toString(), {
11451144
withAuth: options.withAuth,
@@ -1212,6 +1211,8 @@ export class DrupalClient {
12121211
options.params
12131212
)
12141213

1214+
this.debug(`Fetching view, ${viewId}.${displayId}.`)
1215+
12151216
const response = await this.fetch(url.toString(), {
12161217
withAuth: options.withAuth,
12171218
})
@@ -1252,6 +1253,8 @@ export class DrupalClient {
12521253
options.params
12531254
)
12541255

1256+
this.debug(`Fetching search index, ${name}.`)
1257+
12551258
const response = await this.fetch(url.toString(), {
12561259
withAuth: options.withAuth,
12571260
})
@@ -1330,11 +1333,11 @@ export class DrupalClient {
13301333
this._token &&
13311334
Date.now() < this.tokenExpiresOn
13321335
) {
1333-
this._debug(`Using existing access token.`)
1336+
this.debug(`Using existing access token.`)
13341337
return this._token
13351338
}
13361339

1337-
this._debug(`Fetching new access token.`)
1340+
this.debug(`Fetching new access token.`)
13381341

13391342
const basic = Buffer.from(`${clientId}:${clientSecret}`).toString("base64")
13401343

@@ -1343,7 +1346,7 @@ export class DrupalClient {
13431346
if (opts?.scope) {
13441347
body = `${body}&scope=${opts.scope}`
13451348

1346-
this._debug(`Using scope: ${opts.scope}`)
1349+
this.debug(`Using scope: ${opts.scope}`)
13471350
}
13481351

13491352
const response = await this.fetch(url.toString(), {
@@ -1362,8 +1365,6 @@ export class DrupalClient {
13621365

13631366
const result: AccessToken = await response.json()
13641367

1365-
this._debug(result)
1366-
13671368
this.token = result
13681369

13691370
this.accessTokenScope = opts?.scope
@@ -1411,8 +1412,8 @@ export class DrupalClient {
14111412
return message
14121413
}
14131414

1414-
private _debug(message) {
1415-
!!this.debug && this.logger.debug(message)
1415+
debug(message) {
1416+
this.isDebugEnabled && this.logger.debug(message)
14161417
}
14171418

14181419
// Error handling.

packages/next-drupal/src/logger.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import type { Logger } from "./types"
22

3+
export const LOG_MESSAGE_PREFIX = "[next-drupal][log]:"
4+
export const DEBUG_MESSAGE_PREFIX = "[next-drupal][debug]:"
5+
export const WARN_MESSAGE_PREFIX = "[next-drupal][warn]:"
6+
export const ERROR_MESSAGE_PREFIX = "[next-drupal][error]:"
7+
38
// Default logger. Uses console.
49
export const logger: Logger = {
510
log(message) {
6-
console.log(`[next-drupal][log]:`, message)
11+
console.log(LOG_MESSAGE_PREFIX, message)
712
},
813
debug(message) {
9-
console.debug(`[next-drupal][debug]:`, message)
14+
console.debug(DEBUG_MESSAGE_PREFIX, message)
1015
},
1116
warn(message) {
12-
console.warn(`[next-drupal][debug]:`, message)
17+
console.warn(WARN_MESSAGE_PREFIX, message)
1318
},
1419
error(message) {
15-
console.error(`[next-drupal][error]:`, message)
20+
console.error(ERROR_MESSAGE_PREFIX, message)
1621
},
1722
}

packages/next-drupal/tests/client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe("DrupalClient", () => {
5959
"[next-drupal][debug]:",
6060
"Debug mode is on."
6161
)
62-
expect(client.debug).toBe(true)
62+
expect(client.isDebugEnabled).toBe(true)
6363
})
6464
})
6565

0 commit comments

Comments
 (0)