Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version: 4.10.1
#### Date: Oct-01-2025
Enhancement: Added logHandler interceptors for request and response logging

### Version: 4.10.0
#### Date: Sep-22-2025
Fix: Enhance retry logic to use configured retryDelay
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.10.0",
"version": "4.10.1",
"type": "module",
"license": "MIT",
"main": "./dist/legacy/index.cjs",
Expand Down
61 changes: 61 additions & 0 deletions src/lib/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,67 @@ export function stack(config: StackConfig): StackClass {
});
};
}
// LogHandler interceptors
if (config.debug) {
// Request interceptor for logging
client.interceptors.request.use((requestConfig: any) => {
config.logHandler!('info', {
type: 'request',
method: requestConfig.method?.toUpperCase(),
url: requestConfig.url,
headers: requestConfig.headers,
params: requestConfig.params,
timestamp: new Date().toISOString()
});
return requestConfig;
});

// Response interceptor for logging
client.interceptors.response.use(
(response: any) => {
const level = getLogLevelFromStatus(response.status);
config.logHandler!(level, {
type: 'response',
status: response.status,
statusText: response.statusText,
url: response.config?.url,
method: response.config?.method?.toUpperCase(),
headers: response.headers,
data: response.data,
timestamp: new Date().toISOString()
});
return response;
},
(error: any) => {
const status = error.response?.status || 0;
const level = getLogLevelFromStatus(status);
config.logHandler!(level, {
type: 'response_error',
status: status,
statusText: error.response?.statusText || error.message,
url: error.config?.url,
method: error.config?.method?.toUpperCase(),
error: error.message,
timestamp: new Date().toISOString()
});
throw error;
}
);
}

// Helper function to determine log level based on HTTP status code
function getLogLevelFromStatus(status: number): string {
if (status >= 200 && status < 300) {
return 'info';
} else if (status >= 300 && status < 400) {
return 'warn';
} else if (status >= 400) {
return 'error';
} else {
return 'debug';
}
}

// Retry policy handlers
const errorHandler = (error: any) => {
return retryResponseErrorHandler(error, config, client);
Expand Down