Skip to content

Commit 42051b0

Browse files
committed
Added usecase for standalone service
1 parent bc8f845 commit 42051b0

File tree

11 files changed

+364
-4
lines changed

11 files changed

+364
-4
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,61 @@ export class AppModule implements NestModule {
8989
}
9090
```
9191

92+
### Standalone (Axios, Fetch, etc.)
93+
```ts
94+
import axios from 'axios';
95+
import { StandaloneApiLogger, createAxiosLogger } from 'api-logger-mongodb';
96+
97+
// Create logger instance
98+
const logger = new StandaloneApiLogger({
99+
mongoUri: 'mongodb://localhost:27017',
100+
databaseName: 'my_logs',
101+
collectionName: 'api_audit',
102+
maskFields: ['password', 'token'],
103+
logResponseBody: true,
104+
logRequestBody: true
105+
});
106+
107+
// Initialize logger
108+
await logger.init();
109+
110+
// Create axios interceptor for automatic logging
111+
const axiosLogger = createAxiosLogger(logger, () => ({
112+
id: 'user123',
113+
114+
}));
115+
116+
// Add interceptors to axios
117+
axios.interceptors.request.use(axiosLogger.request);
118+
axios.interceptors.response.use(axiosLogger.response, axiosLogger.error);
119+
120+
// Now all axios calls will be automatically logged
121+
const response = await axios.get('https://api.example.com/users');
122+
const postResponse = await axios.post('https://api.example.com/users', {
123+
name: 'John',
124+
125+
});
126+
127+
// Manual logging (for fetch or other HTTP clients)
128+
await logger.logRequest(
129+
'https://api.example.com/users',
130+
'GET',
131+
{
132+
headers: { 'Authorization': 'Bearer token' },
133+
query: { page: 1 }
134+
},
135+
{
136+
statusCode: 200,
137+
body: { users: [] }
138+
},
139+
{ id: 'user123', email: '[email protected]' },
140+
150 // duration in ms
141+
);
142+
143+
// Close connection when done
144+
await logger.close();
145+
```
146+
92147
## Advanced Usage Examples
93148

94149
### Express - Filter by Routes and Methods

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export { apiLoggerExpress } from './middleware/express';
22
export { ApiLogger } from './core/logger';
33
export * from './types';
44
export { createApiLoggerMiddleware, createApiLoggerModule, ApiLoggerNestMiddleware, ApiLoggerModule } from './middleware/nestjs';
5+
export { StandaloneApiLogger, createAxiosLogger } from './utils/standalone';
56
//# sourceMappingURL=index.d.ts.map

dist/index.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/utils/standalone.d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ApiLogger } from '../core/logger';
2+
import { ApiLoggerOptions } from '../types';
3+
/**
4+
* Standalone API Logger for use with axios or any HTTP client
5+
*/
6+
export declare class StandaloneApiLogger {
7+
private logger;
8+
private initialized;
9+
constructor(options: ApiLoggerOptions);
10+
/**
11+
* Initialize MongoDB connection
12+
*/
13+
init(): Promise<void>;
14+
/**
15+
* Log an HTTP request and response
16+
*/
17+
logRequest(url: string, method: string, requestData: {
18+
headers?: Record<string, string>;
19+
body?: any;
20+
query?: Record<string, any>;
21+
params?: Record<string, any>;
22+
}, responseData: {
23+
statusCode: number;
24+
body?: any;
25+
headers?: Record<string, string>;
26+
}, userInfo?: any, durationMs?: number): Promise<void>;
27+
/**
28+
* Close MongoDB connection
29+
*/
30+
close(): Promise<void>;
31+
/**
32+
* Get the underlying logger instance
33+
*/
34+
getLogger(): ApiLogger;
35+
}
36+
/**
37+
* Axios interceptor factory for automatic logging
38+
*/
39+
export declare function createAxiosLogger(logger: StandaloneApiLogger, getUserInfo?: () => any): {
40+
request: (config: any) => any;
41+
response: (response: any) => Promise<any>;
42+
error: (error: any) => Promise<never>;
43+
};
44+
//# sourceMappingURL=standalone.d.ts.map

dist/utils/standalone.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/utils/standalone.js

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/utils/standalone.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export {
66
createApiLoggerModule,
77
ApiLoggerNestMiddleware,
88
ApiLoggerModule
9-
} from './middleware/nestjs';
9+
} from './middleware/nestjs';
10+
export { StandaloneApiLogger, createAxiosLogger } from './utils/standalone';

0 commit comments

Comments
 (0)