Skip to content

Commit 2fa523f

Browse files
feat: reduce debug logging noise
1 parent a721ca5 commit 2fa523f

15 files changed

Lines changed: 635 additions & 56 deletions

File tree

biome.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"linter": {
1919
"enabled": true,
2020
"rules": {
21-
"recommended": true
21+
"recommended": true,
22+
"style": {
23+
"useBlockStatements": "error"
24+
}
2225
}
2326
},
2427
"javascript": {

examples/bun/blog-1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const salesChannel = makeSalesChannel(
2828
{
2929
clientId: "<your_client_id>",
3030
scope: "<your_scope>",
31-
debug: true, // useful to understand what happens behind the scene
31+
debug: { logLevel: "info" }, // useful to understand what happens behind the scene
3232
},
3333
{
3434
storage: memoryStorage(),

examples/bun/blog-2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const integration = makeIntegration(
3737
{
3838
clientId: "<your_client_id>",
3939
clientSecret: "<your_client_secret>",
40-
debug: true,
40+
debug: { logLevel: "info" },
4141
},
4242
{
4343
storage: compositeStorage,

examples/nextjs/src/app/api-credentials/client/page.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"use client"
22

3-
import DefaultTemplate from "@/components/DefaultTemplate"
4-
import { CommerceLayerAuthProvider, useCommerceLayerAuth } from "@/contexts/CommerceLayerAuthContext"
53
import { getCoreApiBaseEndpoint, jwtDecode } from "@commercelayer/js-auth"
64
import { useEffect, useMemo, useState } from "react"
5+
import DefaultTemplate from "@/components/DefaultTemplate"
6+
import {
7+
CommerceLayerAuthProvider,
8+
useCommerceLayerAuth,
9+
} from "@/contexts/CommerceLayerAuthContext"
710

811
const scope = process.env.NEXT_PUBLIC_SCOPE as string
912

@@ -153,7 +156,12 @@ function CustomerOrders() {
153156
} else {
154157
setOrders(null)
155158
}
156-
}, [authorization.accessToken, authorization.ownerType, authorization.ownerId, organizationSlug])
159+
}, [
160+
authorization.accessToken,
161+
authorization.ownerType,
162+
authorization.ownerId,
163+
organizationSlug,
164+
])
157165

158166
if (orders == null) {
159167
return null

examples/nextjs/src/app/utils/getServerSideToken.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ const salesChannel = makeSalesChannel(
1212
{
1313
clientId,
1414
scope,
15-
debug: true,
15+
debug: {
16+
logLevel: "verbose",
17+
},
1618
},
1719
{
1820
storage: createCompositeStorage({
21+
debug: {
22+
logLevel: "verbose",
23+
},
1924
name: "serverCompositeStorage",
2025
storages: [
2126
createStorage({
@@ -34,7 +39,9 @@ const salesChannel = makeSalesChannel(
3439
},
3540
)
3641

37-
export async function getServerSideAuth(): Promise<ReturnType<typeof makeSalesChannel>> {
42+
export async function getServerSideAuth(): Promise<
43+
ReturnType<typeof makeSalesChannel>
44+
> {
3845
"use server"
3946

4047
return {

examples/nextjs/src/contexts/CommerceLayerAuthContext.tsx

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
authenticate,
66
makeSalesChannel,
77
} from "@commercelayer/js-auth"
8-
import cookies from 'js-cookie'
8+
import cookies from "js-cookie"
99
import {
1010
createContext,
1111
type ReactNode,
@@ -22,11 +22,14 @@ type AuthState = {
2222
accessToken?: string
2323
ownerType?: "customer" | "guest"
2424
ownerId?: string
25-
errors?: Extract<AuthenticateReturn<'password'>, { errors?: unknown }>['errors']
25+
errors?: Extract<
26+
AuthenticateReturn<"password">,
27+
{ errors?: unknown }
28+
>["errors"]
2629
}
2730

2831
interface CommerceLayerAuthContextType extends AuthState {
29-
login: ( email: string, password: string ) => Promise<void>
32+
login: (email: string, password: string) => Promise<void>
3033
logout: () => Promise<void>
3134
}
3235

@@ -46,7 +49,10 @@ export function useCommerceLayerAuth() {
4649
export function CommerceLayerAuthProvider({
4750
children,
4851
scope,
49-
}: { children: ReactNode; scope: string }) {
52+
}: {
53+
children: ReactNode
54+
scope: string
55+
}) {
5056
const [salesChannel, setSalesChannel] =
5157
useState<ReturnType<typeof makeSalesChannel>>()
5258

@@ -58,7 +64,9 @@ export function CommerceLayerAuthProvider({
5864
{
5965
clientId,
6066
scope,
61-
debug: true,
67+
debug: {
68+
logLevel: "verbose",
69+
},
6270
},
6371
{
6472
storage: createStorage({
@@ -111,7 +119,10 @@ export function CommerceLayerAuthProvider({
111119
setAuth({
112120
accessToken: authorization.accessToken,
113121
ownerType: authorization.ownerType,
114-
ownerId: authorization.ownerType === 'customer' ? authorization.ownerId : undefined,
122+
ownerId:
123+
authorization.ownerType === "customer"
124+
? authorization.ownerId
125+
: undefined,
115126
errors: authorization.errors,
116127
})
117128
},
@@ -132,10 +143,7 @@ export function CommerceLayerAuthProvider({
132143
void salesChannel?.getAuthorization().then((auth) => {
133144
setAuth({
134145
ownerType: auth.ownerType,
135-
ownerId:
136-
auth.ownerType === "customer"
137-
? auth.ownerId
138-
: undefined,
146+
ownerId: auth.ownerType === "customer" ? auth.ownerId : undefined,
139147
accessToken: auth.accessToken,
140148
errors: auth.errors,
141149
})

examples/nextjs/src/proxies/withCommerceLayer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export const withCommerceLayer: WithProxy = (
4747
{
4848
clientId,
4949
scope,
50-
debug: true,
50+
debug: {
51+
logLevel: "verbose",
52+
},
5153
},
5254
{
5355
storage: createStorage({

packages/js-auth/README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ const salesChannel = makeSalesChannel(
175175
{
176176
clientId: "<your_client_id>",
177177
scope: "market:code:europe",
178-
debug: true,
178+
debug: {
179+
logLevel: "info"
180+
},
179181
},
180182
{
181183
storage: memoryStorage(),
@@ -249,19 +251,32 @@ flowchart TB
249251

250252
You can enable debugging and assign custom names to your storage instances for better visibility into token operations:
251253

252-
- **`debug`** — When set to `true`, logs detailed information about token operations (creation, retrieval, refresh, etc.).
254+
- **`debug`** — Accepts a `DebugConfig` object with two options:
255+
- `logLevel: "info"` — logs meaningful events only: cache misses, token refreshes, authorizations stored/removed, and errors. Routine cache hits are silent, so setups that call `getAuthorization()` on every API request don't flood the logs.
256+
- `logLevel: "verbose"` — also logs steady-state operations (every storage read and cache hit).
257+
- `maskToken` — whether to mask access tokens and refresh tokens in the log output (e.g. `eyJh...A9Xz`). Defaults to `true`. Set to `false` to see full tokens, e.g. for inspecting them at [jwt.io](https://jwt.io).
253258
- **`name`** — A custom identifier for your storage instance, useful when using multiple storages or composite storage configurations. The `name` is an attribute of the storage itself (as shown in the [`memoryStorage` example above](#storage-strategy)).
254259

260+
The `createCompositeStorage` helper accepts its own `debug` option, following the same rule: a hit on the first (fastest) storage is steady state, while falling back to a slower storage is a meaningful event worth logging.
261+
262+
> [!WARNING]
263+
> When `maskToken` is set to `false`, full access tokens and refresh tokens are logged to the console. Intended for local development only — in serverless/edge environments, logs may be forwarded to external log aggregation services.
264+
255265
```diff
256266
const salesChannel = makeSalesChannel(
257267
{
258268
clientId: "<your_client_id>",
259269
scope: "market:code:europe",
260-
+ debug: true,
270+
+ debug: {
271+
+ logLevel: "info"
272+
+ },
261273
},
262274
{
263275
storage: {
264276
+ name: "storage-name",
277+
+ debug: {
278+
+ logLevel: "info"
279+
+ },
265280
async getItem(key) {
266281
// implementation
267282
},
@@ -499,7 +514,9 @@ const integration = makeIntegration(
499514
{
500515
clientId: "<your_client_id>",
501516
clientSecret: "<your_client_secret>",
502-
debug: true,
517+
debug: {
518+
logLevel: "info"
519+
},
503520
},
504521
{
505522
storage: compositeStorage,

0 commit comments

Comments
 (0)