Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/4lessandrodev/curl2axios
This lib provide a interceptor to generate a curl from axios request
https://github.com/4lessandrodev/curl2axios
axios axios-debug curl debug generate-curl testing
Last synced: about 2 months ago
JSON representation
This lib provide a interceptor to generate a curl from axios request
- Host: GitHub
- URL: https://github.com/4lessandrodev/curl2axios
- Owner: 4lessandrodev
- License: mit
- Created: 2024-02-02T22:22:39.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-05-14T13:34:07.000Z (8 months ago)
- Last Synced: 2024-10-31T18:55:32.992Z (2 months ago)
- Topics: axios, axios-debug, curl, debug, generate-curl, testing
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/curl2axios
- Size: 35.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# curl2axios
## Overview
curl2axios is a lightweight Axios interceptor for generating cURL commands from Axios requests. It simplifies the process of converting your Axios requests into cURL commands for easier debugging and sharing.
---
### Installation
Install the library using npm or yarn:
```sh
npm install curl2axios
```
OR
```sh
yarn add curl2axios
```
---
### Usage
Basic Usage
```js
import axios from 'axios';
import { curlInterceptor } from 'curl2axios';axios.interceptors.request.use(curlInterceptor);
axios.get('https://api.com/item/1');
```
This will automatically generate a cURL command in the console based on the Axios request.
```sh
{
occurredAt: '2024-02-02T22:17:45.514Z',
curlCommand: "curl 'https://api.com/item/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '"
}```
---
Custom Callback
You can also provide a custom callback function to handle the generated cURL command. This is useful if you want to do something specific with the command, such as logging it in a different way.```js
import axios from 'axios';
import { curlInterceptor } from 'curl2axios';const myCallback = (curl: string) => console.log(curl);
axios.interceptors.request.use((req) => curlInterceptor(req, myCallback));
axios.get('https://api.com/item/1');
```
In this example, the custom callback function myCallback will be called with the generated cURL command.
### Example Output
For the given Axios request:
```js
axios.get('https://api.com/item/1');
// curl 'https://api.com/item/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '
```
### Bind
```js
import axios from 'axios';
import { bindCurl } from 'curl2axios';const instance = bindCurl(axios.create({ baseURL: 'https://pokeapi.co/api/v2' }));
await instance.get('/berry-flavor/1');
console.log(instance.curl);
// > "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --url 'https://pokeapi.co/api/v2'"
```
### License
axios2curl is licensed under the MIT License - see the LICENSE file for details.