convert-pino-request-to-curl
v1.0.11
Published
Convert Pino HTTP request logs to executable cURL commands with field anonymization support
Downloads
10,426
Maintainers
Readme
Convert Pino Request to cURL
A lightweight TypeScript library that converts Pino HTTP request logs into ready-to-use cURL commands. Perfect for debugging, testing, and documentation purposes.
🚀 Installation
npm install convert-pino-request-to-curl
# or
yarn add convert-pino-request-to-curl
# or
pnpm add convert-pino-request-to-curl📖 Usage
Basic Usage with Pino-HTTP
import { pinoHttp } from 'pino-http';
import { pino } from 'pino';
import { PinoRequestConverter } from 'convert-pino-request-to-curl';
const logger = pinoHttp({
serializers: {
err: pino.stdSerializers.err,
req: (req) => {
return {
method: req.method,
url: req.url,
// Generate cURL command from request
curl: PinoRequestConverter.getCurl(req),
};
},
res: pino.stdSerializers.res,
},
});Anonymizing Sensitive Fields
Protect sensitive data by anonymizing specific fields in the request body:
// Using array (backwards compatible)
const curl = PinoRequestConverter.getCurl(req, ['password', 'token', 'apiKey']);
// Using options object (recommended)
const curl = PinoRequestConverter.getCurl(req, {
anonymizedFields: ['password', 'token', 'apiKey'],
});Input:
{
"login": "admin",
"password": "secret123",
"token": "abc-def-ghi"
}Output:
{
"login": "admin",
"password": "******",
"token": "******"
}Advanced Options
import { PinoRequestConverter, CurlOptions } from 'convert-pino-request-to-curl';
const options: CurlOptions = {
// Fields to anonymize (replaces values with "******")
anonymizedFields: ['password', 'token', 'secret'],
// Headers to exclude from curl command
excludeHeaders: ['content-length', 'user-agent'],
// Add verbose output (-v flag)
verbose: true,
// Add compressed flag (--compressed)
compressed: true,
// Set maximum time for request (--max-time)
maxTime: 30,
};
const curl = PinoRequestConverter.getCurl(req, options);📋 Examples
GET Request
const request = {
method: 'GET',
url: '/api/users?page=1&limit=10',
headers: {
'host': 'api.example.com',
'authorization': 'Bearer token123',
'accept': 'application/json',
},
raw: {
protocol: 'https',
body: {},
},
// ... other fields
};
const curl = PinoRequestConverter.getCurl(request, {
anonymizedFields: ['authorization'],
});Generated cURL:
curl --location -g --request GET 'https://api.example.com/api/users?page=1&limit=10' \
--header 'host: api.example.com' \
--header 'authorization: ******' \
--header 'accept: application/json'POST Request with Body
const request = {
method: 'POST',
url: '/api/login',
headers: {
'host': 'api.example.com',
'content-type': 'application/json',
},
raw: {
protocol: 'https',
body: {
email: '[email protected]',
password: 'mypassword',
},
},
// ... other fields
};
const curl = PinoRequestConverter.getCurl(request, {
anonymizedFields: ['password'],
compressed: true,
});Generated cURL:
curl --location -g --compressed --request POST 'https://api.example.com/api/login' \
--header 'host: api.example.com' \
--header 'content-type: application/json' \
--data-raw '{"email":"[email protected]","password":"******"}'Route Parameters
const request = {
method: 'GET',
url: '/api/users/:id/posts/:postId',
params: {
id: '123',
postId: '456',
},
// ... other fields
};
const curl = PinoRequestConverter.getCurl(request);Generated cURL:
curl --location -g --request GET 'https://api.example.com/api/users/id/123/posts/postId/456' \
--header 'host: api.example.com'🔧 Configuration Options
CurlOptions Interface
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| anonymizedFields | readonly string[] | [] | Fields to anonymize in request body (replaced with "******") |
| excludeHeaders | readonly string[] | ['content-length'] | Headers to exclude from the cURL command |
| verbose | boolean | false | Include verbose output (-v flag) |
| compressed | boolean | false | Include compressed flag (--compressed) |
| maxTime | number | undefined | Maximum time in seconds for the request (--max-time) |
🎯 Use Cases
- Debugging: Quickly reproduce API requests from logs
- Testing: Generate test requests for API endpoints
- Documentation: Create example cURL commands for API docs
- Monitoring: Track and replay requests in production
- Development: Share reproducible requests with team members
🛡️ Security
The library automatically:
- Removes
content-lengthheader by default (can be customized) - Supports anonymization of sensitive fields like passwords, tokens, etc.
- Handles various data types (strings, numbers, booleans) in anonymization
📝 TypeScript Support
Full TypeScript support with comprehensive type definitions:
import {
PinoRequestConverter,
RequestType,
CurlOptions,
RawRequest
} from 'convert-pino-request-to-curl';🧪 Testing
npm test🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
👨💻 Author
Mike Lima
- GitHub: @mikemajesty
- Email: [email protected]
🔗 Links
📊 Changelog
v1.0.10
- ✨ Added comprehensive TypeScript interfaces
- ✨ Added
CurlOptionsfor advanced configuration - ✨ Support for verbose, compressed, and max-time options
- ✨ Improved anonymization (supports numbers and booleans)
- ✨ Better error handling and validation
- ✨ Expanded test coverage
- 📚 Enhanced documentation
v1.0.9
- 🐛 Fixed module resolution issues
Star ⭐ this repository if you find it helpful!
