axios-to-curl

1.3.5 • Public • Published

Codacy Badge CircleCI npm version

Description

This module is an axios third-party module to log any axios request as a curl command in the console. It was originally posted as a suggestion on the axios repository, but since we believed it wasn't in the scope of axios to release such feature, we decided to make it as an independent module.

Post Note:

Axios third-party module to print all axios requests as curl commands in the console. This repository is forked from axios-curlirize https://www.npmjs.com/package/axios-curlirize and supports use with Node JS without having to enable ES6 imports. This repo was inspired by an issue https://github.com/delirius325/axios-curlirize/issues/20 raised on the origional repository https://github.com/delirius325/axios-curlirize.

Special thanks to Anthony Gauthier https://github.com/delirius325, the author of axios-curlirize.

The package name hereafter will be referred to as axios-curlirize as I have contributed very little, for me change its name.

How it works

The module makes use of axios' interceptors to log the request as a cURL command. It also stores it in the response's config object. Therefore, the command can be seen in the app's console, as well as in the res.config.curlCommand property of the response.

Changing the logger

By default, axios-curlirize uses the console.log/error() functions. It is possible to change the logger by doing something similar to this:

// when initiating your curlirize instance
curlirize(axios, (result, err) => {
  const { command } = result;
  if (err) {
    // use your logger here
  } else {
    // use your logger here
  }
});

How to use it

axios-curlirize is super easy to use. First you'll have to install it.

npm i --save axios-curlirize@latest

Then all you have to do is import and instanciate curlirize in your app. Here's a sample:

import axios from 'axios';
import express from 'express';
import curlirize from 'axios-curlirize';
 
const app = express();
 
// initializing axios-curlirize with your axios instance
curlirize(axios);
 
// creating dummy route
app.post('/', (req, res) => {
  res.send({ hello: 'world!' });
});
 
// starting server
app.listen(7500, () => {
  console.log('Dummy server started on port 7500');
  /*
             The output of this in the console will be :
             curl -X POST -H "Content-Type:application/x-www-form-urlencoded" --data {"dummy":"data"} http://localhost:7500/
        */
  axios
    .post('http://localhost:7500/', { dummy: 'data' })
    .then(res => {
      console.log('success');
    })
    .catch(err => {
      console.log(err);
    });
});

Readme

Keywords

Package Sidebar

Install

npm i axios-to-curl

Weekly Downloads

375

Version

1.3.5

License

MIT

Unpacked Size

30.1 kB

Total Files

15

Last publish

Collaborators

  • colidarity