1.0.0 • Published 4 months ago
axios-curlirize-ts v1.0.0
axios-curlirize-ts
This package provides a utility to generate cURL commands from Axios requests using an interceptor. It helps with debugging and logging HTTP requests in an easily reproducible cURL format.
Installation
npm install --save axios-curlirize-ts
OR
yarn add axios-curlirize-ts
OR
pnpm add axios-curlirize-ts
Usage
import axios from "axios";
import QueryString from "query-string";
import curlirize from "axios-curlirize-ts";
const instance = axios.create({
headers: {
"Content-Type": "application/json",
},
timeout: 60000,
paramsSerializer: (params) => QueryString.stringify(params),
});
curlirize(instance);
instance.get("https://api.example.com/data").then((response) => {
console.log("Response:", response.data);
});
API
curlirize(instance: AxiosInstance, callback?: LogCallback)
instance
: The Axios instance to be curlirized.callback
: An optional function(curl: string | null, error: unknown | null) => void
that receives the generated cURL command or an error.
Default Callback Behavior
If no callback is provided, the cURL command is logged using console.info
, and errors (if any) are logged using console.error
.
Example with Custom Callback
const customLogger = (curl: string | null, error: unknown | null) => {
if (error) {
console.error("CURL Generation Error:", error);
} else {
console.log("Generated CURL Command:", curl);
}
};
curlirize(instance, customLogger);
License
This library is open-source and available under the MIT license.
Author
axios-curlirize-ts
is developed by Thong Dang. You can contact me at thongdn.it@gmail.com.
If you like my project, you can support me or star (like) for it.
1.0.0
4 months ago